]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/passwordsettings.php
Merge branch '0.9.x' of git://gitorious.org/statusnet/mainline into 0.9.x
[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     /**
62      * Instructions for use
63      *
64      * @return instructions for use
65      */
66
67     function getInstructions()
68     {
69         return _('Change your password.');
70     }
71
72     function showScripts()
73     {
74         parent::showScripts();
75         $this->autofocus('oldpassword');
76     }
77
78     /**
79      * Content area of the page
80      *
81      * Shows a form for changing the password
82      *
83      * @return void
84      */
85
86     function showContent()
87     {
88         $user = common_current_user();
89         $this->elementStart('form', array('method' => 'POST',
90                                           'id' => 'form_password',
91                                           'class' => 'form_settings',
92                                           'action' =>
93                                           common_local_url('passwordsettings')));
94         $this->elementStart('fieldset');
95         $this->element('legend', null, _('Password change'));
96         $this->hidden('token', common_session_token());
97
98
99         $this->elementStart('ul', 'form_data');
100         // Users who logged in with OpenID will not have a pwd
101         if ($user->password) {
102             $this->elementStart('li');
103             $this->password('oldpassword', _('Old password'));
104             $this->elementEnd('li');
105         }
106         $this->elementStart('li');
107         $this->password('newpassword', _('New password'),
108                         _('6 or more characters'));
109         $this->elementEnd('li');
110         $this->elementStart('li');
111         $this->password('confirm', _('Confirm'),
112                         _('same as password above'));
113         $this->elementEnd('li');
114         $this->elementEnd('ul');
115
116         $this->submit('changepass', _('Change'));
117
118         $this->elementEnd('fieldset');
119         $this->elementEnd('form');
120     }
121
122     /**
123      * Handle a post
124      *
125      * Validate input and save changes. Reload the form with a success
126      * or error message.
127      *
128      * @return void
129      */
130
131     function handlePost()
132     {
133         // CSRF protection
134
135         $token = $this->trimmed('token');
136         if (!$token || $token != common_session_token()) {
137             $this->showForm(_('There was a problem with your session token. '.
138                                'Try again, please.'));
139             return;
140         }
141
142         $user = common_current_user();
143         assert(!is_null($user)); // should already be checked
144
145         // FIXME: scrub input
146
147         $newpassword = $this->arg('newpassword');
148         $confirm     = $this->arg('confirm');
149
150         # Some validation
151
152         if (strlen($newpassword) < 6) {
153             $this->showForm(_('Password must be 6 or more characters.'));
154             return;
155         } else if (0 != strcmp($newpassword, $confirm)) {
156             $this->showForm(_('Passwords don\'t match.'));
157             return;
158         }
159
160         if ($user->password) {
161             $oldpassword = $this->arg('oldpassword');
162
163             if (!common_check_user($user->nickname, $oldpassword)) {
164                 $this->showForm(_('Incorrect old password'));
165                 return;
166             }
167         }else{
168             $oldpassword = null;
169         }
170
171         $errormsg = false;
172         if(! Event::handle('ChangePassword', array($user->nickname, $oldpassword, $newpassword, &$errormsg))){
173             //no handler changed the password, so change the password internally
174             $original = clone($user);
175
176             $user->password = common_munge_password($newpassword, $user->id);
177
178             $val = $user->validate();
179             if ($val !== true) {
180                 $this->showForm(_('Error saving user; invalid.'));
181                 return;
182             }
183
184             if (!$user->update($original)) {
185                 $this->serverError(_('Can\'t save new password.'));
186                 return;
187             }
188         }
189
190         if($errormsg === false)
191             $this->showForm(_('Password saved.'), true);
192         else
193             $this->showForm($errormsg);
194     }
195 }