Code

Updated main.inc
[gosa.git] / gosa-core / html / main.php
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 /* Basic setup, remove eventually registered sessions */
27 require_once ("../include/php_setup.inc");
28 require_once ("functions.inc");
30 /* Set header */
31 header("Content-type: text/html; charset=UTF-8");
33 /* Set the text domain as 'messages' */
34 $domain = 'messages';
35 bindtextdomain($domain, LOCALE_DIR);
36 textdomain($domain);
38 /* Remember everything we did after the last click */
39 session::start();
40 session::set('errorsAlreadyPosted',array());
41 session::global_set('runtime_cache',array());
42 session::set('limit_exceeded',FALSE);
44 // Count number of page reloads 
45 if(!session::is_set('clicks')){
46     session::set('clicks', 0);
47 }
48 $clicks = session::get('clicks');
49 $clicks ++ ;
50 session::set('clicks', $clicks);
53 pathNavigator::clear();
55 if ($_SERVER["REQUEST_METHOD"] == "POST"){
56   @DEBUG (DEBUG_POST, __LINE__, __FUNCTION__, __FILE__, $_POST, "_POST");
57 }
58 @DEBUG (DEBUG_POST, __LINE__, __FUNCTION__, __FILE__, session::get_all(), "_SESSION");
60 /* Logged in? Simple security check */
61 if (!session::global_is_set('config')){
62   new log("security","login","",array(),"main.php called without session - logging out") ;
63   header ("Location: logout.php");
64   exit;
65
67 /* Check for uniqe ip address */
68 $ui= session::global_get('ui');
69 if ($_SERVER['REMOTE_ADDR'] != $ui->ip){
70   new log("security","login","",array(),"main.php called with session which has a changed IP address.") ;
71   header ("Location: logout.php");
72   exit;
73 }
74 $config= session::global_get('config');
75 $config->check_and_reload();
76 $config->configRegistry->reload();
78 // Validate LDAP schema if not done already
79 if( $config->boolValueIsTrue('core','schemaCheck') && 
80     !$config->configRegistry->schemaCheckFinished() && 
81     !$config->configRegistry->validateSchemata($force=FALSE,$disableIncompatiblePlugins=TRUE)){
82     $config->configRegistry->displayRequirementErrors();
83 }
85 /* Enable compressed output */
86 if ($config->get_cfg_value("core","sendCompressedOutput") == "true"){
87   ob_start("ob_gzhandler");
88 }
90 /* Check for invalid sessions */
91 if(session::global_get('_LAST_PAGE_REQUEST') == ""){
92   session::global_set('_LAST_PAGE_REQUEST',time());
93 }else{
95   /* check GOsa.conf for defined session lifetime */
96   $max_life= $config->get_cfg_value("core","sessionLifetime");
98   /* get time difference between last page reload */
99   $request_time = (time()- session::global_get('_LAST_PAGE_REQUEST'));
101   /* If page wasn't reloaded for more than max_life seconds 
102    * kill session
103    */
104   if($request_time > $max_life){
105     session::destroy();
106     new log("security","login","",array(),"main.php called without session - logging out") ;
107     header ("Location: logout.php");
108     exit;
109   }
110   session::global_set('_LAST_PAGE_REQUEST',time());
114 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
116 /* Set template compile directory */
117 $smarty->compile_dir= $config->get_cfg_value("core","templateCompileDirectory");
118 $smarty->error_unassigned= true;
120 /* Set default */
121 $reload_navigation = false;
123 /* Set last initialised language to current, browser settings */
124 if(!session::global_is_set('Last_init_lang')){
125   $reload_navigation = true;
126   session::global_set('Last_init_lang',get_browser_language());
129 /* If last language != current force navi reload */
130 $lang= get_browser_language();
131 if(session::global_get('Last_init_lang') != $lang){
132   $reload_navigation = true;
135 /* Language setup */
136 session::global_set('Last_init_lang',$lang);
138 /* Preset current main base */
139 if(!session::global_is_set('CurrentMainBase')){
140   session::global_set('CurrentMainBase',get_base_from_people($ui->dn));
143 putenv("LANGUAGE=");
144 putenv("LANG=$lang");
145 setlocale(LC_ALL, $lang);
146 $GLOBALS['t_language']= $lang;
147 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
149 /* Check if the config is up to date */
150 $config->check_config_version();
152 /* Set the text domain as 'messages' */
153 $domain = 'messages';
154 bindtextdomain($domain, LOCALE_DIR);
155 textdomain($domain);
156 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
158 /* Prepare plugin list */
159 if (!session::global_is_set('plist')){
160   /* Initially load all classes */
161   $class_list= get_declared_classes();
162   foreach ($class_mapping as $class => $path){
163     if (!in_array($class, $class_list)){
164       if (is_readable("$BASE_DIR/$path")){
165         require_once("$BASE_DIR/$path");
166       } else {
167         msg_dialog::display(_("Fatal error"),
168             sprintf(_("Cannot locate file %s - please run %s to fix this"),
169               bold("$BASE_DIR/$path"), bold("update-gosa")), FATAL_ERROR_DIALOG);
170         exit;
171       }
172     }
173   }
175   session::global_set('plist', new pluglist($config, $ui));
177   /* Load ocMapping into userinfo */
178   $tmp= new acl($config, NULL, $ui->dn);
179   $ui->ocMapping= $tmp->ocMapping;
180   session::global_set('ui',$ui);
182 $plist= session::global_get('plist');
184 /* Check for register globals */
185 if (isset($global_check) && $config->boolValueIsTrue("core","forceGlobals")){
186   msg_dialog::display(
187             _("PHP configuration"),
188             _("FATAL: Register globals is active. Please fix this in order to continue."),
189             FATAL_ERROR_DIALOG);
191   new log("security","login","",array(),"Register globals is on. For security reasons, this should be turned off.") ;
192   session::destroy ();
193   exit;
196 /* Check Plugin variable */
197 if (session::global_is_set('plugin_dir')){
198   $old_plugin_dir= session::global_get('plugin_dir');
199 } else {
200   $old_plugin_dir= "";
203 // Generate menus 
204 $plist->gen_headlines();
205 $plist->gen_menu();
206 $plist->genPathMenu();
208 /* check if we are using account expiration */
209 $smarty->assign("hideMenus", FALSE);
210 if ($config->boolValueIsTrue("core","handleExpiredAccounts")){
211     $expired= ldap_expired_account($config, $ui->dn, $ui->username);
212  
213     if ($expired == POSIX_WARN_ABOUT_EXPIRATION && !session::is_set('POSIX_WARN_ABOUT_EXPIRATION__DONE')){
215         // The users password is about to xpire soon, display a warning message.
216         new log("security","gosa","",array(),"password for user \"$ui->username\" is about to expire") ;
217         msg_dialog::display(_("Password change"), _("Your password is about to expire, please change your password!"), INFO_DIALOG);
218         session::set('POSIX_WARN_ABOUT_EXPIRATION__DONE', TRUE);
220     } elseif ($expired == POSIX_FORCE_PASSWORD_CHANGE){
222         // The password is expired, we are now going to enforce a new one from the user.
224         // Hide the GOsa menus to avoid leaving the enforced password change dialog.
225         $smarty->assign("hideMenus", TRUE);
226         $plug = (isset($_GET['plug'])) ? $_GET['plug'] : null;
228         // Detect password plugin id:
229         $passId =  array_search('password', $plist->pluginList);
230         if($passId !== FALSE){
231             $_GET['plug'] = $passId;
232         }
233     }
236 if (isset($_GET['plug']) && $plist->plugin_access_allowed($_GET['plug'])){
237   $plug= validate($_GET['plug']);
238   $plugin_dir= $plist->get_path($plug);
239   session::global_set('plugin_dir',$plugin_dir);
240   if ($plugin_dir == ""){
241     new log("security","gosa","",array(),"main.php called with invalid plug parameter \"$plug\"") ;
242     header ("Location: logout.php");
243     exit;
244   }
245 } else {
247   /* set to welcome page as default plugin */
248   session::global_set('plugin_dir',"welcome");
249   $plugin_dir= "$BASE_DIR/plugins/generic/welcome";
252 /* Handle plugin locks.
253     - Remove the plugin from session if we switched to another. (cleanup) 
254     - Remove all created locks if "reset" was posted.
255     - Remove all created locks if we switched to another plugin.
256 */
257 $cleanup    = FALSE;
258 $remove_lock= FALSE;
260 /* Check if we have changed the selected plugin 
261 */
262 if($old_plugin_dir != $plugin_dir && $old_plugin_dir != ""){
263   if (is_file("$old_plugin_dir/main.inc")){
264     $cleanup = $remove_lock = TRUE;
265     require ("$old_plugin_dir/main.inc");
266     $cleanup = $remove_lock = FALSE;
267   }
268 }else // elseif
270 /* Reset was posted, remove all created locks for the current plugin
271 */
272 if((isset($_GET['reset']) && $_GET['reset'] == 1) || isset($_POST['delete_lock'])){
273   $remove_lock = TRUE;
276 /* Check for sizelimits */
277 eval_sizelimit();
279 /* Check for memory */
280 if (function_exists("memory_get_usage")){
281   if (memory_get_usage() > (to_byte(ini_get('memory_limit')) - 2048000 )){
282     msg_dialog::display(_("Configuration error"), _("Running out of memory!"), WARNING_DIALOG);
283   }
286 /* Redirect on back event */
287 if ($_SERVER["REQUEST_METHOD"] == "POST"){
289   /* Look for button events that match /^back[0-9]+$/,
290      extract the number and step the correct plugin. */
291   foreach ($_POST as $key => $value){
292     if (preg_match("/^back[0-9]+$/", $key)){
293       $back= substr($key, 4);
294       header ("Location: main.php?plug=$back");
295       exit;
296     }
297   }
300 /* Redirect on password back event */
301 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['password_back'])){
302   header ("Location: main.php");
303   exit;
306 /* Check for multiple windows logout */
307 if ($_SERVER["REQUEST_METHOD"] == "POST"){
308   if (isset($_POST['reset_session'])){
309     header ("Location: logout.php");
310     exit;
311   }
315 /* Load department list when plugin has changed. That is some kind of
316    compromise between speed and beeing up to date */
317 if (isset($_GET['reset'])){
318   set_object_info();
321 /* show web frontend */
322 $smarty->assign ("title","GOsa");
323 $smarty->assign ("logo", image(get_template_path("images/logo.png")));
324 $smarty->assign ("logoutimage", get_template_path("images/btn-logout.png"));
325 $smarty->assign ("date", date("l, dS F Y H:i:s O"));
326 $smarty->assign ("lang", preg_replace('/_.*$/', '', $lang));
327 $smarty->assign ("must", "<span class='required'>*</span>");
328 if (isset($plug)){
329   $plug= "?plug=$plug";
330 } else {
331   $plug= "";
333 if (session::global_get('js')==FALSE){
334   $smarty->assign("javascript", "false");
335   $smarty->assign("help_method", "href='helpviewer.php$plug' target='_blank'");
336 } else {
337   $smarty->assign("javascript", "true");
338   $smarty->assign("help_method"," onclick=\"return popup('helpviewer.php$plug','GOsa help');\"");
342 $loggedin = sprintf(_("You're logged in as %s"), 
343     "<span>".$ui->cn." [".$ui->username."] / ".$config->current['NAME']."</span> &nbsp;");
344 if($ui->ignore_acl_for_current_user()){
345     $loggedin = "<font color='red'>"._("ACLs are disabled")."</font>&nbsp;".$loggedin;
348 $smarty->assign ("loggedin", $loggedin);
349 $smarty->assign ("go_logo", get_template_path('images/go_logo.png'));
350 $smarty->assign ("go_base", get_template_path('images/dtree.png'));
351 $smarty->assign ("go_home", get_template_path('images/gohome.png'));
352 $smarty->assign ("go_out", get_template_path('images/logout.png'));
353 $smarty->assign ("go_top", get_template_path('images/go_top.png'));
354 $smarty->assign ("go_corner", get_template_path('images/go_corner.png'));
355 $smarty->assign ("go_left", get_template_path('images/go_left.png'));
356 $smarty->assign ("go_help", get_template_path('images/help.png'));
358 /* reload navigation if language changed*/  
359 if($reload_navigation){
360   $plist->menu="";
362 $smarty->assign ("menu", $plist->gen_menu());
363 $smarty->assign ("plug", "$plug");
366 /* React on clicks */
367 if ($_SERVER["REQUEST_METHOD"] == "POST"){
368   if (isset($_POST['delete_lock']) || isset($_POST['open_readonly'])){
370     /* Set old Post data */
371     if(session::global_is_set('LOCK_VARS_USED_GET')){
372       foreach(session::global_get('LOCK_VARS_USED_GET') as $name => $value){
373         $_GET[$name]  = $value;
374       } 
375     } 
376     if(session::global_is_set('LOCK_VARS_USED_POST')){
377       foreach(session::global_get('LOCK_VARS_USED_POST') as $name => $value){
378         $_POST[$name] = $value;
379       } 
380     }
381     if(session::global_is_set('LOCK_VARS_USED_REQUEST')){
382       foreach(session::global_get('LOCK_VARS_USED_REQUEST') as $name => $value){
383         $_REQUEST[$name] = $value;
384       } 
385     }
386   }
389 /* Load plugin */
390 if (is_file("$plugin_dir/main.inc")){
391   $display ="";
392   require ("$plugin_dir/main.inc");
393 } else {
394   msg_dialog::display(
395       _("Plug-in"),
396       sprintf(_("FATAL: Cannot find any plugin definitions for plugin %s!"), bold($plug)),
397       FATAL_ERROR_DIALOG);
398   exit();
402 /* Print_out last ErrorMessage repeated string. */
403 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
404 $smarty->assign ("pathMenu", $plist->genPathMenu());
405 $smarty->assign("contents", $display);
406 $smarty->assign("sessionLifetime", $config->get_cfg_value('core','sessionLifetime'));
408 /* If there's some post, take a look if everything is there... */
409 if (isset($_POST) && count($_POST)){
410   if (!isset($_POST['php_c_check'])){
411     msg_dialog::display(
412             _("Configuration Error"),
413             sprintf(_("FATAL: not all POST variables have been transfered by PHP - please inform your administrator!")),
414             FATAL_ERROR_DIALOG);
415     exit();
416   }
419 /* Assign erros to smarty */
420 if (session::is_set('errors')){
421   $smarty->assign("errors", session::get('errors'));
423 if ($error_collector != ""){
424   $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
425 } else {
426   $smarty->assign("php_errors", "");
429 /* Set focus to the error button if we've an error message */
430 $focus= "";
431 if (session::is_set('errors') && session::get('errors') != ""){
432   $focus= '<script language="JavaScript" type="text/javascript">';
433   $focus.= 'document.forms[0].error_accept.focus();';
434   $focus.= '</script>';
437 $focus= '<script language="JavaScript" type="text/javascript">';
438 $focus.= 'next_msg_dialog();';
439 $focus.= '</script>';
440 $smarty->assign("focus", $focus);
442 /* Set channel if needed */
443 #TODO: * move all global session calls to global_
444 #      * create a new channel where needed (mostly management dialogues)
445 #      * remove regulary created channels when not needed anymore
446 #      * take a look at external php calls (i.e. get fax, ldif, etc.)
447 #      * handle aborted sessions (by pressing anachors i.e. Main, Menu, etc.)
448 #      * check lock removals, is "dn" global or not in this case?
449 #      * last page request -> global or not?
450 #      * check that filters are still global
451 #      * maxC global?
452 if (isset($_POST['_channel_'])){
453         echo "DEBUG - current channel: ".$_POST['_channel_'];
454         $smarty->assign("channel", $_POST['_channel_']);
455 } else {
456         $smarty->assign("channel", "");
459 $display= "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl')).
460           $smarty->fetch(get_template_path('framework.tpl'));
462 /* Save dialog filters and selected base in a cookie. 
463    So we may be able to restore the filter an base settings on reload.
464 */
465 $cookie = array();
467 if(isset($_COOKIE['GOsa_Filter_Settings'])){
468   $cookie = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
469 }elseif(isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])){
470   $cookie = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
473 /* Save filters? */
474 if($config->get_cfg_value("core","storeFilterSettings") == "true"){
475   $cookie_vars = array("MultiDialogFilters","CurrentMainBase");
476   foreach($cookie_vars as $var){
477     if(session::global_is_set($var)){
478       $cookie[$ui->dn][$var] = session::global_get($var);
479     }
480   }
481   if(isset($_GET['plug'])){
482     $cookie[$ui->dn]['plug'] = $_GET['plug'];
483   }
484   @setcookie("GOsa_Filter_Settings",base64_encode(serialize($cookie)),time() + (60*60*24));
487 stats::show();
489 /* Show page... */
490 echo $display;
492 /* Save plist and config */
493 session::global_set('plist',$plist);
494 session::global_set('config',$config);
495 session::set('errorsAlreadyPosted',array());
497 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
498 ?>