]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/passwordsettings.php
Merge branch '0.9.x' into finish-account-api
[quix0rs-gnu-social.git] / actions / passwordsettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Change user password
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
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 Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @copyright 2008-2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
36
37 /**
38  * Change password
39  *
40  * @category Settings
41  * @package  StatusNet
42  * @author   Evan Prodromou <evan@status.net>
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class PasswordsettingsAction extends AccountSettingsAction
49 {
50     /**
51      * Title of the page
52      *
53      * @return string Title of the page
54      */
55
56     function title()
57     {
58         return _('Change password');
59     }
60
61     function prepare($args){
62         parent::prepare($args);
63
64         $user = common_current_user();
65
66         Event::handle('CanUserChangeField', array($user->nickname, 'password'));
67
68         if(! $fields['password']){
69             //user is not allowed to change his password
70             $this->clientError(_('You are not allowed to change your password'));
71         }
72     }
73
74     /**
75      * Instructions for use
76      *
77      * @return instructions for use
78      */
79
80     function getInstructions()
81     {
82         return _('Change your password.');
83     }
84
85     function showScripts()
86     {
87         parent::showScripts();
88         $this->autofocus('oldpassword');
89     }
90
91     /**
92      * Content area of the page
93      *
94      * Shows a form for changing the password
95      *
96      * @return void
97      */
98
99     function showContent()
100     {
101         $user = common_current_user();
102
103         $this->elementStart('form', array('method' => 'POST',
104                                           'id' => 'form_password',
105                                           'class' => 'form_settings',
106                                           'action' =>
107                                           common_local_url('passwordsettings')));
108         $this->elementStart('fieldset');
109         $this->element('legend', null, _('Password change'));
110         $this->hidden('token', common_session_token());
111
112
113         $this->elementStart('ul', 'form_data');
114         // Users who logged in with OpenID will not have a pwd
115         if ($user->password) {
116             $this->elementStart('li');
117             $this->password('oldpassword', _('Old password'));
118             $this->elementEnd('li');
119         }
120         $this->elementStart('li');
121         $this->password('newpassword', _('New password'),
122                         _('6 or more characters'));
123         $this->elementEnd('li');
124         $this->elementStart('li');
125         $this->password('confirm', _('Confirm'),
126                         _('same as password above'));
127         $this->elementEnd('li');
128         $this->elementEnd('ul');
129
130         $this->submit('changepass', _('Change'));
131
132         $this->elementEnd('fieldset');
133         $this->elementEnd('form');
134     }
135
136     /**
137      * Handle a post
138      *
139      * Validate input and save changes. Reload the form with a success
140      * or error message.
141      *
142      * @return void
143      */
144
145     function handlePost()
146     {
147         // CSRF protection
148
149         $token = $this->trimmed('token');
150         if (!$token || $token != common_session_token()) {
151             $this->showForm(_('There was a problem with your session token. '.
152                                'Try again, please.'));
153             return;
154         }
155
156         $user = common_current_user();
157         assert(!is_null($user)); // should already be checked
158
159         // FIXME: scrub input
160
161         $newpassword = $this->arg('newpassword');
162         $confirm     = $this->arg('confirm');
163
164         # Some validation
165
166         if (strlen($newpassword) < 6) {
167             $this->showForm(_('Password must be 6 or more characters.'));
168             return;
169         } else if (0 != strcmp($newpassword, $confirm)) {
170             $this->showForm(_('Passwords don\'t match.'));
171             return;
172         }
173
174         if ($user->password) {
175             $oldpassword = $this->arg('oldpassword');
176
177             if (!common_check_user($user->nickname, $oldpassword)) {
178                 $this->showForm(_('Incorrect old password'));
179                 return;
180             }
181         }else{
182             $oldpassword = null;
183         }
184
185         $errormsg = false;
186         if(! Event::handle('ChangePassword', array($user->nickname, $oldpassword, $newpassword, &$errormsg))){
187             //no handler changed the password, so change the password internally
188             $original = clone($user);
189
190             $user->password = common_munge_password($newpassword, $user->id);
191
192             $val = $user->validate();
193             if ($val !== true) {
194                 $this->showForm(_('Error saving user; invalid.'));
195                 return;
196             }
197
198             if (!$user->update($original)) {
199                 $this->serverError(_('Can\'t save new password.'));
200                 return;
201             }
202         }
203
204         if($errormsg === false)
205             $this->showForm(_('Password saved.'), true);
206         else
207             $this->showForm($errormsg);
208     }
209 }