]> git.donarmstrong.com Git - roundcube.git/blob - plugins/password/password.php
Imported Upstream version 0.3.1
[roundcube.git] / plugins / password / password.php
1 <?php
2
3 /*
4  +-------------------------------------------------------------------------+
5  | Password Plugin for Roundcube                                           |
6  | Version 1.3.1                                                           |
7  |                                                                         |
8  | Copyright (C) 2009, 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  * @version 1.3.1
46  * @author Aleksander Machniak
47  */
48 class password extends rcube_plugin
49 {
50   public $task = 'settings';
51
52   function init()
53   {
54     $rcmail = rcmail::get_instance();
55     // add Tab label
56     $rcmail->output->add_label('password');
57     $this->register_action('plugin.password', array($this, 'password_init'));
58     $this->register_action('plugin.password-save', array($this, 'password_save'));
59     $this->include_script('password.js');
60   }
61
62   function password_init()
63   {
64     $this->add_texts('localization/');
65     $this->register_handler('plugin.body', array($this, 'password_form'));
66
67     $rcmail = rcmail::get_instance();
68     $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
69     $rcmail->output->send('plugin');
70   }
71   
72   function password_save()
73   {
74     $rcmail = rcmail::get_instance();
75     $this->load_config();
76
77     $this->add_texts('localization/');
78     $this->register_handler('plugin.body', array($this, 'password_form'));
79     $rcmail->output->set_pagetitle($this->gettext('changepasswd'));
80
81     $confirm = $rcmail->config->get('password_confirm_current');
82     $required_length = intval($rcmail->config->get('password_minimum_length'));
83     $check_strength = $rcmail->config->get('password_require_nonalpha');
84
85     if (($confirm && !isset($_POST['_curpasswd'])) || !isset($_POST['_newpasswd'])) {
86       $rcmail->output->command('display_message', $this->gettext('nopassword'), 'error');
87     }
88     else {
89
90       $curpwd = get_input_value('_curpasswd', RCUBE_INPUT_POST);
91       $newpwd = get_input_value('_newpasswd', RCUBE_INPUT_POST);
92       $conpwd = get_input_value('_confpasswd', RCUBE_INPUT_POST);
93
94       if ($conpwd != $newpwd) {
95         $rcmail->output->command('display_message', $this->gettext('passwordinconsistency'), 'error');
96       }
97       else if ($confirm && $rcmail->decrypt($_SESSION['password']) != $curpwd) {
98         $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error');
99       }
100       else if ($required_length && strlen($newpwd) < $required_length) {
101         $rcmail->output->command('display_message', $this->gettext(
102           array('name' => 'passwordshort', 'vars' => array('length' => $required_length))), 'error');
103       }
104       else if ($check_strength && (!preg_match("/[0-9]/", $newpwd) || !preg_match("/[^A-Za-z0-9]/", $newpwd))) {
105         $rcmail->output->command('display_message', $this->gettext('passwordweak'), 'error');
106       }
107       else if (!($res = $this->_save($curpwd,$newpwd))) {
108         $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
109         $_SESSION['password'] = $rcmail->encrypt($newpwd);
110       }
111       else
112         $rcmail->output->command('display_message', $res, 'error');
113     }
114
115     rcmail_overwrite_action('plugin.password');
116     $rcmail->output->send('plugin');
117   }
118
119   function password_form()
120   {
121     $rcmail = rcmail::get_instance();
122     $this->load_config();
123
124     // add some labels to client
125     $rcmail->output->add_label(
126       'password.nopassword',
127       'password.nocurpassword',
128       'password.passwordinconsistency'
129     );
130
131     $rcmail->output->set_env('product_name', $rcmail->config->get('product_name'));
132
133     $table = new html_table(array('cols' => 2));
134
135     if ($rcmail->config->get('password_confirm_current')) {
136       // show current password selection
137       $field_id = 'curpasswd';
138       $input_curpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id,
139         'size' => 20, 'autocomplete' => 'off'));
140   
141       $table->add('title', html::label($field_id, Q($this->gettext('curpasswd'))));
142       $table->add(null, $input_curpasswd->show());
143     }
144
145     // show new password selection
146     $field_id = 'newpasswd';
147     $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id,
148       'size' => 20, 'autocomplete' => 'off'));
149
150     $table->add('title', html::label($field_id, Q($this->gettext('newpasswd'))));
151     $table->add(null, $input_newpasswd->show());
152
153     // show confirm password selection
154     $field_id = 'confpasswd';
155     $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id,
156       'size' => 20, 'autocomplete' => 'off'));
157
158     $table->add('title', html::label($field_id, Q($this->gettext('confpasswd'))));
159     $table->add(null, $input_confpasswd->show());
160
161     $out = html::div(array('class' => 'settingsbox', 'style' => 'margin:0'),
162       html::div(array('id' => 'prefs-title', 'class' => 'boxtitle'), $this->gettext('changepasswd')) .
163       html::div(array('class' => 'boxcontent'), $table->show() .
164         html::p(null,
165           $rcmail->output->button(array(
166             'command' => 'plugin.password-save',
167             'type' => 'input',
168             'class' => 'button mainaction',
169             'label' => 'save'
170         )))
171       )
172     );
173
174     $rcmail->output->add_gui_object('passform', 'password-form');
175
176     return $rcmail->output->form_tag(array(
177       'id' => 'password-form',
178       'name' => 'password-form',
179       'method' => 'post',
180       'action' => './?_task=settings&_action=plugin.password-save',
181       ), $out);
182   }
183
184   private function _save($curpass, $passwd)
185   {
186     $config = rcmail::get_instance()->config;
187     $driver = $this->home.'/drivers/'.$config->get('password_driver', 'sql').'.php';
188     
189     if (!is_readable($driver)) {
190       raise_error(array(
191         'code' => 600,
192         'type' => 'php',
193         'file' => __FILE__,
194         'message' => "Password plugin: Unable to open driver file $driver"
195         ), true, false);
196       return $this->gettext('internalerror');
197     }
198     
199     include($driver);
200
201     if (!function_exists('password_save')) {
202       raise_error(array(
203         'code' => 600,
204         'type' => 'php',
205         'file' => __FILE__,
206         'message' => "Password plugin: Broken driver: $driver"
207         ), true, false);
208       return $this->gettext('internalerror');
209     }
210
211     $result = password_save($curpass, $passwd);
212
213     switch ($result) {
214       case PASSWORD_SUCCESS:
215         return;
216       case PASSWORD_CRYPT_ERROR;
217         return $this->gettext('crypterror');
218       case PASSWORD_CONNECT_ERROR;
219         return $this->gettext('connecterror');
220       case PASSWORD_ERROR:
221       default:
222         return $this->gettext('internalerror');
223     }
224   }
225
226 }
227
228 ?>