Code

Moved pwminlen to passwordMinLength
[gosa.git] / gosa-core / html / password.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 function displayPWchanger()
24 {
25   global $smarty;
27   $smarty->display(get_template_path('password.tpl'));
28   exit();
29 }
31 /* Load required includes */
32 require_once ("../include/php_setup.inc");
33 require_once ("functions.inc");
35 if(!class_exists("log")){
36   require_once("class_log.inc");
37 }
39 header("Content-type: text/html; charset=UTF-8");
41 session::start();
43 /* Destroy old session if exists.
44     Else you will get your old session back, if you not logged out correctly. */
45 if(is_array(session::get_all()) && count(session::get_all())){
46   session::destroy();
47   session::start();
48 }
50 /* Reset errors */
51 session::set('js',true);
52 session::set('errors',"");
53 session::set('errorsAlreadyPosted',array());
54 session::set('LastError',"");
56 /* Check if CONFIG_FILE is accessible */
57 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
58   msg_dialog::display(_("Fatal error"), sprintf(_("GOsa configuration %s/%s is not readable. Aborted."),CONFIG_DIR,CONFIG_FILE), FATAL_ERROR_DIALOG);
59   exit;
60 }
62 /* Parse configuration file */
63 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
64 session::set('DEBUGLEVEL', $config->get_cfg_value("debuglevel"));
65 if ($_SERVER["REQUEST_METHOD"] != "POST"){
66   @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
67 }
69 /* Set template compile directory */
70 $smarty->compile_dir= $config->get_cfg_value("compile", '/var/spool/gosa');
72 /* Check for compile directory */
73 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
74   msg_dialog::display(_("Configuration error"), sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
75         $smarty->compile_dir), FATAL_ERROR_DIALOG);
76   exit;
77 }
79 /* Check for old files in compile directory */
80 clean_smarty_compile_dir($smarty->compile_dir);
82 /* Language setup */
83 if ($config->get_cfg_value("lang") == ""){
84   $lang= get_browser_language();
85 } else {
86   $lang= $config->get_cfg_value("lang");
87 }
88 $lang.=".UTF-8";
89 putenv("LANGUAGE=");
90 putenv("LANG=$lang");
91 setlocale(LC_ALL, $lang);
92 $GLOBALS['t_language']= $lang;
93 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
95 /* Set the text domain as 'messages' */
96 $domain = 'messages';
97 bindtextdomain($domain, LOCALE_DIR);
98 textdomain($domain);
100 /* Generate server list */
101 $servers= array();
102 foreach ($config->data['LOCATIONS'] as $key => $ignored){
103         $servers[$key]= $key;
105 if (isset($_POST['server'])){
106         $directory= validate($_POST['server']);
107 } else {
108         $directory= $config->data['MAIN']['DEFAULT'];
110   if(!isset($servers[$directory])){
111     $directory = key($servers);
112   }
114 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])){
115         $smarty->assign ("show_directory_chooser", false);
116         $directory= validate($_GET['directory']);
117 } else {
118         $smarty->assign ("server_options", $servers);
119         $smarty->assign ("server_id", $directory);
120         $smarty->assign ("show_directory_chooser", true);
123 /* Set config to selected one */
124 $config->set_current($directory);
125 session::set('config',$config);
127 if ($_SERVER["REQUEST_METHOD"] != "POST"){
128   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
132 /* Check for SSL connection */
133 $ssl= "";
134 if (!isset($_SERVER['HTTPS']) ||
135     !stristr($_SERVER['HTTPS'], "on")) {
137   if (empty($_SERVER['REQUEST_URI'])) {
138     $ssl= "https://".$_SERVER['HTTP_HOST'].
139       $_SERVER['PATH_INFO'];
140   } else {
141     $ssl= "https://".$_SERVER['HTTP_HOST'].
142       $_SERVER['REQUEST_URI'];
143   }
146 /* If SSL is forced, just forward to the SSL enabled site */
147 if ($config->get_cfg_value("forcessl") == 'true' && $ssl != ''){
148   header ("Location: $ssl");
149   exit;
152 /* Check for selected password method */
153 $method= $config->get_cfg_value("hash", "crypt/md5");
154 if (isset($_GET['method'])){
155         $method= validate($_GET['method']);
156         $tmp = new passwordMethod($config);
157         $available = $tmp->get_available_methods();
158         if (!isset($available[$method])){
159     msg_dialog::display(_("Password method"),_("Error: Password method not available!"),FATAL_ERROR_DIALOG);
160                 exit;
161         }
165 /* Check for selected user... */
166 if (isset($_GET['uid']) && $_GET['uid'] != ""){
167         $uid= validate($_GET['uid']);
168         $smarty->assign('display_username', false);
169 } elseif (isset($_POST['uid'])){
170         $uid= validate($_POST['uid']);
171         $smarty->assign('display_username', true);
172 } else {
173         $uid= "";
174         $smarty->assign('display_username', true);
176 $current_password= "";
177 $smarty->assign("changed", false);
179 /* Got a formular answer, validate and try to log in */
180 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])){
182   /* Destroy old sessions, they cause a successfull login to relog again ...*/
183   if(session::is_set('_LAST_PAGE_REQUEST')){
184     session::set('_LAST_PAGE_REQUEST',time());
185   }
187   $message= array();
188   $current_password= $_POST['current_password'];
190   /* Do new and repeated password fields match? */
191   $new_password= $_POST['new_password'];
192   if ($_POST['new_password'] != $_POST['new_password_repeated']){
193           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
194   } else {
195           if ($_POST['new_password'] == ""){
196       $message[]= msgPool::required(_("New password"));
197           }
198   }
200   /* Password policy fulfilled? */
201   if ($config->get_cfg_value("pwdiffer") != ""){
202           $l= $config->get_cfg_value("pwdiffer");
203           if (substr($_POST['current_password'], 0, $l) == substr($_POST['new_password'], 0, $l)){
204                   $message[]= _("The password used as new and current are too similar.");
205           }
206   }
207   if ($config->get_cfg_value("passwordMinLength") != ""){
208           if (strlen($_POST['new_password']) < $config->get_cfg_value("passwordMinLength")){
209                   $message[]= _("The password used as new is to short.");
210           }
211   }
213   /* Validate */
214   if (!tests::is_uid($uid)){
215           $message[]= msgPool::invalid(_("Login"));
216   } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0){
217     $message[]= msgPool::required(_("Current password"));
218   } else {
220     /* Do we have the selected user somewhere? */
221     $ui= ldap_login_user ($uid, $current_password);
223     if ($ui === NULL){
224       $message[]= _("Please check the username/password combination.");
225     } else {
226       $acls = $ui->get_permissions($ui->dn,"users/password");
227       if(!preg_match("/w/i",$acls)){
228         $message[]= _("You have no permissions to change your password.");
229       }
230     }
231   }
233   /* Do we need to show error messages? */
234   if (count ($message) != 0){
235           /* Show error message and continue editing */
236           msg_dialog::displayChecks($message);
237   } else {
239           /* Passed quality check, just try to change the password now */
240           $output= "";
241           if ($config->get_cfg_value("externalpwdhook") != ""){
242                   exec($config->get_cfg_value("externalpwdhook")." ".$ui->username." ".
243                                   $_POST['current_password']." ".$_POST['new_password'], $resarr);
244                   if(count($resarr) > 0) {
245                           $output= join('\n', $resarr);
246                   }
247           }
248           if ($output != ""){
249                   $message[]= _("External password changer reported a problem: ".$output);
250                   msg_dialog::displayChecks($message);
251           } else {
252                   if ($method != ""){
253                           change_password ($ui->dn, $_POST['new_password'], 0, $method);
254                   } else {
255                           change_password ($ui->dn, $_POST['new_password']);
256                   }
257                   gosa_log ("User/password has been changed");
258                   $smarty->assign("changed", true);
259           }
260   }
265 /* Parameter fill up */
266 $params= "";
267 foreach (array('uid', 'method', 'directory') as $index){
268         $params.= "&amp;$index=".urlencode($$index);
270 $params= preg_replace('/^&amp;/', '?', $params);
271 $smarty->assign('params', $params);
273 /* Fill template with required values */
274 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
275 $smarty->assign ('uid', $uid);
276 $smarty->assign ('password_img', get_template_path('images/password.png'));
278 /* Displasy SSL mode warning? */
279 if ($ssl != "" && $config->get_cfg_value("warnssl") == 'true'){
280   $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>"._("Enter SSL session")."</b></a>!");
281 } else {
282   $smarty->assign ("ssl", "");
285 /* show login screen */
286 $smarty->assign("JS",session::get('js'));
287 $smarty->assign ("PHPSESSID", session_id());
288 if (session::is_set('errors')){
289   $smarty->assign("errors", session::get('errors'));;
291 if ($error_collector != ""){
292   $smarty->assign("php_errors", $error_collector."</div>");
293 } else {
294   $smarty->assign("php_errors", "");
297 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
298 displayPWchanger();
300 ?>
302 </body>
303 </html>
304 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: