Code

Added schema requrements
[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::global_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(
59         _("Fatal error"),
60         sprintf(
61             _("GOsa configuration %s/%s is not readable. Aborted."),
62             CONFIG_DIR, CONFIG_FILE
63         ),
64         FATAL_ERROR_DIALOG
65     );
66     exit;
67 }
69 /* Parse configuration file */
70 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
71 session::global_set('debugLevel', $config->get_cfg_value("core","debugLevel"));
72 if ($_SERVER["REQUEST_METHOD"] != "POST") {
73     @DEBUG(
74         DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config"
75     );
76 }
78 /* Set template compile directory */
79 $smarty->compile_dir= $config->get_cfg_value("core", "templateCompileDirectory");
81 /* Check for compile directory */
82 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
83     msg_dialog::display(
84         _("Configuration error"),
85         sprintf(
86             _("Compile directory %s is not accessible!"),
87             bold($smarty->compile_dir)
88         ),
89         FATAL_ERROR_DIALOG
90     );
91     exit;
92 }
94 /* Check for old files in compile directory */
95 clean_smarty_compile_dir($smarty->compile_dir);
97 /* Language setup */
98 if ($config->get_cfg_value("core","language") == "") {
99     $lang= get_browser_language();
100 } else {
101     $lang= $config->get_cfg_value("core","language");
103 $lang.=".UTF-8";
104 putenv("LANGUAGE=");
105 putenv("LANG=$lang");
106 setlocale(LC_ALL, $lang);
107 $GLOBALS['t_language']= $lang;
108 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
110 /* Set the text domain as 'messages' */
111 $domain = 'messages';
112 bindtextdomain($domain, LOCALE_DIR);
113 textdomain($domain);
115 /* Generate server list */
116 $servers= array();
117 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
118     $servers[$key]= $key;
120 if (isset($_POST['server'])) {
121     $directory= validate($_POST['server']);
122 } else {
123     $directory= $config->data['MAIN']['DEFAULT'];
125     if (!isset($servers[$directory])) {
126         $directory = key($servers);
127     }
129 $smarty->assign ("title","GOsa");
130 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])) {
131     $smarty->assign("show_directory_chooser", false);
132     $directory= validate($_GET['directory']);
133 } else {
134     $smarty->assign("server_options", $servers);
135     $smarty->assign("server_id", $directory);
136     $smarty->assign("show_directory_chooser", true);
139 /* Set config to selected one */
140 $config->set_current($directory);
141 session::global_set('config', $config);
143 if ($_SERVER["REQUEST_METHOD"] != "POST") {
144     @DEBUG(
145         DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
146         $lang, "Setting language to"
147     );
151 /* Check for SSL connection */
152 $ssl= "";
153 if (!isset($_SERVER['HTTPS']) ||
154     !stristr($_SERVER['HTTPS'], "on")) {
156         if (empty($_SERVER['REQUEST_URI'])) {
157             $ssl= "https://".$_SERVER['HTTP_HOST'].
158                 $_SERVER['PATH_INFO'];
159         } else {
160             $ssl= "https://".$_SERVER['HTTP_HOST'].
161                 $_SERVER['REQUEST_URI'];
162         }
165 /* If SSL is forced, just forward to the SSL enabled site */
166 if ($config->get_cfg_value("core","forceSSL") == 'true' && $ssl != '') {
167     header("Location: $ssl");
168     exit;
171 /* Check for selected password method */
172 $method= $config->get_cfg_value("core","passwordDefaultHash");
173 if (isset($_GET['method'])) {
174     $method= validate($_GET['method']);
175     $tmp = new passwordMethod($config);
176     $available = $tmp->get_available_methods();
177     if (!isset($available[$method])) {
178         msg_dialog::display(
179             _("Password method"),
180             _("Error: Password method not available!"),
181             FATAL_ERROR_DIALOG
182         );
183         exit;
184     }
188 /* Check for selected user... */
189 if (isset($_GET['uid']) && $_GET['uid'] != "") {
190     $uid= validate($_GET['uid']);
191     $smarty->assign('display_username', false);
192 } elseif (isset($_POST['uid'])) {
193     $uid= validate($_POST['uid']);
194     $smarty->assign('display_username', true);
195 } else {
196     $uid= "";
197     $smarty->assign('display_username', true);
199 $current_password= "";
200 $smarty->assign("changed", false);
202 /* Got a formular answer, validate and try to log in */
203 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])) {
205     /* Destroy old sessions, they cause a successfull login to relog again ...*/
206     if (session::global_is_set('_LAST_PAGE_REQUEST')) {
207         session::global_set('_LAST_PAGE_REQUEST', time());
208     }
210     $message= array();
211     $current_password= $_POST['current_password'];
213     /* Do new and repeated password fields match? */
214     $new_password= $_POST['new_password'];
215     if ($_POST['new_password'] != $_POST['new_password_repeated']) {
216         $message[]= _("The values for 'New password' and 'Repeated new password' differ!");
217     } else {
218         if ($_POST['new_password'] == "") {
219             $message[]= msgPool::required(_("New password"));
220         }
221     }
223     /* Password policy fulfilled? */
224     if ($config->get_cfg_value("core","passwordMinDiffer") != "") {
225         $l= $config->get_cfg_value("core","passwordMinDiffer");
226         if (substr($_POST['current_password'], 0, $l) ==
227             substr($_POST['new_password'], 0, $l)) {
228             $message[]= _("The password used as new and current are too similar!");
229         }
230     }
231     if ($config->get_cfg_value("core","passwordMinLength") != "") {
232         if (strlen($_POST['new_password']) <
233            $config->get_cfg_value("core","passwordMinLength")) {
234             $message[]= _("The password used as new is to short!");
235         }
236     }
237     if(!passwordMethod::is_harmless($_POST['new_password'])){
238         $message[]= _("The password contains possibly problematic unicode characters!");
239     }
241     /* Validate */
242     if (!tests::is_uid($uid)) {
243         $message[]= msgPool::invalid(_("Login"));
244     } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0) {
245         $message[]= msgPool::required(_("Current password"));
246     } else {
248         /* Do we have the selected user somewhere? */
249         $ui= ldap_login_user($uid, $current_password);
251         if ($ui === NULL) {
252             $message[]= _("Please check the username/password combination!");
253         } else {
254             $acls = $ui->get_permissions($ui->dn, "users/password");
255             if (!preg_match("/w/i", $acls)) {
256                 $message[]= _("You have no permissions to change your password!");
257             }
258         }
259     }
261     /* Do we need to show error messages? */
262     if (count($message) != 0) {
263         /* Show error message and continue editing */
264         msg_dialog::displayChecks($message);
265     } else {
267         /* Passed quality check, just try to change the password now */
268         $output= "";
269         if ($config->get_cfg_value("core","passwordHook") != "") {
270             exec(
271                 $config->get_cfg_value("core","passwordHook")." ".$ui->username." ".
272                 $_POST['current_password']." ".$_POST['new_password'],
273                 $resarr
274             );
275             if (count($resarr) > 0) {
276                 $output= join('\n', $resarr);
277             }
278         }
279         if ($output != "") {
280             $message[]= sprintf(
281                 _("External password changer reported a problem: %s"),
282                 $output
283             );
284             msg_dialog::displayChecks($message);
285         } else {
286             if ($method != "") {
287                 change_password($ui->dn, $_POST['new_password'], 0, $method);
288             } else {
289                 change_password($ui->dn, $_POST['new_password']);
290             }
291             gosa_log("User/password has been changed");
292             $smarty->assign("changed", true);
293         }
294     }
299 /* Parameter fill up */
300 $params= "";
301 foreach (array('uid', 'method', 'directory') as $index) {
302     $params.= "&amp;$index=".urlencode($$index);
304 $params= preg_replace('/^&amp;/', '?', $params);
305 $smarty->assign('params', $params);
307 /* Fill template with required values */
308 $smarty->assign('date', gmdate("D, d M Y H:i:s"));
309 $smarty->assign('uid', $uid);
310 $smarty->assign('password_img', get_template_path('images/password.png'));
312 /* Displasy SSL mode warning? */
313 if ($ssl != "" && $config->get_cfg_value("core","warnSSL") == 'true') {
314     $smarty->assign(
315         "ssl",
316         "<b>"._("Warning").":</b> "._("Session will not be encrypted.").
317         " <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>".
318         _("Enter SSL session")."</b></a>!"
319     );
320 } else {
321     $smarty->assign("ssl", "");
324 /* show login screen */
325 $smarty->assign("JS", session::global_get('js'));
326 $smarty->assign("PHPSESSID", session_id());
327 if (session::is_set('errors')) {
328     $smarty->assign("errors", session::get('errors'));;
330 if ($error_collector != "") {
331     $smarty->assign("php_errors", $error_collector."</div>");
332 } else {
333     $smarty->assign("php_errors", "");
336 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
337 displayPWchanger();
339 ?>
341 </body>
342 </html>
343 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: