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;
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() && $wgRequest->getBool( 'wpCancel' ) ) {
45 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
47 $this->attemptReset( $this->mNewpass, $this->mRetype );
48 $wgOut->addWikiMsg( 'resetpass_success' );
49 if( !$wgUser->isLoggedIn() ) {
51 'action' => 'submitlogin',
52 'wpName' => $this->mUserName,
53 'wpPassword' => $this->mNewpass,
54 'returnto' => $wgRequest->getVal( 'returnto' ),
56 if( $wgRequest->getCheck( 'wpRemember' ) ) {
57 $data['wpRemember'] = 1;
59 $login = new LoginForm( new FauxRequest( $data, true ) );
63 } catch( PasswordError $e ) {
64 $this->error( $e->getMessage() );
70 function doReturnTo() {
71 global $wgRequest, $wgOut;
72 $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
73 if ( !$titleObj instanceof Title ) {
74 $titleObj = Title::newMainPage();
76 $wgOut->redirect( $titleObj->getFullURL() );
79 function error( $msg ) {
81 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
85 global $wgOut, $wgUser, $wgRequest;
87 $wgOut->disallowUserJs();
89 $self = SpecialPage::getTitleFor( 'Resetpass' );
90 if ( !$this->mUserName ) {
91 $this->mUserName = $wgUser->getName();
94 if ( !$wgUser->isLoggedIn() ) {
95 $rememberMe = '<tr>' .
97 '<td class="mw-input">' .
98 Xml::checkLabel( wfMsg( 'remembermypassword' ),
99 'wpRemember', 'wpRemember',
100 $wgRequest->getCheck( 'wpRemember' ) ) .
103 $submitMsg = 'resetpass_submit';
104 $oldpassMsg = 'resetpass-temp-password';
106 $oldpassMsg = 'oldpassword';
107 $submitMsg = 'resetpass-submit-loggedin';
110 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
111 Xml::openElement( 'form',
114 'action' => $self->getLocalUrl(),
115 'id' => 'mw-resetpass-form' ) ) . "\n" .
116 Xml::hidden( 'token', $wgUser->editToken() ) . "\n" .
117 Xml::hidden( 'wpName', $this->mUserName ) . "\n" .
118 Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
119 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
120 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
121 $this->pretty( array(
122 array( 'wpName', 'username', 'text', $this->mUserName ),
123 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
124 array( 'wpNewPassword', 'newpassword', 'password', null ),
125 array( 'wpRetype', 'retypenew', 'password', null ),
130 '<td class="mw-input">' .
131 Xml::submitButton( wfMsg( $submitMsg ) ) .
132 Xml::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
135 Xml::closeElement( 'table' ) .
136 Xml::closeElement( 'form' ) .
137 Xml::closeElement( 'fieldset' ) . "\n"
141 function pretty( $fields ) {
143 foreach ( $fields as $list ) {
144 list( $name, $label, $type, $value ) = $list;
145 if( $type == 'text' ) {
146 $field = htmlspecialchars( $value );
148 $attribs = array( 'id' => $name );
149 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
150 $attribs = array_merge( $attribs,
151 User::passwordChangeInputAttribs() );
153 if ( $name == 'wpPassword' ) {
154 $attribs[] = 'autofocus';
156 $field = Html::input( $name, $value, $type, $attribs );
159 $out .= "\t<td class='mw-label'>";
160 if ( $type != 'text' )
161 $out .= Xml::label( wfMsg( $label ), $name );
163 $out .= wfMsgHtml( $label );
165 $out .= "\t<td class='mw-input'>";
174 * @throws PasswordError when cannot set the new password because requirements not met.
176 protected function attemptReset( $newpass, $retype ) {
177 $user = User::newFromName( $this->mUserName );
178 if( !$user || $user->isAnon() ) {
179 throw new PasswordError( 'no such user' );
182 if( $newpass !== $retype ) {
183 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
184 throw new PasswordError( wfMsg( 'badretype' ) );
187 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
188 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
189 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
193 $user->setPassword( $this->mNewpass );
194 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
195 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
196 } catch( PasswordError $e ) {
197 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
198 throw new PasswordError( $e->getMessage() );
203 $user->saveSettings();