]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/passwordsettings.php
Merge branch 'master' into testing
[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
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 SettingsAction
49 {
50     /**
51      * Title of the page
52      *
53      * @return string Title of the page
54      */
55
56     function title()
57     {
58         // TRANS: Title for page where to change password.
59         return _m('TITLE','Change password');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return instructions for use
66      */
67
68     function getInstructions()
69     {
70         // TRANS: Instructions for page where to change password.
71         return _('Change your password.');
72     }
73
74     function showScripts()
75     {
76         parent::showScripts();
77         $this->autofocus('oldpassword');
78     }
79
80     /**
81      * Content area of the page
82      *
83      * Shows a form for changing the password
84      *
85      * @return void
86      */
87
88     function showContent()
89     {
90         $user = common_current_user();
91
92         $this->elementStart('form', array('method' => 'POST',
93                                           'id' => 'form_password',
94                                           'class' => 'form_settings',
95                                           'action' =>
96                                           common_local_url('passwordsettings')));
97         $this->elementStart('fieldset');
98         // TRANS: Fieldset legend on page where to change password.
99         $this->element('legend', null, _('Password change'));
100         $this->hidden('token', common_session_token());
101
102
103         $this->elementStart('ul', 'form_data');
104         // Users who logged in with OpenID won't have a pwd
105         if ($user->password) {
106             $this->elementStart('li');
107             // TRANS: Field label on page where to change password.
108             $this->password('oldpassword', _('Old password'));
109             $this->elementEnd('li');
110         }
111         $this->elementStart('li');
112         // TRANS: Field label on page where to change password.
113         $this->password('newpassword', _('New password'),
114                         // TRANS: Field title on page where to change password.
115                         _('6 or more characters.'));
116         $this->elementEnd('li');
117         $this->elementStart('li');
118         // TRANS: Field label on page where to change password. In this field the new password should be typed a second time.
119         $this->password('confirm', _m('LABEL','Confirm'),
120                         // TRANS: Field title on page where to change password.
121                         _('Same as password above.'));
122         $this->elementEnd('li');
123         $this->elementEnd('ul');
124
125         // TRANS: Button text on page where to change password.
126         $this->submit('changepass', _m('BUTTON','Change'));
127
128         $this->elementEnd('fieldset');
129         $this->elementEnd('form');
130     }
131
132     /**
133      * Handle a post
134      *
135      * Validate input and save changes. Reload the form with a success
136      * or error message.
137      *
138      * @return void
139      */
140     function handlePost()
141     {
142         // CSRF protection
143
144         $token = $this->trimmed('token');
145         if (!$token || $token != common_session_token()) {
146             // TRANS: Client error displayed when the session token does not match or is not given.
147             $this->showForm(_('There was a problem with your session token. '.
148                                'Try again, please.'));
149             return;
150         }
151
152         $user = common_current_user();
153         assert(!is_null($user)); // should already be checked
154
155         // FIXME: scrub input
156
157         $newpassword = $this->arg('newpassword');
158         $confirm     = $this->arg('confirm');
159
160         // Some validation
161
162         if (strlen($newpassword) < 6) {
163             // TRANS: Form validation error on page where to change password.
164             $this->showForm(_('Password must be 6 or more characters.'));
165             return;
166         } else if (0 != strcmp($newpassword, $confirm)) {
167             // TRANS: Form validation error on password change when password confirmation does not match.
168             $this->showForm(_('Passwords do not match.'));
169             return;
170         }
171
172         if ($user->password) {
173             $oldpassword = $this->arg('oldpassword');
174
175             if (!common_check_user($user->nickname, $oldpassword)) {
176                 // TRANS: Form validation error on page where to change password.
177                 $this->showForm(_('Incorrect old password.'));
178                 return;
179             }
180         }else{
181             $oldpassword = null;
182         }
183
184         $success = false;
185         if(Event::handle('StartChangePassword', array($user, $oldpassword, $newpassword))){
186             //no handler changed the password, so change the password internally
187             $original = clone($user);
188
189             $user->password = common_munge_password($newpassword, $user->id);
190
191             $val = $user->validate();
192             if ($val !== true) {
193                 // TRANS: Form validation error on page where to change password.
194                 $this->showForm(_('Error saving user; invalid.'));
195                 return;
196             }
197
198             if (!$user->update($original)) {
199             // TRANS: Server error displayed on page where to change password when password change
200             // TRANS: could not be made because of a server error.
201             $this->serverError(_('Cannot save new password.'));
202                 return;
203             }
204             Event::handle('EndChangePassword', array($user));
205         }
206
207         // TRANS: Form validation notice on page where to change password.
208         $this->showForm(_('Password saved.'), true);
209     }
210 }