00001 <?php
00011 class SpecialResetpass extends SpecialPage {
00012 public function __construct() {
00013 parent::__construct( 'Resetpass' );
00014 }
00015
00019 function execute( $par ) {
00020 global $wgUser, $wgAuth, $wgOut, $wgRequest;
00021
00022 $this->mUserName = $wgRequest->getVal( 'wpName' );
00023 $this->mOldpass = $wgRequest->getVal( 'wpPassword' );
00024 $this->mNewpass = $wgRequest->getVal( 'wpNewPassword' );
00025 $this->mRetype = $wgRequest->getVal( 'wpRetype' );
00026
00027 $this->setHeaders();
00028 $this->outputHeader();
00029
00030 if( !$wgAuth->allowPasswordChange() ) {
00031 $this->error( wfMsg( 'resetpass_forbidden' ) );
00032 return;
00033 }
00034
00035 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
00036 $this->error( wfMsg( 'resetpass-no-info' ) );
00037 return;
00038 }
00039
00040 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
00041 $this->doReturnTo();
00042 return;
00043 }
00044
00045 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
00046 try {
00047 $this->attemptReset( $this->mNewpass, $this->mRetype );
00048 $wgOut->addWikiMsg( 'resetpass_success' );
00049 if( !$wgUser->isLoggedIn() ) {
00050 $data = array(
00051 'action' => 'submitlogin',
00052 'wpName' => $this->mUserName,
00053 'wpPassword' => $this->mNewpass,
00054 'returnto' => $wgRequest->getVal( 'returnto' ),
00055 );
00056 if( $wgRequest->getCheck( 'wpRemember' ) ) {
00057 $data['wpRemember'] = 1;
00058 }
00059 $login = new LoginForm( new FauxRequest( $data, true ) );
00060 $login->execute();
00061 }
00062 $this->doReturnTo();
00063 } catch( PasswordError $e ) {
00064 $this->error( $e->getMessage() );
00065 }
00066 }
00067 $this->showForm();
00068 }
00069
00070 function doReturnTo() {
00071 global $wgRequest, $wgOut;
00072 $titleObj = Title::newFromText( $wgRequest->getVal( 'returnto' ) );
00073 if ( !$titleObj instanceof Title ) {
00074 $titleObj = Title::newMainPage();
00075 }
00076 $wgOut->redirect( $titleObj->getFullURL() );
00077 }
00078
00079 function error( $msg ) {
00080 global $wgOut;
00081 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
00082 }
00083
00084 function showForm() {
00085 global $wgOut, $wgUser, $wgRequest;
00086
00087 $wgOut->disallowUserJs();
00088
00089 $self = SpecialPage::getTitleFor( 'Resetpass' );
00090 if ( !$this->mUserName ) {
00091 $this->mUserName = $wgUser->getName();
00092 }
00093 $rememberMe = '';
00094 if ( !$wgUser->isLoggedIn() ) {
00095 $rememberMe = '<tr>' .
00096 '<td></td>' .
00097 '<td class="mw-input">' .
00098 Xml::checkLabel( wfMsg( 'remembermypassword' ),
00099 'wpRemember', 'wpRemember',
00100 $wgRequest->getCheck( 'wpRemember' ) ) .
00101 '</td>' .
00102 '</tr>';
00103 $submitMsg = 'resetpass_submit';
00104 $oldpassMsg = 'resetpass-temp-password';
00105 } else {
00106 $oldpassMsg = 'oldpassword';
00107 $submitMsg = 'resetpass-submit-loggedin';
00108 }
00109 $wgOut->addHTML(
00110 Xml::fieldset( wfMsg( 'resetpass_header' ) ) .
00111 Xml::openElement( 'form',
00112 array(
00113 'method' => 'post',
00114 'action' => $self->getLocalUrl(),
00115 'id' => 'mw-resetpass-form' ) ) . "\n" .
00116 Xml::hidden( 'token', $wgUser->editToken() ) . "\n" .
00117 Xml::hidden( 'wpName', $this->mUserName ) . "\n" .
00118 Xml::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
00119 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
00120 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
00121 $this->pretty( array(
00122 array( 'wpName', 'username', 'text', $this->mUserName ),
00123 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
00124 array( 'wpNewPassword', 'newpassword', 'password', null ),
00125 array( 'wpRetype', 'retypenew', 'password', null ),
00126 ) ) . "\n" .
00127 $rememberMe .
00128 "<tr>\n" .
00129 "<td></td>\n" .
00130 '<td class="mw-input">' .
00131 Xml::submitButton( wfMsg( $submitMsg ) ) .
00132 Xml::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
00133 "</td>\n" .
00134 "</tr>\n" .
00135 Xml::closeElement( 'table' ) .
00136 Xml::closeElement( 'form' ) .
00137 Xml::closeElement( 'fieldset' ) . "\n"
00138 );
00139 }
00140
00141 function pretty( $fields ) {
00142 $out = '';
00143 foreach ( $fields as $list ) {
00144 list( $name, $label, $type, $value ) = $list;
00145 if( $type == 'text' ) {
00146 $field = htmlspecialchars( $value );
00147 } else {
00148 $attribs = array( 'id' => $name );
00149 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
00150 $attribs = array_merge( $attribs,
00151 User::passwordChangeInputAttribs() );
00152 }
00153 if ( $name == 'wpPassword' ) {
00154 $attribs[] = 'autofocus';
00155 }
00156 $field = Html::input( $name, $value, $type, $attribs );
00157 }
00158 $out .= "<tr>\n";
00159 $out .= "\t<td class='mw-label'>";
00160 if ( $type != 'text' )
00161 $out .= Xml::label( wfMsg( $label ), $name );
00162 else
00163 $out .= wfMsgHtml( $label );
00164 $out .= "</td>\n";
00165 $out .= "\t<td class='mw-input'>";
00166 $out .= $field;
00167 $out .= "</td>\n";
00168 $out .= "</tr>";
00169 }
00170 return $out;
00171 }
00172
00176 protected function attemptReset( $newpass, $retype ) {
00177 $user = User::newFromName( $this->mUserName );
00178 if( !$user || $user->isAnon() ) {
00179 throw new PasswordError( 'no such user' );
00180 }
00181
00182 if( $newpass !== $retype ) {
00183 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
00184 throw new PasswordError( wfMsg( 'badretype' ) );
00185 }
00186
00187 if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
00188 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
00189 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
00190 }
00191
00192 try {
00193 $user->setPassword( $this->mNewpass );
00194 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
00195 $this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
00196 } catch( PasswordError $e ) {
00197 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
00198 throw new PasswordError( $e->getMessage() );
00199 return;
00200 }
00201
00202 $user->setCookies();
00203 $user->saveSettings();
00204 }
00205 }