]> git.donarmstrong.com Git - roundcube.git/blob - plugins/password/password.php
Imported Upstream version 0.3
[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
83     if (($confirm && !isset($_POST['_curpasswd'])) || !isset($_POST['_newpasswd'])) {
84       $rcmail->output->command('display_message', $this->gettext('nopassword'), 'error');
85     }
86     else {
87       $curpwd = get_input_value('_curpasswd', RCUBE_INPUT_POST);
88       $newpwd = get_input_value('_newpasswd', RCUBE_INPUT_POST);
89
90       if ($confirm && $rcmail->decrypt($_SESSION['password']) != $curpwd)
91         $rcmail->output->command('display_message', $this->gettext('passwordincorrect'), 'error');
92       else if (!($res = $this->_save($curpwd,$newpwd))) {
93         $rcmail->output->command('display_message', $this->gettext('successfullysaved'), 'confirmation');
94         $_SESSION['password'] = $rcmail->encrypt($newpwd);
95       } else
96         $rcmail->output->command('display_message', $res, 'error');
97     }
98
99     rcmail_overwrite_action('plugin.password');
100     $rcmail->output->send('plugin');
101   }
102
103   function password_form()
104   {
105     $rcmail = rcmail::get_instance();
106     $this->load_config();
107
108     // add some labels to client
109     $rcmail->output->add_label(
110       'password.nopassword',
111       'password.nocurpassword',
112       'password.passwordinconsistency'
113     );
114
115     $rcmail->output->set_env('product_name', $rcmail->config->get('product_name'));
116
117     $table = new html_table(array('cols' => 2));
118
119     if ($rcmail->config->get('password_confirm_current')) {
120       // show current password selection
121       $field_id = 'curpasswd';
122       $input_curpasswd = new html_passwordfield(array('name' => '_curpasswd', 'id' => $field_id,
123         'size' => 20, 'autocomplete' => 'off'));
124   
125       $table->add('title', html::label($field_id, Q($this->gettext('curpasswd'))));
126       $table->add(null, $input_curpasswd->show());
127     }
128
129     // show new password selection
130     $field_id = 'newpasswd';
131     $input_newpasswd = new html_passwordfield(array('name' => '_newpasswd', 'id' => $field_id,
132       'size' => 20, 'autocomplete' => 'off'));
133
134     $table->add('title', html::label($field_id, Q($this->gettext('newpasswd'))));
135     $table->add(null, $input_newpasswd->show());
136
137     // show confirm password selection
138     $field_id = 'confpasswd';
139     $input_confpasswd = new html_passwordfield(array('name' => '_confpasswd', 'id' => $field_id,
140       'size' => 20, 'autocomplete' => 'off'));
141
142     $table->add('title', html::label($field_id, Q($this->gettext('confpasswd'))));
143     $table->add(null, $input_confpasswd->show());
144
145     $out = html::div(array('class' => "settingsbox", 'style' => "margin:0"),
146       html::div(array('id' => "prefs-title"), $this->gettext('changepasswd')) .
147       html::div(array('style' => "padding:15px"), $table->show() .
148         html::p(null,
149           $rcmail->output->button(array(
150             'command' => 'plugin.password-save',
151             'type' => 'input',
152             'class' => 'button mainaction',
153             'label' => 'save'
154         )))
155       )
156     );
157
158     $rcmail->output->add_gui_object('passform', 'password-form');
159
160     return $rcmail->output->form_tag(array(
161       'id' => 'password-form',
162       'name' => 'password-form',
163       'method' => 'post',
164       'action' => './?_task=settings&_action=plugin.password-save',
165       ), $out);
166   }
167
168   private function _save($curpass, $passwd)
169   {
170     $config = rcmail::get_instance()->config;
171     $driver = $this->home.'/drivers/'.$config->get('password_driver', 'sql').'.php';
172     
173     if (!is_readable($driver)) {
174       raise_error(array(
175         'code' => 600,
176         'type' => 'php',
177         'file' => __FILE__,
178         'message' => "Password plugin: Unable to open driver file $driver"
179         ), true, false);
180       return $this->gettext('internalerror');
181     }
182     
183     include($driver);
184
185     if (!function_exists('password_save')) {
186       raise_error(array(
187         'code' => 600,
188         'type' => 'php',
189         'file' => __FILE__,
190         'message' => "Password plugin: Broken driver: $driver"
191         ), true, false);
192       return $this->gettext('internalerror');
193     }
194
195     $result = password_save($curpass, $passwd);
196
197     switch ($result) {
198       case PASSWORD_SUCCESS:
199         return;
200       case PASSWORD_CRYPT_ERROR;
201         return $this->gettext('crypterror');
202       case PASSWORD_CONNECT_ERROR;
203         return $this->gettext('connecterror');
204       case PASSWORD_ERROR:
205       default:
206         return $this->gettext('internalerror');
207     }
208   }
209
210 }
211
212 ?>