Merge "SpecialChangePassword: Use Config instead of globals"
[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 /**
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 Message 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 $user = $this->getUser();
81 $request = $this->getRequest();
82
83 $oldpassMsg = $this->mOldPassMsg;
84 if ( $oldpassMsg === null ) {
85 $oldpassMsg = $user->isLoggedIn() ? 'oldpassword' : 'resetpass-temp-password';
86 }
87
88 $fields = array(
89 'Name' => array(
90 'type' => 'info',
91 'label-message' => 'username',
92 'default' => $request->getVal( 'wpName', $user->getName() ),
93 ),
94 'Password' => array(
95 'type' => 'password',
96 'label-message' => $oldpassMsg,
97 ),
98 'NewPassword' => array(
99 'type' => 'password',
100 'label-message' => 'newpassword',
101 ),
102 'Retype' => array(
103 'type' => 'password',
104 'label-message' => 'retypenew',
105 ),
106 );
107
108 if ( !$this->getUser()->isLoggedIn() ) {
109 if ( !LoginForm::getLoginToken() ) {
110 LoginForm::setLoginToken();
111 }
112 $fields['LoginOnChangeToken'] = array(
113 'type' => 'hidden',
114 'label' => 'Change Password Token',
115 'default' => LoginForm::getLoginToken(),
116 );
117 }
118
119 $extraFields = array();
120 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
121 foreach ( $extraFields as $extra ) {
122 list( $name, $label, $type, $default ) = $extra;
123 $fields[$name] = array(
124 'type' => $type,
125 'name' => $name,
126 'label-message' => $label,
127 'default' => $default,
128 );
129 }
130
131 if ( !$user->isLoggedIn() ) {
132 $fields['Remember'] = array(
133 'type' => 'check',
134 'label' => $this->msg( 'remembermypassword' )
135 ->numParams(
136 ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
137 )->text(),
138 'default' => $request->getVal( 'wpRemember' ),
139 );
140 }
141
142 return $fields;
143 }
144
145 protected function alterForm( HTMLForm $form ) {
146 $form->setId( 'mw-resetpass-form' );
147 $form->setTableId( 'mw-resetpass-table' );
148 $form->setWrapperLegendMsg( 'resetpass_header' );
149 $form->setSubmitTextMsg(
150 $this->getUser()->isLoggedIn()
151 ? 'resetpass-submit-loggedin'
152 : 'resetpass_submit'
153 );
154 $form->addButton( 'wpCancel', $this->msg( 'resetpass-submit-cancel' )->text() );
155 $form->setHeaderText( $this->msg( 'resetpass_text' )->parseAsBlock() );
156 if ( $this->mPreTextMessage instanceof Message ) {
157 $form->addPreText( $this->mPreTextMessage->parseAsBlock() );
158 }
159 $form->addHiddenFields(
160 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
161 }
162
163 public function onSubmit( array $data ) {
164 global $wgAuth;
165
166 $request = $this->getRequest();
167
168 if ( $request->getCheck( 'wpLoginToken' ) ) {
169 // This comes from Special:Userlogin when logging in with a temporary password
170 return false;
171 }
172
173 if ( !$this->getUser()->isLoggedIn()
174 && $request->getVal( 'wpLoginOnChangeToken' ) !== LoginForm::getLoginToken()
175 ) {
176 // Potential CSRF (bug 62497)
177 return false;
178 }
179
180 if ( $request->getCheck( 'wpCancel' ) ) {
181 $titleObj = Title::newFromText( $request->getVal( 'returnto' ) );
182 if ( !$titleObj instanceof Title ) {
183 $titleObj = Title::newMainPage();
184 }
185 $query = $request->getVal( 'returntoquery' );
186 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
187
188 return true;
189 }
190
191 try {
192 $this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
193 $this->mDomain = $wgAuth->getDomain();
194
195 if ( !$wgAuth->allowPasswordChange() ) {
196 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
197 }
198
199 $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
200
201 return true;
202 } catch ( PasswordError $e ) {
203 return $e->getMessage();
204 }
205 }
206
207 public function onSuccess() {
208 if ( $this->getUser()->isLoggedIn() ) {
209 $this->getOutput()->wrapWikiMsg(
210 "<div class=\"successbox\">\n$1\n</div>",
211 'changepassword-success'
212 );
213 $this->getOutput()->returnToMain();
214 } else {
215 $request = $this->getRequest();
216 LoginForm::setLoginToken();
217 $token = LoginForm::getLoginToken();
218 $data = array(
219 'action' => 'submitlogin',
220 'wpName' => $this->mUserName,
221 'wpDomain' => $this->mDomain,
222 'wpLoginToken' => $token,
223 'wpPassword' => $request->getVal( 'wpNewPassword' ),
224 ) + $request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
225 $login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
226 $login->setContext( $this->getContext() );
227 $login->execute( null );
228 }
229 }
230
231 /**
232 * @throws PasswordError When cannot set the new password because requirements not met.
233 */
234 protected function attemptReset( $oldpass, $newpass, $retype ) {
235 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
236 if ( $isSelf ) {
237 $user = $this->getUser();
238 } else {
239 $user = User::newFromName( $this->mUserName );
240 }
241
242 if ( !$user || $user->isAnon() ) {
243 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
244 }
245
246 if ( $newpass !== $retype ) {
247 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
248 throw new PasswordError( $this->msg( 'badretype' )->text() );
249 }
250
251 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
252 if ( $throttleCount === true ) {
253 $lang = $this->getLanguage();
254 $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
255 throw new PasswordError( $this->msg( 'changepassword-throttled' )
256 ->params( $lang->formatDuration( $throttleInfo['seconds'] ) )
257 ->text()
258 );
259 }
260
261 // @todo Make these separate messages, since the message is written for both cases
262 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
263 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
264 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
265 }
266
267 // User is resetting their password to their old password
268 if ( $oldpass === $newpass ) {
269 throw new PasswordError( $this->msg( 'resetpass-recycled' )->text() );
270 }
271
272 // Do AbortChangePassword after checking mOldpass, so we don't leak information
273 // by possibly aborting a new password before verifying the old password.
274 $abortMsg = 'resetpass-abort-generic';
275 if ( !wfRunHooks( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
276 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
277 throw new PasswordError( $this->msg( $abortMsg )->text() );
278 }
279
280 // Please reset throttle for successful logins, thanks!
281 if ( $throttleCount ) {
282 LoginForm::clearLoginThrottle( $this->mUserName );
283 }
284
285 try {
286 $user->setPassword( $newpass );
287 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
288 } catch ( PasswordError $e ) {
289 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
290 throw new PasswordError( $e->getMessage() );
291 }
292
293 if ( $isSelf ) {
294 // This is needed to keep the user connected since
295 // changing the password also modifies the user's token.
296 $remember = $this->getRequest()->getCookie( 'Token' ) !== null;
297 $user->setCookies( null, null, $remember );
298 }
299 $user->resetPasswordExpiration();
300 $user->saveSettings();
301 }
302
303 public function requiresUnblock() {
304 return false;
305 }
306
307 protected function getGroupName() {
308 return 'users';
309 }
310 }