Code

Updated password.php
[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->assign ("logo", image(get_template_path("images/logo.png")));
28     $smarty->assign ("date", date("l, dS F Y H:i:s O"));
29     $smarty->display(get_template_path('password.tpl'));
30     exit();
31 }
33 /* Load required includes */
34 require_once "../include/php_setup.inc";
35 require_once "functions.inc";
37 if (!class_exists("log")) {
38     require_once("class_log.inc");
39 }
41 header("Content-type: text/html; charset=UTF-8");
43 session::start();
45 /* Destroy old session if exists.
46 Else you will get your old session back, if you not logged out correctly. */
47 if (is_array(session::get_all()) && count(session::get_all())) {
48     session::destroy();
49     session::start();
50 }
52 /* Reset errors */
53 session::global_set('js', true);
54 session::set('errors', "");
55 session::set('errorsAlreadyPosted', array());
56 session::set('LastError', "");
58 /* Check if CONFIG_FILE is accessible */
59 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)) {
60     msg_dialog::display(
61         _("Fatal error"),
62         sprintf(
63             _("GOsa configuration %s/%s is not readable. Aborted."),
64             CONFIG_DIR, CONFIG_FILE
65         ),
66         FATAL_ERROR_DIALOG
67     );
68     exit;
69 }
71 /* Parse configuration file */
72 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
74 /* Generate server list */
75 $servers= array();
76 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
77     $servers[$key]= $key;
78 }
80 if (isset($_POST['server'])) {
81     $directory= get_post('server');
82 }elseif (isset($_GET['directory'])) {
83     $directory= $_GET['directory'];
84 } else {
85     $directory= $config->data['MAIN']['DEFAULT'];
86     if (!isset($servers[$directory])) {
87         $directory = key($servers);
88     }
89     
90 }
92 // Set location and reload the configRegistry - we've now access to the ldap. 
93 if(isset($servers[$directory])){
94     $config->set_current($directory);
95     $config->check_and_reload();
96     $config->configRegistry->reload(TRUE);
97 }
99 session::global_set('debugLevel', $config->get_cfg_value("core","debugLevel"));
100 if ($_SERVER["REQUEST_METHOD"] != "POST") {
101     @DEBUG(
102         DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config"
103     );
106 /* Set template compile directory */
107 $smarty->compile_dir= $config->get_cfg_value("core", "templateCompileDirectory");
109 /* Check for compile directory */
110 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
111     msg_dialog::display(
112         _("Configuration error"),
113         sprintf(
114             _("Compile directory %s is not accessible!"),
115             bold($smarty->compile_dir)
116         ),
117         FATAL_ERROR_DIALOG
118     );
119     exit;
122 /* Check for old files in compile directory */
123 clean_smarty_compile_dir($smarty->compile_dir);
125 /* Language setup */
126 if ($config->get_cfg_value("core","language") == "") {
127     $lang= get_browser_language();
128 } else {
129     $lang= $config->get_cfg_value("core","language");
131 $lang.=".UTF-8";
132 putenv("LANGUAGE=");
133 putenv("LANG=$lang");
134 setlocale(LC_ALL, $lang);
135 $GLOBALS['t_language']= $lang;
136 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
138 /* Set the text domain as 'messages' */
139 $domain = 'messages';
140 bindtextdomain($domain, LOCALE_DIR);
141 textdomain($domain);
143 $smarty->assign ("title","GOsa");
144 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])) {
145     $smarty->assign("show_directory_chooser", false);
146     $directory= validate($_GET['directory']);
147 } else {
148     $smarty->assign("server_options", $servers);
149     $smarty->assign("server_id", $directory);
150     $smarty->assign("show_directory_chooser", true);
153 /* Set config to selected one */
154 $config->set_current($directory);
155 session::global_set('config', $config);
157 if ($_SERVER["REQUEST_METHOD"] != "POST") {
158     @DEBUG(
159         DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
160         $lang, "Setting language to"
161     );
165 /* Check for SSL connection */
166 $ssl= "";
167 if (!isset($_SERVER['HTTPS']) ||
168     !stristr($_SERVER['HTTPS'], "on")) {
170         if (empty($_SERVER['REQUEST_URI'])) {
171             $ssl= "https://".$_SERVER['HTTP_HOST'].
172                 $_SERVER['PATH_INFO'];
173         } else {
174             $ssl= "https://".$_SERVER['HTTP_HOST'].
175                 $_SERVER['REQUEST_URI'];
176         }
179 /* If SSL is forced, just forward to the SSL enabled site */
180 if ($config->get_cfg_value("core","forceSSL") == 'true' && $ssl != '') {
181     header("Location: $ssl");
182     exit;
185 /* Check for selected password method */
186 $method= $config->get_cfg_value("core","passwordDefaultHash");
187 if (isset($_GET['method'])) {
188     $method= validate($_GET['method']);
189     $tmp = new passwordMethod($config, "dummy");
190     $available = $tmp->get_available_methods();
191     if (!isset($available[$method])) {
192         msg_dialog::display(
193             _("Password method"),
194             _("Error: Password method not available!"),
195             FATAL_ERROR_DIALOG
196         );
197         exit;
198     }
202 /* Check for selected user... */
203 if (isset($_GET['uid']) && $_GET['uid'] != "") {
204     $uid= validate($_GET['uid']);
205     $smarty->assign('display_username', false);
206 } elseif (isset($_POST['uid'])) {
207     $uid= validate($_POST['uid']);
208     $smarty->assign('display_username', true);
209 } else {
210     $uid= "";
211     $smarty->assign('display_username', true);
213 $current_password= "";
214 $smarty->assign("changed", false);
216 /* Got a formular answer, validate and try to log in */
217 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])) {
219     /* Destroy old sessions, they cause a successfull login to relog again ...*/
220     if (session::global_is_set('_LAST_PAGE_REQUEST')) {
221         session::global_set('_LAST_PAGE_REQUEST', time());
222     }
224     $message= array();
225     $current_password= $_POST['current_password'];
227     /* Do new and repeated password fields match? */
228     $new_password= $_POST['new_password'];
229     if ($_POST['new_password'] != $_POST['new_password_repeated']) {
230         $message[]= _("The values for 'New password' and 'Repeated new password' differ!");
231     } else {
232         if ($_POST['new_password'] == "") {
233             $message[]= msgPool::required(_("New password"));
234         }
235     }
237     /* Password policy fulfilled? */
238     if ($config->get_cfg_value("core","passwordMinDiffer") != "") {
239         $l= $config->get_cfg_value("core","passwordMinDiffer");
240         if (substr($_POST['current_password'], 0, $l) ==
241             substr($_POST['new_password'], 0, $l)) {
242             $message[]= _("The password used as new and current are too similar!");
243         }
244     }
245     if ($config->get_cfg_value("core","passwordMinLength") != "") {
246         if (strlen($_POST['new_password']) <
247            $config->get_cfg_value("core","passwordMinLength")) {
248             $message[]= _("The password used as new is to short!");
249         }
250     }
251     if(!passwordMethod::is_harmless($_POST['new_password'])){
252         $message[]= _("The password contains possibly problematic unicode characters!");
253     }
255     /* Validate */
256     if (!tests::is_uid($uid)) {
257         $message[]= msgPool::invalid(_("Login"));
258     } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0) {
259         $message[]= msgPool::required(_("Current password"));
260     } else {
262         /* Do we have the selected user somewhere? */
263         $ui= ldap_login_user($uid, $current_password);
265         /* Load ocMapping into userinfo */
266         $tmp= new acl($config, NULL, $ui->dn);
267         $ui->ocMapping= $tmp->ocMapping;
270         $ui->loadACL();
272         if ($ui === NULL) {
273             $message[]= _("Please check the username/password combination!");
274         } else {
275             $acls = $ui->get_permissions($ui->dn, "users/password");
276             if (!preg_match("/w/i", $acls)) {
277                 $message[]= _("You have no permissions to change your password!");
278             }
279         }
280     }
282     /* Do we need to show error messages? */
283     if (count($message) != 0) {
284         /* Show error message and continue editing */
285         msg_dialog::displayChecks($message);
286     } else {
288         /* Passed quality check, just try to change the password now */
289         $output= "";
290         if ($config->get_cfg_value("core","passwordHook") != "") {
291             exec(
292                 $config->get_cfg_value("core","passwordHook")." ".$ui->username." ".
293                 $_POST['current_password']." ".$_POST['new_password'],
294                 $resarr
295             );
296             if (count($resarr) > 0) {
297                 $output= join('\n', $resarr);
298             }
299         }
300         if ($output != "") {
301             $message[]= sprintf(
302                 _("External password changer reported a problem: %s"),
303                 $output
304             );
305             msg_dialog::displayChecks($message);
306         } else {
307             if ($method != "") {
308                 change_password($ui->dn, $_POST['new_password'], 0, $method);
309             } else {
310                 change_password($ui->dn, $_POST['new_password']);
311             }
312             gosa_log("User/password has been changed");
313             $smarty->assign("changed", true);
314         }
315     }
320 /* Parameter fill up */
321 $params= "";
322 foreach (array('uid', 'method', 'directory') as $index) {
323     $params.= "&amp;$index=".urlencode($$index);
325 $params= preg_replace('/^&amp;/', '?', $params);
326 $smarty->assign('params', $params);
328 /* Fill template with required values */
329 $smarty->assign('date', gmdate("D, d M Y H:i:s"));
330 $smarty->assign('uid', $uid);
331 $smarty->assign('password_img', get_template_path('images/password.png'));
333 /* Displasy SSL mode warning? */
334 if ($ssl != "" && $config->get_cfg_value("core","warnSSL") == 'true') {
335     $smarty->assign(
336         "ssl",
337         "<b>"._("Warning").":</b> "._("Session will not be encrypted.").
338         " <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>".
339         _("Enter SSL session")."</b></a>!"
340     );
341 } else {
342     $smarty->assign("ssl", "");
345 /* show login screen */
346 $smarty->assign("JS", session::global_get('js'));
347 $smarty->assign("PHPSESSID", session_id());
348 if (session::is_set('errors')) {
349     $smarty->assign("errors", session::get('errors'));;
351 if ($error_collector != "") {
352     $smarty->assign("php_errors", $error_collector."</div>");
353 } else {
354     $smarty->assign("php_errors", "");
357 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
358 displayPWchanger();
360 ?>
362 </body>
363 </html>
364 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: