Code

Added speed improvements
[gosa.git] / html / main.php
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 /* Basic setup, remove eventually registered sessions */
22 $start = microtime();
23 $timing= array();
24 require_once ("../include/php_setup.inc");
25 require_once ("functions.inc");
26 header("Content-type: text/html; charset=UTF-8");
28 /* Find all class files and include them */
29 get_dir_list("$BASE_DIR/plugins");
31 /* Set the text domain as 'messages' */
32 $domain = 'messages';
33 bindtextdomain($domain, "$BASE_DIR/locale");
34 textdomain($domain);
36 /* Set cookie lifetime to one day */
37 session_set_cookie_params(24*60*60);
39 /* Remember everything we did after the last click */
40 session_start ();
43 if ($_SERVER["REQUEST_METHOD"] == "POST"){
44   @DEBUG (DEBUG_POST, __LINE__, __FUNCTION__, __FILE__, $_POST, "_POST");
45 }
46 @DEBUG (DEBUG_POST, __LINE__, __FUNCTION__, __FILE__, $_SESSION, "_SESSION");
48 /* Logged in? Simple security check */
49 if (!isset($_SESSION['config'])){
50   gosa_log ("main.php called without session - logging out");
51   header ("Location: logout.php");
52   exit;
53
55 /* Reset errors */
56 $_SESSION['errors']= "";
58 /* Check for uniqe ip address */
59 $ui= $_SESSION["ui"];
60 if ($_SERVER['REMOTE_ADDR'] != $ui->ip){
61   gosa_log ("main.php called with session which has a changed IP address.", 3);
62   header ("Location: logout.php");
63   exit;
64 }
65 $config= $_SESSION['config'];
68 /* Check for invalid sessions */
69 if(empty($_SESSION['_LAST_PAGE_REQUEST'])){
70   $_SESSION['_LAST_PAGE_REQUEST']= time();
71 }else{
73   /* check GOsa.conf for defined session lifetime */
74   if(isset($config->data['MAIN']['SESSION_LIFETIME'])){
75     $max_life = $config->data['MAIN']['SESSION_LIFETIME'];
76   }else{
77     $max_life = 60*60*2;
78   }
80   /* get time difference between last page reload */
81   $request_time = (time()-$_SESSION['_LAST_PAGE_REQUEST']);
83   /* If page wasn't reloaded for more than max_life seconds 
84    * kill session
85    */
86   if($request_time > $max_life){
87     session_unset();
88     gosa_log ("main.php called without session - logging out");
89     header ("Location: logout.php");
90     exit;
91   }
92 //  echo "Session was ".$request_time." s inactive";
93   $_SESSION['_LAST_PAGE_REQUEST'] = time();
94 }
97 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
99 /* Set template compile directory */
100 if (isset ($config->data['MAIN']['COMPILE'])){
101   $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
102 } else {
103   $smarty->compile_dir= '/var/spool/gosa/';
106 /* Set default */
107 $reload_navigation = false;
109 /* Set last initialised language to current, browser settings */
110 if((!isset($_SESSION['Last_init_lang']))){
111   $reload_navigation = true;
112   $_SESSION['Last_init_lang'] = get_browser_language();
115 /* Language setup */
116 if ($config->data['MAIN']['LANG'] == ""){
117   
118   /* If last language != current force navi reload */
119   if($_SESSION['Last_init_lang'] != get_browser_language()){
120     $reload_navigation = true;
121   }
122   $lang= get_browser_language();
123   $_SESSION['Last_init_lang'] = $lang;
124 } else {
125   $lang= $config->data['MAIN']['LANG'];
128 $lang.=".UTF-8";
129 putenv("LANGUAGE=");
130 putenv("LANG=$lang");
131 setlocale(LC_ALL, $lang);
132 $GLOBALS['t_language']= $lang;
133 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
135 /* Set the text domain as 'messages' */
136 $domain = 'messages';
137 bindtextdomain($domain, "$BASE_DIR/locale");
138 textdomain($domain);
139 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
141 /* Prepare plugin list */
142 if (!isset($_SESSION['plist'])){
143   $_SESSION['plist']= new pluglist($config, $ui);
145 $plist= $_SESSION['plist'];
147 /* Check for register globals */
148 if (isset($global_check) && $config->data['MAIN']['FORCEGLOBALS'] == 'true'){
149   print_red (_("Register globals is on. GOsa will refuse to login unless this is fixed by an administrator."));
150   echo $_SESSION['errors'];
151   gosa_log ("Register globals is on. For security reasons, this should be turned off.");
152   session_destroy ();
153   exit ();
156 /* Check Plugin variable */
157 if (isset($_SESSION['plugin_dir'])){
158   $old_plugin_dir= $_SESSION['plugin_dir'];
159 } else {
160   $old_plugin_dir= "";
162 if (isset($_GET['plug'])){
163   $plug= validate($_GET['plug']);
164   $plugin_dir= $plist->get_path($plug);
165   $_SESSION['plugin_dir']= $plugin_dir;
166   if ($plugin_dir == ""){
167     gosa_log ("main.php called with invalid plug parameter \"$plug\"", 3);
168     header ("Location: logout.php");
169     exit;
170   }
171 } else {
172   /* set to welcome page as default plugin */
173   $_SESSION['plugin_dir']= "welcome";
174   $plugin_dir= "$BASE_DIR/plugins/generic/welcome";
177 /* Check if we need to delete a lock */
178 if ($old_plugin_dir != $plugin_dir){
179   if (is_file("$old_plugin_dir/main.inc")){
180     $remove_lock= true;
181     require_once ("$old_plugin_dir/main.inc");
182   }
184 $remove_lock= false;
186 /* Check for sizelimits */
187 eval_sizelimit();
189 /* Check for memory */
190 if (function_exists("memory_get_usage")){
191   if (memory_get_usage() > (to_byte(ini_get('memory_limit')) - 2048000 )){
192     print_red(_("Warning: memory is getting low - please increase the memory_limit!"));
193   }
196 /* Redirect on back event */
197 if ($_SERVER["REQUEST_METHOD"] == "POST"){
199   /* Look for button events that match /^back[0-9]+$/,
200      extract the number and step the correct plugin. */
201   foreach ($_POST as $key => $value){
202     if (preg_match("/^back[0-9]+$/", $key)){
203       $back= substr($key, 4);
204       header ("Location: main.php?plug=$back");
205       exit;
206     }
207   }
210 /* Redirect on password back event */
211 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['password_back'])){
212   header ("Location: main.php");
213   exit;
216 /* Check for multiple windows logout */
217 if ($_SERVER["REQUEST_METHOD"] == "POST"){
218   if (isset($_POST['reset_session'])){
219     header ("Location: logout.php");
220     exit;
221   }
223   if (isset($_POST['cancel_lock'])){
224     unset ($_SESSION['dn']);
225   }
229 /* Load department list when plugin has changed. That is some kind of
230    compromise between speed and beeing up to date */
231 if (isset($_GET['reset'])){
232   if (!isset($_SESSION['cached_departments'])){
233     $_SESSION['cached_departments']= get_departments();
234   }
235   $config->departments= $_SESSION['cached_departments'];
236   $config->make_idepartments ();
237   if (isset($_SESSION['objectinfo'])){
238     unset ($_SESSION['objectinfo']);
239   }
242 /* Install eGOsa hooks, convert _POST to _SESSION['POST'] */
243 if(isset($_GET['explorer'])){
244   $_SESSION{'eGosa'}=1;
246 if(isset($_SESSION['POST'])){
247   $_SERVER["REQUEST_METHOD"] = "POST";
248   foreach ($_SESSION['POST'] as $key => $dummy){
249     $_POST[$key]=$_SESSION['POST'][$key];
250   }
251   unset($_SESSION['POST']);
254 /* show web frontend */
255 $smarty->assign ("date", date("l, dS F Y H:i:s O"));
256 $smarty->assign ("must", "<font class=\"must\">*</font>");
257 if (isset($plug)){
258   $plug= "?plug=$plug";
259 } else {
260   $plug= "";
262 if ($_SESSION['js']==FALSE){
263   $smarty->assign("javascript", "false");
264   $smarty->assign("help_method", "href='helpviewer.php$plug' target='_blank'");
265 } else {
266   $smarty->assign("javascript", "true");
267   $smarty->assign("help_method"," onclick=\"return popup('helpviewer.php$plug','GOsa help');\"");
270 $smarty->assign ("username", $ui->username);
271 $smarty->assign ("go_logo", get_template_path('images/go_logo.png'));
272 $smarty->assign ("go_base", get_template_path('images/dtree.png'));
273 $smarty->assign ("go_home", get_template_path('images/gohome.png'));
274 $smarty->assign ("go_out", get_template_path('images/stop.png'));
275 $smarty->assign ("go_top", get_template_path('images/go_top.png'));
276 $smarty->assign ("go_corner", get_template_path('images/go_corner.png'));
277 $smarty->assign ("go_left", get_template_path('images/go_left.png'));
278 $smarty->assign ("go_help", get_template_path('images/help.png'));
280 /* reload navigation if language changed*/  
281 if($reload_navigation){
282   $plist->menu="";;
284 $plist->gen_headlines();
285 $plist->gen_menu();
286 $smarty->assign ("menu", $plist->menu);
287 $smarty->assign ("plug", "$plug");
289 $header= "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
292 /* React on clicks */
293 if ($_SERVER["REQUEST_METHOD"] == "POST"){
295   /* 'delete_lock' is set by the lock removal dialog. We should remove the
296      lock at this point globally. Plugins do not need to remove it. */
297   if (isset($_POST['delete_lock']) && isset($_SESSION['dn'])){
298     del_lock ($_SESSION['dn']);
299     sess_del ('dn');
300   }
303   /* I don't want multiple browser windows open. One way to check it is
304      to count a hidden field and warn the user if SESSION and INPUT count
305      differ. */
306   if (isset($_POST['session_cnt'])){
307     if ($_POST['session_cnt'] != $_SESSION['session_cnt']){
308       $smarty->display(get_template_path('conflict.tpl'));
309       exit ();
310     }
311     $_SESSION['session_cnt']= $_SESSION['session_cnt'] + 1;
312     $_SESSION['post_cnt']= validate($_POST['session_cnt']) + 1;
313   }
316 /* Only generate hidden click counter, if post_cnt is defined */
317 if (isset ($_SESSION['post_cnt'])){
318   echo "<input type=\"hidden\" name=\"session_cnt\" value=\"".$_SESSION['post_cnt']."\">\n";
321 /* Load plugin */
322 if (is_file("$plugin_dir/main.inc")){
323   require_once ("$plugin_dir/main.inc");
324 } else {
325   print_red(sprintf(_("Can't find any plugin definitions for plugin '%s'!"), $plug));
326   echo $_SESSION['errors'];
327   exit();
330 /* Close div/tables */
331 $smarty->assign("contents", $display);
332 if (isset($_SESSION['errors'])){
333   $smarty->assign("errors", $_SESSION['errors']);
335 if ($error_collector != ""){
336   $smarty->assign("php_errors", $error_collector."</div>");
337 } else {
338   $smarty->assign("php_errors", "");
340 $display= $header.$smarty->fetch(get_template_path('framework.tpl'));
342 if ((isset($config->data['MAIN']['W3CTEST']) && preg_match('/true/i', $config->data['MAIN']['W3CTEST']))&&(!empty($display))&&(is_callable("tidy_parse_string"))) {
343   tidy_parse_string(utf8_decode($display));
344   $err = nl2br(htmlentities(tidy_get_error_buffer()));
345   
346   if($err){
347     echo "<table summary=\"\" width=\"100%\" style='background-color:#E0E0E0;border-bottom:1px solid black'><tr><td><img alt=\"W3C\"            align=\"middle\" src='images/warning.png'>&nbsp;<font style='font-size:14px;font-weight:bold'>"._("Generating this page caused the W3C          conformance checker to raise some errors!")."</font></td><td align=right><button onClick='toggle(\"w3cbox\")'>"._("Toggle information")."</     button></td></tr></table><div id='w3cbox' style='width:100%; position:absolute; z-index:0; visibility: hidden; background-color:white; border-  bottom:1px solid black;'>";
348     echo $err."</div>";
349   }
351   tidy_clean_repair($display);
354 /* Show page... */
355 echo $display;
357 /* Save plist */
358 $_SESSION['plist']= $plist;
360 /* Echo compilation time * /
361 $r = split(" ",$start);
362 $ms = $r[0];
363 $s= $r[1];
365 $re = split(" ",microtime());
366 $mse = $re[0];
367 $se= $re[1];
369 $add = 0;
370 if(($mse -$ms)<0){
371   $se --;
372   $add = 1;
374 echo ($se -$s).",";
375 echo (int)(($add+($mse -$ms))*1000)." s";
376 */
377 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
378 ?>