Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / specials / SpecialChangePassword.php
blob91ac4e041e96e19359f9b0ccc483cd6d9c005f46
1 <?php
2 /**
3 * Implements Special:ChangePassword
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Let users recover their password.
27 * @ingroup SpecialPage
29 class SpecialChangePassword extends FormSpecialPage {
30 protected $mUserName;
31 protected $mDomain;
33 // Optional Wikitext Message to show above the password change form
34 protected $mPreTextMessage = null;
36 // label for old password input
37 protected $mOldPassMsg = null;
39 public function __construct() {
40 parent::__construct( 'ChangePassword', 'editmyprivateinfo' );
41 $this->listed( false );
44 /**
45 * Main execution point
46 * @param string|null $par
48 function execute( $par ) {
49 $this->getOutput()->disallowUserJs();
51 parent::execute( $par );
54 protected function checkExecutePermissions( User $user ) {
55 parent::checkExecutePermissions( $user );
57 if ( !$this->getRequest()->wasPosted() ) {
58 $this->requireLogin( 'resetpass-no-info' );
62 /**
63 * Set a message at the top of the Change Password form
64 * @since 1.23
65 * @param Message $msg Message to parse and add to the form header
67 public function setChangeMessage( Message $msg ) {
68 $this->mPreTextMessage = $msg;
71 /**
72 * Set a message at the top of the Change Password form
73 * @since 1.23
74 * @param string $msg Message label for old/temp password field
76 public function setOldPasswordMessage( $msg ) {
77 $this->mOldPassMsg = $msg;
80 protected function getFormFields() {
81 $user = $this->getUser();
82 $request = $this->getRequest();
84 $oldpassMsg = $this->mOldPassMsg;
85 if ( $oldpassMsg === null ) {
86 $oldpassMsg = $user->isLoggedIn() ? 'oldpassword' : 'resetpass-temp-password';
89 $fields = array(
90 'Name' => array(
91 'type' => 'info',
92 'label-message' => 'username',
93 'default' => $request->getVal( 'wpName', $user->getName() ),
95 'Password' => array(
96 'type' => 'password',
97 'label-message' => $oldpassMsg,
99 'NewPassword' => array(
100 'type' => 'password',
101 'label-message' => 'newpassword',
103 'Retype' => array(
104 'type' => 'password',
105 'label-message' => 'retypenew',
109 if ( !$this->getUser()->isLoggedIn() ) {
110 if ( !LoginForm::getLoginToken() ) {
111 LoginForm::setLoginToken();
113 $fields['LoginOnChangeToken'] = array(
114 'type' => 'hidden',
115 'label' => 'Change Password Token',
116 'default' => LoginForm::getLoginToken(),
120 $extraFields = array();
121 Hooks::run( 'ChangePasswordForm', array( &$extraFields ) );
122 foreach ( $extraFields as $extra ) {
123 list( $name, $label, $type, $default ) = $extra;
124 $fields[$name] = array(
125 'type' => $type,
126 'name' => $name,
127 'label-message' => $label,
128 'default' => $default,
132 if ( !$user->isLoggedIn() ) {
133 $fields['Remember'] = array(
134 'type' => 'check',
135 'label' => $this->msg( 'remembermypassword' )
136 ->numParams(
137 ceil( $this->getConfig()->get( 'CookieExpiration' ) / ( 3600 * 24 ) )
138 )->text(),
139 'default' => $request->getVal( 'wpRemember' ),
143 return $fields;
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'
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() );
160 $form->addHiddenFields(
161 $this->getRequest()->getValues( 'wpName', 'wpDomain', 'returnto', 'returntoquery' ) );
164 public function onSubmit( array $data ) {
165 global $wgAuth;
167 $request = $this->getRequest();
169 if ( $request->getCheck( 'wpLoginToken' ) ) {
170 // This comes from Special:Userlogin when logging in with a temporary password
171 return false;
174 if ( !$this->getUser()->isLoggedIn()
175 && $request->getVal( 'wpLoginOnChangeToken' ) !== LoginForm::getLoginToken()
177 // Potential CSRF (bug 62497)
178 return false;
181 if ( $request->getCheck( 'wpCancel' ) ) {
182 $returnto = $request->getVal( 'returnto' );
183 $titleObj = $returnto !== null ? Title::newFromText( $returnto ) : null;
184 if ( !$titleObj instanceof Title ) {
185 $titleObj = Title::newMainPage();
187 $query = $request->getVal( 'returntoquery' );
188 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
190 return true;
193 $this->mUserName = $request->getVal( 'wpName', $this->getUser()->getName() );
194 $this->mDomain = $wgAuth->getDomain();
196 if ( !$wgAuth->allowPasswordChange() ) {
197 throw new ErrorPageError( 'changepassword', 'resetpass_forbidden' );
200 $status = $this->attemptReset( $data['Password'], $data['NewPassword'], $data['Retype'] );
202 return $status;
205 public function onSuccess() {
206 if ( $this->getUser()->isLoggedIn() ) {
207 $this->getOutput()->wrapWikiMsg(
208 "<div class=\"successbox\">\n$1\n</div>",
209 'changepassword-success'
211 $this->getOutput()->returnToMain();
212 } else {
213 $request = $this->getRequest();
214 LoginForm::setLoginToken();
215 $token = LoginForm::getLoginToken();
216 $data = array(
217 'action' => 'submitlogin',
218 'wpName' => $this->mUserName,
219 'wpDomain' => $this->mDomain,
220 'wpLoginToken' => $token,
221 'wpPassword' => $request->getVal( 'wpNewPassword' ),
222 ) + $request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
223 $login = new LoginForm( new DerivativeRequest( $request, $data, true ) );
224 $login->setContext( $this->getContext() );
225 $login->execute( null );
230 * Checks the new password if it meets the requirements for passwords and set
231 * it as a current password, otherwise set the passed Status object to fatal
232 * and doesn't change anything
234 * @param string $oldpass The current (temporary) password.
235 * @param string $newpass The password to set.
236 * @param string $retype The string of the retype password field to check with newpass
237 * @return Status
239 protected function attemptReset( $oldpass, $newpass, $retype ) {
240 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
241 if ( $isSelf ) {
242 $user = $this->getUser();
243 } else {
244 $user = User::newFromName( $this->mUserName );
247 if ( !$user || $user->isAnon() ) {
248 return Status::newFatal( $this->msg( 'nosuchusershort', $this->mUserName ) );
251 if ( $newpass !== $retype ) {
252 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
253 return Status::newFatal( $this->msg( 'badretype' ) );
256 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
257 if ( $throttleCount === true ) {
258 $lang = $this->getLanguage();
259 $throttleInfo = $this->getConfig()->get( 'PasswordAttemptThrottle' );
260 return Status::newFatal( $this->msg( 'changepassword-throttled' )
261 ->params( $lang->formatDuration( $throttleInfo['seconds'] ) )
265 // @todo Make these separate messages, since the message is written for both cases
266 if ( !$user->checkTemporaryPassword( $oldpass ) && !$user->checkPassword( $oldpass ) ) {
267 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
268 return Status::newFatal( $this->msg( 'resetpass-wrong-oldpass' ) );
271 // User is resetting their password to their old password
272 if ( $oldpass === $newpass ) {
273 return Status::newFatal( $this->msg( 'resetpass-recycled' ) );
276 // Do AbortChangePassword after checking mOldpass, so we don't leak information
277 // by possibly aborting a new password before verifying the old password.
278 $abortMsg = 'resetpass-abort-generic';
279 if ( !Hooks::run( 'AbortChangePassword', array( $user, $oldpass, $newpass, &$abortMsg ) ) ) {
280 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
281 return Status::newFatal( $this->msg( $abortMsg ) );
284 // Please reset throttle for successful logins, thanks!
285 if ( $throttleCount ) {
286 LoginForm::clearLoginThrottle( $this->mUserName );
289 try {
290 $user->setPassword( $newpass );
291 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
292 } catch ( PasswordError $e ) {
293 Hooks::run( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
294 return Status::newFatal( new RawMessage( $e->getMessage() ) );
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 );
303 $user->saveSettings();
304 $this->resetPasswordExpiration( $user );
305 return Status::newGood();
308 public function requiresUnblock() {
309 return false;
312 protected function getGroupName() {
313 return 'users';
317 * For resetting user password expiration, until AuthManager comes along
318 * @param User $user
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 )
329 // Give extensions a chance to force an expiration
330 Hooks::run( 'ResetPasswordExpiration', array( $this, &$newExpire ) );
331 $dbw = wfGetDB( DB_MASTER );
332 $dbw->update(
333 'user',
334 array( 'user_password_expires' => $dbw->timestampOrNull( $newExpire ) ),
335 array( 'user_id' => $user->getID() ),
336 __METHOD__
340 protected function getDisplayFormat() {
341 return 'ooui';