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' );
51 $this->outputHeader();
52 $wgOut->disallowUserJs();
54 if( !$wgAuth->allowPasswordChange() ) {
55 $this->error( wfMsg( 'resetpass_forbidden' ) );
59 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
60 $this->error( wfMsg( 'resetpass-no-info' ) );
64 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
69 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal('token') ) ) {
71 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
72 $wgOut->addWikiMsg( 'resetpass_success' );
73 if( !$wgUser->isLoggedIn() ) {
75 'action' => 'submitlogin',
76 'wpName' => $this->mUserName
,
77 'wpPassword' => $this->mNewpass
,
78 'returnto' => $wgRequest->getVal( 'returnto' ),
80 if( $wgRequest->getCheck( 'wpRemember' ) ) {
81 $data['wpRemember'] = 1;
83 $login = new LoginForm( new FauxRequest( $data, true ) );
87 } catch( PasswordError
$e ) {
88 $this->error( $e->getMessage() );
94 function doReturnTo() {
95 global $wgRequest, $wgOut;
96 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
97 if ( !$titleObj instanceof Title
) {
98 $titleObj = Title
::newMainPage();
100 $wgOut->redirect( $titleObj->getFullURL() );
103 function error( $msg ) {
105 $wgOut->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
108 function showForm() {
109 global $wgOut, $wgUser, $wgRequest, $wgLivePasswordStrengthChecks;
111 if ( $wgLivePasswordStrengthChecks ) {
112 $wgOut->addPasswordSecurity( 'wpNewPassword', 'wpRetype' );
114 $self = $this->getTitle();
115 if ( !$this->mUserName
) {
116 $this->mUserName
= $wgUser->getName();
119 if ( !$wgUser->isLoggedIn() ) {
120 global $wgCookieExpiration, $wgLang;
121 $rememberMe = '<tr>' .
123 '<td class="mw-input">' .
125 wfMsgExt( 'remembermypassword', 'parsemag', $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) ),
126 'wpRemember', 'wpRemember',
127 $wgRequest->getCheck( 'wpRemember' ) ) .
130 $submitMsg = 'resetpass_submit';
131 $oldpassMsg = 'resetpass-temp-password';
133 $oldpassMsg = 'oldpassword';
134 $submitMsg = 'resetpass-submit-loggedin';
137 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
138 Xml
::openElement( 'form',
141 'action' => $self->getLocalUrl(),
142 'id' => 'mw-resetpass-form' ) ) . "\n" .
143 Xml
::hidden( 'token', $wgUser->editToken() ) . "\n" .
144 Xml
::hidden( 'wpName', $this->mUserName
) . "\n" .
145 Xml
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
146 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
147 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
148 $this->pretty( array(
149 array( 'wpName', 'username', 'text', $this->mUserName
, '' ),
150 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
, '' ),
151 array( 'wpNewPassword', 'newpassword', 'password', null, '<div id="password-strength"></div>' ),
152 array( 'wpRetype', 'retypenew', 'password', null, '<div id="password-retype"></div>' ),
157 '<td class="mw-input">' .
158 Xml
::submitButton( wfMsg( $submitMsg ) ) .
159 Xml
::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
162 Xml
::closeElement( 'table' ) .
163 Xml
::closeElement( 'form' ) .
164 Xml
::closeElement( 'fieldset' ) . "\n"
168 function pretty( $fields ) {
170 foreach ( $fields as $list ) {
171 list( $name, $label, $type, $value, $extra ) = $list;
172 if( $type == 'text' ) {
173 $field = htmlspecialchars( $value );
175 $attribs = array( 'id' => $name );
176 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
177 $attribs = array_merge( $attribs,
178 User
::passwordChangeInputAttribs() );
180 if ( $name == 'wpPassword' ) {
181 $attribs[] = 'autofocus';
183 $field = Html
::input( $name, $value, $type, $attribs );
186 $out .= "\t<td class='mw-label'>";
187 if ( $type != 'text' )
188 $out .= Xml
::label( wfMsg( $label ), $name );
190 $out .= wfMsgHtml( $label );
192 $out .= "\t<td class='mw-input'>$field</td>\n";
193 $out .= "\t<td>$extra</td>\n";
200 * @throws PasswordError when cannot set the new password because requirements not met.
202 protected function attemptReset( $newpass, $retype ) {
203 $user = User
::newFromName( $this->mUserName
);
204 if( !$user ||
$user->isAnon() ) {
205 throw new PasswordError( 'no such user' );
208 if( $newpass !== $retype ) {
209 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
210 throw new PasswordError( wfMsg( 'badretype' ) );
213 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
214 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
215 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
219 $user->setPassword( $this->mNewpass
);
220 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
221 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
222 } catch( PasswordError
$e ) {
223 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
224 throw new PasswordError( $e->getMessage() );
228 $user->saveSettings();