8 * Let users recover their password.
11 class PasswordReset
extends SpecialPage
{
12 public function __construct() {
13 parent
::__construct( 'Resetpass' );
17 * Main execution point
19 function execute( $par ) {
20 global $wgUser, $wgAuth, $wgOut, $wgRequest;
22 $this->mUserName
= $wgRequest->getVal( 'wpName' );
23 $this->mOldpass
= $wgRequest->getVal( 'wpPassword' );
24 $this->mNewpass
= $wgRequest->getVal( 'wpNewPassword' );
25 $this->mRetype
= $wgRequest->getVal( 'wpRetype' );
28 $this->outputHeader();
30 if( !$wgAuth->allowPasswordChange() ) {
31 $this->error( wfMsg( 'resetpass_forbidden' ) );
35 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
36 $this->error( wfMsg( 'resetpass-no-info' ) );
40 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
42 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
43 $wgOut->addWikiMsg( 'resetpass_success' );
44 if( !$wgUser->isLoggedIn() ) {
46 'action' => 'submitlogin',
47 'wpName' => $this->mUserName
,
48 'wpPassword' => $this->mNewpass
,
49 'returnto' => $wgRequest->getVal( 'returnto' ),
51 if( $wgRequest->getCheck( 'wpRemember' ) ) {
52 $data['wpRemember'] = 1;
54 $login = new LoginForm( new FauxRequest( $data, true ) );
57 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
58 if ( !$titleObj instanceof Title
) {
59 $titleObj = Title
::newMainPage();
61 $wgOut->redirect( $titleObj->getFullURL() );
62 } catch( PasswordError
$e ) {
63 $this->error( $e->getMessage() );
69 function error( $msg ) {
71 $wgOut->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
75 global $wgOut, $wgUser, $wgRequest;
77 $wgOut->disallowUserJs();
79 $self = SpecialPage
::getTitleFor( 'Resetpass' );
80 if ( !$this->mUserName
) {
81 $this->mUserName
= $wgUser->getName();
84 if ( !$wgUser->isLoggedIn() ) {
85 $rememberMe = '<tr>' .
88 Xml
::checkLabel( wfMsg( 'remembermypassword' ),
89 'wpRemember', 'wpRemember',
90 $wgRequest->getCheck( 'wpRemember' ) ) .
93 $submitMsg = 'resetpass_submit';
94 $oldpassMsg = 'resetpass-temp-password';
96 $oldpassMsg = 'oldpassword';
97 $submitMsg = 'resetpass-submit-loggedin';
100 Xml
::openElement( 'fieldset' ) .
101 Xml
::element( 'legend', null, wfMsg( 'resetpass_header' ) ) .
102 Xml
::openElement( 'form',
105 'action' => $self->getLocalUrl(),
106 'id' => 'mw-resetpass-form' ) ) .
107 Xml
::hidden( 'token', $wgUser->editToken() ) .
108 Xml
::hidden( 'wpName', $this->mUserName
) .
109 Xml
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) .
110 wfMsgExt( 'resetpass_text', array( 'parse' ) ) .
112 $this->pretty( array(
113 array( 'wpName', 'username', 'text', $this->mUserName
),
114 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
115 array( 'wpNewPassword', 'newpassword', 'password', '' ),
116 array( 'wpRetype', 'yourpasswordagain', 'password', '' ),
122 Xml
::submitButton( wfMsg( $submitMsg ) ) .
126 Xml
::closeElement( 'form' ) .
127 Xml
::closeElement( 'fieldset' )
131 function pretty( $fields ) {
133 foreach( $fields as $list ) {
134 list( $name, $label, $type, $value ) = $list;
135 if( $type == 'text' ) {
136 $field = htmlspecialchars( $value );
138 $field = Xml
::input( $name, 20, $value,
139 array( 'id' => $name, 'type' => $type ) );
142 $out .= "<td class='mw-label'>";
143 if ( $type != 'text' )
144 $out .= Xml
::label( wfMsg( $label ), $name );
146 $out .= wfMsg( $label );
148 $out .= "<td class='mw-input'>";
157 * @throws PasswordError when cannot set the new password because requirements not met.
159 protected function attemptReset( $newpass, $retype ) {
160 $user = User
::newFromName( $this->mUserName
);
161 if( !$user ||
$user->isAnon() ) {
162 throw new PasswordError( 'no such user' );
165 if( $newpass !== $retype ) {
166 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
167 throw new PasswordError( wfMsg( 'badretype' ) );
170 if( !$user->checkPassword( $this->mOldpass
) ) {
171 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
172 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
176 $user->setPassword( $this->mNewpass
);
177 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
178 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
179 } catch( PasswordError
$e ) {
180 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
181 $this->mainPrefsForm( 'error', $e->getMessage() );
186 $user->saveSettings();