Code

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