Code

Updated logout to recognize htaccess option
[gosa.git] / gosa-core / html / password.php
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003-2007  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 function displayPWchanger()
22 {
23   global $smarty;
25   $smarty->display(get_template_path('password.tpl'));
26   exit();
27 }
29 /* Load required includes */
30 require_once ("../include/php_setup.inc");
31 require_once ("functions.inc");
33 if(!class_exists("log")){
34   require_once("class_log.inc");
35 }
37 header("Content-type: text/html; charset=UTF-8");
39 session_start();
41 /* Destroy old session if exists.
42     Else you will get your old session back, if you not logged out correctly. */
43 if(is_array($_SESSION) && count($_SESSION)){
44   session_destroy();
45   session_start();
46 }
48 /* Reset errors */
49 $_SESSION['js']                 = true;
50 $_SESSION['errors']             = "";
51 $_SESSION['errorsAlreadyPosted']= array();
52 $_SESSION['LastError']          = "";
54 /* Check if CONFIG_FILE is accessible */
55 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
56   echo sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE);
57   exit();
58 }
60 /* Parse configuration file */
61 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
62 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
63 if ($_SERVER["REQUEST_METHOD"] != "POST"){
64   @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
65 }
67 /* Set template compile directory */
68 if (isset ($config->data['MAIN']['COMPILE'])){
69   $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
70 } else {
71   $smarty->compile_dir= '/var/spool/gosa';
72 }
74 /* Check for compile directory */
75 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
76   echo sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
77         $smarty->compile_dir);
78   exit();
79 }
81 /* Check for old files in compile directory */
82 clean_smarty_compile_dir($smarty->compile_dir);
84 /* Language setup */
85 if ($config->data['MAIN']['LANG'] == ""){
86   $lang= get_browser_language();
87 } else {
88   $lang= $config->data['MAIN']['LANG'];
89 }
90 $lang.=".UTF-8";
91 putenv("LANGUAGE=");
92 putenv("LANG=$lang");
93 setlocale(LC_ALL, $lang);
94 $GLOBALS['t_language']= $lang;
95 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
97 /* Set the text domain as 'messages' */
98 $domain = 'messages';
99 bindtextdomain($domain, "$BASE_DIR/locale");
100 textdomain($domain);
102 /* Generate server list */
103 $servers= array();
104 foreach ($config->data['LOCATIONS'] as $key => $ignored){
105         $servers[$key]= $key;
107 if (isset($_POST['server'])){
108         $directory= validate($_POST['server']);
109 } else {
110         $directory= $config->data['MAIN']['DEFAULT'];
112   if(!isset($servers[$directory])){
113     $directory = key($servers);
114   }
116 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])){
117         $smarty->assign ("show_directory_chooser", false);
118         $directory= validate($_GET['directory']);
119 } else {
120         $smarty->assign ("server_options", $servers);
121         $smarty->assign ("server_id", $directory);
122         $smarty->assign ("show_directory_chooser", true);
125 /* Set config to selected one */
126 $config->set_current($directory);
127 $_SESSION['config']= $config;
129 if ($_SERVER["REQUEST_METHOD"] != "POST"){
130   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
134 /* Check for SSL connection */
135 $ssl= "";
136 if (!isset($_SERVER['HTTPS']) ||
137     !stristr($_SERVER['HTTPS'], "on")) {
139   if (empty($_SERVER['REQUEST_URI'])) {
140     $ssl= "https://".$_SERVER['HTTP_HOST'].
141       $_SERVER['PATH_INFO'];
142   } else {
143     $ssl= "https://".$_SERVER['HTTP_HOST'].
144       $_SERVER['REQUEST_URI'];
145   }
148 /* If SSL is forced, just forward to the SSL enabled site */
149 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
150   header ("Location: $ssl");
151   exit;
154 /* Check for selected password method */
155 $method= $config->current['HASH'];
156 if (isset($_GET['method'])){
157         $method= validate($_GET['method']);
158         $tmp = new passwordMethod($config);
159         $available = $tmp->get_available_methods();
160         if (!isset($available[$method])){
161                 echo _("Error: Password method not available!");
162                 exit;
163         }
167 /* Check for selected user... */
168 if (isset($_GET['uid']) && $_GET['uid'] != ""){
169         $uid= validate($_GET['uid']);
170         $smarty->assign('display_username', false);
171 } elseif (isset($_POST['uid'])){
172         $uid= validate($_POST['uid']);
173         $smarty->assign('display_username', true);
174 } else {
175         $uid= "";
176         $smarty->assign('display_username', true);
178 $current_password= "";
179 $smarty->assign("changed", false);
181 /* Got a formular answer, validate and try to log in */
182 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])){
184   /* Destroy old sessions, they cause a successfull login to relog again ...*/
185   if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
186     $_SESSION['_LAST_PAGE_REQUEST'] = time();
187   }
189   $message= array();
190   $current_password= $_POST['current_password'];
192   /* Do new and repeated password fields match? */
193   $new_password= $_POST['new_password'];
194   if ($_POST['new_password'] != $_POST['new_password_repeated']){
195           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
196   } else {
197           if ($_POST['new_password'] == ""){
198                   $message[]= _("The password you've entered as 'New password' is empty.");
199           }
200   }
202   /* Password policy fulfilled? */
203   if (isset($config->data['MAIN']['PWDIFFER'])){
204           $l= $config->data['MAIN']['PWDIFFER'];
205           if (substr($_POST['current_password'], 0, $l) == substr($_POST['new_password'], 0, $l)){
206                   $message[]= _("The password used as new and current are too similar.");
207           }
208   }
209   if (isset($config->data['MAIN']['PWMINLEN'])){
210           if (strlen($_POST['new_password']) < $config->data['MAIN']['PWMINLEN']){
211                   $message[]= _("The password used as new is to short.");
212           }
213   }
215   /* Validate */
216   if (!ereg("^[A-Za-z0-9_.-]+$", $uid)){
217           $message[]= _("Please specify a valid username!");
218   } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0){
219     $message[]= _("Please specify your password!");
220   } else {
222     /* Do we have the selected user somewhere? */
223     $ui= ldap_login_user ($uid, $current_password);
225     if ($ui === NULL){
226       $message[]= _("Please check the username/password combination.");
227     } else {
228       $acls = $ui->get_permissions($ui->dn,"users/password");
229       if(!preg_match("/w/i",$acls)){
230         $message[]= _("You have no permissions to change your password.");
231       }
232     }
233   }
235   /* Do we need to show error messages? */
236   if (count ($message) != 0){
237           /* Show error message and continue editing */
238           show_errors($message);
239   } else {
241           /* Passed quality check, just try to change the password now */
242           $output= "";
243           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
244                   exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$ui->username." ".
245                                   $_POST['current_password']." ".$_POST['new_password'], $resarr);
246                   if(count($resarr) > 0) {
247                           $output= join('\n', $resarr);
248                   }
249           }
250           if ($output != ""){
251                   $message[]= _("External password changer reported a problem: ".$output);
252                   show_errors($message);
253           } else {
254                   if ($method != ""){
255                           change_password ($ui->dn, $_POST['new_password'], 0, $method);
256                   } else {
257                           change_password ($ui->dn, $_POST['new_password']);
258                   }
259                   gosa_log ("User/password has been changed");
260                   $smarty->assign("changed", true);
261           }
262   }
267 /* Parameter fill up */
268 $params= "";
269 foreach (array('uid', 'method', 'directory') as $index){
270         $params.= "&amp;$index=".urlencode($$index);
272 $params= preg_replace('/^&amp;/', '?', $params);
273 $smarty->assign('params', $params);
275 /* Fill template with required values */
276 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
277 $smarty->assign ('uid', $uid);
278 $smarty->assign ('password_img', get_template_path('images/password.png'));
280 /* Displasy SSL mode warning? */
281 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
282   $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>"._("Enter SSL session")."</b></a>!");
283 } else {
284   $smarty->assign ("ssl", "");
287 /* show login screen */
288 $smarty->assign ("PHPSESSID", session_id());
289 if (isset($_SESSION['errors'])){
290   $smarty->assign("errors", $_SESSION['errors']);
292 if ($error_collector != ""){
293   $smarty->assign("php_errors", $error_collector."</div>");
294 } else {
295   $smarty->assign("php_errors", "");
298 displayPWchanger();
300 ?>
302 </body>
303 </html>
304 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: