8 * Let users recover their password.
11 class SpecialResetpass
extends SpecialPage
{
12 public function __construct() {
13 parent
::__construct( 'Resetpass' );
17 * Main execution point
19 function execute( $par ) {
20 global $wgUser, $wgAuth, $wgOut, $wgRequest;
23 $wgOut->readOnlyPage();
27 $this->mUserName
= $wgRequest->getVal( 'wpName' );
28 $this->mOldpass
= $wgRequest->getVal( 'wpPassword' );
29 $this->mNewpass
= $wgRequest->getVal( 'wpNewPassword' );
30 $this->mRetype
= $wgRequest->getVal( 'wpRetype' );
33 $this->outputHeader();
34 $wgOut->disallowUserJs();
36 if( !$wgAuth->allowPasswordChange() ) {
37 $this->error( wfMsg( 'resetpass_forbidden' ) );
41 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
42 $this->error( wfMsg( 'resetpass-no-info' ) );
46 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
51 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
53 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
54 $wgOut->addWikiMsg( 'resetpass_success' );
55 if( !$wgUser->isLoggedIn() ) {
57 'action' => 'submitlogin',
58 'wpName' => $this->mUserName
,
59 'wpPassword' => $this->mNewpass
,
60 'returnto' => $wgRequest->getVal( 'returnto' ),
62 if( $wgRequest->getCheck( 'wpRemember' ) ) {
63 $data['wpRemember'] = 1;
65 $login = new LoginForm( new FauxRequest( $data, true ) );
69 } catch( PasswordError
$e ) {
70 $this->error( $e->getMessage() );
76 function doReturnTo() {
77 global $wgRequest, $wgOut;
78 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
79 if ( !$titleObj instanceof Title
) {
80 $titleObj = Title
::newMainPage();
82 $wgOut->redirect( $titleObj->getFullURL() );
85 function error( $msg ) {
87 $wgOut->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
91 global $wgOut, $wgUser, $wgRequest;
93 $wgOut->disallowUserJs();
95 $self = SpecialPage
::getTitleFor( 'Resetpass' );
96 if ( !$this->mUserName
) {
97 $this->mUserName
= $wgUser->getName();
100 if ( !$wgUser->isLoggedIn() ) {
101 $rememberMe = '<tr>' .
103 '<td class="mw-input">' .
104 Xml
::checkLabel( wfMsg( 'remembermypassword' ),
105 'wpRemember', 'wpRemember',
106 $wgRequest->getCheck( 'wpRemember' ) ) .
109 $submitMsg = 'resetpass_submit';
110 $oldpassMsg = 'resetpass-temp-password';
112 $oldpassMsg = 'oldpassword';
113 $submitMsg = 'resetpass-submit-loggedin';
116 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
117 Xml
::openElement( 'form',
120 'action' => $self->getLocalUrl(),
121 'id' => 'mw-resetpass-form' ) ) . "\n" .
122 Xml
::hidden( 'token', $wgUser->editToken() ) . "\n" .
123 Xml
::hidden( 'wpName', $this->mUserName
) . "\n" .
124 Xml
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
125 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
126 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
127 $this->pretty( array(
128 array( 'wpName', 'username', 'text', $this->mUserName
),
129 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
130 array( 'wpNewPassword', 'newpassword', 'password', null ),
131 array( 'wpRetype', 'retypenew', 'password', null ),
136 '<td class="mw-input">' .
137 Xml
::submitButton( wfMsg( $submitMsg ) ) .
138 Xml
::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
141 Xml
::closeElement( 'table' ) .
142 Xml
::closeElement( 'form' ) .
143 Xml
::closeElement( 'fieldset' ) . "\n"
147 function pretty( $fields ) {
149 foreach ( $fields as $list ) {
150 list( $name, $label, $type, $value ) = $list;
151 if( $type == 'text' ) {
152 $field = htmlspecialchars( $value );
154 $attribs = array( 'id' => $name );
155 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
156 $attribs = array_merge( $attribs,
157 User
::passwordChangeInputAttribs() );
159 if ( $name == 'wpPassword' ) {
160 $attribs[] = 'autofocus';
162 $field = Html
::input( $name, $value, $type, $attribs );
165 $out .= "\t<td class='mw-label'>";
166 if ( $type != 'text' )
167 $out .= Xml
::label( wfMsg( $label ), $name );
169 $out .= wfMsgHtml( $label );
171 $out .= "\t<td class='mw-input'>";
180 * @throws PasswordError when cannot set the new password because requirements not met.
182 protected function attemptReset( $newpass, $retype ) {
183 $user = User
::newFromName( $this->mUserName
);
184 if( !$user ||
$user->isAnon() ) {
185 throw new PasswordError( 'no such user' );
188 if( $newpass !== $retype ) {
189 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
190 throw new PasswordError( wfMsg( 'badretype' ) );
193 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
194 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
195 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
199 $user->setPassword( $this->mNewpass
);
200 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
201 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
202 } catch( PasswordError
$e ) {
203 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
204 throw new PasswordError( $e->getMessage() );
209 $user->saveSettings();