Code

Check things only once
[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 pathNavigator::clear();
46 if ($_SERVER["REQUEST_METHOD"] == "POST"){
47   @DEBUG (DEBUG_POST, __LINE__, __FUNCTION__, __FILE__, $_POST, "_POST");
48 }
49 @DEBUG (DEBUG_POST, __LINE__, __FUNCTION__, __FILE__, session::get_all(), "_SESSION");
51 /* Logged in? Simple security check */
52 if (!session::global_is_set('config')){
53   new log("security","login","",array(),"main.php called without session - logging out") ;
54   header ("Location: logout.php");
55   exit;
56
58 /* Check for uniqe ip address */
59 $ui= session::global_get('ui');
60 if ($_SERVER['REMOTE_ADDR'] != $ui->ip){
61   new log("security","login","",array(),"main.php called with session which has a changed IP address.") ;
62   header ("Location: logout.php");
63   exit;
64 }
65 $config= session::global_get('config');
66 $config->check_and_reload();
67 $config->configRegistry->reload();
69 // Validate LDAP schema if not done already
70 if(!$config->configRegistry->schemaCheckFinished() && 
71     !$config->configRegistry->validateSchemata($force=FALSE,$disableIncompatiblePlugins=TRUE)){
72     $config->configRegistry->displayErrors();
73 }
75 /* Enable compressed output */
76 if ($config->get_cfg_value("core","sendCompressedOutput") == "true"){
77   ob_start("ob_gzhandler");
78 }
80 /* Check for invalid sessions */
81 if(session::global_get('_LAST_PAGE_REQUEST') == ""){
82   session::global_set('_LAST_PAGE_REQUEST',time());
83 }else{
85   /* check GOsa.conf for defined session lifetime */
86   $max_life= $config->get_cfg_value("core","sessionLifetime");
88   /* get time difference between last page reload */
89   $request_time = (time()- session::global_get('_LAST_PAGE_REQUEST'));
91   /* If page wasn't reloaded for more than max_life seconds 
92    * kill session
93    */
94   if($request_time > $max_life){
95     session::destroy();
96     new log("security","login","",array(),"main.php called without session - logging out") ;
97     header ("Location: logout.php");
98     exit;
99   }
100   session::global_set('_LAST_PAGE_REQUEST',time());
104 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
106 /* Set template compile directory */
107 $smarty->compile_dir= $config->get_cfg_value("core","templateCompileDirectory");
108 $smarty->error_unassigned= true;
110 /* Set default */
111 $reload_navigation = false;
113 /* Set last initialised language to current, browser settings */
114 if(!session::global_is_set('Last_init_lang')){
115   $reload_navigation = true;
116   session::global_set('Last_init_lang',get_browser_language());
119 /* If last language != current force navi reload */
120 $lang= get_browser_language();
121 if(session::global_get('Last_init_lang') != $lang){
122   $reload_navigation = true;
125 /* Language setup */
126 session::global_set('Last_init_lang',$lang);
128 /* Preset current main base */
129 if(!session::global_is_set('CurrentMainBase')){
130   session::global_set('CurrentMainBase',get_base_from_people($ui->dn));
133 putenv("LANGUAGE=");
134 putenv("LANG=$lang");
135 setlocale(LC_ALL, $lang);
136 $GLOBALS['t_language']= $lang;
137 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
139 /* Check if the config is up to date */
140 $config->check_config_version();
142 /* Set the text domain as 'messages' */
143 $domain = 'messages';
144 bindtextdomain($domain, LOCALE_DIR);
145 textdomain($domain);
146 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
148 /* Prepare plugin list */
149 if (!session::global_is_set('plist')){
150   /* Initially load all classes */
151   $class_list= get_declared_classes();
152   foreach ($class_mapping as $class => $path){
153     if (!in_array($class, $class_list)){
154       if (is_readable("$BASE_DIR/$path")){
155         require_once("$BASE_DIR/$path");
156       } else {
157         msg_dialog::display(_("Fatal error"),
158             sprintf(_("Cannot locate file %s - please run %s to fix this"),
159               bold("$BASE_DIR/$path"), bold("update-gosa")), FATAL_ERROR_DIALOG);
160         exit;
161       }
162     }
163   }
165   session::global_set('plist', new pluglist($config, $ui));
167   /* Load ocMapping into userinfo */
168   $tmp= new acl($config, NULL, $ui->dn);
169   $ui->ocMapping= $tmp->ocMapping;
170   session::global_set('ui',$ui);
172 $plist= session::global_get('plist');
174 /* Check for register globals */
175 if (isset($global_check) && $config->boolValueIsTrue("core","forceGlobals")){
176   msg_dialog::display(
177             _("PHP configuration"),
178             _("FATAL: Register globals is active. Please fix this in order to continue."),
179             FATAL_ERROR_DIALOG);
181   new log("security","login","",array(),"Register globals is on. For security reasons, this should be turned off.") ;
182   session::destroy ();
183   exit;
186 /* Check Plugin variable */
187 if (session::global_is_set('plugin_dir')){
188   $old_plugin_dir= session::global_get('plugin_dir');
189 } else {
190   $old_plugin_dir= "";
192 if (isset($_GET['plug']) && $plist->plugin_access_allowed($_GET['plug'])){
193   $plug= validate($_GET['plug']);
194   $plugin_dir= $plist->get_path($plug);
195   session::global_set('plugin_dir',$plugin_dir);
196   if ($plugin_dir == ""){
197     new log("security","gosa","",array(),"main.php called with invalid plug parameter \"$plug\"") ;
198     header ("Location: logout.php");
199     exit;
200   }
201 } else {
203   /* set to welcome page as default plugin */
204   session::global_set('plugin_dir',"welcome");
205   $plugin_dir= "$BASE_DIR/plugins/generic/welcome";
208 /* Handle plugin locks.
209     - Remove the plugin from session if we switched to another. (cleanup) 
210     - Remove all created locks if "reset" was posted.
211     - Remove all created locks if we switched to another plugin.
212 */
213 $cleanup    = FALSE;
214 $remove_lock= FALSE;
216 /* Check if we have changed the selected plugin 
217 */
218 if($old_plugin_dir != $plugin_dir && $old_plugin_dir != ""){
219   if (is_file("$old_plugin_dir/main.inc")){
220     $cleanup = $remove_lock = TRUE;
221     require ("$old_plugin_dir/main.inc");
222     $cleanup = $remove_lock = FALSE;
223   }
224 }else // elseif
226 /* Reset was posted, remove all created locks for the current plugin
227 */
228 if((isset($_GET['reset']) && $_GET['reset'] == 1) || isset($_POST['delete_lock'])){
229   $remove_lock = TRUE;
232 /* Check for sizelimits */
233 eval_sizelimit();
235 /* Check for memory */
236 if (function_exists("memory_get_usage")){
237   if (memory_get_usage() > (to_byte(ini_get('memory_limit')) - 2048000 )){
238     msg_dialog::display(_("Configuration error"), _("Running out of memory!"), WARNING_DIALOG);
239   }
242 /* Redirect on back event */
243 if ($_SERVER["REQUEST_METHOD"] == "POST"){
245   /* Look for button events that match /^back[0-9]+$/,
246      extract the number and step the correct plugin. */
247   foreach ($_POST as $key => $value){
248     if (preg_match("/^back[0-9]+$/", $key)){
249       $back= substr($key, 4);
250       header ("Location: main.php?plug=$back");
251       exit;
252     }
253   }
256 /* Redirect on password back event */
257 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['password_back'])){
258   header ("Location: main.php");
259   exit;
262 /* Check for multiple windows logout */
263 if ($_SERVER["REQUEST_METHOD"] == "POST"){
264   if (isset($_POST['reset_session'])){
265     header ("Location: logout.php");
266     exit;
267   }
271 /* Load department list when plugin has changed. That is some kind of
272    compromise between speed and beeing up to date */
273 if (isset($_GET['reset'])){
274   set_object_info();
277 /* show web frontend */
278 $smarty->assign ("title","GOsa");
279 $smarty->assign ("logo", image(get_template_path("images/logo.png")));
280 $smarty->assign ("logoutimage", get_template_path("images/btn-logout.png"));
281 $smarty->assign ("date", date("l, dS F Y H:i:s O"));
282 $smarty->assign ("lang", preg_replace('/_.*$/', '', $lang));
283 $smarty->assign ("must", "<span class='required'>*</span>");
284 if (isset($plug)){
285   $plug= "?plug=$plug";
286 } else {
287   $plug= "";
289 if (session::global_get('js')==FALSE){
290   $smarty->assign("javascript", "false");
291   $smarty->assign("help_method", "href='helpviewer.php$plug' target='_blank'");
292 } else {
293   $smarty->assign("javascript", "true");
294   $smarty->assign("help_method"," onclick=\"return popup('helpviewer.php$plug','GOsa help');\"");
297 if($ui->ignore_acl_for_current_user()){
298   $smarty->assign ("loggedin", "<font color='red'>"._("ACLs are disabled")."</font>&nbsp;".sprintf(_("You're logged in as %s"), "<span>".$ui->cn." [".$ui->username."]</span>"));
299 }else{
300   $smarty->assign ("loggedin", sprintf(_("You're logged in as %s"), "<span>".$ui->cn." [".$ui->username."]</span>"));
302 $smarty->assign ("go_logo", get_template_path('images/go_logo.png'));
303 $smarty->assign ("go_base", get_template_path('images/dtree.png'));
304 $smarty->assign ("go_home", get_template_path('images/gohome.png'));
305 $smarty->assign ("go_out", get_template_path('images/logout.png'));
306 $smarty->assign ("go_top", get_template_path('images/go_top.png'));
307 $smarty->assign ("go_corner", get_template_path('images/go_corner.png'));
308 $smarty->assign ("go_left", get_template_path('images/go_left.png'));
309 $smarty->assign ("go_help", get_template_path('images/help.png'));
311 /* reload navigation if language changed*/  
312 if($reload_navigation){
313   $plist->menu="";
315 $smarty->assign ("menu", $plist->gen_menu());
316 $smarty->assign ("plug", "$plug");
319 /* React on clicks */
320 if ($_SERVER["REQUEST_METHOD"] == "POST"){
321   if (isset($_POST['delete_lock']) || isset($_POST['open_readonly'])){
323     /* Set old Post data */
324     if(session::global_is_set('LOCK_VARS_USED_GET')){
325       foreach(session::global_get('LOCK_VARS_USED_GET') as $name => $value){
326         $_GET[$name]  = $value;
327       } 
328     } 
329     if(session::global_is_set('LOCK_VARS_USED_POST')){
330       foreach(session::global_get('LOCK_VARS_USED_POST') as $name => $value){
331         $_POST[$name] = $value;
332       } 
333     }
334     if(session::global_is_set('LOCK_VARS_USED_REQUEST')){
335       foreach(session::global_get('LOCK_VARS_USED_REQUEST') as $name => $value){
336         $_REQUEST[$name] = $value;
337       } 
338     }
339   }
342 /* check if we are using account expiration */
343 if ($config->get_cfg_value("core","handleExpiredAccounts") == "true"){
344   $expired= ldap_expired_account($config, $ui->dn, $ui->username);
346   if ($expired == 2){
347     new log("security","gosa","",array(),"password for user \"$ui->username\" is about to expire") ;
348     msg_dialog::display(_("Password change"), _("Your password is about to expire, please change your password!"), INFO_DIALOG);
349   }
352 /* Load plugin */
353 if (is_file("$plugin_dir/main.inc")){
354   $display ="";
355   require ("$plugin_dir/main.inc");
356 } else {
357   msg_dialog::display(
358       _("Plugin"),
359       sprintf(_("FATAL: Cannot find any plugin definitions for plugin %s!"), bold($plug)),
360       FATAL_ERROR_DIALOG);
361   exit();
365 /* Print_out last ErrorMessage repeated string. */
366 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
367 $smarty->assign ("pathMenu", $plist->genPathMenu());
368 $smarty->assign("contents", $display);
369 $smarty->assign("sessionLifetime", $config->get_cfg_value('core','sessionLifetime'));
371 /* If there's some post, take a look if everything is there... */
372 if (isset($_POST) && count($_POST)){
373   if (!isset($_POST['php_c_check'])){
374     msg_dialog::display(
375             _("Configuration Error"),
376             sprintf(_("FATAL: not all POST variables have been transfered by PHP - please inform your administrator!")),
377             FATAL_ERROR_DIALOG);
378     exit();
379   }
382 /* Assign erros to smarty */
383 if (session::is_set('errors')){
384   $smarty->assign("errors", session::get('errors'));
386 if ($error_collector != ""){
387   $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
388 } else {
389   $smarty->assign("php_errors", "");
392 /* Set focus to the error button if we've an error message */
393 $focus= "";
394 if (session::is_set('errors') && session::get('errors') != ""){
395   $focus= '<script language="JavaScript" type="text/javascript">';
396   $focus.= 'document.forms[0].error_accept.focus();';
397   $focus.= '</script>';
400 $focus= '<script language="JavaScript" type="text/javascript">';
401 $focus.= 'next_msg_dialog();';
402 $focus.= '</script>';
403 $smarty->assign("focus", $focus);
405 /* Set channel if needed */
406 #TODO: * move all global session calls to global_
407 #      * create a new channel where needed (mostly management dialogues)
408 #      * remove regulary created channels when not needed anymore
409 #      * take a look at external php calls (i.e. get fax, ldif, etc.)
410 #      * handle aborted sessions (by pressing anachors i.e. Main, Menu, etc.)
411 #      * check lock removals, is "dn" global or not in this case?
412 #      * last page request -> global or not?
413 #      * check that filters are still global
414 #      * maxC global?
415 if (isset($_POST['_channel_'])){
416         echo "DEBUG - current channel: ".$_POST['_channel_'];
417         $smarty->assign("channel", $_POST['_channel_']);
418 } else {
419         $smarty->assign("channel", "");
422 $display= "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl')).
423           $smarty->fetch(get_template_path('framework.tpl'));
425 /* Save dialog filters and selected base in a cookie. 
426    So we may be able to restore the filter an base settings on reload.
427 */
428 $cookie = array();
430 if(isset($_COOKIE['GOsa_Filter_Settings'])){
431   $cookie = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
432 }elseif(isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])){
433   $cookie = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
436 /* Save filters? */
437 if($config->get_cfg_value("core","storeFilterSettings") == "true"){
438   $cookie_vars = array("MultiDialogFilters","CurrentMainBase");
439   foreach($cookie_vars as $var){
440     if(session::global_is_set($var)){
441       $cookie[$ui->dn][$var] = session::global_get($var);
442     }
443   }
444   if(isset($_GET['plug'])){
445     $cookie[$ui->dn]['plug'] = $_GET['plug'];
446   }
447   @setcookie("GOsa_Filter_Settings",base64_encode(serialize($cookie)),time() + (60*60*24));
450 /* Show page... */
451 echo $display;
453 /* Save plist and config */
454 session::global_set('plist',$plist);
455 session::global_set('config',$config);
456 session::set('errorsAlreadyPosted',array());
458 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
459 ?>