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