Code

Set default title to GOsa
[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("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(
80     "templateCompileDirectory", '/var/spool/gosa'
81 );
83 /* Check for compile directory */
84 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
85     msg_dialog::display(
86         _("Configuration error"),
87         sprintf(
88             _("Directory '%s' specified as compile directory is not accessible!"),
89             $smarty->compile_dir
90         ),
91         FATAL_ERROR_DIALOG
92     );
93     exit;
94 }
96 /* Check for old files in compile directory */
97 clean_smarty_compile_dir($smarty->compile_dir);
99 /* Language setup */
100 if ($config->get_cfg_value("language") == "") {
101     $lang= get_browser_language();
102 } else {
103     $lang= $config->get_cfg_value("language");
105 $lang.=".UTF-8";
106 putenv("LANGUAGE=");
107 putenv("LANG=$lang");
108 setlocale(LC_ALL, $lang);
109 $GLOBALS['t_language']= $lang;
110 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
112 /* Set the text domain as 'messages' */
113 $domain = 'messages';
114 bindtextdomain($domain, LOCALE_DIR);
115 textdomain($domain);
117 /* Generate server list */
118 $servers= array();
119 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
120     $servers[$key]= $key;
122 if (isset($_POST['server'])) {
123     $directory= validate($_POST['server']);
124 } else {
125     $directory= $config->data['MAIN']['DEFAULT'];
127     if (!isset($servers[$directory])) {
128         $directory = key($servers);
129     }
131 $smarty->assign ("title","GOsa");
132 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])) {
133     $smarty->assign("show_directory_chooser", false);
134     $directory= validate($_GET['directory']);
135 } else {
136     $smarty->assign("server_options", $servers);
137     $smarty->assign("server_id", $directory);
138     $smarty->assign("show_directory_chooser", true);
141 /* Set config to selected one */
142 $config->set_current($directory);
143 session::global_set('config', $config);
145 if ($_SERVER["REQUEST_METHOD"] != "POST") {
146     @DEBUG(
147         DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
148         $lang, "Setting language to"
149     );
153 /* Check for SSL connection */
154 $ssl= "";
155 if (!isset($_SERVER['HTTPS']) ||
156     !stristr($_SERVER['HTTPS'], "on")) {
158         if (empty($_SERVER['REQUEST_URI'])) {
159             $ssl= "https://".$_SERVER['HTTP_HOST'].
160                 $_SERVER['PATH_INFO'];
161         } else {
162             $ssl= "https://".$_SERVER['HTTP_HOST'].
163                 $_SERVER['REQUEST_URI'];
164         }
167 /* If SSL is forced, just forward to the SSL enabled site */
168 if ($config->get_cfg_value("forcessl") == 'true' && $ssl != '') {
169     header("Location: $ssl");
170     exit;
173 /* Check for selected password method */
174 $method= $config->get_cfg_value("hash", "crypt/md5");
175 if (isset($_GET['method'])) {
176     $method= validate($_GET['method']);
177     $tmp = new passwordMethod($config);
178     $available = $tmp->get_available_methods();
179     if (!isset($available[$method])) {
180         msg_dialog::display(
181             _("Password method"),
182             _("Error: Password method not available!"),
183             FATAL_ERROR_DIALOG
184         );
185         exit;
186     }
190 /* Check for selected user... */
191 if (isset($_GET['uid']) && $_GET['uid'] != "") {
192     $uid= validate($_GET['uid']);
193     $smarty->assign('display_username', false);
194 } elseif (isset($_POST['uid'])) {
195     $uid= validate($_POST['uid']);
196     $smarty->assign('display_username', true);
197 } else {
198     $uid= "";
199     $smarty->assign('display_username', true);
201 $current_password= "";
202 $smarty->assign("changed", false);
204 /* Got a formular answer, validate and try to log in */
205 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])) {
207     /* Destroy old sessions, they cause a successfull login to relog again ...*/
208     if (session::global_is_set('_LAST_PAGE_REQUEST')) {
209         session::global_set('_LAST_PAGE_REQUEST', time());
210     }
212     $message= array();
213     $current_password= $_POST['current_password'];
215     /* Do new and repeated password fields match? */
216     $new_password= $_POST['new_password'];
217     if ($_POST['new_password'] != $_POST['new_password_repeated']) {
218         $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
219     } else {
220         if ($_POST['new_password'] == "") {
221             $message[]= msgPool::required(_("New password"));
222         }
223     }
225     /* Password policy fulfilled? */
226     if ($config->get_cfg_value("passwordMinDiffer") != "") {
227         $l= $config->get_cfg_value("passwordMinDiffer");
228         if (substr($_POST['current_password'], 0, $l) ==
229             substr($_POST['new_password'], 0, $l)) {
230             $message[]= _("The password used as new and current are too similar.");
231         }
232     }
233     if ($config->get_cfg_value("passwordMinLength") != "") {
234         if (strlen($_POST['new_password']) <
235            $config->get_cfg_value("passwordMinLength")) {
236             $message[]= _("The password used as new is to short.");
237         }
238     }
240     /* Validate */
241     if (!tests::is_uid($uid)) {
242         $message[]= msgPool::invalid(_("Login"));
243     } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0) {
244         $message[]= msgPool::required(_("Current password"));
245     } else {
247         /* Do we have the selected user somewhere? */
248         $ui= ldap_login_user($uid, $current_password);
250         if ($ui === NULL) {
251             $message[]= _("Please check the username/password combination.");
252         } else {
253             $acls = $ui->get_permissions($ui->dn, "users/password");
254             if (!preg_match("/w/i", $acls)) {
255                 $message[]= _("You have no permissions to change your password.");
256             }
257         }
258     }
260     /* Do we need to show error messages? */
261     if (count($message) != 0) {
262         /* Show error message and continue editing */
263         msg_dialog::displayChecks($message);
264     } else {
266         /* Passed quality check, just try to change the password now */
267         $output= "";
268         if ($config->get_cfg_value("passwordHook") != "") {
269             exec(
270                 $config->get_cfg_value("passwordHook")." ".$ui->username." ".
271                 $_POST['current_password']." ".$_POST['new_password'],
272                 $resarr
273             );
274             if (count($resarr) > 0) {
275                 $output= join('\n', $resarr);
276             }
277         }
278         if ($output != "") {
279             $message[]= sprintf(
280                 _("External password changer reported a problem: %s"),
281                 $output
282             );
283             msg_dialog::displayChecks($message);
284         } else {
285             if ($method != "") {
286                 change_password($ui->dn, $_POST['new_password'], 0, $method);
287             } else {
288                 change_password($ui->dn, $_POST['new_password']);
289             }
290             gosa_log("User/password has been changed");
291             $smarty->assign("changed", true);
292         }
293     }
298 /* Parameter fill up */
299 $params= "";
300 foreach (array('uid', 'method', 'directory') as $index) {
301     $params.= "&amp;$index=".urlencode($$index);
303 $params= preg_replace('/^&amp;/', '?', $params);
304 $smarty->assign('params', $params);
306 /* Fill template with required values */
307 $smarty->assign('date', gmdate("D, d M Y H:i:s"));
308 $smarty->assign('uid', $uid);
309 $smarty->assign('password_img', get_template_path('images/password.png'));
311 /* Displasy SSL mode warning? */
312 if ($ssl != "" && $config->get_cfg_value("warnssl") == 'true') {
313     $smarty->assign(
314         "ssl",
315         "<b>"._("Warning").":</b> "._("Session will not be encrypted.").
316         " <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>".
317         _("Enter SSL session")."</b></a>!"
318     );
319 } else {
320     $smarty->assign("ssl", "");
323 /* show login screen */
324 $smarty->assign("JS", session::global_get('js'));
325 $smarty->assign("PHPSESSID", session_id());
326 if (session::is_set('errors')) {
327     $smarty->assign("errors", session::get('errors'));;
329 if ($error_collector != "") {
330     $smarty->assign("php_errors", $error_collector."</div>");
331 } else {
332     $smarty->assign("php_errors", "");
335 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
336 displayPWchanger();
338 ?>
340 </body>
341 </html>
342 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: