Code

Enabled smarty unassigned errors
[gosa.git] / gosa-core / html / index.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 /* Load required includes */
24 require_once "../include/php_setup.inc";
25 require_once "functions.inc";
26 require_once "class_log.inc";
27 header("Content-type: text/html; charset=UTF-8");
30 /**
31  * Display the login page and exit().
32  *
33  */
34 function displayLogin()
35 {
36     global $smarty,$message,$config,$ssl,$error_collector, $BASE_DIR;
37     error_reporting(E_ALL | E_STRICT);
39     /* Check theme compatibility */
40     $theme= $config->get_cfg_value('theme', 'default');
41     if (file_exists("$BASE_DIR/ihtml/themes/$theme/blacklist")) {
42         $blocks= file("$BASE_DIR/ihtml/themes/$theme/blacklist");
43         foreach ($blocks as $block) {
44             if (preg_match('/'.preg_quote($block).'/', $_SERVER['HTTP_USER_AGENT'])) {
45                 die(sprintf(_("Your browser (%s) is blacklisted for the current theme."), $block));
46             }
47         }
48     }
50     /* Fill template with required values */
51     $username = "";
52     if(isset($_POST["username"])) {
53         $username= $_POST["username"];
54     }
55     $smarty->assign("logo", image(get_template_path("images/logo.png")));
56     $smarty->assign('date', gmdate("D, d M Y H:i:s"));
57     $smarty->assign('username', $username);
58     $smarty->assign('personal_img', get_template_path('images/login-head.png'));
59     $smarty->assign('password_img', get_template_path('images/password.png'));
60     $smarty->assign('directory_img', get_template_path('images/ldapserver.png'));
62     /* Some error to display? */
63     if (!isset($message)) {
64         $message= "";
65     }
66     $smarty->assign("message", $message);
68     /* Displasy SSL mode warning? */
69     if ($ssl != "" && $config->get_cfg_value('warnssl') == 'true') {
70         $smarty->assign("ssl", sprintf(_("This session is not ecrypted. Click %s to enter an encrypted session."), "<a href=\"$ssl\">"._("here")."</a>"));
71     } else {
72         $smarty->assign("ssl", "");
73     }
75     if(!$config->check_session_lifetime()) {
76         $smarty->assign ("lifetime", _("The session lifetime configured in your gosa.conf will be overridden by php.ini settings."));
77     } else {
78         $smarty->assign ("lifetime", "");
79     }
81     /* Generate server list */
82     $servers= array();
83     if (isset($_POST['server'])) {
84         $selected= validate($_POST['server']);
85     } else {
86         $selected= $config->data['MAIN']['DEFAULT'];
87     }
88     foreach ($config->data['LOCATIONS'] as $key => $ignored) {
89         $servers[$key]= $key;
90     }
91     $smarty->assign ("server_options", $servers);
92     $smarty->assign ("server_id", $selected);
94     /* show login screen */
95     $smarty->assign ("PHPSESSID", session_id());
96     if (session::is_set('errors')) {
97         $smarty->assign("errors", session::get('errors'));
98     }
99     if ($error_collector != "") {
100         $smarty->assign("php_errors", $error_collector."</div>");
101     } else {
102         $smarty->assign("php_errors", "");
103     }
104     $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
105     $smarty->assign("iePngWorkaround", $config->get_cfg_value("iePngWorkaround","false" ) == "true");
106     $smarty->assign("usePrototype", "false");
107     $smarty->display (get_template_path('headers.tpl'));
108     $smarty->assign("version",get_gosa_version());
109     $smarty->display(get_template_path('login.tpl'));
110     exit();
115 /*****************************************************************************
116  *                               M   A   I   N                               *
117  *****************************************************************************/
119 /* Set error handler to own one, initialize time calculation
120 and start session. */
121 session::start();
122 session::set('errorsAlreadyPosted',array());
124 /* Destroy old session if exists. 
125 Else you will get your old session back, if you not logged out correctly. */
126 if(is_array(session::get_all()) && count(session::get_all())) {
127     session::destroy();
128     session::start();
131 $username= "";
133 /* Reset errors */
134 session::set('errors',"");
135 session::set('errorsAlreadyPosted',"");
136 session::set('LastError',"");
138 /* Check if we need to run setup */
139 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)) {
140     header("location:setup.php");
141     exit();
144 /* Reset errors */
145 session::set('errors',"");
147 /* Check for java script */
148 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
149     session::global_set('js',TRUE);
150 }elseif(isset($_POST['javascript'])) {
151     session::global_set('js',FALSE);
154 /* Check if gosa.conf (.CONFIG_FILE) is accessible */
155 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)) {
156     msg_dialog::display(_("Configuration error"),sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE),FATAL_ERROR_DIALOG);
157     exit();
160 /* Parse configuration file */
161 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
162 session::global_set('DEBUGLEVEL',$config->get_cfg_value('DEBUGLEVEL'));
163 if ($_SERVER["REQUEST_METHOD"] != "POST") {
164     @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
167 /* Enable compressed output */
168 if ($config->get_cfg_value("sendCompressedOutput") != "") {
169     ob_start("ob_gzhandler");
172 /* Set template compile directory */
173 $smarty->compile_dir= $config->get_cfg_value("templateCompileDirectory", '/var/spool/gosa');
174 $smarty->error_unassigned= true;
176 /* Check for compile directory */
177 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
178     msg_dialog::display(_("Smarty error"),sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
179         $smarty->compile_dir),FATAL_ERROR_DIALOG);
180     exit();
183 /* Check for old files in compile directory */
184 clean_smarty_compile_dir($smarty->compile_dir);
186 /* Language setup */
187 $lang= get_browser_language();
188 putenv("LANGUAGE=");
189 putenv("LANG=$lang");
190 setlocale(LC_ALL, $lang);
191 $GLOBALS['t_language']= $lang;
192 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
194 /* Set the text domain as 'messages' */
195 $domain = 'messages';
196 bindtextdomain($domain, LOCALE_DIR);
197 textdomain($domain);
198 $smarty->assign ('nextfield', 'username');
200 if ($_SERVER["REQUEST_METHOD"] != "POST") {
201     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
205 /* Check for SSL connection */
206 $ssl= "";
207 if (!isset($_SERVER['HTTPS']) ||
208     !stristr($_SERVER['HTTPS'], "on")) {
210         if (empty($_SERVER['REQUEST_URI'])) {
211             $ssl= "https://".$_SERVER['HTTP_HOST'].
212                 $_SERVER['PATH_INFO'];
213         } else {
214             $ssl= "https://".$_SERVER['HTTP_HOST'].
215                 $_SERVER['REQUEST_URI'];
216         }
217     }
219 /* If SSL is forced, just forward to the SSL enabled site */
220 if ($config->get_cfg_value("forcessl") == 'true' && $ssl != '') {
221     header ("Location: $ssl");
222     exit;
225 /* Do we have htaccess authentification enabled? */
226 $htaccess_authenticated= FALSE;
227 if ($config->get_cfg_value("htaccessAuthentication") == "true" ) {
228     if (!isset($_SERVER['REMOTE_USER'])) {
229         msg_dialog::display(_("Configuration error"), _("There is a problem with the authentication setup!"), FATAL_ERROR_DIALOG);
230         exit;
231     }
233     $tmp= process_htaccess($_SERVER['REMOTE_USER'], isset($_SERVER['KRB5CCNAME']));
234     $username= $tmp['username'];
235     $server= $tmp['server'];
236     if ($username == "") {
237         msg_dialog::display(_("Error"), _("Cannot find a valid user for the current authentication setup!"), FATAL_ERROR_DIALOG);
238         exit;
239     }
240     if ($server == "") {
241         msg_dialog::display(_("Error"), _("User information is not unique accross the configured LDAP trees!"), FATAL_ERROR_DIALOG);
242         exit;
243     }
245     $htaccess_authenticated= TRUE;
248 /* Got a formular answer, validate and try to log in */
249 if (($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) || $htaccess_authenticated) {
251     /* Reset error messages */
252     $message= "";
254     /* Destroy old sessions, they cause a successfull login to relog again ...*/
255     if(session::global_is_set('_LAST_PAGE_REQUEST')) {
256         session::global_set('_LAST_PAGE_REQUEST',time());
257     }
259     if (!$htaccess_authenticated) {
260         $server= validate($_POST["server"]);
261     }
262     $config->set_current($server);
264     /* Admin-logon and verify */
265     $ldap = $config->get_ldap_link();
266     if (is_null($ldap) || (is_int($ldap) && $ldap == 0)) {
267         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
268         displayLogin();
269         exit();
270     }
272     /* Check for schema file presence */
273     if ($config->get_cfg_value("schemaCheck") == "true") {
274         $recursive = ($config->get_cfg_value("ldapFollowReferrals") == "true");
275         $tls =       ($config->get_cfg_value("ldapTLS") == "true");
277         if(!count($ldap->get_objectclasses())) {
278             msg_dialog::display(_("LDAP error"), _("Cannot detect information about the installed LDAP schema!"), ERROR_DIALOG);
279             displayLogin();
280             exit()  ;
281         }else{
282             $cfg = array();
283             $cfg['admin']     = $config->current['ADMINDN'];
284             $cfg['password']  = $config->current['ADMINPASSWORD'];
285             $cfg['connection']= $config->current['SERVER'];
286             $cfg['tls']       = $tls;
287             $str = check_schema($cfg, $config->get_cfg_value("rfc2307bis") == "true");
288             $checkarr = array();
289             foreach($str as $tr) {
290                 if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']) {
291                     msg_dialog::display(_("LDAP error"), _("Your LDAP setup contains old schema definitions:")."<br><br><i>".$tr['MSG']."</i>", ERROR_DIALOG);
292                     displayLogin();
293                     exit();
294                 }
295             }
296         }
297     }
299     /* Check for locking area */
300     $ldap->cat($config->get_cfg_value("config"), array("dn"));
301     $attrs= $ldap->fetch();
302     if (!count ($attrs)) {
303         $ldap->cd($config->current['BASE']);
304         $ldap->create_missing_trees($config->get_cfg_value("config"));
305     }
307     /* Check for valid input */
308     $ok= true;
309     if (!$htaccess_authenticated) {
310         $username= $_POST["username"];
311         if (!preg_match("/^[@A-Za-z0-9_.-]+$/", $username)) {
312             $message= _("Please specify a valid username!");
313             $ok= false;
314         } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0) {
315             $message= _("Please specify your password!");
316             $smarty->assign ('nextfield', 'password');
317             $ok= false;
318         }
319     }
321     if ($ok) {
323         /* Login as user, initialize user ACL's */
324         if ($htaccess_authenticated) {
325             $ui= ldap_login_user_htaccess($username);
326             if ($ui === NULL || !$ui) {
327                 msg_dialog::display(_("Authentication error"), _("Cannot retrieve user information for htaccess authentication!"), FATAL_ERROR_DIALOG);
328                 exit;
329             }
330         } else {
331             $ui= ldap_login_user($username, $_POST["password"]);
332         }
333         if ($ui === NULL || !$ui) {
334             $message= _("Please check the username/password combination.");
335             $smarty->assign ('nextfield', 'password');
336             session::global_set('config',$config);
337             new log("security","login","",array(),"Authentication failed for user \"$username\"");
338         } else {
339             /* Remove all locks of this user */
340             del_user_locks($ui->dn);
342             /* Save userinfo and plugin structure */
343             session::global_set('ui',$ui);
344             session::global_set('session_cnt',0);
346       /* Let GOsa trigger a new connection for each POST, save
347       config to session. */
348             $config->get_departments();
349             $config->make_idepartments();
350             session::global_set('config',$config);
352             /* Restore filter settings from cookie, if available */
353             if($config->get_cfg_value("storeFilterSettings") == "true") {
355                 if(isset($_COOKIE['GOsa_Filter_Settings']) || isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])) {
357                     if(isset($_COOKIE['GOsa_Filter_Settings'])) {
358                         $cookie_all = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
359                     }else{
360                         $cookie_all = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
361                     }
362                     if(isset($cookie_all[$ui->dn])) {
363                         $cookie = $cookie_all[$ui->dn];
364                         $cookie_vars= array("MultiDialogFilters","CurrentMainBase","plug");
365                         foreach($cookie_vars as $var) {
366                             if(isset($cookie[$var])) {
367                                 session::global_set($var,$cookie[$var]);
368                             }
369                         }
370                         if(isset($cookie['plug'])) {
371                             $plug =$cookie['plug'];
372                         }
373                     }
374                 }
375             }
377             /* are we using accountexpiration */
378             if ($config->get_cfg_value("handleExpiredAccounts") == "true") {
379                 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
381                 if ($expired == 1) {
382                     $message= _("Account locked. Please contact your system administrator!");
383                     $smarty->assign ('nextfield', 'password');
384                     new log("security","login","",array(),"Account for user \"$username\" has expired") ;
385                 } elseif ($expired == 3) {
386                     $plist= new pluglist($config, $ui);
387                     foreach ($plist->dirlist as $key => $value) {
388                         if (preg_match("/\bpassword\b/i",$value)) {
389                             $plug=$key;
390                             new log("security","login","",array(),"User \"$username\" password forced to change") ;
391                             header ("Location: main.php?plug=$plug&amp;reset=1");
392                             exit;
393                         }
394                     }
395                 }
396             }
398             /* Not account expired or password forced change go to main page */
399             new log("security","login","",array(),"User \"$username\" logged in successfully") ;
400             $plist= new pluglist($config, $ui);
401             if(isset($plug) && isset($plist->dirlist[$plug])) {
402                 header ("Location: main.php?plug=".$plug."&amp;global_check=1");
403             }else{
404                 header ("Location: main.php?global_check=1");
405             }
406             exit;
407         }
408     }
411 /* Fill template with required values */
412 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
413 $smarty->assign ('username', $username);
414 $smarty->assign ('personal_img', get_template_path('images/login-head.png'));
415 $smarty->assign ('password_img', get_template_path('images/password.png'));
416 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
418 /* Some error to display? */
419 if (!isset($message)) {
420     $message= "";
423 $smarty->assign ("message", $message);
425 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
426 $smarty->assign ("cookies", _("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
428 /* Generate server list */
429 $servers= array();
430 if (isset($_POST['server'])) {
431     $selected= validate($_POST['server']);
432 } else {
433     $selected= $config->data['MAIN']['DEFAULT'];
435 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
436     $servers[$key]= $key;
438 $smarty->assign ("server_options", $servers);
439 $smarty->assign ("server_id", $selected);
441 /* show login screen */
442 $smarty->assign ("PHPSESSID", session_id());
443 if (session::is_set('errors')) {
444     $smarty->assign("errors", session::get('errors'));
446 if ($error_collector != "") {
447     $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
448 } else {
449     $smarty->assign("php_errors", "");
452 /* Set focus to the error button if we've an error message */
453 $focus= "";
454 if (session::is_set('errors') && session::get('errors') != "") {
455     $focus= '<script language="JavaScript" type="text/javascript">';
456     $focus.= 'document.forms[0].error_accept.focus();';
457     $focus.= '</script>';
459 $smarty->assign("focus", $focus);
460 displayLogin();
462 // vim:tabstop=2:expandtab:shiftwidth=2:softtabstop=2:filetype=php:syntax:ruler:
463 ?>
465 </body>
466 </html>