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();
35 if( !$wgAuth->allowPasswordChange() ) {
36 $this->error( wfMsg( 'resetpass_forbidden' ) );
40 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
41 $this->error( wfMsg( 'resetpass-no-info' ) );
45 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
50 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
52 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
53 $wgOut->addWikiMsg( 'resetpass_success' );
54 if( !$wgUser->isLoggedIn() ) {
56 'action' => 'submitlogin',
57 'wpName' => $this->mUserName
,
58 'wpPassword' => $this->mNewpass
,
59 'returnto' => $wgRequest->getVal( 'returnto' ),
61 if( $wgRequest->getCheck( 'wpRemember' ) ) {
62 $data['wpRemember'] = 1;
64 $login = new LoginForm( new FauxRequest( $data, true ) );
68 } catch( PasswordError
$e ) {
69 $this->error( $e->getMessage() );
75 function doReturnTo() {
76 global $wgRequest, $wgOut;
77 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
78 if ( !$titleObj instanceof Title
) {
79 $titleObj = Title
::newMainPage();
81 $wgOut->redirect( $titleObj->getFullURL() );
84 function error( $msg ) {
86 $wgOut->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
90 global $wgOut, $wgUser, $wgRequest;
92 $wgOut->disallowUserJs();
94 $self = SpecialPage
::getTitleFor( 'Resetpass' );
95 if ( !$this->mUserName
) {
96 $this->mUserName
= $wgUser->getName();
99 if ( !$wgUser->isLoggedIn() ) {
100 $rememberMe = '<tr>' .
102 '<td class="mw-input">' .
103 Xml
::checkLabel( wfMsg( 'remembermypassword' ),
104 'wpRemember', 'wpRemember',
105 $wgRequest->getCheck( 'wpRemember' ) ) .
108 $submitMsg = 'resetpass_submit';
109 $oldpassMsg = 'resetpass-temp-password';
111 $oldpassMsg = 'oldpassword';
112 $submitMsg = 'resetpass-submit-loggedin';
115 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
116 Xml
::openElement( 'form',
119 'action' => $self->getLocalUrl(),
120 'id' => 'mw-resetpass-form' ) ) . "\n" .
121 Xml
::hidden( 'token', $wgUser->editToken() ) . "\n" .
122 Xml
::hidden( 'wpName', $this->mUserName
) . "\n" .
123 Xml
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
124 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
125 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
126 $this->pretty( array(
127 array( 'wpName', 'username', 'text', $this->mUserName
),
128 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
129 array( 'wpNewPassword', 'newpassword', 'password', null ),
130 array( 'wpRetype', 'retypenew', 'password', null ),
135 '<td class="mw-input">' .
136 Xml
::submitButton( wfMsg( $submitMsg ) ) .
137 Xml
::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
140 Xml
::closeElement( 'table' ) .
141 Xml
::closeElement( 'form' ) .
142 Xml
::closeElement( 'fieldset' ) . "\n"
146 function pretty( $fields ) {
148 foreach ( $fields as $list ) {
149 list( $name, $label, $type, $value ) = $list;
150 if( $type == 'text' ) {
151 $field = htmlspecialchars( $value );
153 $attribs = array( 'id' => $name );
154 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
155 $attribs = array_merge( $attribs,
156 User
::passwordChangeInputAttribs() );
158 if ( $name == 'wpPassword' ) {
159 $attribs[] = 'autofocus';
161 $field = Html
::input( $name, $value, $type, $attribs );
164 $out .= "\t<td class='mw-label'>";
165 if ( $type != 'text' )
166 $out .= Xml
::label( wfMsg( $label ), $name );
168 $out .= wfMsgHtml( $label );
170 $out .= "\t<td class='mw-input'>";
179 * @throws PasswordError when cannot set the new password because requirements not met.
181 protected function attemptReset( $newpass, $retype ) {
182 $user = User
::newFromName( $this->mUserName
);
183 if( !$user ||
$user->isAnon() ) {
184 throw new PasswordError( 'no such user' );
187 if( $newpass !== $retype ) {
188 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
189 throw new PasswordError( wfMsg( 'badretype' ) );
192 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
193 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
194 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
198 $user->setPassword( $this->mNewpass
);
199 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
200 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
201 } catch( PasswordError
$e ) {
202 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
203 throw new PasswordError( $e->getMessage() );
208 $user->saveSettings();