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