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 $self = $this->getTitle();
94 if ( !$this->mUserName
) {
95 $this->mUserName
= $wgUser->getName();
98 if ( !$wgUser->isLoggedIn() ) {
99 $rememberMe = '<tr>' .
101 '<td class="mw-input">' .
102 Xml
::checkLabel( wfMsg( 'remembermypassword' ),
103 'wpRemember', 'wpRemember',
104 $wgRequest->getCheck( 'wpRemember' ) ) .
107 $submitMsg = 'resetpass_submit';
108 $oldpassMsg = 'resetpass-temp-password';
110 $oldpassMsg = 'oldpassword';
111 $submitMsg = 'resetpass-submit-loggedin';
114 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
115 Xml
::openElement( 'form',
118 'action' => $self->getLocalUrl(),
119 'id' => 'mw-resetpass-form' ) ) . "\n" .
120 Xml
::hidden( 'token', $wgUser->editToken() ) . "\n" .
121 Xml
::hidden( 'wpName', $this->mUserName
) . "\n" .
122 Xml
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
123 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
124 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
125 $this->pretty( array(
126 array( 'wpName', 'username', 'text', $this->mUserName
),
127 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
128 array( 'wpNewPassword', 'newpassword', 'password', null ),
129 array( 'wpRetype', 'retypenew', 'password', null ),
134 '<td class="mw-input">' .
135 Xml
::submitButton( wfMsg( $submitMsg ) ) .
136 Xml
::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
139 Xml
::closeElement( 'table' ) .
140 Xml
::closeElement( 'form' ) .
141 Xml
::closeElement( 'fieldset' ) . "\n"
145 function pretty( $fields ) {
147 foreach ( $fields as $list ) {
148 list( $name, $label, $type, $value ) = $list;
149 if( $type == 'text' ) {
150 $field = htmlspecialchars( $value );
152 $attribs = array( 'id' => $name );
153 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
154 $attribs = array_merge( $attribs,
155 User
::passwordChangeInputAttribs() );
157 if ( $name == 'wpPassword' ) {
158 $attribs[] = 'autofocus';
160 $field = Html
::input( $name, $value, $type, $attribs );
163 $out .= "\t<td class='mw-label'>";
164 if ( $type != 'text' )
165 $out .= Xml
::label( wfMsg( $label ), $name );
167 $out .= wfMsgHtml( $label );
169 $out .= "\t<td class='mw-input'>";
178 * @throws PasswordError when cannot set the new password because requirements not met.
180 protected function attemptReset( $newpass, $retype ) {
181 $user = User
::newFromName( $this->mUserName
);
182 if( !$user ||
$user->isAnon() ) {
183 throw new PasswordError( 'no such user' );
186 if( $newpass !== $retype ) {
187 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
188 throw new PasswordError( wfMsg( 'badretype' ) );
191 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
192 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
193 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
197 $user->setPassword( $this->mNewpass
);
198 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
199 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
200 } catch( PasswordError
$e ) {
201 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
202 throw new PasswordError( $e->getMessage() );
207 $user->saveSettings();