Merge "Http::getProxy() method to get proxy configuration"
[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 = [
94 'Name' => [
95 'type' => 'info',
96 'label-message' => 'username',
97 'default' => $request->getVal( 'wpName', $user->getName() ),
98 ],
99 'Password' => [
100 'type' => 'password',
101 'label-message' => $oldpassMsg,
102 ],
103 'NewPassword' => [
104 'type' => 'password',
105 'label-message' => 'newpassword',
106 ],
107 'Retype' => [
108 'type' => 'password',
109 'label-message' => 'retypenew',
110 ],
111 ];
112
113 if ( !$this->getUser()->isLoggedIn() ) {
114 $fields['LoginOnChangeToken'] = [
115 'type' => 'hidden',
116 'label' => 'Change Password Token',
117 'default' => LoginForm::getLoginToken()->toString(),
118 ];
119 }
120
121 $extraFields = [];
122 Hooks::run( 'ChangePasswordForm', [ &$extraFields ] );
123 foreach ( $extraFields as $extra ) {
124 list( $name, $label, $type, $default ) = $extra;
125 $fields[$name] = [
126 'type' => $type,
127 'name' => $name,
128 'label-message' => $label,
129 'default' => $default,
130 ];
131 }
132
133 if ( !$user->isLoggedIn() ) {
134 $fields['Remember'] = [
135 'type' => 'check',
136 'label' => $this->msg( 'remembermypassword' )
137 ->numParams(
138 ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
139 )->text(),
140 'default' => $request->getVal( 'wpRemember' ),
141 ];
142 }
143
144 return $fields;
145 }
146
147 protected function alterForm( HTMLForm $form ) {
148 $form->setId( 'mw-resetpass-form' );
149 $form->setTableId( 'mw-resetpass-table' );
150 $form->setWrapperLegendMsg( 'resetpass_header' );
151 $form->setSubmitTextMsg(
152 $this->getUser()->isLoggedIn()
153 ? 'resetpass-submit-loggedin'
154 : 'resetpass_submit'
155 );
156 $form->addButton( [
157 'name' => 'wpCancel',
158 'value' => $this->msg( 'resetpass-submit-cancel' )->text()
159 ] );
160 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
161 if ( $this->mPreTextMessage instanceof Message ) {
162 $form->addPreText( $this->mPreTextMessage->parseAsBlock() );
163 }
164 $form->addHiddenFields(
165 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
166 }
167
168 public function onSubmit( array $data ) {
169 global $wgAuth;
170
171 $request = $this->getRequest();
172
173 if ( $request->getCheck( 'wpLoginToken' ) ) {
174 // This comes from Special:Userlogin when logging in with a temporary password
175 return false;
176 }
177
178 if ( !$this->getUser()->isLoggedIn()
179 && !LoginForm::getLoginToken()->match( $request->getVal( 'wpLoginOnChangeToken' ) )
180 ) {
181 // Potential CSRF (bug 62497)
182 return false;
183 }
184
185 if ( $request->getCheck( 'wpCancel' ) ) {
186 $returnto = $request->getVal( 'returnto' );
187 $titleObj = $returnto !== null ? Title::newFromText( $returnto ) : null;
188 if ( !$titleObj instanceof Title ) {
189 $titleObj = Title::newMainPage();
190 }
191 $query = $request->getVal( 'returntoquery' );
192 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
193
194 return true;
195 }
196
197 $this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
198 $this->mDomain = $wgAuth->getDomain();
199
200 if ( !$wgAuth->allowPasswordChange() ) {
201 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
202 }
203
204 $status = $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
205
206 return $status;
207 }
208
209 public function onSuccess() {
210 if ( $this->getUser()->isLoggedIn() ) {
211 $this->getOutput()->wrapWikiMsg(
212 "<div class=\"successbox\">\n$1\n</div>",
213 'changepassword-success'
214 );
215 $this->getOutput()->returnToMain();
216 } else {
217 $request = $this->getRequest();
218 LoginForm::clearLoginToken();
219 $token = LoginForm::getLoginToken()->toString();
220 $data = [
221 'action' => 'submitlogin',
222 'wpName' => $this->mUserName,
223 'wpDomain' => $this->mDomain,
224 'wpLoginToken' => $token,
225 'wpPassword' => $request->getVal( 'wpNewPassword' ),
226 ] + $request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
227 $login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
228 $login->setContext( $this->getContext() );
229 $login->execute( null );
230 }
231 }
232
233 /**
234 * Checks the new password if it meets the requirements for passwords and set
235 * it as a current password, otherwise set the passed Status object to fatal
236 * and doesn't change anything
237 *
238 * @param string $oldpass The current (temporary) password.
239 * @param string $newpass The password to set.
240 * @param string $retype The string of the retype password field to check with newpass
241 * @return Status
242 */
243 protected function attemptReset( $oldpass, $newpass, $retype ) {
244 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
245 if ( $isSelf ) {
246 $user = $this->getUser();
247 } else {
248 $user = User::newFromName( $this->mUserName );
249 }
250
251 if ( !$user || $user->isAnon() ) {
252 return Status::newFatal( $this->msg( 'nosuchusershort', $this->mUserName ) );
253 }
254
255 if ( $newpass !== $retype ) {
256 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'badretype' ] );
257 return Status::newFatal( $this->msg( 'badretype' ) );
258 }
259
260 $throttleInfo = LoginForm::incrementLoginThrottle( $this->mUserName );
261 if ( $throttleInfo ) {
262 return Status::newFatal( $this->msg( 'changepassword-throttled' )
263 ->durationParams( $throttleInfo['wait'] )
264 );
265 }
266
267 // @todo Make these separate messages, since the message is written for both cases
268 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
269 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'wrongpassword' ] );
270 return Status::newFatal( $this->msg( 'resetpass-wrong-oldpass' ) );
271 }
272
273 // User is resetting their password to their old password
274 if ( $oldpass === $newpass ) {
275 return Status::newFatal( $this->msg( 'resetpass-recycled' ) );
276 }
277
278 // Do AbortChangePassword after checking mOldpass, so we don't leak information
279 // by possibly aborting a new password before verifying the old password.
280 $abortMsg = 'resetpass-abort-generic';
281 if ( !Hooks::run( 'AbortChangePassword', [ $user, $oldpass, $newpass, &$abortMsg ] ) ) {
282 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'abortreset' ] );
283 return Status::newFatal( $this->msg( $abortMsg ) );
284 }
285
286 // Please reset throttle for successful logins, thanks!
287 LoginForm::clearLoginThrottle( $this->mUserName );
288
289 try {
290 $user->setPassword( $newpass );
291 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'success' ] );
292 } catch ( PasswordError $e ) {
293 Hooks::run( 'PrefsPasswordAudit', [ $user, $newpass, 'error' ] );
294 return Status::newFatal( new RawMessage( $e->getMessage() ) );
295 }
296
297 if ( $isSelf ) {
298 // This is needed to keep the user connected since
299 // changing the password also modifies the user's token.
300 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
301 $user->setCookies( null, null, $remember );
302 }
303 $user->saveSettings();
304 $this->resetPasswordExpiration( $user );
305 return Status::newGood();
306 }
307
308 public function requiresUnblock() {
309 return false;
310 }
311
312 protected function getGroupName() {
313 return 'users';
314 }
315
316 /**
317 * For resetting user password expiration, until AuthManager comes along
318 * @param User $user
319 */
320 private function resetPasswordExpiration( User $user ) {
321 global $wgPasswordExpirationDays;
322 $newExpire = null;
323 if ( $wgPasswordExpirationDays ) {
324 $newExpire = wfTimestamp(
325 TS_MW,
326 time() + ( $wgPasswordExpirationDays * 24 * 3600 )
327 );
328 }
329 // Give extensions a chance to force an expiration
330 Hooks::run( 'ResetPasswordExpiration', [ $this, &$newExpire ] );
331 $dbw = wfGetDB( DB_MASTER );
332 $dbw->update(
333 'user',
334 [ 'user_password_expires' => $dbw->timestampOrNull( $newExpire ) ],
335 [ 'user_id' => $user->getId() ],
336 __METHOD__
337 );
338 }
339
340 protected function getDisplayFormat() {
341 return 'ooui';
342 }
343 }