]> git.donarmstrong.com Git - roundcube.git/blob - plugins/password/password.php
Imported Debian patch 0.5.1+dfsg-7
[roundcube.git] / plugins / password / password.php
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
31 define('PASSWORD_CRYPT_ERROR', 1);
32 define('PASSWORD_ERROR', 2);
33 define('PASSWORD_CONNECT_ERROR', 3);
34 define('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  */
47 class 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             $curpwd = get_input_value('_curpasswd', RCUBE_INPUT_POST, true, $charset);
95             $newpwd = get_input_value('_newpasswd', RCUBE_INPUT_POST, true);
96             $conpwd = get_input_value('_confpasswd', RCUBE_INPUT_POST, true);
97
98             // check allowed characters according to the configured 'password_charset' option
99             // by converting the password entered by the user to this charset and back to UTF-8
100             $orig_pwd = $newpwd;
101             $chk_pwd = rcube_charset_convert($orig_pwd, $rc_charset, $charset);
102             $chk_pwd = rcube_charset_convert($chk_pwd, $charset, $rc_charset);
103
104             // WARNING: Default password_charset is ISO-8859-1, so conversion will
105             // change national characters. This may disable possibility of using
106             // the same password in other MUA's.
107             // We're doing this for consistence with Roundcube core
108             $newpwd = rcube_charset_convert($newpwd, $rc_charset, $charset);
109             $conpwd = rcube_charset_convert($conpwd, $rc_charset, $charset);
110
111             if ($chk_pwd != $orig_pwd) {
112                 $rcmail->output->command('display_message', $this->gettext('passwordforbidden'), 'error');
113             }
114             // other passwords validity checks
115             else if ($conpwd != $newpwd) {
116                 $rcmail->output->command('display_message', $this->gettext('passwordinconsistency'), 'error');
117             }
118             else if ($confirm && $rcmail->decrypt($_SESSION['password']) != $curpwd) {
119                 $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error');
120             }
121             else if ($required_length && strlen($newpwd) < $required_length) {
122                 $rcmail->output->command('display_message', $this->gettext(
123                         array('name' => 'passwordshort', 'vars' => array('length' => $required_length))), 'error');
124             }
125             else if ($check_strength && (!preg_match("/[0-9]/", $newpwd) || !preg_match("/[^A-Za-z0-9]/", $newpwd))) {
126                 $rcmail->output->command('display_message', $this->gettext('passwordweak'), 'error');
127             }
128             // try to save the password
129             else if (!($res = $this->_save($curpwd, $newpwd))) {
130                 $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
131                 $_SESSION['password'] = $rcmail->encrypt($newpwd);
132             }
133             else {
134                 $rcmail->output->command('display_message', $res, 'error');
135             }
136         }
137
138         rcmail_overwrite_action('plugin.password');
139         $rcmail->output->send('plugin');
140     }
141
142     function password_form()
143     {
144         $rcmail = rcmail::get_instance();
145         $this->load_config();
146
147         // add some labels to client
148         $rcmail->output->add_label(
149             'password.nopassword',
150             'password.nocurpassword',
151             'password.passwordinconsistency'
152         );
153
154         $rcmail->output->set_env('product_name', $rcmail->config->get('product_name'));
155
156         $table = new html_table(array('cols' => 2));
157
158         if ($rcmail->config->get('password_confirm_current')) {
159             // show current password selection
160             $field_id = 'curpasswd';
161             $input_curpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id,
162                 'size' => 20, 'autocomplete' => 'off'));
163   
164             $table->add('title', html::label($field_id, Q($this->gettext('curpasswd'))));
165             $table->add(null, $input_curpasswd->show());
166         }
167
168         // show new password selection
169         $field_id = 'newpasswd';
170         $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id,
171             'size' => 20, 'autocomplete' => 'off'));
172
173         $table->add('title', html::label($field_id, Q($this->gettext('newpasswd'))));
174         $table->add(null, $input_newpasswd->show());
175
176         // show confirm password selection
177         $field_id = 'confpasswd';
178         $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id,
179             'size' => 20, 'autocomplete' => 'off'));
180
181         $table->add('title', html::label($field_id, Q($this->gettext('confpasswd'))));
182         $table->add(null, $input_confpasswd->show());
183
184         $out = html::div(array('class' => 'box'),
185             html::div(array('id' => 'prefs-title', 'class' => 'boxtitle'), $this->gettext('changepasswd')) .
186             html::div(array('class' => 'boxcontent'), $table->show() .
187             html::p(null,
188                 $rcmail->output->button(array(
189                     'command' => 'plugin.password-save',
190                     'type' => 'input',
191                     'class' => 'button mainaction',
192                     'label' => 'save'
193             )))));
194
195         $rcmail->output->add_gui_object('passform', 'password-form');
196
197         return $rcmail->output->form_tag(array(
198             'id' => 'password-form',
199             'name' => 'password-form',
200             'method' => 'post',
201             'action' => './?_task=settings&_action=plugin.password-save',
202         ), $out);
203     }
204
205     private function _save($curpass, $passwd)
206     {
207         $config = rcmail::get_instance()->config;
208         $driver = $this->home.'/drivers/'.$config->get('password_driver', 'sql').'.php';
209     
210         if (!is_readable($driver)) {
211             raise_error(array(
212                 'code' => 600,
213                 'type' => 'php',
214                 'file' => __FILE__, 'line' => __LINE__,
215                 'message' => "Password plugin: Unable to open driver file $driver"
216             ), true, false);
217             return $this->gettext('internalerror');
218         }
219     
220         include($driver);
221
222         if (!function_exists('password_save')) {
223             raise_error(array(
224                 'code' => 600,
225                 'type' => 'php',
226                 'file' => __FILE__, 'line' => __LINE__,
227                 'message' => "Password plugin: Broken driver: $driver"
228             ), true, false);
229             return $this->gettext('internalerror');
230         }
231
232         $result = password_save($curpass, $passwd);
233
234         if (is_array($result)) {
235             $result  = $result['code'];
236             $message = $result['message'];
237         }
238
239         switch ($result) {
240             case PASSWORD_SUCCESS:
241                 return;
242             case PASSWORD_CRYPT_ERROR;
243                 $reason = $this->gettext('crypterror');
244             case PASSWORD_CONNECT_ERROR;
245                 $reason = $this->gettext('connecterror');
246             case PASSWORD_ERROR:
247             default:
248                 $reason = $this->gettext('internalerror');
249         }
250
251         if ($message) {
252             $reason .= ' ' . $message;
253         }
254
255         return $reason;
256     }                                     
257 }