1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 /* Save start time */
24 $start = microtime();
26 // Will be used in the "stats" plugin later, to be able calculate the elapsed render time.
27 $overallRenderTimer = microtime(TRUE);
29 /* Basic setup, remove eventually registered sessions */
30 require_once ("../include/php_setup.inc");
31 require_once ("functions.inc");
33 /* Set header */
34 header("Content-type: text/html; charset=UTF-8");
36 /* Set the text domain as 'messages' */
37 $domain = 'messages';
38 bindtextdomain($domain, LOCALE_DIR);
39 textdomain($domain);
42 /* Remember everything we did after the last click */
43 session::start();
44 session::set('errorsAlreadyPosted',array());
45 session::global_set('runtime_cache',array());
46 session::set('limit_exceeded',FALSE);
48 // Count number of page reloads
49 if(!session::is_set('clicks')){
50 session::set('clicks', 0);
51 }
52 $clicks = session::get('clicks');
53 $clicks ++ ;
54 session::set('clicks', $clicks);
56 pathNavigator::clear();
58 if ($_SERVER["REQUEST_METHOD"] == "POST"){
59 @DEBUG (DEBUG_POST, __LINE__, __FUNCTION__, __FILE__, $_POST, "_POST");
60 }
61 @DEBUG (DEBUG_POST, __LINE__, __FUNCTION__, __FILE__, session::get_all(), "_SESSION");
63 /* Logged in? Simple security check */
64 if (!session::global_is_set('config')){
65 new log("security","login","",array(),"main.php called without session - logging out") ;
66 header ("Location: logout.php");
67 exit;
68 }
70 /* Check for uniqe ip address */
71 $ui= session::global_get('ui');
72 if ($_SERVER['REMOTE_ADDR'] != $ui->ip){
73 new log("security","login","",array(),"main.php called with session which has a changed IP address.") ;
74 header ("Location: logout.php");
75 exit;
76 }
77 $config= session::global_get('config');
78 $config->check_and_reload();
79 $config->configRegistry->reload();
81 /* Enable compressed output */
82 if ($config->get_cfg_value("core","sendCompressedOutput") == "true"){
83 ob_start("ob_gzhandler");
84 }
86 /* Check for invalid sessions */
87 if(session::global_get('_LAST_PAGE_REQUEST') == ""){
88 session::global_set('_LAST_PAGE_REQUEST',time());
89 }else{
91 /* check GOsa.conf for defined session lifetime */
92 $max_life= $config->get_cfg_value("core","sessionLifetime");
94 /* get time difference between last page reload */
95 $request_time = (time()- session::global_get('_LAST_PAGE_REQUEST'));
97 /* If page wasn't reloaded for more than max_life seconds
98 * kill session
99 */
100 if($request_time > $max_life){
101 session::destroy();
102 new log("security","login","",array(),"main.php called without session - logging out") ;
103 header ("Location: logout.php");
104 exit;
105 }
106 session::global_set('_LAST_PAGE_REQUEST',time());
107 }
110 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
112 /* Set template compile directory */
113 $smarty->compile_dir= $config->get_cfg_value("core","templateCompileDirectory");
114 $smarty->error_unassigned= true;
116 /* Set default */
117 $reload_navigation = false;
119 /* Set last initialised language to current, browser settings */
120 if(!session::global_is_set('Last_init_lang')){
121 $reload_navigation = true;
122 session::global_set('Last_init_lang',get_browser_language());
123 }
125 /* If last language != current force navi reload */
126 $lang= get_browser_language();
127 if(session::global_get('Last_init_lang') != $lang){
128 $reload_navigation = true;
129 }
131 /* Language setup */
132 session::global_set('Last_init_lang',$lang);
134 /* Preset current main base */
135 if(!session::global_is_set('CurrentMainBase')){
136 session::global_set('CurrentMainBase',get_base_from_people($ui->dn));
137 }
139 putenv("LANGUAGE=");
140 putenv("LANG=$lang");
141 setlocale(LC_ALL, $lang);
142 $GLOBALS['t_language']= $lang;
143 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
145 // Validate LDAP schema if not done already
146 if( $config->boolValueIsTrue('core','schemaCheck') &&
147 !$config->configRegistry->schemaCheckFinished() &&
148 !$config->configRegistry->validateSchemata($force=FALSE,$disableIncompatiblePlugins=TRUE)){
149 $config->configRegistry->displayRequirementErrors();
150 }
152 /* Check if the config is up to date */
153 $config->check_config_version();
155 /* Set the text domain as 'messages' */
156 $domain = 'messages';
157 bindtextdomain($domain, LOCALE_DIR);
158 textdomain($domain);
159 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
161 /* Prepare plugin list */
162 if (!session::global_is_set('plist')){
163 /* Initially load all classes */
164 $class_list= get_declared_classes();
165 foreach ($class_mapping as $class => $path){
166 if (!in_array_strict($class, $class_list)){
167 if (is_readable("$BASE_DIR/$path")){
168 require_once("$BASE_DIR/$path");
169 } else {
170 msg_dialog::display(_("Fatal error"),
171 sprintf(_("Cannot locate file %s - please run %s to fix this"),
172 bold("$BASE_DIR/$path"), bold("update-gosa")), FATAL_ERROR_DIALOG);
173 exit;
174 }
175 }
176 }
178 session::global_set('plist', new pluglist($config, $ui));
180 /* Load ocMapping into userinfo */
181 $tmp= new acl($config, NULL, $ui->dn);
182 $ui->ocMapping= $tmp->ocMapping;
183 session::global_set('ui',$ui);
184 }
185 $plist= session::global_get('plist');
187 /* Check for register globals */
188 if (isset($global_check) && $config->boolValueIsTrue("core","forceGlobals")){
189 msg_dialog::display(
190 _("PHP configuration"),
191 _("Fatal error: Register globals is active. Please fix this in order to continue."),
192 FATAL_ERROR_DIALOG);
194 new log("security","login","",array(),"Register globals is on. For security reasons, this should be turned off.") ;
195 session::destroy ();
196 exit;
197 }
199 /* Check Plugin variable */
200 if (session::global_is_set('plugin_dir')){
201 $old_plugin_dir= session::global_get('plugin_dir');
202 } else {
203 $old_plugin_dir= "";
204 }
206 // Generate menus
207 $plist->gen_headlines();
208 $plist->gen_menu();
209 $plist->genPathMenu();
211 /* check if we are using account expiration */
212 $smarty->assign("hideMenus", FALSE);
213 if ($config->boolValueIsTrue("core","handleExpiredAccounts")){
214 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
216 if ($expired == POSIX_WARN_ABOUT_EXPIRATION && !session::is_set('POSIX_WARN_ABOUT_EXPIRATION__DONE')){
218 // The users password is about to xpire soon, display a warning message.
219 new log("security","gosa","",array(),"password for user \"$ui->username\" is about to expire") ;
220 msg_dialog::display(_("Password change"), _("Your password is about to expire, please change your password!"), INFO_DIALOG);
221 session::set('POSIX_WARN_ABOUT_EXPIRATION__DONE', TRUE);
223 } elseif ($expired == POSIX_FORCE_PASSWORD_CHANGE){
225 // The password is expired, we are now going to enforce a new one from the user.
227 // Hide the GOsa menus to avoid leaving the enforced password change dialog.
228 $smarty->assign("hideMenus", TRUE);
229 $plug = (isset($_GET['plug'])) ? $_GET['plug'] : null;
231 // Detect password plugin id:
232 $passId = array_search('password', $plist->pluginList);
233 if($passId !== FALSE){
234 $_GET['plug'] = $passId;
235 }
236 }
237 }
239 $smarty->assign("noMenuMode", count($plist->getRegisteredMenuEntries()) == 0);
240 if (isset($_GET['plug']) && $plist->plugin_access_allowed($_GET['plug'])){
241 $plug= validate($_GET['plug']);
242 $plugin_dir= $plist->get_path($plug);
243 $plugin= $plist->get_class($plug);
244 session::global_set('currentPlugin',$plugin);
245 session::global_set('plugin_dir',$plugin_dir);
246 if ($plugin_dir == ""){
247 new log("security","gosa","",array(),"main.php called with invalid plug parameter \"$plug\"") ;
248 header ("Location: logout.php");
249 exit;
250 }
251 } else {
252 session::global_set('plugin_dir',"welcome");
253 session::global_set('currentPlugin','welcome');
254 $plugin_dir= "$BASE_DIR/plugins/generic/welcome";
255 }
257 // Display the welcome page for admins (iconmenu) and an info page for those
258 // who are not allowed to adminstrate anything (user)
259 if(count($plist->getRegisteredMenuEntries()) == 0 && session::global_get('currentPlugin') == "welcome"){
260 session::global_set('plugin_dir',"infoPage");
261 session::global_set('currentPlugin','welcome');
262 $plugin_dir= "$BASE_DIR/plugins/generic/infoPage";
263 }
265 /* Handle plugin locks.
266 - Remove the plugin from session if we switched to another. (cleanup)
267 - Remove all created locks if "reset" was posted.
268 - Remove all created locks if we switched to another plugin.
269 */
270 $cleanup = FALSE;
271 $remove_lock= FALSE;
273 /* Check if we have changed the selected plugin
274 */
275 if($old_plugin_dir != $plugin_dir && $old_plugin_dir != ""){
276 if (is_file("$old_plugin_dir/main.inc")){
277 $cleanup = $remove_lock = TRUE;
278 require ("$old_plugin_dir/main.inc");
279 $cleanup = $remove_lock = FALSE;
280 }
281 }else // elseif
283 /* Reset was posted, remove all created locks for the current plugin
284 */
285 if((isset($_GET['reset']) && $_GET['reset'] == 1) || isset($_POST['delete_lock'])){
286 $remove_lock = TRUE;
287 }
289 /* Check for sizelimits */
290 eval_sizelimit();
292 /* Check for memory */
293 if (function_exists("memory_get_usage")){
294 if (memory_get_usage() > (to_byte(ini_get('memory_limit')) - 2048000 )){
295 msg_dialog::display(_("Configuration error"), _("Running out of memory!"), WARNING_DIALOG);
296 }
297 }
299 /* Redirect on back event */
300 if ($_SERVER["REQUEST_METHOD"] == "POST"){
302 /* Look for button events that match /^back[0-9]+$/,
303 extract the number and step the correct plugin. */
304 foreach ($_POST as $key => $value){
305 if (preg_match("/^back[0-9]+$/", $key)){
306 $back= substr($key, 4);
307 header ("Location: main.php?plug=$back");
308 exit;
309 }
310 }
311 }
313 /* Redirect on password back event */
314 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['password_back'])){
315 header ("Location: main.php");
316 exit;
317 }
319 /* Check for multiple windows logout */
320 if ($_SERVER["REQUEST_METHOD"] == "POST"){
321 if (isset($_POST['reset_session'])){
322 header ("Location: logout.php");
323 exit;
324 }
325 }
328 /* Load department list when plugin has changed. That is some kind of
329 compromise between speed and beeing up to date */
330 if (isset($_GET['reset'])){
331 set_object_info();
332 }
334 /* show web frontend */
335 $smarty->assign ("title","GOsa");
336 $smarty->assign ("logo", image(get_template_path("images/logo.png")));
337 $smarty->assign ("logoutimage", get_template_path("images/btn-logout.png"));
338 $smarty->assign ("date", date("l, dS F Y H:i:s O"));
339 $smarty->assign ("lang", preg_replace('/_.*$/', '', $lang));
340 $smarty->assign ("must", "<span class='required'>*</span>");
341 if (isset($plug)){
342 $plug= "?plug=$plug";
343 } else {
344 $plug= "";
345 }
346 if (session::global_get('js')==FALSE){
347 $smarty->assign("javascript", "false");
348 $smarty->assign("help_method", "href='helpviewer.php$plug' target='_blank'");
349 } else {
350 $smarty->assign("javascript", "true");
351 $smarty->assign("help_method"," onclick=\"return popup('helpviewer.php$plug','GOsa help');\"");
352 }
355 $loggedin = sprintf(_("You're logged in as %s"),
356 "<span>".$ui->cn." [".$ui->username."] / ".$config->current['NAME']."</span> ");
357 if($ui->ignore_acl_for_current_user()){
358 $loggedin = "<font color='red'>"._("ACLs are disabled")."</font> ".$loggedin;
359 }
361 $smarty->assign ("loggedin", $loggedin);
362 $smarty->assign ("go_logo", get_template_path('images/go_logo.png'));
363 $smarty->assign ("go_base", get_template_path('images/dtree.png'));
364 $smarty->assign ("go_home", get_template_path('images/gohome.png'));
365 $smarty->assign ("go_out", get_template_path('images/logout.png'));
366 $smarty->assign ("go_top", get_template_path('images/go_top.png'));
367 $smarty->assign ("go_corner", get_template_path('images/go_corner.png'));
368 $smarty->assign ("go_left", get_template_path('images/go_left.png'));
369 $smarty->assign ("go_help", get_template_path('images/help.png'));
371 /* reload navigation if language changed*/
372 if($reload_navigation){
373 $plist->menu="";
374 }
375 $smarty->assign ("menu", $plist->gen_menu());
376 $smarty->assign ("plug", "$plug");
379 /* React on clicks */
380 if ($_SERVER["REQUEST_METHOD"] == "POST"){
381 if (isset($_POST['delete_lock']) || isset($_POST['open_readonly'])){
383 /* Set old Post data */
384 if(session::global_is_set('LOCK_VARS_USED_GET')){
385 foreach(session::global_get('LOCK_VARS_USED_GET') as $name => $value){
386 $_GET[$name] = $value;
387 }
388 }
389 if(session::global_is_set('LOCK_VARS_USED_POST')){
390 foreach(session::global_get('LOCK_VARS_USED_POST') as $name => $value){
391 $_POST[$name] = $value;
392 }
393 }
394 if(session::global_is_set('LOCK_VARS_USED_REQUEST')){
395 foreach(session::global_get('LOCK_VARS_USED_REQUEST') as $name => $value){
396 $_REQUEST[$name] = $value;
397 }
398 }
399 }
400 }
402 /* Load plugin */
403 if (is_file("$plugin_dir/main.inc")){
404 $display ="";
405 require ("$plugin_dir/main.inc");
406 } else {
407 msg_dialog::display(
408 _("Plug-in"),
409 sprintf(_("Fatal error: Cannot find any plugin definitions for plugin %s!"), bold($plug)),
410 FATAL_ERROR_DIALOG);
411 exit();
412 }
415 /* Print_out last ErrorMessage repeated string. */
416 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
417 $smarty->assign ("pathMenu", $plist->genPathMenu());
418 $smarty->assign("contents", $display);
419 $smarty->assign("sessionLifetime", $config->get_cfg_value('core','sessionLifetime'));
421 /* If there's some post, take a look if everything is there... */
422 if (isset($_POST) && count($_POST)){
423 if (!isset($_POST['php_c_check'])){
424 msg_dialog::display(
425 _("Configuration Error"),
426 sprintf(_("Fatal error: not all POST variables have been transfered by PHP - please inform your administrator!")),
427 FATAL_ERROR_DIALOG);
428 exit();
429 }
430 }
432 /* Assign erros to smarty */
433 if (session::is_set('errors')){
434 $smarty->assign("errors", session::get('errors'));
435 }
436 if ($error_collector != ""){
437 $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
438 } else {
439 $smarty->assign("php_errors", "");
440 }
442 /* Set focus to the error button if we've an error message */
443 $focus= "";
444 if (session::is_set('errors') && session::get('errors') != ""){
445 $focus= '<script language="JavaScript" type="text/javascript">';
446 $focus.= 'document.forms[0].error_accept.focus();';
447 $focus.= '</script>';
448 }
450 $focus= '<script language="JavaScript" type="text/javascript">';
451 $focus.= 'next_msg_dialog();';
452 $focus.= '</script>';
453 $smarty->assign("focus", $focus);
455 /* Set channel if needed */
456 #TODO: * move all global session calls to global_
457 # * create a new channel where needed (mostly management dialogues)
458 # * remove regulary created channels when not needed anymore
459 # * take a look at external php calls (i.e. get fax, ldif, etc.)
460 # * handle aborted sessions (by pressing anachors i.e. Main, Menu, etc.)
461 # * check lock removals, is "dn" global or not in this case?
462 # * last page request -> global or not?
463 # * check that filters are still global
464 # * maxC global?
465 if (isset($_POST['_channel_'])){
466 echo "DEBUG - current channel: ".$_POST['_channel_'];
467 $smarty->assign("channel", $_POST['_channel_']);
468 } else {
469 $smarty->assign("channel", "");
470 }
472 $display= "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl')).
473 $smarty->fetch(get_template_path('framework.tpl'));
475 /* Save dialog filters and selected base in a cookie.
476 So we may be able to restore the filter an base settings on reload.
477 */
478 $cookie = array();
480 if(isset($_COOKIE['GOsa_Filter_Settings'])){
481 $cookie = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
482 }elseif(isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])){
483 $cookie = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
484 }
486 /* Save filters? */
487 if($config->get_cfg_value("core","storeFilterSettings") == "true"){
488 $cookie_vars = array("MultiDialogFilters","CurrentMainBase");
489 foreach($cookie_vars as $var){
490 if(session::global_is_set($var)){
491 $cookie[$ui->dn][$var] = session::global_get($var);
492 }
493 }
494 if(isset($_GET['plug'])){
495 $cookie[$ui->dn]['plug'] = $_GET['plug'];
496 }
497 @setcookie("GOsa_Filter_Settings",base64_encode(serialize($cookie)),time() + (60*60*24));
498 }
500 /* Show page... */
501 echo $display;
503 /* Save plist and config */
504 session::global_set('plist',$plist);
505 session::global_set('config',$config);
506 session::set('errorsAlreadyPosted',array());
508 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
509 ?>