Code

Updated objectListing
[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 }
98 session::global_set('plist', new pluglist($config, $ui));
100 session::global_set('debugLevel', $config->get_cfg_value("core","debugLevel"));
101 if ($_SERVER["REQUEST_METHOD"] != "POST") {
102     @DEBUG(
103         DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config"
104     );
107 /* Set template compile directory */
108 $smarty->compile_dir= $config->get_cfg_value("core", "templateCompileDirectory");
110 /* Check for compile directory */
111 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
112     msg_dialog::display(
113         _("Configuration error"),
114         sprintf(
115             _("Compile directory %s is not accessible!"),
116             bold($smarty->compile_dir)
117         ),
118         FATAL_ERROR_DIALOG
119     );
120     exit;
123 /* Check for old files in compile directory */
124 clean_smarty_compile_dir($smarty->compile_dir);
126 /* Language setup */
127 if ($config->get_cfg_value("core","language") == "") {
128     $lang= get_browser_language();
129 } else {
130     $lang= $config->get_cfg_value("core","language");
132 $lang.=".UTF-8";
133 putenv("LANGUAGE=");
134 putenv("LANG=$lang");
135 setlocale(LC_ALL, $lang);
136 $GLOBALS['t_language']= $lang;
137 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
139 /* Set the text domain as 'messages' */
140 $domain = 'messages';
141 bindtextdomain($domain, LOCALE_DIR);
142 textdomain($domain);
144 $smarty->assign ("title","GOsa");
145 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])) {
146     $smarty->assign("show_directory_chooser", false);
147     $directory= validate($_GET['directory']);
148 } else {
149     $smarty->assign("server_options", $servers);
150     $smarty->assign("server_id", $directory);
151     $smarty->assign("show_directory_chooser", true);
154 /* Set config to selected one */
155 $config->set_current($directory);
156 session::global_set('config', $config);
158 if ($_SERVER["REQUEST_METHOD"] != "POST") {
159     @DEBUG(
160         DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
161         $lang, "Setting language to"
162     );
166 /* Check for SSL connection */
167 $ssl= "";
168 if (!isset($_SERVER['HTTPS']) ||
169     !stristr($_SERVER['HTTPS'], "on")) {
171         if (empty($_SERVER['REQUEST_URI'])) {
172             $ssl= "https://".$_SERVER['HTTP_HOST'].
173                 $_SERVER['PATH_INFO'];
174         } else {
175             $ssl= "https://".$_SERVER['HTTP_HOST'].
176                 $_SERVER['REQUEST_URI'];
177         }
180 /* If SSL is forced, just forward to the SSL enabled site */
181 if ($config->get_cfg_value("core","forceSSL") == 'true' && $ssl != '') {
182     header("Location: $ssl");
183     exit;
186 /* Check for selected password method */
187 $method= $config->get_cfg_value("core","passwordDefaultHash");
188 if (isset($_GET['method'])) {
189     $method= validate($_GET['method']);
190     $tmp = new passwordMethod($config, "dummy");
191     $available = $tmp->get_available_methods();
192     if (!isset($available[$method])) {
193         msg_dialog::display(
194             _("Password method"),
195             _("Error: Password method not available!"),
196             FATAL_ERROR_DIALOG
197         );
198         exit;
199     }
203 /* Check for selected user... */
204 if (isset($_GET['uid']) && $_GET['uid'] != "") {
205     $uid= validate($_GET['uid']);
206     $smarty->assign('display_username', false);
207 } elseif (isset($_POST['uid'])) {
208     $uid= get_post('uid');
209     $smarty->assign('display_username', true);
210 } else {
211     $uid= "";
212     $smarty->assign('display_username', true);
214 $current_password= "";
215 $smarty->assign("changed", false);
217 /* Got a formular answer, validate and try to log in */
218 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])) {
220     /* Destroy old sessions, they cause a successfull login to relog again ...*/
221     if (session::global_is_set('_LAST_PAGE_REQUEST')) {
222         session::global_set('_LAST_PAGE_REQUEST', time());
223     }
225     // Get posted values
226     $current_password = get_post('current_password');
227     $new_password = get_post('new_password');
228     $repeated_password = get_post('new_password_repeated');
231     // Get configuration flags for further input checks.
232     $check_differ = $config->get_cfg_value("core","passwordMinDiffer") != "";
233     $differ       = $config->get_cfg_value("core","passwordMinDiffer");
234     $check_length = $config->get_cfg_value("core","passwordMinLength") != "";
235     $length       = $config->get_cfg_value("core","passwordMinLength");
237     // Once an error has occured it is stored here.
238     $message = array();
240     // Perform GOsa password policy checks
241     if(!tests::is_uid($uid)) {
242         $message[]= msgPool::invalid(_("Login"));
243     }elseif(empty($current_password)){
244         $message[] = _("You need to specify your current password in order to proceed.");
245     }elseif($new_password  != $repeated_password){
246         $message[] = _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
247     }elseif($new_password == ""){
248         $message[] = _("The password you've entered as 'New password' is empty.");
249     }elseif($check_differ && (substr($current_password, 0, $differ) == substr($new_password, 0, $differ))){
250         $message[] = _("The password used as new and current are too similar.");
251     }elseif($check_length && (strlen($new_password) < $length)){
252         $message[] = _("The password used as new is to short.");
253     }elseif(!passwordMethod::is_harmless($new_password)){
254         $message[] = _("The password contains possibly problematic Unicode characters!");
255     }
257     // Connect as the given user and load its ACLs
258     if(!count($message)){
259         $ui= ldap_login_user($uid, $current_password);
260         if ($ui === NULL) {
261             $message[]= _("Please check the username/password combination!");
262         } else {
263             $tmp= new acl($config, NULL, $ui->dn);
264             $ui->ocMapping= $tmp->ocMapping;
265             $ui->loadACL();
266             $acls = $ui->get_permissions($ui->dn, "users/password");
267             if (!preg_match("/w/i", $acls)) {
268                 $message[]= _("You have no permissions to change your password!");
269             }
270         }
271     }
273     // Call external check hook to validate the password change
274     if(!count($message)){
275         $attrs = array();
276         $attrs['current_password'] = ($current_password);
277         $attrs['new_password'] = ($new_password);
278         $checkRes = password::callCheckHook($config,$ui->dn,$attrs);
279         if(count($checkRes)){
280             $message[] = sprintf(_("Check-hook reported a problem: %s. Password change canceled!"),implode($checkRes));
281         }
282     }
284     // Display error messages
285     if (count($message) != 0) {
286         msg_dialog::displayChecks($message);
287     } else
289         // Try to change the password
290         if(!change_password($ui->dn, $_POST['new_password'], FALSE, $method,get_post('current_password'),$msg)){
291             msg_dialog::displayChecks(array($msg));
292         } else {
293             gosa_log("User/password has been changed");
294             $smarty->assign("changed", true);
295         }
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("core","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: