source: subversion/branches/release-0.7/plugins/password/password.php @ 5426

Last change on this file since 5426 was 5426, checked in by alec, 19 months ago
  • Applied fixes from trunk up to r5425
File size: 11.4 KB
Line 
1<?php
2
3/*
4 +-------------------------------------------------------------------------+
5 | Password Plugin for Roundcube                                           |
6 | @version @package_version@                                                             |
7 |                                                                         |
8 | Copyright (C) 2009-2010, Roundcube Dev.                                 |
9 |                                                                         |
10 | This program is free software; you can redistribute it and/or modify    |
11 | it under the terms of the GNU General Public License version 2          |
12 | as published by the Free Software Foundation.                           |
13 |                                                                         |
14 | This program is distributed in the hope that it will be useful,         |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
17 | GNU General Public License for more details.                            |
18 |                                                                         |
19 | You should have received a copy of the GNU General Public License along |
20 | with this program; if not, write to the Free Software Foundation, Inc., |
21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.             |
22 |                                                                         |
23 +-------------------------------------------------------------------------+
24 | Author: Aleksander Machniak <alec@alec.pl>                              |
25 +-------------------------------------------------------------------------+
26
27 $Id: index.php 2645 2009-06-15 07:01:36Z alec $
28
29*/
30
31define('PASSWORD_CRYPT_ERROR', 1);
32define('PASSWORD_ERROR', 2);
33define('PASSWORD_CONNECT_ERROR', 3);
34define('PASSWORD_SUCCESS', 0);
35
36/**
37 * Change password plugin
38 *
39 * Plugin that adds functionality to change a users password.
40 * It provides common functionality and user interface and supports
41 * several backends to finally update the password.
42 *
43 * For installation and configuration instructions please read the README file.
44 *
45 * @author Aleksander Machniak
46 */
47class password extends rcube_plugin
48{
49    public $task    = 'settings';
50    public $noframe = true;
51    public $noajax  = true;
52
53    function init()
54    {
55        $rcmail = rcmail::get_instance();
56        // add Tab label
57        $rcmail->output->add_label('password');
58        $this->register_action('plugin.password', array($this, 'password_init'));
59        $this->register_action('plugin.password-save', array($this, 'password_save'));
60        $this->include_script('password.js');
61    }
62
63    function password_init()
64    {
65        $this->add_texts('localization/');
66        $this->register_handler('plugin.body', array($this, 'password_form'));
67
68        $rcmail = rcmail::get_instance();
69        $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
70        $rcmail->output->send('plugin');
71    }
72
73    function password_save()
74    {
75        $rcmail = rcmail::get_instance();
76        $this->load_config();
77
78        $this->add_texts('localization/');
79        $this->register_handler('plugin.body', array($this, 'password_form'));
80        $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
81
82        $confirm = $rcmail->config->get('password_confirm_current');
83        $required_length = intval($rcmail->config->get('password_minimum_length'));
84        $check_strength = $rcmail->config->get('password_require_nonalpha');
85
86        if (($confirm && !isset($_POST['_curpasswd'])) || !isset($_POST['_newpasswd'])) {
87            $rcmail->output->command('display_message', $this->gettext('nopassword'), 'error');
88        }
89        else {
90
91            $charset    = strtoupper($rcmail->config->get('password_charset', 'ISO-8859-1'));
92            $rc_charset = strtoupper($rcmail->output->get_charset());
93
94            $sespwd = $rcmail->decrypt($_SESSION['password']);
95            $curpwd = $confirm ? get_input_value('_curpasswd', RCUBE_INPUT_POST, true, $charset) : $sespwd;
96            $newpwd = get_input_value('_newpasswd', RCUBE_INPUT_POST, true);
97            $conpwd = get_input_value('_confpasswd', RCUBE_INPUT_POST, true);
98
99            // check allowed characters according to the configured 'password_charset' option
100            // by converting the password entered by the user to this charset and back to UTF-8
101            $orig_pwd = $newpwd;
102            $chk_pwd = rcube_charset_convert($orig_pwd, $rc_charset, $charset);
103            $chk_pwd = rcube_charset_convert($chk_pwd, $charset, $rc_charset);
104
105            // WARNING: Default password_charset is ISO-8859-1, so conversion will
106            // change national characters. This may disable possibility of using
107            // the same password in other MUA's.
108            // We're doing this for consistence with Roundcube core
109            $newpwd = rcube_charset_convert($newpwd, $rc_charset, $charset);
110            $conpwd = rcube_charset_convert($conpwd, $rc_charset, $charset);
111
112            if ($chk_pwd != $orig_pwd) {
113                $rcmail->output->command('display_message', $this->gettext('passwordforbidden'), 'error');
114            }
115            // other passwords validity checks
116            else if ($conpwd != $newpwd) {
117                $rcmail->output->command('display_message', $this->gettext('passwordinconsistency'), 'error');
118            }
119            else if ($confirm && $sespwd != $curpwd) {
120                $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error');
121            }
122            else if ($required_length && strlen($newpwd) < $required_length) {
123                $rcmail->output->command('display_message', $this->gettext(
124                        array('name' => 'passwordshort', 'vars' => array('length' => $required_length))), 'error');
125            }
126            else if ($check_strength && (!preg_match("/[0-9]/", $newpwd) || !preg_match("/[^A-Za-z0-9]/", $newpwd))) {
127                $rcmail->output->command('display_message', $this->gettext('passwordweak'), 'error');
128            }
129            // password is the same as the old one, do nothing, return success
130            else if ($sespwd == $newpwd) {
131                $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
132            }
133            // try to save the password
134            else if (!($res = $this->_save($curpwd, $newpwd))) {
135                $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
136
137                // allow additional actions after password change (e.g. reset some backends)
138                $plugin = $rcmail->plugins->exec_hook('password_change', array(
139                    'old_pass' => $curpwd, 'new_pass' => $newpwd));
140
141                // Reset session password
142                $_SESSION['password'] = $rcmail->encrypt($plugin['new_pass']);
143
144                // Log password change
145                if ($rcmail->config->get('password_log')) {
146                    write_log('password', sprintf('Password changed for user %s (ID: %d) from %s',
147                        $rcmail->user->get_username(), $rcmail->user->ID, rcmail_remote_ip()));
148                }
149            }
150            else {
151                $rcmail->output->command('display_message', $res, 'error');
152            }
153        }
154
155        rcmail_overwrite_action('plugin.password');
156        $rcmail->output->send('plugin');
157    }
158
159    function password_form()
160    {
161        $rcmail = rcmail::get_instance();
162        $this->load_config();
163
164        // add some labels to client
165        $rcmail->output->add_label(
166            'password.nopassword',
167            'password.nocurpassword',
168            'password.passwordinconsistency'
169        );
170
171        $rcmail->output->set_env('product_name', $rcmail->config->get('product_name'));
172
173        $table = new html_table(array('cols' => 2));
174
175        if ($rcmail->config->get('password_confirm_current')) {
176            // show current password selection
177            $field_id = 'curpasswd';
178            $input_curpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id,
179                'size' => 20, 'autocomplete' => 'off'));
180 
181            $table->add('title', html::label($field_id, Q($this->gettext('curpasswd'))));
182            $table->add(null, $input_curpasswd->show());
183        }
184
185        // show new password selection
186        $field_id = 'newpasswd';
187        $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id,
188            'size' => 20, 'autocomplete' => 'off'));
189
190        $table->add('title', html::label($field_id, Q($this->gettext('newpasswd'))));
191        $table->add(null, $input_newpasswd->show());
192
193        // show confirm password selection
194        $field_id = 'confpasswd';
195        $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id,
196            'size' => 20, 'autocomplete' => 'off'));
197
198        $table->add('title', html::label($field_id, Q($this->gettext('confpasswd'))));
199        $table->add(null, $input_confpasswd->show());
200
201        $out = html::div(array('class' => 'box'),
202            html::div(array('id' => 'prefs-title', 'class' => 'boxtitle'), $this->gettext('changepasswd')) .
203            html::div(array('class' => 'boxcontent'), $table->show() .
204            html::p(null,
205                $rcmail->output->button(array(
206                    'command' => 'plugin.password-save',
207                    'type' => 'input',
208                    'class' => 'button mainaction',
209                    'label' => 'save'
210            )))));
211
212        $rcmail->output->add_gui_object('passform', 'password-form');
213
214        return $rcmail->output->form_tag(array(
215            'id' => 'password-form',
216            'name' => 'password-form',
217            'method' => 'post',
218            'action' => './?_task=settings&_action=plugin.password-save',
219        ), $out);
220    }
221
222    private function _save($curpass, $passwd)
223    {
224        $config = rcmail::get_instance()->config;
225        $driver = $this->home.'/drivers/'.$config->get('password_driver', 'sql').'.php';
226
227        if (!is_readable($driver)) {
228            raise_error(array(
229                'code' => 600,
230                'type' => 'php',
231                'file' => __FILE__, 'line' => __LINE__,
232                'message' => "Password plugin: Unable to open driver file $driver"
233            ), true, false);
234            return $this->gettext('internalerror');
235        }
236
237        include($driver);
238
239        if (!function_exists('password_save')) {
240            raise_error(array(
241                'code' => 600,
242                'type' => 'php',
243                'file' => __FILE__, 'line' => __LINE__,
244                'message' => "Password plugin: Broken driver: $driver"
245            ), true, false);
246            return $this->gettext('internalerror');
247        }
248
249        $result = password_save($curpass, $passwd);
250
251        if (is_array($result)) {
252            $message = $result['message'];
253            $result  = $result['code'];
254        }
255
256        switch ($result) {
257            case PASSWORD_SUCCESS:
258                return;
259            case PASSWORD_CRYPT_ERROR;
260                $reason = $this->gettext('crypterror');
261            case PASSWORD_CONNECT_ERROR;
262                $reason = $this->gettext('connecterror');
263            case PASSWORD_ERROR:
264            default:
265                $reason = $this->gettext('internalerror');
266        }
267
268        if ($message) {
269            $reason .= ' ' . $message;
270        }
271
272        return $reason;
273    }
274}
Note: See TracBrowser for help on using the repository browser.