Code

Updated error messages.
[gosa.git] / gosa-core / html / index.php
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003-2005  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 /* Load required includes */
22 require_once ("../include/php_setup.inc");
23 require_once ("functions.inc");
24 require_once ("class_log.inc");
25 header("Content-type: text/html; charset=UTF-8");
27 function displayLogin()
28 {
29   global $smarty,$message,$config,$ssl,$error_collector;
30   error_reporting(E_ALL | E_STRICT);
31   /* Fill template with required values */
32   $username = "";
33   if(isset($_POST["username"])){
34     $username= $_POST["username"];
35   }
36   $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
37   $smarty->assign ('username', $username);
38   $smarty->assign ('personal_img', get_template_path('images/personal.png'));
39   $smarty->assign ('password_img', get_template_path('images/password.png'));
40   $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
42   /* Some error to display? */
43   if (!isset($message)){
44     $message= "";
45   }
46   $smarty->assign ("message", $message);
48   /* Displasy SSL mode warning? */
49   if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
50     $smarty->assign ("ssl", _("Warning").": <a style=\"color:red;\" href=\"$ssl\">"._("Session is not encrypted!")."</a>");
51   } else {
52     $smarty->assign ("ssl", "");
53   }
55   if(!$config->check_session_lifetime()){
56     $smarty->assign ("lifetime", _("Warning").": ".
57                                  _("The session lifetime configured in your gosa.conf will be overridden by php.ini settings."));
58   }else{
59     $smarty->assign ("lifetime", "");
60   }
62   /* Generate server list */
63   $servers= array();
64   if (isset($_POST['server'])){
65     $selected= validate($_POST['server']);
66   } else {
67     $selected= $config->data['MAIN']['DEFAULT'];
68   }
69   foreach ($config->data['LOCATIONS'] as $key => $ignored){
70     $servers[$key]= $key;
71   }
72   $smarty->assign ("server_options", $servers);
73   $smarty->assign ("server_id", $selected);
75   /* show login screen */
76   $smarty->assign ("PHPSESSID", session_id());
77   if (session::is_set('errors')){
78     $smarty->assign("errors", session::get('errors'));
79   }
80   if ($error_collector != ""){
81     $smarty->assign("php_errors", $error_collector."</div>");
82   } else {
83     $smarty->assign("php_errors", "");
84   }
86   $smarty->display (get_template_path('headers.tpl'));
87   $smarty->display(get_template_path('login.tpl'));
88   exit();
89 }
93 /* Set error handler to own one, initialize time calculation
94    and start session. */
95 session::start();
96 session::set('errorsAlreadyPosted',array());
98 /* Destroy old session if exists. 
99    Else you will get your old session back, if you not logged out correctly. */
100 if(is_array(session::get_all()) && count(session::get_all())){
101   session::destroy();
102   session::start();
105 $username= "";
107 /* Reset errors */
108 session::set('errors',"");
109 session::set('errorsAlreadyPosted',"");
110 session::set('LastError',"");
112 /* Check if we need to run setup */
113 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)){
114   header("location:setup.php");
115   exit();
118 /* Reset errors */
119 session::set('errors',"");
121 /* Check for java script */
122 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
123   session::set('js',TRUE);
124 }elseif(isset($_POST['javascript'])) {
125   session::set('js',FALSE);
128 /* Check if gosa.conf (.CONFIG_FILE) is accessible */
129 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
130   msg_dialog::display(_("Configuration accessibility"),sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE),FATAL_ERROR_DIALOG);
131   exit();
134 /* Parse configuration file */
135 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
136 session::set('DEBUGLEVEL',$config->data['MAIN']['DEBUGLEVEL']);
137 if ($_SERVER["REQUEST_METHOD"] != "POST"){
138   @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
141 /* Enable compressed output */
142 if (isset($config->data['MAIN']['COMPRESSED']) && preg_match('/^(true|on)$/i', $config->data['MAIN']['COMPRESSED'])){
143   ob_start("ob_gzhandler");
146 /* Set template compile directory */
147 if (isset ($config->data['MAIN']['COMPILE'])){
148   $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
149 } else {
150   $smarty->compile_dir= '/var/spool/gosa';
153 /* Check for compile directory */
154 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
155   msg_dialog::display(_("Smarty"),sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
156     $smarty->compile_dir),FATAL_ERROR_DIALOG);
157   exit();
160 /* Check for old files in compile directory */
161 clean_smarty_compile_dir($smarty->compile_dir);
163 /* Language setup */
164 $lang= get_browser_language();
165 putenv("LANGUAGE=");
166 putenv("LANG=$lang");
167 setlocale(LC_ALL, $lang);
168 $GLOBALS['t_language']= $lang;
169 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
171 /* Set the text domain as 'messages' */
172 $domain = 'messages';
173 bindtextdomain($domain, LOCALE_DIR);
174 textdomain($domain);
175 $smarty->assign ('nextfield', 'username');
177 if ($_SERVER["REQUEST_METHOD"] != "POST"){
178   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
182 /* Check for SSL connection */
183 $ssl= "";
184 if (!isset($_SERVER['HTTPS']) ||
185     !stristr($_SERVER['HTTPS'], "on")) {
187   if (empty($_SERVER['REQUEST_URI'])) {
188     $ssl= "https://".$_SERVER['HTTP_HOST'].
189       $_SERVER['PATH_INFO'];
190   } else {
191     $ssl= "https://".$_SERVER['HTTP_HOST'].
192       $_SERVER['REQUEST_URI'];
193   }
196 /* If SSL is forced, just forward to the SSL enabled site */
197 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
198   header ("Location: $ssl");
199   exit;
202 /* Do we have htaccess authentification enabled? */
203 $htaccess_authenticated= FALSE;
204 if (isset($config->data['MAIN']['HTACCESS_AUTH']) && preg_match('/^(yes|true)$/i', $config->data['MAIN']['HTACCESS_AUTH'])){
205   if (!isset($_SERVER['REMOTE_USER'])){
206     msg_dialog::display(_("Configuration error"), _("There is a problem with the authentication setup!"), FATAL_ERROR_DIALOG);
207     exit;
208   }
210   $tmp= process_htaccess($_SERVER['REMOTE_USER'], isset($_SERVER['KRB5CCNAME']));
211   $username= $tmp['username'];
212   $server= $tmp['server'];
213   if ($username == ""){
214     msg_dialog::display(_("Error"), _("Cannot find a valid user for the current authentication setup!"), FATAL_ERROR_DIALOG);
215     exit;
216   }
217   if ($server == ""){
218     msg_dialog::display(_("Error"), _("User information is not uniq accross the configured LDAP trees!"), FATAL_ERROR_DIALOG);
219     exit;
220   }
222   $htaccess_authenticated= TRUE;
225 /* Got a formular answer, validate and try to log in */
226 if (($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) || $htaccess_authenticated){
228   /* Reset error messages */
229   $message= "";
231   /* Destroy old sessions, they cause a successfull login to relog again ...*/
232   if(session::is_set('_LAST_PAGE_REQUEST')){
233     session::set('_LAST_PAGE_REQUEST',time());
234   }
236   if (!$htaccess_authenticated){
237     $server= validate($_POST["server"]);
238   }
239   $config->set_current($server);
241   /* Admin-logon and verify */
242   $ldap = $config->get_ldap_link();
243   if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
244     msg_dialog::display(_("LDAP error"), _("Can't bind to LDAP. Please contact the system administrator."), ERROR_DIALOG);
245     displayLogin();
246     exit();
247   }
249   /* Check for schema file presence */
250   if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
251     $config->data['MAIN']['SCHEMA_CHECK'] = "true";
252   }
253   if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
254     $recursive = (isset($config->current['RECURSIVE']) && $config->current['RECURSIVE'] == "true");
255     $tls =       (isset($config->current['TLS'])       && $config->current['TLS'] == "true");
257     if(!count($ldap->get_objectclasses())){
258       msg_dialog::display(_("LDAP error"), _("Cannot detect information about the installed LDAP schema!"), ERROR_DIALOG);
259       displayLogin();
260       exit()  ;
261     }else{
262       $cfg = array();
263       $cfg['admin']     = $config->current['ADMIN'];
264       $cfg['password']  = $config->current['PASSWORD'];
265       $cfg['connection']= $config->current['SERVER'];
266       $cfg['tls']       = $tls;
267       $str = check_schema($cfg,isset($config->current['RFC2307BIS']) && preg_match("/(true|yes|on|1)/i",$config->current['RFC2307BIS']));
268       $checkarr = array();
269       foreach($str as $tr){
270         if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']){
271           msg_dialog::display(_("LDAP error"), _("Your ldap setup contains old schema definitions:")."<br><br><i>".$tr['MSG']."</i>", ERROR_DIALOG);
272           displayLogin();
273           exit();
274         }
275       }
276     }
277   }
279   /* Check for locking area */
280   $ldap->cat($config->current['CONFIG'], array("dn"));
281   $attrs= $ldap->fetch();
282   if (!count ($attrs)){
283     $ldap->cd($config->current['BASE']);
284     $ldap->create_missing_trees($config->current['CONFIG']);
285   }
287   /* Check for valid input */
288   $ok= true;
289   if (!$htaccess_authenticated){
290     $username= $_POST["username"];
291     if (!ereg("^[@A-Za-z0-9_.-]+$", $username)){
292       $message= _("Please specify a valid username!");
293       $ok= false;
294     } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
295       $message= _("Please specify your password!");
296       $smarty->assign ('nextfield', 'password');
297       $ok= false;
298     }
299   }
300   
301   if ($ok) {
303     /* Login as user, initialize user ACL's */
304     if ($htaccess_authenticated){
305       $ui= ldap_login_user_htaccess($username);
306       if ($ui === NULL || !$ui){
307         msg_dialog::display(_("Authentication error"), _("Cannot retrieve user information for htaccess authentication!"), FATAL_ERROR_DIALOG);
308         exit;
309       }
310     } else {
311       $ui= ldap_login_user($username, $_POST["password"]);
312     }
313     if ($ui === NULL || !$ui){
314       $message= _("Please check the username/password combination.");
315       $smarty->assign ('nextfield', 'password');
316       new log("security","login","",array(),"Authentication failed for user \"$username\"") ;
317     } else {
318       /* Remove all locks of this user */
319       del_user_locks($ui->dn);
321       /* Save userinfo and plugin structure */
322       session::set('ui',$ui);
323       session::set('session_cnt',0);
325       /* Let GOsa trigger a new connection for each POST, save
326          config to session. */
327       $config->get_departments();
328       $config->make_idepartments();
329       session::set('config',$config);
331       /* Restore filter settings from cookie, if available */
332       if(isset($config->data['MAIN']['SAVE_FILTER']) && preg_match("/true/",$config->data['MAIN']['SAVE_FILTER'])){
334         if(isset($_COOKIE['GOsa_Filter_Settings']) || isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])){
336           if(isset($_COOKIE['GOsa_Filter_Settings'])){
337             $cookie_all = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
338           }else{
339             $cookie_all = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
340           }
341           if(isset($cookie_all[$ui->dn])){
342             $cookie = $cookie_all[$ui->dn];
343             $cookie_vars= array("MultiDialogFilters","CurrentMainBase","plug");
344             foreach($cookie_vars as $var){
345               if(isset($cookie[$var])){
346                 session::set($var,$cookie[$var]);
347               }
348             }
349             if(isset($cookie['plug'])){
350               $plug =$cookie['plug'];
351             }
352           }
353         }
354       }
356       /* are we using accountexpiration */
357       if((isset($config->data['MAIN']['ACCOUNT_EXPIRATION'])) && 
358           preg_match('/true/i', $config->data['MAIN']['ACCOUNT_EXPIRATION'])){
360         $expired= ldap_expired_account($config, $ui->dn, $ui->username);
362         if ($expired == 1){
363           $message= _("Account locked. Please contact your system administrator.");
364           $smarty->assign ('nextfield', 'password');
365           new log("security","login","",array(),"Account for user \"$username\" has expired") ;
366         } elseif ($expired == 3){
367           $plist= new pluglist($config, $ui);
368           foreach ($plist->dirlist as $key => $value){
369             if (preg_match("/\bpassword\b/i",$value)){
370               $plug=$key;
371               new log("security","login","",array(),"User \"$username\" password forced to change") ;
372               header ("Location: main.php?plug=$plug&amp;reset=1");
373               exit;
374             }
375           }
376         }
377       }
379       /* Not account expired or password forced change go to main page */
380       new log("security","login","",array(),"User \"$username\" logged in successfully") ;
381       $plist= new pluglist($config, $ui);
382       if(isset($plug) && isset($plist->dirlist[$plug])){
383         header ("Location: main.php?plug=".$plug."&amp;global_check=1");
384       }else{
385         header ("Location: main.php?global_check=1");
386       }
387       exit;
388     }
389   }
392 /* Fill template with required values */
393 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
394 $smarty->assign ('username', $username);
395 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
396 $smarty->assign ('password_img', get_template_path('images/password.png'));
397 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
399 /* Some error to display? */
400 if (!isset($message)){
401   $message= "";
404 $smarty->assign ("message", $message);
406 /* Displasy SSL mode warning? */
407 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
408   $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
409 } else {
410   $smarty->assign ("ssl", "");
413 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
414 $smarty->assign ("cookies", "<b>"._("Warning").":<\/b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
417 /* Generate server list */
418 $servers= array();
419 if (isset($_POST['server'])){
420   $selected= validate($_POST['server']);
421 } else {
422   $selected= $config->data['MAIN']['DEFAULT'];
424 foreach ($config->data['LOCATIONS'] as $key => $ignored){
425   $servers[$key]= $key;
427 $smarty->assign ("server_options", $servers);
428 $smarty->assign ("server_id", $selected);
430 /* show login screen */
431 $smarty->assign ("PHPSESSID", session_id());
432 if (session::is_set('errors')){
433   $smarty->assign("errors", session::get('errors'));
435 if ($error_collector != ""){
436   $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
437 } else {
438   $smarty->assign("php_errors", "");
441 /* Set focus to the error button if we've an error message */
442 $focus= "";
443 if (session::is_set('errors') && session::get('errors') != ""){
444   $focus= '<script language="JavaScript" type="text/javascript">';
445   $focus.= 'document.forms[0].error_accept.focus();';
446   $focus.= '</script>';
448 $smarty->assign("focus", $focus);
449 displayLogin();
451 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
452 ?>
454 </body>
455 </html>