3 * Implements Special:Resetpass
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * Let users recover their password.
27 * @ingroup SpecialPage
29 class SpecialResetpass
extends SpecialPage
{
30 public function __construct() {
31 parent
::__construct( 'Resetpass' );
35 * Main execution point
37 function execute( $par ) {
38 global $wgUser, $wgAuth, $wgOut, $wgRequest;
41 $wgOut->readOnlyPage();
45 $this->mUserName
= $wgRequest->getVal( 'wpName' );
46 $this->mOldpass
= $wgRequest->getVal( 'wpPassword' );
47 $this->mNewpass
= $wgRequest->getVal( 'wpNewPassword' );
48 $this->mRetype
= $wgRequest->getVal( 'wpRetype' );
49 $this->mDomain
= $wgRequest->getVal( 'wpDomain' );
52 $this->outputHeader();
53 $wgOut->disallowUserJs();
55 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
56 $this->error( wfMsg( 'resetpass-no-info' ) );
60 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
65 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'token' ) ) ) {
67 $wgAuth->setDomain( $this->mDomain
);
68 if( !$wgAuth->allowPasswordChange() ) {
69 $this->error( wfMsg( 'resetpass_forbidden' ) );
73 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
74 $wgOut->addWikiMsg( 'resetpass_success' );
75 if( !$wgUser->isLoggedIn() ) {
76 LoginForm
::setLoginToken();
77 $token = LoginForm
::getLoginToken();
79 'action' => 'submitlogin',
80 'wpName' => $this->mUserName
,
81 'wpDomain' => $this->mDomain
,
82 'wpLoginToken' => $token,
83 'wpPassword' => $this->mNewpass
,
84 'returnto' => $wgRequest->getVal( 'returnto' ),
86 if( $wgRequest->getCheck( 'wpRemember' ) ) {
87 $data['wpRemember'] = 1;
89 $login = new LoginForm( new FauxRequest( $data, true ) );
90 $login->execute( null );
93 } catch( PasswordError
$e ) {
94 $this->error( $e->getMessage() );
100 function doReturnTo() {
101 global $wgRequest, $wgOut;
102 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
103 if ( !$titleObj instanceof Title
) {
104 $titleObj = Title
::newMainPage();
106 $wgOut->redirect( $titleObj->getFullURL() );
109 function error( $msg ) {
111 $wgOut->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
114 function showForm() {
115 global $wgOut, $wgUser, $wgRequest, $wgLivePasswordStrengthChecks;
117 if ( $wgLivePasswordStrengthChecks ) {
118 $wgOut->addPasswordSecurity( 'wpNewPassword', 'wpRetype' );
120 $self = $this->getTitle();
121 if ( !$this->mUserName
) {
122 $this->mUserName
= $wgUser->getName();
125 if ( !$wgUser->isLoggedIn() ) {
126 global $wgCookieExpiration, $wgLang;
127 $rememberMe = '<tr>' .
129 '<td class="mw-input">' .
131 wfMsgExt( 'remembermypassword', 'parsemag', $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) ),
132 'wpRemember', 'wpRemember',
133 $wgRequest->getCheck( 'wpRemember' ) ) .
136 $submitMsg = 'resetpass_submit';
137 $oldpassMsg = 'resetpass-temp-password';
139 $oldpassMsg = 'oldpassword';
140 $submitMsg = 'resetpass-submit-loggedin';
143 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
144 Xml
::openElement( 'form',
147 'action' => $self->getLocalUrl(),
148 'id' => 'mw-resetpass-form' ) ) . "\n" .
149 Html
::hidden( 'token', $wgUser->editToken() ) . "\n" .
150 Html
::hidden( 'wpName', $this->mUserName
) . "\n" .
151 Html
::hidden( 'wpDomain', $this->mDomain
) . "\n" .
152 Html
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
153 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
154 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
155 $this->pretty( array(
156 array( 'wpName', 'username', 'text', $this->mUserName
, '' ),
157 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
, '' ),
158 array( 'wpNewPassword', 'newpassword', 'password', null, '<div id="password-strength"></div>' ),
159 array( 'wpRetype', 'retypenew', 'password', null, '<div id="password-retype"></div>' ),
164 '<td class="mw-input">' .
165 Xml
::submitButton( wfMsg( $submitMsg ) ) .
166 Xml
::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
169 Xml
::closeElement( 'table' ) .
170 Xml
::closeElement( 'form' ) .
171 Xml
::closeElement( 'fieldset' ) . "\n"
175 function pretty( $fields ) {
177 foreach ( $fields as $list ) {
178 list( $name, $label, $type, $value, $extra ) = $list;
179 if( $type == 'text' ) {
180 $field = htmlspecialchars( $value );
182 $attribs = array( 'id' => $name );
183 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
184 $attribs = array_merge( $attribs,
185 User
::passwordChangeInputAttribs() );
187 if ( $name == 'wpPassword' ) {
188 $attribs[] = 'autofocus';
190 $field = Html
::input( $name, $value, $type, $attribs );
193 $out .= "\t<td class='mw-label'>";
194 if ( $type != 'text' )
195 $out .= Xml
::label( wfMsg( $label ), $name );
197 $out .= wfMsgHtml( $label );
199 $out .= "\t<td class='mw-input'>$field</td>\n";
200 $out .= "\t<td>$extra</td>\n";
207 * @throws PasswordError when cannot set the new password because requirements not met.
209 protected function attemptReset( $newpass, $retype ) {
210 $user = User
::newFromName( $this->mUserName
);
211 if( !$user ||
$user->isAnon() ) {
212 throw new PasswordError( 'no such user' );
215 if( $newpass !== $retype ) {
216 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
217 throw new PasswordError( wfMsg( 'badretype' ) );
220 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
221 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
222 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
226 $user->setPassword( $this->mNewpass
);
227 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
228 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
229 } catch( PasswordError
$e ) {
230 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
231 throw new PasswordError( $e->getMessage() );
235 $user->saveSettings();