checkReadOnly();
$request = $this->getRequest();
$this->mUserName = trim( $request->getVal( 'wpName' ) );
$this->mOldpass = $request->getVal( 'wpPassword' );
$this->mNewpass = $request->getVal( 'wpNewPassword' );
$this->mRetype = $request->getVal( 'wpRetype' );
$this->mDomain = $request->getVal( 'wpDomain' );
$this->setHeaders();
$this->outputHeader();
$this->getOutput()->disallowUserJs();
$user = $this->getUser();
if( !$request->wasPosted() && !$user->isLoggedIn() ) {
$this->error( $this->msg( 'resetpass-no-info' )->text() );
return;
}
if( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
$this->doReturnTo();
return;
}
if( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
try {
if ( isset( $_SESSION['wsDomain'] ) ) {
$this->mDomain = $_SESSION['wsDomain'];
}
$wgAuth->setDomain( $this->mDomain );
if( !$wgAuth->allowPasswordChange() ) {
$this->error( $this->msg( 'resetpass_forbidden' )->text() );
return;
}
$this->attemptReset( $this->mNewpass, $this->mRetype );
$this->getOutput()->addWikiMsg( 'resetpass_success' );
if( !$user->isLoggedIn() ) {
LoginForm::setLoginToken();
$token = LoginForm::getLoginToken();
$data = array(
'action' => 'submitlogin',
'wpName' => $this->mUserName,
'wpDomain' => $this->mDomain,
'wpLoginToken' => $token,
'wpPassword' => $this->mNewpass,
'returnto' => $request->getVal( 'returnto' ),
);
if( $request->getCheck( 'wpRemember' ) ) {
$data['wpRemember'] = 1;
}
$login = new LoginForm( new FauxRequest( $data, true ) );
$login->setContext( $this->getContext() );
$login->execute( null );
}
$this->doReturnTo();
} catch( PasswordError $e ) {
$this->error( $e->getMessage() );
}
}
$this->showForm();
}
function doReturnTo() {
$titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
if ( !$titleObj instanceof Title ) {
$titleObj = Title::newMainPage();
}
$this->getOutput()->redirect( $titleObj->getFullURL() );
}
function error( $msg ) {
$this->getOutput()->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
}
function showForm() {
global $wgCookieExpiration;
$user = $this->getUser();
if ( !$this->mUserName ) {
$this->mUserName = $user->getName();
}
$rememberMe = '';
if ( !$user->isLoggedIn() ) {
$rememberMe = '
' .
' | ' .
'' .
Xml::checkLabel(
$this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
'wpRemember', 'wpRemember',
$this->getRequest()->getCheck( 'wpRemember' ) ) .
' | ' .
'
';
$submitMsg = 'resetpass_submit';
$oldpassMsg = 'resetpass-temp-password';
} else {
$oldpassMsg = 'oldpassword';
$submitMsg = 'resetpass-submit-loggedin';
}
$this->getOutput()->addHTML(
Xml::fieldset( $this->msg( 'resetpass_header' )->text() ) .
Xml::openElement( 'form',
array(
'method' => 'post',
'action' => $this->getTitle()->getLocalUrl(),
'id' => 'mw-resetpass-form' ) ) . "\n" .
Html::hidden( 'token', $user->getEditToken() ) . "\n" .
Html::hidden( 'wpName', $this->mUserName ) . "\n" .
Html::hidden( 'wpDomain', $this->mDomain ) . "\n" .
Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
$this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
$this->pretty( array(
array( 'wpName', 'username', 'text', $this->mUserName ),
array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
array( 'wpNewPassword', 'newpassword', 'password', null ),
array( 'wpRetype', 'retypenew', 'password', null ),
) ) . "\n" .
$rememberMe .
"\n" .
" | \n" .
'' .
Xml::submitButton( $this->msg( $submitMsg )->text() ) .
Xml::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
" | \n" .
"
\n" .
Xml::closeElement( 'table' ) .
Xml::closeElement( 'form' ) .
Xml::closeElement( 'fieldset' ) . "\n"
);
}
function pretty( $fields ) {
$out = '';
foreach ( $fields as $list ) {
list( $name, $label, $type, $value ) = $list;
if( $type == 'text' ) {
$field = htmlspecialchars( $value );
} else {
$attribs = array( 'id' => $name );
if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
$attribs = array_merge( $attribs,
User::passwordChangeInputAttribs() );
}
if ( $name == 'wpPassword' ) {
$attribs[] = 'autofocus';
}
$field = Html::input( $name, $value, $type, $attribs );
}
$out .= "\n";
$out .= "\t";
if ( $type != 'text' )
$out .= Xml::label( $this->msg( $label )->text(), $name );
else
$out .= $this->msg( $label )->escaped();
$out .= " | \n";
$out .= "\t";
$out .= $field;
$out .= " | \n";
$out .= "
";
}
return $out;
}
/**
* @throws PasswordError when cannot set the new password because requirements not met.
*/
protected function attemptReset( $newpass, $retype ) {
$user = User::newFromName( $this->mUserName );
if( !$user || $user->isAnon() ) {
throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
}
if( $newpass !== $retype ) {
wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
throw new PasswordError( $this->msg( 'badretype' )->text() );
}
$throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
if ( $throttleCount === true ) {
throw new PasswordError( $this->msg( 'login-throttled' )->text() );
}
if( !$user->checkTemporaryPassword($this->mOldpass) && !$user->checkPassword($this->mOldpass) ) {
wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
}
// Please reset throttle for successful logins, thanks!
if ( $throttleCount ) {
LoginForm::clearLoginThrottle( $this->mUserName );
}
try {
$user->setPassword( $this->mNewpass );
wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
$this->mNewpass = $this->mOldpass = $this->mRetypePass = '';
} catch( PasswordError $e ) {
wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
throw new PasswordError( $e->getMessage() );
}
$user->setCookies();
$user->saveSettings();
}
}