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
21 * @ingroup SpecialPage
25 * Let users recover their password.
27 * @ingroup SpecialPage
29 class SpecialChangePassword
extends UnlistedSpecialPage
{
31 protected $mUserName, $mOldpass, $mNewpass, $mRetype, $mDomain;
33 public function __construct() {
34 parent
::__construct( 'ChangePassword' );
38 * Main execution point
40 function execute( $par ) {
44 $this->outputHeader();
45 $this->getOutput()->disallowUserJs();
47 $request = $this->getRequest();
48 $this->mUserName
= trim( $request->getVal( 'wpName' ) );
49 $this->mOldpass
= $request->getVal( 'wpPassword' );
50 $this->mNewpass
= $request->getVal( 'wpNewPassword' );
51 $this->mRetype
= $request->getVal( 'wpRetype' );
52 $this->mDomain
= $request->getVal( 'wpDomain' );
54 $user = $this->getUser();
55 if( !$request->wasPosted() && !$user->isLoggedIn() ) {
56 $this->error( $this->msg( 'resetpass-no-info' )->text() );
60 if( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
65 $this->checkReadOnly();
67 if( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
69 $this->mDomain
= $wgAuth->getDomain();
70 if( !$wgAuth->allowPasswordChange() ) {
71 $this->error( $this->msg( 'resetpass_forbidden' )->text() );
75 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
77 if( $user->isLoggedIn() ) {
80 LoginForm
::setLoginToken();
81 $token = LoginForm
::getLoginToken();
83 'action' => 'submitlogin',
84 'wpName' => $this->mUserName
,
85 'wpDomain' => $this->mDomain
,
86 'wpLoginToken' => $token,
87 'wpPassword' => $request->getVal( 'wpNewPassword' ),
88 'returnto' => $request->getVal( 'returnto' ),
90 if( $request->getCheck( 'wpRemember' ) ) {
91 $data['wpRemember'] = 1;
93 $login = new LoginForm( new FauxRequest( $data, true ) );
94 $login->setContext( $this->getContext() );
95 $login->execute( null );
98 } catch( PasswordError
$e ) {
99 $this->error( $e->getMessage() );
105 function doReturnTo() {
106 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
107 if ( !$titleObj instanceof Title
) {
108 $titleObj = Title
::newMainPage();
110 $this->getOutput()->redirect( $titleObj->getFullURL() );
116 function error( $msg ) {
117 $this->getOutput()->addHTML( Xml
::element( 'p', array( 'class' => 'error' ), $msg ) );
120 function showForm() {
121 global $wgCookieExpiration;
123 $user = $this->getUser();
124 if ( !$this->mUserName
) {
125 $this->mUserName
= $user->getName();
128 if ( !$user->isLoggedIn() ) {
129 $rememberMe = '<tr>' .
131 '<td class="mw-input">' .
133 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
134 'wpRemember', 'wpRemember',
135 $this->getRequest()->getCheck( 'wpRemember' ) ) .
138 $submitMsg = 'resetpass_submit';
139 $oldpassMsg = 'resetpass-temp-password';
141 $oldpassMsg = 'oldpassword';
142 $submitMsg = 'resetpass-submit-loggedin';
144 $extraFields = array();
145 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
146 $prettyFields = array(
147 array( 'wpName', 'username', 'text', $this->mUserName
),
148 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
149 array( 'wpNewPassword', 'newpassword', 'password', null ),
150 array( 'wpRetype', 'retypenew', 'password', null ),
152 $prettyFields = array_merge( $prettyFields, $extraFields );
153 $this->getOutput()->addHTML(
154 Xml
::fieldset( $this->msg( 'resetpass_header' )->text() ) .
155 Xml
::openElement( 'form',
158 'action' => $this->getTitle()->getLocalUrl(),
159 'id' => 'mw-resetpass-form' ) ) . "\n" .
160 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
161 Html
::hidden( 'wpName', $this->mUserName
) . "\n" .
162 Html
::hidden( 'wpDomain', $this->mDomain
) . "\n" .
163 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
164 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
165 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
166 $this->pretty( $prettyFields ) . "\n" .
170 '<td class="mw-input">' .
171 Xml
::submitButton( $this->msg( $submitMsg )->text() ) .
172 Xml
::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
175 Xml
::closeElement( 'table' ) .
176 Xml
::closeElement( 'form' ) .
177 Xml
::closeElement( 'fieldset' ) . "\n"
182 * @param $fields array
185 function pretty( $fields ) {
187 foreach ( $fields as $list ) {
188 list( $name, $label, $type, $value ) = $list;
189 if( $type == 'text' ) {
190 $field = htmlspecialchars( $value );
192 $attribs = array( 'id' => $name );
193 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
194 $attribs = array_merge( $attribs,
195 User
::passwordChangeInputAttribs() );
197 if ( $name == 'wpPassword' ) {
198 $attribs[] = 'autofocus';
200 $field = Html
::input( $name, $value, $type, $attribs );
203 $out .= "\t<td class='mw-label'>";
204 if ( $type != 'text' )
205 $out .= Xml
::label( $this->msg( $label )->text(), $name );
207 $out .= $this->msg( $label )->escaped();
209 $out .= "\t<td class='mw-input'>";
218 * @throws PasswordError when cannot set the new password because requirements not met.
220 protected function attemptReset( $newpass, $retype ) {
221 $isSelf = ( $this->mUserName
=== $this->getUser()->getName() );
223 $user = $this->getUser();
225 $user = User
::newFromName( $this->mUserName
);
228 if( !$user ||
$user->isAnon() ) {
229 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName
)->text() );
232 if( $newpass !== $retype ) {
233 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
234 throw new PasswordError( $this->msg( 'badretype' )->text() );
237 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
238 if ( $throttleCount === true ) {
239 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
242 if( !$user->checkTemporaryPassword( $this->mOldpass
) && !$user->checkPassword( $this->mOldpass
) ) {
243 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
244 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
247 // Please reset throttle for successful logins, thanks!
248 if ( $throttleCount ) {
249 LoginForm
::clearLoginThrottle( $this->mUserName
);
253 $user->setPassword( $this->mNewpass
);
254 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
255 $this->mNewpass
= $this->mOldpass
= $this->mRetype
= '';
256 } catch( PasswordError
$e ) {
257 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
258 throw new PasswordError( $e->getMessage() );
262 // This is needed to keep the user connected since
263 // changing the password also modifies the user's token.
267 $user->saveSettings();