Code

first part of the account expiration code
[gosa.git] / 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 header("Content-type: text/html; charset=UTF-8");
26 /* Set error handler to own one, initialize time calculation
27    and start session. */
28 session_start ();
29 $username= "";
31 /* Reset errors */
32 $_SESSION['errors']             = "";
33 $_SESSION['errorsAlreadyPosted']= array();
34 $_SESSION['LastError']          = "";
36 /* Check if we need to run setup */
37 if (!file_exists(CONFIG_DIR."/gosa.conf")){
38   header("location:setup.php");
39   exit();
40 }
42 /* Reset errors */
43 $_SESSION['errors']= "";
45 /* Check for java script */
46 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
47   $_SESSION['js']= TRUE;
48 }elseif(isset($_POST['javascript'])) {
49   $_SESSION['js']= FALSE;
50 }
52 /* Check if gosa.conf is accessible */
53 if (!is_readable(CONFIG_DIR."/gosa.conf")){
54   echo sprintf(_("GOsa configuration %s/gosa.conf is not readable. Aborted."), CONFIG_DIR);
55   exit();
56 }
58 /* Parse configuration file */
59 $config= new config(CONFIG_DIR."/gosa.conf", $BASE_DIR);
60 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
61 if ($_SERVER["REQUEST_METHOD"] != "POST"){
62   @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
63 }
65 /* Set template compile directory */
66 if (isset ($config->data['MAIN']['COMPILE'])){
67   $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
68 } else {
69   $smarty->compile_dir= '/var/spool/gosa';
70 }
71 $smarty->assign ('nextfield', 'username');
73 /* Check for compile directory */
74 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
75   echo sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
76         $smarty->compile_dir);
77   exit();
78 }
80 /* Check for old files in compile directory */
81 clean_smarty_compile_dir($smarty->compile_dir);
83 /* Language setup */
84 if ($config->data['MAIN']['LANG'] == ""){
85   $lang= get_browser_language();
86 } else {
87   $lang= $config->data['MAIN']['LANG'];
88 }
89 $lang.=".UTF-8";
90 putenv("LANGUAGE=");
91 putenv("LANG=$lang");
92 setlocale(LC_ALL, $lang);
93 $GLOBALS['t_language']= $lang;
94 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
96 /* Set the text domain as 'messages' */
97 $domain = 'messages';
98 bindtextdomain($domain, "$BASE_DIR/locale");
99 textdomain($domain);
102 if ($_SERVER["REQUEST_METHOD"] != "POST"){
103   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
107 /* Check for SSL connection */
108 $ssl= "";
109 if (!isset($_SERVER['HTTPS']) ||
110     !stristr($_SERVER['HTTPS'], "on")) {
112   if (empty($_SERVER['REQUEST_URI'])) {
113     $ssl= "https://".$_SERVER['HTTP_HOST'].
114       $_SERVER['PATH_INFO'];
115   } else {
116     $ssl= "https://".$_SERVER['HTTP_HOST'].
117       $_SERVER['REQUEST_URI'];
118   }
121 /* If SSL is forced, just forward to the SSL enabled site */
122 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
123   header ("Location: $ssl");
124   exit;
127 /* Got a formular answer, validate and try to log in */
128 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])){
130   /* Reset error messages */
131   $message= "";
133   /* Destroy old sessions, they cause a successfull login to relog again ...*/
134   if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
135     $_SESSION['_LAST_PAGE_REQUEST'] = time();
136   }
138   $server= validate($_POST["server"]);
139   $config->set_current($server);
141   /* Admin-logon and verify */
142   $ldap = $config->get_ldap_link();
143   if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
144     print_red (_("Can't bind to LDAP. Please contact the system administrator."));
145     echo $_SESSION['errors'];
146     $smarty->display(get_template_path('login.tpl'));
147     exit();
148   }
150   /* Check for schema file presence */
151   if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
152     $config->data['MAIN']['SCHEMA_CHECK'] = "true";
153   }
154   if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
155     require_once("functions_setup.inc");
156     if(!is_schema_readable($config->current['SERVER'],$config->current['ADMIN'],$config->current['PASSWORD'])){
157       print_red(_("GOsa cannot retrieve information about the installed schema files. Please make sure, that this is possible."));
158       echo $_SESSION['errors'];
159       exit();
160     }else{
161       $str = (schema_check($config->current['SERVER'],$config->current['ADMIN'],$config->current['PASSWORD'],0,TRUE));
162       $checkarr = array();
163       foreach($str as $tr){
164         if(isset($tr['needonstartup'])){
165           print_red($tr['msg']."<br>"._("Your ldap setup contains old schema definitions. Please re-run the setup."));
166           print $_SESSION['errors'];
167           exit();
168         }
169       }
170     }
171   }
172   /* Check for locking area */
173   $ldap->cat($config->current['CONFIG'], array("dn"));
174   $attrs= $ldap->fetch();
175   if (!count ($attrs)){
176     $ldap->cd($config->current['BASE']);
177     $ldap->create_missing_trees($config->current['CONFIG']);
178   }
180   /* Check for at least one subtreeACL in the complete tree */
181   $ldap->cd($config->current['BASE']);
182   $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
183   if ($ldap->count() < 1){
184     print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
185     displayLogin();
186     exit();
187   }
189   /* Check for valid input */
190   $username= $_POST["username"];
191   if (!ereg("^[A-Za-z0-9_.-]+$", $username)){
192     $message= _("Please specify a valid username!");
193   } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
194     $message= _("Please specify your password!");
195     $smarty->assign ('nextfield', 'password');
196   } else {
198     /* Login as user, initialize user ACL's */
199     $ui= ldap_login_user($username, $_POST["password"]);
200     if ($ui === NULL || $ui == 0){
201       $message= _("Please check the username/password combination.");
202       $smarty->assign ('nextfield', 'password');
203       gosa_log ("Authentication failed for user \"$username\"");
204     } else {
205       /* Remove all locks of this user */
206       del_user_locks($ui->dn);
208       /* Save userinfo and plugin structure */
209       $_SESSION['ui']= $ui;
210       $_SESSION['session_cnt']= 0;
212       /* Let GOsa trigger a new connection for each POST, save
213          config to session. */
214       $config->get_departments();
215       $config->make_idepartments();
216       $_SESSION['config']= $config;
218       /* Go to main page */
219       gosa_log ("User \"$username\" logged in successfully");
220       header ("Location: main.php?global_check=1");
221       exit;
222     }
223   }
226 /* Fill template with required values */
227 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
228 $smarty->assign ('username', $username);
229 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
230 $smarty->assign ('password_img', get_template_path('images/password.png'));
231 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
233 /* Some error to display? */
234 if (!isset($message)){
235   $message= "";
238 $smarty->assign ("message", $message);
240 /* Displasy SSL mode warning? */
241 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
242   $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
243 } else {
244   $smarty->assign ("ssl", "");
247 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
248 $smarty->assign ("cookies", "<b>"._("Warning").":</b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
251 /* Generate server list */
252 $servers= array();
253 if (isset($_POST['server'])){
254   $selected= validate($_POST['server']);
255 } else {
256   $selected= $config->data['MAIN']['DEFAULT'];
258 foreach ($config->data['LOCATIONS'] as $key => $ignored){
259   $servers[$key]= $key;
261 $smarty->assign ("server_options", $servers);
262 $smarty->assign ("server_id", $selected);
264 /* show login screen */
265 $smarty->display (get_template_path('headers.tpl'));
266 $smarty->assign ("PHPSESSID", session_id());
267 if (isset($_SESSION['errors'])){
268   $smarty->assign("errors", $_SESSION['errors']);
270 if ($error_collector != ""){
271   $smarty->assign("php_errors", $error_collector."</div>");
272 } else {
273   $smarty->assign("php_errors", "");
275 $smarty->display (get_template_path('login.tpl'));
283 function displayLogin()
285   global $smarty,$message,$config,$ssl,$error_collector;
286   error_reporting(E_ALL);
287     /* Fill template with required values */
288     $username = "";
289     if(isset($_POST["username"])){
290       $username= $_POST["username"];
291     }
292     $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
293     $smarty->assign ('username', $username);
294     $smarty->assign ('personal_img', get_template_path('images/personal.png'));
295     $smarty->assign ('password_img', get_template_path('images/password.png'));
296     $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
298     /* Some error to display? */
299     if (!isset($message)){
300       $message= "";
301     }
302     $smarty->assign ("message", $message);
304     /* Displasy SSL mode warning? */
305     if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
306       $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
307     } else {
308       $smarty->assign ("ssl", "");
309     }
311     /* Generate server list */
312     $servers= array();
313     if (isset($_POST['server'])){
314       $selected= validate($_POST['server']);
315     } else {
316       $selected= $config->data['MAIN']['DEFAULT'];
317     }
318     foreach ($config->data['LOCATIONS'] as $key => $ignored){
319       $servers[$key]= $key;
320     }
321     $smarty->assign ("server_options", $servers);
322     $smarty->assign ("server_id", $selected);
324     /* show login screen */
325     $smarty->display (get_template_path('headers.tpl'));
326     $smarty->assign ("PHPSESSID", session_id());
327     if (isset($_SESSION['errors'])){
328       $smarty->assign("errors", $_SESSION['errors']);
329     }
330     if ($error_collector != ""){
331       $smarty->assign("php_errors", $error_collector."</div>");
332     } else {
333       $smarty->assign("php_errors", "");
334     }
336     $smarty->display(get_template_path('login.tpl'));
337     exit();
342 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
343 ?>
345 </body>
346 </html>