Fix access to invalid symbol in Special:Userrights
[lhc/web/wiklou.git] / includes / specials / SpecialChangePassword.php
1 <?php
2 /**
3 * Implements Special:ChangePassword
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Let users recover their password.
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialChangePassword extends FormSpecialPage {
30 protected $mUserName;
31 protected $mDomain;
32
33 // Optional Wikitext Message to show above the password change form
34 protected $mPreTextMessage = null;
35
36 // label for old password input
37 protected $mOldPassMsg = null;
38
39 public function __construct() {
40 parent::__construct( 'ChangePassword', 'editmyprivateinfo' );
41 $this->listed( false );
42 }
43
44 public function doesWrites() {
45 return true;
46 }
47
48 /**
49 * Main execution point
50 * @param string|null $par
51 */
52 function execute( $par ) {
53 $this->getOutput()->disallowUserJs();
54
55 parent::execute( $par );
56 }
57
58 protected function checkExecutePermissions( User $user ) {
59 parent::checkExecutePermissions( $user );
60
61 if ( !$this->getRequest()->wasPosted() ) {
62 $this->requireLogin( 'resetpass-no-info' );
63 }
64 }
65
66 /**
67 * Set a message at the top of the Change Password form
68 * @since 1.23
69 * @param Message $msg Message to parse and add to the form header
70 */
71 public function setChangeMessage( Message $msg ) {
72 $this->mPreTextMessage = $msg;
73 }
74
75 /**
76 * Set a message at the top of the Change Password form
77 * @since 1.23
78 * @param string $msg Message label for old/temp password field
79 */
80 public function setOldPasswordMessage( $msg ) {
81 $this->mOldPassMsg = $msg;
82 }
83
84 protected function getFormFields() {
85 $user = $this->getUser();
86 $request = $this->getRequest();
87
88 $oldpassMsg = $this->mOldPassMsg;
89 if ( $oldpassMsg === null ) {
90 $oldpassMsg = $user->isLoggedIn() ? 'oldpassword' : 'resetpass-temp-password';
91 }
92
93 $fields = array(
94 'Name' => array(
95 'type' => 'info',
96 'label-message' => 'username',
97 'default' => $request->getVal( 'wpName', $user->getName() ),
98 ),
99 'Password' => array(
100 'type' => 'password',
101 'label-message' => $oldpassMsg,
102 ),
103 'NewPassword' => array(
104 'type' => 'password',
105 'label-message' => 'newpassword',
106 ),
107 'Retype' => array(
108 'type' => 'password',
109 'label-message' => 'retypenew',
110 ),
111 );
112
113 if ( !$this->getUser()->isLoggedIn() ) {
114 if ( !LoginForm::getLoginToken() ) {
115 LoginForm::setLoginToken();
116 }
117 $fields['LoginOnChangeToken'] = array(
118 'type' => 'hidden',
119 'label' => 'Change Password Token',
120 'default' => LoginForm::getLoginToken(),
121 );
122 }
123
124 $extraFields = array();
125 Hooks::run( 'ChangePasswordForm', array( &$extraFields ) );
126 foreach ( $extraFields as $extra ) {
127 list( $name, $label, $type, $default ) = $extra;
128 $fields[$name] = array(
129 'type' => $type,
130 'name' => $name,
131 'label-message' => $label,
132 'default' => $default,
133 );
134 }
135
136 if ( !$user->isLoggedIn() ) {
137 $fields['Remember'] = array(
138 'type' => 'check',
139 'label' => $this->msg( 'remembermypassword' )
140 ->numParams(
141 ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
142 )->text(),
143 'default' => $request->getVal( 'wpRemember' ),
144 );
145 }
146
147 return $fields;
148 }
149
150 protected function alterForm( HTMLForm $form ) {
151 $form->setId( 'mw-resetpass-form' );
152 $form->setTableId( 'mw-resetpass-table' );
153 $form->setWrapperLegendMsg( 'resetpass_header' );
154 $form->setSubmitTextMsg(
155 $this->getUser()->isLoggedIn()
156 ? 'resetpass-submit-loggedin'
157 : 'resetpass_submit'
158 );
159 $form->addButton( array(
160 'name' => 'wpCancel',
161 'value' => $this->msg( 'resetpass-submit-cancel' )->text()
162 ) );
163 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
164 if ( $this->mPreTextMessage instanceof Message ) {
165 $form->addPreText( $this->mPreTextMessage->parseAsBlock() );
166 }
167 $form->addHiddenFields(
168 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
169 }
170
171 public function onSubmit( array $data ) {
172 global $wgAuth;
173
174 $request = $this->getRequest();
175
176 if ( $request->getCheck( 'wpLoginToken' ) ) {
177 // This comes from Special:Userlogin when logging in with a temporary password
178 return false;
179 }
180
181 if ( !$this->getUser()->isLoggedIn()
182 && $request->getVal( 'wpLoginOnChangeToken' ) !== LoginForm::getLoginToken()
183 ) {
184 // Potential CSRF (bug 62497)
185 return false;
186 }
187
188 if ( $request->getCheck( 'wpCancel' ) ) {
189 $returnto = $request->getVal( 'returnto' );
190 $titleObj = $returnto !== null ? Title::newFromText( $returnto ) : null;
191 if ( !$titleObj instanceof Title ) {
192 $titleObj = Title::newMainPage();
193 }
194 $query = $request->getVal( 'returntoquery' );
195 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
196
197 return true;
198 }
199
200 $this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
201 $this->mDomain = $wgAuth->getDomain();
202
203 if ( !$wgAuth->allowPasswordChange() ) {
204 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
205 }
206
207 $status = $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
208
209 return $status;
210 }
211
212 public function onSuccess() {
213 if ( $this->getUser()->isLoggedIn() ) {
214 $this->getOutput()->wrapWikiMsg(
215 "<div class=\"successbox\">\n$1\n</div>",
216 'changepassword-success'
217 );
218 $this->getOutput()->returnToMain();
219 } else {
220 $request = $this->getRequest();
221 LoginForm::setLoginToken();
222 $token = LoginForm::getLoginToken();
223 $data = array(
224 'action' => 'submitlogin',
225 'wpName' => $this->mUserName,
226 'wpDomain' => $this->mDomain,
227 'wpLoginToken' => $token,
228 'wpPassword' => $request->getVal( 'wpNewPassword' ),
229 ) + $request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
230 $login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
231 $login->setContext( $this->getContext() );
232 $login->execute( null );
233 }
234 }
235
236 /**
237 * Checks the new password if it meets the requirements for passwords and set
238 * it as a current password, otherwise set the passed Status object to fatal
239 * and doesn't change anything
240 *
241 * @param string $oldpass The current (temporary) password.
242 * @param string $newpass The password to set.
243 * @param string $retype The string of the retype password field to check with newpass
244 * @return Status
245 */
246 protected function attemptReset( $oldpass, $newpass, $retype ) {
247 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
248 if ( $isSelf ) {
249 $user = $this->getUser();
250 } else {
251 $user = User::newFromName( $this->mUserName );
252 }
253
254 if ( !$user || $user->isAnon() ) {
255 return Status::newFatal( $this->msg( 'nosuchusershort', $this->mUserName ) );
256 }
257
258 if ( $newpass !== $retype ) {
259 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
260 return Status::newFatal( $this->msg( 'badretype' ) );
261 }
262
263 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
264 if ( $throttleCount === true ) {
265 $lang = $this->getLanguage();
266 $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
267 return Status::newFatal( $this->msg( 'changepassword-throttled' )
268 ->params( $lang->formatDuration( $throttleInfo['seconds'] ) )
269 );
270 }
271
272 // @todo Make these separate messages, since the message is written for both cases
273 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
274 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
275 return Status::newFatal( $this->msg( 'resetpass-wrong-oldpass' ) );
276 }
277
278 // User is resetting their password to their old password
279 if ( $oldpass === $newpass ) {
280 return Status::newFatal( $this->msg( 'resetpass-recycled' ) );
281 }
282
283 // Do AbortChangePassword after checking mOldpass, so we don't leak information
284 // by possibly aborting a new password before verifying the old password.
285 $abortMsg = 'resetpass-abort-generic';
286 if ( !Hooks::run( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
287 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
288 return Status::newFatal( $this->msg( $abortMsg ) );
289 }
290
291 // Please reset throttle for successful logins, thanks!
292 if ( $throttleCount ) {
293 LoginForm::clearLoginThrottle( $this->mUserName );
294 }
295
296 try {
297 $user->setPassword( $newpass );
298 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
299 } catch ( PasswordError $e ) {
300 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
301 return Status::newFatal( new RawMessage( $e->getMessage() ) );
302 }
303
304 if ( $isSelf ) {
305 // This is needed to keep the user connected since
306 // changing the password also modifies the user's token.
307 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
308 $user->setCookies( null, null, $remember );
309 }
310 $user->saveSettings();
311 $this->resetPasswordExpiration( $user );
312 return Status::newGood();
313 }
314
315 public function requiresUnblock() {
316 return false;
317 }
318
319 protected function getGroupName() {
320 return 'users';
321 }
322
323 /**
324 * For resetting user password expiration, until AuthManager comes along
325 * @param User $user
326 */
327 private function resetPasswordExpiration( User $user ) {
328 global $wgPasswordExpirationDays;
329 $newExpire = null;
330 if ( $wgPasswordExpirationDays ) {
331 $newExpire = wfTimestamp(
332 TS_MW,
333 time() + ( $wgPasswordExpirationDays * 24 * 3600 )
334 );
335 }
336 // Give extensions a chance to force an expiration
337 Hooks::run( 'ResetPasswordExpiration', array( $this, &$newExpire ) );
338 $dbw = wfGetDB( DB_MASTER );
339 $dbw->update(
340 'user',
341 array( 'user_password_expires' => $dbw->timestampOrNull( $newExpire ) ),
342 array( 'user_id' => $user->getID() ),
343 __METHOD__
344 );
345 }
346
347 protected function getDisplayFormat() {
348 return 'ooui';
349 }
350 }