4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup SpecialPage
26 * 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;
111 $self = $this->getTitle();
112 if ( !$this->mUserName
) {
113 $this->mUserName
= $wgUser->getName();
116 if ( !$wgUser->isLoggedIn() ) {
117 global $wgCookieExpiration, $wgLang;
118 $rememberMe = '<tr>' .
120 '<td class="mw-input">' .
122 wfMsgExt( 'remembermypassword', 'parsemag', $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) ),
123 'wpRemember', 'wpRemember',
124 $wgRequest->getCheck( 'wpRemember' ) ) .
127 $submitMsg = 'resetpass_submit';
128 $oldpassMsg = 'resetpass-temp-password';
130 $oldpassMsg = 'oldpassword';
131 $submitMsg = 'resetpass-submit-loggedin';
134 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
135 Xml
::openElement( 'form',
138 'action' => $self->getLocalUrl(),
139 'id' => 'mw-resetpass-form' ) ) . "\n" .
140 Xml
::hidden( 'token', $wgUser->editToken() ) . "\n" .
141 Xml
::hidden( 'wpName', $this->mUserName
) . "\n" .
142 Xml
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
143 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
144 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
145 $this->pretty( array(
146 array( 'wpName', 'username', 'text', $this->mUserName
),
147 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
148 array( 'wpNewPassword', 'newpassword', 'password', null ),
149 array( 'wpRetype', 'retypenew', 'password', null ),
154 '<td class="mw-input">' .
155 Xml
::submitButton( wfMsg( $submitMsg ) ) .
156 Xml
::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
159 Xml
::closeElement( 'table' ) .
160 Xml
::closeElement( 'form' ) .
161 Xml
::closeElement( 'fieldset' ) . "\n"
165 function pretty( $fields ) {
167 foreach ( $fields as $list ) {
168 list( $name, $label, $type, $value ) = $list;
169 if( $type == 'text' ) {
170 $field = htmlspecialchars( $value );
172 $attribs = array( 'id' => $name );
173 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
174 $attribs = array_merge( $attribs,
175 User
::passwordChangeInputAttribs() );
177 if ( $name == 'wpPassword' ) {
178 $attribs[] = 'autofocus';
180 $field = Html
::input( $name, $value, $type, $attribs );
183 $out .= "\t<td class='mw-label'>";
184 if ( $type != 'text' )
185 $out .= Xml
::label( wfMsg( $label ), $name );
187 $out .= wfMsgHtml( $label );
189 $out .= "\t<td class='mw-input'>";
198 * @throws PasswordError when cannot set the new password because requirements not met.
200 protected function attemptReset( $newpass, $retype ) {
201 $user = User
::newFromName( $this->mUserName
);
202 if( !$user ||
$user->isAnon() ) {
203 throw new PasswordError( 'no such user' );
206 if( $newpass !== $retype ) {
207 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
208 throw new PasswordError( wfMsg( 'badretype' ) );
211 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
212 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
213 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
217 $user->setPassword( $this->mNewpass
);
218 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
219 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
220 } catch( PasswordError
$e ) {
221 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
222 throw new PasswordError( $e->getMessage() );
227 $user->saveSettings();