Code

Replaced config->search with get_cfg_value
[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",
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             _("Compile directory %s is not accessible!"),
89             bold($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("core","language") == "") {
101     $lang= get_browser_language();
102 } else {
103     $lang= $config->get_cfg_value("core","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("core","forceSSL") == 'true' && $ssl != '') {
169     header("Location: $ssl");
170     exit;
173 /* Check for selected password method */
174 $method= $config->get_cfg_value("core","passwordDefaultHash", "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 values for 'New password' and 'Repeated new password' differ!");
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("core","passwordMinDiffer") != "") {
227         $l= $config->get_cfg_value("core","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("core","passwordMinLength") != "") {
234         if (strlen($_POST['new_password']) <
235            $config->get_cfg_value("core","passwordMinLength")) {
236             $message[]= _("The password used as new is to short!");
237         }
238     }
239     if(!passwordMethod::is_harmless($_POST['new_password'])){
240         $message[]= _("The password contains possibly problematic unicode characters!");
241     }
243     /* Validate */
244     if (!tests::is_uid($uid)) {
245         $message[]= msgPool::invalid(_("Login"));
246     } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0) {
247         $message[]= msgPool::required(_("Current password"));
248     } else {
250         /* Do we have the selected user somewhere? */
251         $ui= ldap_login_user($uid, $current_password);
253         if ($ui === NULL) {
254             $message[]= _("Please check the username/password combination!");
255         } else {
256             $acls = $ui->get_permissions($ui->dn, "users/password");
257             if (!preg_match("/w/i", $acls)) {
258                 $message[]= _("You have no permissions to change your password!");
259             }
260         }
261     }
263     /* Do we need to show error messages? */
264     if (count($message) != 0) {
265         /* Show error message and continue editing */
266         msg_dialog::displayChecks($message);
267     } else {
269         /* Passed quality check, just try to change the password now */
270         $output= "";
271         if ($config->get_cfg_value("core","passwordHook") != "") {
272             exec(
273                 $config->get_cfg_value("core","passwordHook")." ".$ui->username." ".
274                 $_POST['current_password']." ".$_POST['new_password'],
275                 $resarr
276             );
277             if (count($resarr) > 0) {
278                 $output= join('\n', $resarr);
279             }
280         }
281         if ($output != "") {
282             $message[]= sprintf(
283                 _("External password changer reported a problem: %s"),
284                 $output
285             );
286             msg_dialog::displayChecks($message);
287         } else {
288             if ($method != "") {
289                 change_password($ui->dn, $_POST['new_password'], 0, $method);
290             } else {
291                 change_password($ui->dn, $_POST['new_password']);
292             }
293             gosa_log("User/password has been changed");
294             $smarty->assign("changed", true);
295         }
296     }
301 /* Parameter fill up */
302 $params= "";
303 foreach (array('uid', 'method', 'directory') as $index) {
304     $params.= "&amp;$index=".urlencode($$index);
306 $params= preg_replace('/^&amp;/', '?', $params);
307 $smarty->assign('params', $params);
309 /* Fill template with required values */
310 $smarty->assign('date', gmdate("D, d M Y H:i:s"));
311 $smarty->assign('uid', $uid);
312 $smarty->assign('password_img', get_template_path('images/password.png'));
314 /* Displasy SSL mode warning? */
315 if ($ssl != "" && $config->get_cfg_value("core","warnSSL") == 'true') {
316     $smarty->assign(
317         "ssl",
318         "<b>"._("Warning").":</b> "._("Session will not be encrypted.").
319         " <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>".
320         _("Enter SSL session")."</b></a>!"
321     );
322 } else {
323     $smarty->assign("ssl", "");
326 /* show login screen */
327 $smarty->assign("JS", session::global_get('js'));
328 $smarty->assign("PHPSESSID", session_id());
329 if (session::is_set('errors')) {
330     $smarty->assign("errors", session::get('errors'));;
332 if ($error_collector != "") {
333     $smarty->assign("php_errors", $error_collector."</div>");
334 } else {
335     $smarty->assign("php_errors", "");
338 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
339 displayPWchanger();
341 ?>
343 </body>
344 </html>
345 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: