Merge "Add version comments for 1.24 to all updaters"
[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
31 protected $mUserName, $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 /**
45 * Main execution point
46 */
47 function execute( $par ) {
48 $this->getOutput()->disallowUserJs();
49
50 parent::execute( $par );
51 }
52
53 protected function checkExecutePermissions( User $user ) {
54 parent::checkExecutePermissions( $user );
55
56 if ( !$this->getRequest()->wasPosted() ) {
57 $this->requireLogin( 'resetpass-no-info' );
58 }
59 }
60
61 /**
62 * Set a message at the top of the Change Password form
63 * @since 1.23
64 * @param Message $msg to parse and add to the form header
65 */
66 public function setChangeMessage( Message $msg ) {
67 $this->mPreTextMessage = $msg;
68 }
69
70 /**
71 * Set a message at the top of the Change Password form
72 * @since 1.23
73 * @param string $msg Message label for old/temp password field
74 */
75 public function setOldPasswordMessage( $msg ) {
76 $this->mOldPassMsg = $msg;
77 }
78
79 protected function getFormFields() {
80 global $wgCookieExpiration;
81
82 $user = $this->getUser();
83 $request = $this->getRequest();
84
85 $oldpassMsg = $this->mOldPassMsg;
86 if ( !isset( $oldpassMsg ) ) {
87 $oldpassMsg = $user->isLoggedIn() ? 'oldpassword' : 'resetpass-temp-password';
88 }
89
90 $fields = array(
91 'Name' => array(
92 'type' => 'info',
93 'label-message' => 'username',
94 'default' => $request->getVal( 'wpName', $user->getName() ),
95 ),
96 'Password' => array(
97 'type' => 'password',
98 'label-message' => $oldpassMsg,
99 ),
100 'NewPassword' => array(
101 'type' => 'password',
102 'label-message' => 'newpassword',
103 ),
104 'Retype' => array(
105 'type' => 'password',
106 'label-message' => 'retypenew',
107 ),
108 );
109
110 if ( !$this->getUser()->isLoggedIn() ) {
111 if ( !LoginForm::getLoginToken() ) {
112 LoginForm::setLoginToken();
113 }
114 $fields['LoginOnChangeToken'] = array(
115 'type' => 'hidden',
116 'label' => 'Change Password Token',
117 'default' => LoginForm::getLoginToken(),
118 );
119 }
120
121 $extraFields = array();
122 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
123 foreach ( $extraFields as $extra ) {
124 list( $name, $label, $type, $default ) = $extra;
125 $fields[$name] = array(
126 'type' => $type,
127 'name' => $name,
128 'label-message' => $label,
129 'default' => $default,
130 );
131 }
132
133 if ( !$user->isLoggedIn() ) {
134 $fields['Remember'] = array(
135 'type' => 'check',
136 'label' => $this->msg( 'remembermypassword' )
137 ->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )
138 ->text(),
139 'default' => $request->getVal( 'wpRemember' ),
140 );
141 }
142
143 return $fields;
144 }
145
146 protected function alterForm( HTMLForm $form ) {
147 $form->setId( 'mw-resetpass-form' );
148 $form->setTableId( 'mw-resetpass-table' );
149 $form->setWrapperLegendMsg( 'resetpass_header' );
150 $form->setSubmitTextMsg(
151 $this->getUser()->isLoggedIn()
152 ? 'resetpass-submit-loggedin'
153 : 'resetpass_submit'
154 );
155 $form->addButton( 'wpCancel', $this->msg( 'resetpass-submit-cancel' )->text() );
156 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
157 if ( $this->mPreTextMessage instanceof Message ) {
158 $form->addPreText( $this->mPreTextMessage->parseAsBlock() );
159 }
160 $form->addHiddenFields(
161 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
162 }
163
164 public function onSubmit( array $data ) {
165 global $wgAuth;
166
167 $request = $this->getRequest();
168
169 if ( $request->getCheck( 'wpLoginToken' ) ) {
170 // This comes from Special:Userlogin when logging in with a temporary password
171 return false;
172 }
173
174 if ( !$this->getUser()->isLoggedIn()
175 && $request->getVal( 'wpLoginOnChangeToken' ) !== LoginForm::getLoginToken()
176 ) {
177 // Potential CSRF (bug 62497)
178 return false;
179 }
180
181
182 if ( $request->getCheck( 'wpCancel' ) ) {
183 $titleObj = Title::newFromText( $request->getVal( 'returnto' ) );
184 if ( !$titleObj instanceof Title ) {
185 $titleObj = Title::newMainPage();
186 }
187 $query = $request->getVal( 'returntoquery' );
188 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
189
190 return true;
191 }
192
193 try {
194 $this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
195 $this->mDomain = $wgAuth->getDomain();
196
197 if ( !$wgAuth->allowPasswordChange() ) {
198 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
199 }
200
201 $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
202
203 return true;
204 } catch ( PasswordError $e ) {
205 return $e->getMessage();
206 }
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::setLoginToken();
219 $token = LoginForm::getLoginToken();
220 $data = array(
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 * @throws PasswordError when cannot set the new password because requirements not met.
235 */
236 protected function attemptReset( $oldpass, $newpass, $retype ) {
237 global $wgPasswordAttemptThrottle;
238
239 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
240 if ( $isSelf ) {
241 $user = $this->getUser();
242 } else {
243 $user = User::newFromName( $this->mUserName );
244 }
245
246 if ( !$user || $user->isAnon() ) {
247 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
248 }
249
250 if ( $newpass !== $retype ) {
251 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
252 throw new PasswordError( $this->msg( 'badretype' )->text() );
253 }
254
255 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
256 if ( $throttleCount === true ) {
257 $lang = $this->getLanguage();
258 throw new PasswordError( $this->msg( 'changepassword-throttled' )
259 ->params( $lang->formatDuration( $wgPasswordAttemptThrottle['seconds'] ) )
260 ->text()
261 );
262 }
263
264 // @TODO Make these separate messages, since the message is written for both cases
265 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
266 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
267 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
268 }
269
270 // User is resetting their password to their old password
271 if ( $oldpass === $newpass ) {
272 throw new PasswordError( $this->msg( 'resetpass-recycled' )->text() );
273 }
274
275 // Do AbortChangePassword after checking mOldpass, so we don't leak information
276 // by possibly aborting a new password before verifying the old password.
277 $abortMsg = 'resetpass-abort-generic';
278 if ( !wfRunHooks( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
279 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
280 throw new PasswordError( $this->msg( $abortMsg )->text() );
281 }
282
283 // Please reset throttle for successful logins, thanks!
284 if ( $throttleCount ) {
285 LoginForm::clearLoginThrottle( $this->mUserName );
286 }
287
288 try {
289 $user->setPassword( $newpass );
290 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
291 } catch ( PasswordError $e ) {
292 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
293 throw new PasswordError( $e->getMessage() );
294 }
295
296 if ( $isSelf ) {
297 // This is needed to keep the user connected since
298 // changing the password also modifies the user's token.
299 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
300 $user->setCookies( null, null, $remember );
301 }
302 $user->resetPasswordExpiration();
303 $user->saveSettings();
304 }
305
306 public function requiresUnblock() {
307 return false;
308 }
309
310 protected function getGroupName() {
311 return 'users';
312 }
313 }