3 * Implements Special:ChangePassword
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 SpecialChangePassword
extends SpecialPage
{
30 public function __construct() {
31 parent
::__construct( 'ChangePassword' );
35 * Main execution point
37 function execute( $par ) {
41 throw new ReadOnlyError
;
44 $request = $this->getRequest();
45 $this->mUserName
= trim( $request->getVal( 'wpName' ) );
46 $this->mOldpass
= $request->getVal( 'wpPassword' );
47 $this->mNewpass
= $request->getVal( 'wpNewPassword' );
48 $this->mRetype
= $request->getVal( 'wpRetype' );
49 $this->mDomain
= $request->getVal( 'wpDomain' );
52 $this->outputHeader();
53 $this->getOutput()->disallowUserJs();
55 $user = $this->getUser();
56 if( !$request->wasPosted() && !$user->isLoggedIn() ) {
57 $this->error( wfMsg( 'resetpass-no-info' ) );
61 if( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
66 if( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
68 if ( isset( $_SESSION['wsDomain'] ) ) {
69 $this->mDomain
= $_SESSION['wsDomain'];
71 $wgAuth->setDomain( $this->mDomain
);
72 if( !$wgAuth->allowPasswordChange() ) {
73 $this->error( wfMsg( 'resetpass_forbidden' ) );
77 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
78 $this->getOutput()->addWikiMsg( 'resetpass_success' );
79 if( !$user->isLoggedIn() ) {
80 LoginForm
::setLoginToken();
81 $token = LoginForm
::getLoginToken();
83 'action' => 'submitlogin',
84 'wpName' => $this->mUserName
,
85 'wpDomain' => $this->mDomain
,
86 'wpLoginToken' => $token,
87 'wpPassword' => $this->mNewpass
,
88 'returnto' => $request->getVal( 'returnto' ),
90 if( $request->getCheck( 'wpRemember' ) ) {
91 $data['wpRemember'] = 1;
93 $login = new LoginForm( new FauxRequest( $data, true ) );
94 $login->execute( null );
97 } catch( PasswordError
$e ) {
98 $this->error( $e->getMessage() );
104 function doReturnTo() {
105 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
106 if ( !$titleObj instanceof Title
) {
107 $titleObj = Title
::newMainPage();
109 $this->getOutput()->redirect( $titleObj->getFullURL() );
112 function error( $msg ) {
113 $this->getOutput()->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
116 function showForm() {
117 global $wgCookieExpiration;
119 $user = $this->getUser();
120 if ( !$this->mUserName
) {
121 $this->mUserName
= $user->getName();
124 if ( !$user->isLoggedIn() ) {
125 $rememberMe = '<tr>' .
127 '<td class="mw-input">' .
129 wfMsgExt( 'remembermypassword', 'parsemag', $this->getLang()->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) ),
130 'wpRemember', 'wpRemember',
131 $this->getRequest()->getCheck( 'wpRemember' ) ) .
134 $submitMsg = 'resetpass_submit';
135 $oldpassMsg = 'resetpass-temp-password';
137 $oldpassMsg = 'oldpassword';
138 $submitMsg = 'resetpass-submit-loggedin';
140 $this->getOutput()->addHTML(
141 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
142 Xml
::openElement( 'form',
145 'action' => $this->getTitle()->getLocalUrl(),
146 'id' => 'mw-resetpass-form' ) ) . "\n" .
147 Html
::hidden( 'token', $user->editToken() ) . "\n" .
148 Html
::hidden( 'wpName', $this->mUserName
) . "\n" .
149 Html
::hidden( 'wpDomain', $this->mDomain
) . "\n" .
150 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
151 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\n" .
152 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
153 $this->pretty( array(
154 array( 'wpName', 'username', 'text', $this->mUserName
),
155 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
156 array( 'wpNewPassword', 'newpassword', 'password', null ),
157 array( 'wpRetype', 'retypenew', 'password', null ),
162 '<td class="mw-input">' .
163 Xml
::submitButton( wfMsg( $submitMsg ) ) .
164 Xml
::submitButton( wfMsg( 'resetpass-submit-cancel' ), array( 'name' => 'wpCancel' ) ) .
167 Xml
::closeElement( 'table' ) .
168 Xml
::closeElement( 'form' ) .
169 Xml
::closeElement( 'fieldset' ) . "\n"
173 function pretty( $fields ) {
175 foreach ( $fields as $list ) {
176 list( $name, $label, $type, $value ) = $list;
177 if( $type == 'text' ) {
178 $field = htmlspecialchars( $value );
180 $attribs = array( 'id' => $name );
181 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
182 $attribs = array_merge( $attribs,
183 User
::passwordChangeInputAttribs() );
185 if ( $name == 'wpPassword' ) {
186 $attribs[] = 'autofocus';
188 $field = Html
::input( $name, $value, $type, $attribs );
191 $out .= "\t<td class='mw-label'>";
192 if ( $type != 'text' )
193 $out .= Xml
::label( wfMsg( $label ), $name );
195 $out .= wfMsgHtml( $label );
197 $out .= "\t<td class='mw-input'>";
206 * @throws PasswordError when cannot set the new password because requirements not met.
208 protected function attemptReset( $newpass, $retype ) {
209 $user = User
::newFromName( $this->mUserName
);
210 if( !$user ||
$user->isAnon() ) {
211 throw new PasswordError( wfMsg( 'nosuchusershort', $this->mUserName
) );
214 if( $newpass !== $retype ) {
215 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
216 throw new PasswordError( wfMsg( 'badretype' ) );
219 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
220 if ( $throttleCount === true ) {
221 throw new PasswordError( wfMsg( 'login-throttled' ) );
224 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
225 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
226 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
229 // Please reset throttle for successful logins, thanks!
230 if ( $throttleCount ) {
231 LoginForm
::clearLoginThrottle( $this->mUserName
);
235 $user->setPassword( $this->mNewpass
);
236 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
237 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
238 } catch( PasswordError
$e ) {
239 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
240 throw new PasswordError( $e->getMessage() );
244 $user->saveSettings();