]> git.donarmstrong.com Git - roundcube.git/blob - plugins/password/password.php
Imported Upstream version 0.5.2+dfsg
[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
132                 // Reset session password
133                 $_SESSION['password'] = $rcmail->encrypt($newpwd);
134
135                 // Log password change
136                 if ($rcmail->config->get('password_log')) {
137                     write_log('password', sprintf('Password changed for user %s (ID: %d) from %s',
138                         $rcmail->user->get_username(), $rcmail->user->ID, rcmail_remote_ip()));
139                 }
140             }
141             else {
142                 $rcmail->output->command('display_message', $res, 'error');
143             }
144         }
145
146         rcmail_overwrite_action('plugin.password');
147         $rcmail->output->send('plugin');
148     }
149
150     function password_form()
151     {
152         $rcmail = rcmail::get_instance();
153         $this->load_config();
154
155         // add some labels to client
156         $rcmail->output->add_label(
157             'password.nopassword',
158             'password.nocurpassword',
159             'password.passwordinconsistency'
160         );
161
162         $rcmail->output->set_env('product_name', $rcmail->config->get('product_name'));
163
164         $table = new html_table(array('cols' => 2));
165
166         if ($rcmail->config->get('password_confirm_current')) {
167             // show current password selection
168             $field_id = 'curpasswd';
169             $input_curpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id,
170                 'size' => 20, 'autocomplete' => 'off'));
171   
172             $table->add('title', html::label($field_id, Q($this->gettext('curpasswd'))));
173             $table->add(null, $input_curpasswd->show());
174         }
175
176         // show new password selection
177         $field_id = 'newpasswd';
178         $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id,
179             'size' => 20, 'autocomplete' => 'off'));
180
181         $table->add('title', html::label($field_id, Q($this->gettext('newpasswd'))));
182         $table->add(null, $input_newpasswd->show());
183
184         // show confirm password selection
185         $field_id = 'confpasswd';
186         $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id,
187             'size' => 20, 'autocomplete' => 'off'));
188
189         $table->add('title', html::label($field_id, Q($this->gettext('confpasswd'))));
190         $table->add(null, $input_confpasswd->show());
191
192         $out = html::div(array('class' => 'box'),
193             html::div(array('id' => 'prefs-title', 'class' => 'boxtitle'), $this->gettext('changepasswd')) .
194             html::div(array('class' => 'boxcontent'), $table->show() .
195             html::p(null,
196                 $rcmail->output->button(array(
197                     'command' => 'plugin.password-save',
198                     'type' => 'input',
199                     'class' => 'button mainaction',
200                     'label' => 'save'
201             )))));
202
203         $rcmail->output->add_gui_object('passform', 'password-form');
204
205         return $rcmail->output->form_tag(array(
206             'id' => 'password-form',
207             'name' => 'password-form',
208             'method' => 'post',
209             'action' => './?_task=settings&_action=plugin.password-save',
210         ), $out);
211     }
212
213     private function _save($curpass, $passwd)
214     {
215         $config = rcmail::get_instance()->config;
216         $driver = $this->home.'/drivers/'.$config->get('password_driver', 'sql').'.php';
217     
218         if (!is_readable($driver)) {
219             raise_error(array(
220                 'code' => 600,
221                 'type' => 'php',
222                 'file' => __FILE__, 'line' => __LINE__,
223                 'message' => "Password plugin: Unable to open driver file $driver"
224             ), true, false);
225             return $this->gettext('internalerror');
226         }
227     
228         include($driver);
229
230         if (!function_exists('password_save')) {
231             raise_error(array(
232                 'code' => 600,
233                 'type' => 'php',
234                 'file' => __FILE__, 'line' => __LINE__,
235                 'message' => "Password plugin: Broken driver: $driver"
236             ), true, false);
237             return $this->gettext('internalerror');
238         }
239
240         $result = password_save($curpass, $passwd);
241
242         if (is_array($result)) {
243             $message = $result['message'];
244             $result  = $result['code'];
245         }
246
247         switch ($result) {
248             case PASSWORD_SUCCESS:
249                 return;
250             case PASSWORD_CRYPT_ERROR;
251                 $reason = $this->gettext('crypterror');
252             case PASSWORD_CONNECT_ERROR;
253                 $reason = $this->gettext('connecterror');
254             case PASSWORD_ERROR:
255             default:
256                 $reason = $this->gettext('internalerror');
257         }
258
259         if ($message) {
260             $reason .= ' ' . $message;
261         }
262
263         return $reason;
264     }                                     
265 }