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);
41 /* Remember everything we did after the last click */
42 session::start();
43 session::set('errorsAlreadyPosted',array());
44 session::global_set('runtime_cache',array());
45 session::set('limit_exceeded',FALSE);
47 // Count number of page reloads
48 if(!session::is_set('clicks')){
49 session::set('clicks', 0);
50 }
51 $clicks = session::get('clicks');
52 $clicks ++ ;
53 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 // Validate LDAP schema if not done already
82 if( $config->boolValueIsTrue('core','schemaCheck') &&
83 !$config->configRegistry->schemaCheckFinished() &&
84 !$config->configRegistry->validateSchemata($force=FALSE,$disableIncompatiblePlugins=TRUE)){
85 $config->configRegistry->displayRequirementErrors();
86 }
88 /* Enable compressed output */
89 if ($config->get_cfg_value("core","sendCompressedOutput") == "true"){
90 ob_start("ob_gzhandler");
91 }
93 /* Check for invalid sessions */
94 if(session::global_get('_LAST_PAGE_REQUEST') == ""){
95 session::global_set('_LAST_PAGE_REQUEST',time());
96 }else{
98 /* check GOsa.conf for defined session lifetime */
99 $max_life= $config->get_cfg_value("core","sessionLifetime");
101 /* get time difference between last page reload */
102 $request_time = (time()- session::global_get('_LAST_PAGE_REQUEST'));
104 /* If page wasn't reloaded for more than max_life seconds
105 * kill session
106 */
107 if($request_time > $max_life){
108 session::destroy();
109 new log("security","login","",array(),"main.php called without session - logging out") ;
110 header ("Location: logout.php");
111 exit;
112 }
113 session::global_set('_LAST_PAGE_REQUEST',time());
114 }
117 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
119 /* Set template compile directory */
120 $smarty->compile_dir= $config->get_cfg_value("core","templateCompileDirectory");
121 $smarty->error_unassigned= true;
123 /* Set default */
124 $reload_navigation = false;
126 /* Set last initialised language to current, browser settings */
127 if(!session::global_is_set('Last_init_lang')){
128 $reload_navigation = true;
129 session::global_set('Last_init_lang',get_browser_language());
130 }
132 /* If last language != current force navi reload */
133 $lang= get_browser_language();
134 if(session::global_get('Last_init_lang') != $lang){
135 $reload_navigation = true;
136 }
138 /* Language setup */
139 session::global_set('Last_init_lang',$lang);
141 /* Preset current main base */
142 if(!session::global_is_set('CurrentMainBase')){
143 session::global_set('CurrentMainBase',get_base_from_people($ui->dn));
144 }
146 putenv("LANGUAGE=");
147 putenv("LANG=$lang");
148 setlocale(LC_ALL, $lang);
149 $GLOBALS['t_language']= $lang;
150 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
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($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: 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 if (isset($_GET['plug']) && $plist->plugin_access_allowed($_GET['plug'])){
240 $plug= validate($_GET['plug']);
241 $plugin_dir= $plist->get_path($plug);
242 session::global_set('plugin_dir',$plugin_dir);
243 if ($plugin_dir == ""){
244 new log("security","gosa","",array(),"main.php called with invalid plug parameter \"$plug\"") ;
245 header ("Location: logout.php");
246 exit;
247 }
248 } else {
250 /* set to welcome page as default plugin */
251 session::global_set('plugin_dir',"welcome");
252 $plugin_dir= "$BASE_DIR/plugins/generic/welcome";
253 }
255 /* Handle plugin locks.
256 - Remove the plugin from session if we switched to another. (cleanup)
257 - Remove all created locks if "reset" was posted.
258 - Remove all created locks if we switched to another plugin.
259 */
260 $cleanup = FALSE;
261 $remove_lock= FALSE;
263 /* Check if we have changed the selected plugin
264 */
265 if($old_plugin_dir != $plugin_dir && $old_plugin_dir != ""){
266 if (is_file("$old_plugin_dir/main.inc")){
267 $cleanup = $remove_lock = TRUE;
268 require ("$old_plugin_dir/main.inc");
269 $cleanup = $remove_lock = FALSE;
270 }
271 }else // elseif
273 /* Reset was posted, remove all created locks for the current plugin
274 */
275 if((isset($_GET['reset']) && $_GET['reset'] == 1) || isset($_POST['delete_lock'])){
276 $remove_lock = TRUE;
277 }
279 /* Check for sizelimits */
280 eval_sizelimit();
282 /* Check for memory */
283 if (function_exists("memory_get_usage")){
284 if (memory_get_usage() > (to_byte(ini_get('memory_limit')) - 2048000 )){
285 msg_dialog::display(_("Configuration error"), _("Running out of memory!"), WARNING_DIALOG);
286 }
287 }
289 /* Redirect on back event */
290 if ($_SERVER["REQUEST_METHOD"] == "POST"){
292 /* Look for button events that match /^back[0-9]+$/,
293 extract the number and step the correct plugin. */
294 foreach ($_POST as $key => $value){
295 if (preg_match("/^back[0-9]+$/", $key)){
296 $back= substr($key, 4);
297 header ("Location: main.php?plug=$back");
298 exit;
299 }
300 }
301 }
303 /* Redirect on password back event */
304 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['password_back'])){
305 header ("Location: main.php");
306 exit;
307 }
309 /* Check for multiple windows logout */
310 if ($_SERVER["REQUEST_METHOD"] == "POST"){
311 if (isset($_POST['reset_session'])){
312 header ("Location: logout.php");
313 exit;
314 }
315 }
318 /* Load department list when plugin has changed. That is some kind of
319 compromise between speed and beeing up to date */
320 if (isset($_GET['reset'])){
321 set_object_info();
322 }
324 /* show web frontend */
325 $smarty->assign ("title","GOsa");
326 $smarty->assign ("logo", image(get_template_path("images/logo.png")));
327 $smarty->assign ("logoutimage", get_template_path("images/btn-logout.png"));
328 $smarty->assign ("date", date("l, dS F Y H:i:s O"));
329 $smarty->assign ("lang", preg_replace('/_.*$/', '', $lang));
330 $smarty->assign ("must", "<span class='required'>*</span>");
331 if (isset($plug)){
332 $plug= "?plug=$plug";
333 } else {
334 $plug= "";
335 }
336 if (session::global_get('js')==FALSE){
337 $smarty->assign("javascript", "false");
338 $smarty->assign("help_method", "href='helpviewer.php$plug' target='_blank'");
339 } else {
340 $smarty->assign("javascript", "true");
341 $smarty->assign("help_method"," onclick=\"return popup('helpviewer.php$plug','GOsa help');\"");
342 }
345 $loggedin = sprintf(_("You're logged in as %s"),
346 "<span>".$ui->cn." [".$ui->username."] / ".$config->current['NAME']."</span> ");
347 if($ui->ignore_acl_for_current_user()){
348 $loggedin = "<font color='red'>"._("ACLs are disabled")."</font> ".$loggedin;
349 }
351 $smarty->assign ("loggedin", $loggedin);
352 $smarty->assign ("go_logo", get_template_path('images/go_logo.png'));
353 $smarty->assign ("go_base", get_template_path('images/dtree.png'));
354 $smarty->assign ("go_home", get_template_path('images/gohome.png'));
355 $smarty->assign ("go_out", get_template_path('images/logout.png'));
356 $smarty->assign ("go_top", get_template_path('images/go_top.png'));
357 $smarty->assign ("go_corner", get_template_path('images/go_corner.png'));
358 $smarty->assign ("go_left", get_template_path('images/go_left.png'));
359 $smarty->assign ("go_help", get_template_path('images/help.png'));
361 /* reload navigation if language changed*/
362 if($reload_navigation){
363 $plist->menu="";
364 }
365 $smarty->assign ("menu", $plist->gen_menu());
366 $smarty->assign ("plug", "$plug");
369 /* React on clicks */
370 if ($_SERVER["REQUEST_METHOD"] == "POST"){
371 if (isset($_POST['delete_lock']) || isset($_POST['open_readonly'])){
373 /* Set old Post data */
374 if(session::global_is_set('LOCK_VARS_USED_GET')){
375 foreach(session::global_get('LOCK_VARS_USED_GET') as $name => $value){
376 $_GET[$name] = $value;
377 }
378 }
379 if(session::global_is_set('LOCK_VARS_USED_POST')){
380 foreach(session::global_get('LOCK_VARS_USED_POST') as $name => $value){
381 $_POST[$name] = $value;
382 }
383 }
384 if(session::global_is_set('LOCK_VARS_USED_REQUEST')){
385 foreach(session::global_get('LOCK_VARS_USED_REQUEST') as $name => $value){
386 $_REQUEST[$name] = $value;
387 }
388 }
389 }
390 }
392 /* Load plugin */
393 if (is_file("$plugin_dir/main.inc")){
394 $display ="";
395 require ("$plugin_dir/main.inc");
396 } else {
397 msg_dialog::display(
398 _("Plug-in"),
399 sprintf(_("FATAL: Cannot find any plugin definitions for plugin %s!"), bold($plug)),
400 FATAL_ERROR_DIALOG);
401 exit();
402 }
405 /* Print_out last ErrorMessage repeated string. */
406 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
407 $smarty->assign ("pathMenu", $plist->genPathMenu());
408 $smarty->assign("contents", $display);
409 $smarty->assign("sessionLifetime", $config->get_cfg_value('core','sessionLifetime'));
411 /* If there's some post, take a look if everything is there... */
412 if (isset($_POST) && count($_POST)){
413 if (!isset($_POST['php_c_check'])){
414 msg_dialog::display(
415 _("Configuration Error"),
416 sprintf(_("FATAL: not all POST variables have been transfered by PHP - please inform your administrator!")),
417 FATAL_ERROR_DIALOG);
418 exit();
419 }
420 }
422 /* Assign erros to smarty */
423 if (session::is_set('errors')){
424 $smarty->assign("errors", session::get('errors'));
425 }
426 if ($error_collector != ""){
427 $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
428 } else {
429 $smarty->assign("php_errors", "");
430 }
432 /* Set focus to the error button if we've an error message */
433 $focus= "";
434 if (session::is_set('errors') && session::get('errors') != ""){
435 $focus= '<script language="JavaScript" type="text/javascript">';
436 $focus.= 'document.forms[0].error_accept.focus();';
437 $focus.= '</script>';
438 }
440 $focus= '<script language="JavaScript" type="text/javascript">';
441 $focus.= 'next_msg_dialog();';
442 $focus.= '</script>';
443 $smarty->assign("focus", $focus);
445 /* Set channel if needed */
446 #TODO: * move all global session calls to global_
447 # * create a new channel where needed (mostly management dialogues)
448 # * remove regulary created channels when not needed anymore
449 # * take a look at external php calls (i.e. get fax, ldif, etc.)
450 # * handle aborted sessions (by pressing anachors i.e. Main, Menu, etc.)
451 # * check lock removals, is "dn" global or not in this case?
452 # * last page request -> global or not?
453 # * check that filters are still global
454 # * maxC global?
455 if (isset($_POST['_channel_'])){
456 echo "DEBUG - current channel: ".$_POST['_channel_'];
457 $smarty->assign("channel", $_POST['_channel_']);
458 } else {
459 $smarty->assign("channel", "");
460 }
462 $display= "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl')).
463 $smarty->fetch(get_template_path('framework.tpl'));
465 /* Save dialog filters and selected base in a cookie.
466 So we may be able to restore the filter an base settings on reload.
467 */
468 $cookie = array();
470 if(isset($_COOKIE['GOsa_Filter_Settings'])){
471 $cookie = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
472 }elseif(isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])){
473 $cookie = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
474 }
476 /* Save filters? */
477 if($config->get_cfg_value("core","storeFilterSettings") == "true"){
478 $cookie_vars = array("MultiDialogFilters","CurrentMainBase");
479 foreach($cookie_vars as $var){
480 if(session::global_is_set($var)){
481 $cookie[$ui->dn][$var] = session::global_get($var);
482 }
483 }
484 if(isset($_GET['plug'])){
485 $cookie[$ui->dn]['plug'] = $_GET['plug'];
486 }
487 @setcookie("GOsa_Filter_Settings",base64_encode(serialize($cookie)),time() + (60*60*24));
488 }
490 /* Show page... */
491 echo $display;
493 /* Save plist and config */
494 session::global_set('plist',$plist);
495 session::global_set('config',$config);
496 session::set('errorsAlreadyPosted',array());
498 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
499 ?>