]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/passwordsettings.php
Merge remote-tracking branch 'upstream/master' into social-master
[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')) { exit(1); }
32
33 /**
34  * Change password
35  *
36  * @category Settings
37  * @package  StatusNet
38  * @author   Evan Prodromou <evan@status.net>
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  */
43
44 class PasswordsettingsAction extends SettingsAction
45 {
46     /**
47      * Title of the page
48      *
49      * @return string Title of the page
50      */
51
52     function title()
53     {
54         // TRANS: Title for page where to change password.
55         return _m('TITLE','Change password');
56     }
57
58     /**
59      * Instructions for use
60      *
61      * @return instructions for use
62      */
63
64     function getInstructions()
65     {
66         // TRANS: Instructions for page where to change password.
67         return _('Change your password.');
68     }
69
70     function showScripts()
71     {
72         parent::showScripts();
73         $this->autofocus('oldpassword');
74     }
75
76     function showContent()
77     {
78         $this->elementStart('form', array('method' => 'POST',
79                                           'id' => 'form_password',
80                                           'class' => 'form_settings',
81                                           'action' =>
82                                           common_local_url('passwordsettings')));
83         $this->elementStart('fieldset');
84         // TRANS: Fieldset legend on page where to change password.
85         $this->element('legend', null, _('Password change'));
86         $this->hidden('token', common_session_token());
87
88
89         $this->elementStart('ul', 'form_data');
90         // Users who logged in with OpenID won't have a pwd
91         if ($this->scoped->hasPassword()) {
92             $this->elementStart('li');
93             // TRANS: Field label on page where to change password.
94             $this->password('oldpassword', _('Old password'));
95             $this->elementEnd('li');
96         }
97         $this->elementStart('li');
98         // TRANS: Field label on page where to change password.
99         $this->password('newpassword', _('New password'),
100                         // TRANS: Field title on page where to change password.
101                         _('6 or more characters.'));
102         $this->elementEnd('li');
103         $this->elementStart('li');
104         // TRANS: Field label on page where to change password. In this field the new password should be typed a second time.
105         $this->password('confirm', _m('LABEL','Confirm'),
106                         // TRANS: Field title on page where to change password.
107                         _('Same as password above.'));
108         $this->elementEnd('li');
109         $this->elementEnd('ul');
110
111         // TRANS: Button text on page where to change password.
112         $this->submit('changepass', _m('BUTTON','Change'));
113
114         $this->elementEnd('fieldset');
115         $this->elementEnd('form');
116     }
117
118     protected function doPost()
119     {
120         // FIXME: scrub input
121
122         $newpassword = $this->arg('newpassword');
123         $confirm     = $this->arg('confirm');
124
125         // Some validation
126
127         if (strlen($newpassword) < 6) {
128             // TRANS: Form validation error on page where to change password.
129             throw new ClientException(_('Password must be 6 or more characters.'));
130         } else if (0 != strcmp($newpassword, $confirm)) {
131             // TRANS: Form validation error on password change when password confirmation does not match.
132             throw new ClientException(_('Passwords do not match.'));
133         }
134
135         $oldpassword = null;
136         if ($this->scoped->hasPassword()) {
137             $oldpassword = $this->arg('oldpassword');
138
139             if (!common_check_user($this->scoped->getNickname(), $oldpassword)) {
140                 // TRANS: Form validation error on page where to change password.
141                 throw new ClientException(_('Incorrect old password.'));
142             }
143         }
144
145         if (Event::handle('StartChangePassword', array($this->scoped, $oldpassword, $newpassword))) {
146             //no handler changed the password, so change the password internally
147             $user = $this->scoped->getUser();
148             $original = clone($user);
149
150             $user->password = common_munge_password($newpassword, $this->scoped);
151
152             $val = $user->validate();
153             if ($val !== true) {
154                 // TRANS: Form validation error on page where to change password.
155                 throw new ServerException(_('Error saving user; invalid.'));
156             }
157
158             if (!$user->update($original)) {
159                 // TRANS: Server error displayed on page where to change password when password change
160                 // TRANS: could not be made because of a server error.
161                 throw new ServerException(_('Cannot save new password.'));
162             }
163             Event::handle('EndChangePassword', array($this->scoped));
164         }
165
166         // TRANS: Form validation notice on page where to change password.
167         return _('Password saved.');
168     }
169 }