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 UnlistedSpecialPage
{
30 public function __construct() {
31 parent
::__construct( 'ChangePassword' );
35 * Main execution point
37 function execute( $par ) {
40 $this->checkReadOnly();
42 $request = $this->getRequest();
43 $this->mUserName
= trim( $request->getVal( 'wpName' ) );
44 $this->mOldpass
= $request->getVal( 'wpPassword' );
45 $this->mNewpass
= $request->getVal( 'wpNewPassword' );
46 $this->mRetype
= $request->getVal( 'wpRetype' );
47 $this->mDomain
= $request->getVal( 'wpDomain' );
50 $this->outputHeader();
51 $this->getOutput()->disallowUserJs();
53 $user = $this->getUser();
54 if( !$request->wasPosted() && !$user->isLoggedIn() ) {
55 $this->error( $this->msg( 'resetpass-no-info' )->text() );
59 if( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
64 if( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
66 if ( isset( $_SESSION['wsDomain'] ) ) {
67 $this->mDomain
= $_SESSION['wsDomain'];
69 $wgAuth->setDomain( $this->mDomain
);
70 if( !$wgAuth->allowPasswordChange() ) {
71 $this->error( $this->msg( 'resetpass_forbidden' )->text() );
75 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
76 $this->getOutput()->addWikiMsg( 'resetpass_success' );
77 if( !$user->isLoggedIn() ) {
78 LoginForm
::setLoginToken();
79 $token = LoginForm
::getLoginToken();
81 'action' => 'submitlogin',
82 'wpName' => $this->mUserName
,
83 'wpDomain' => $this->mDomain
,
84 'wpLoginToken' => $token,
85 'wpPassword' => $this->mNewpass
,
86 'returnto' => $request->getVal( 'returnto' ),
88 if( $request->getCheck( 'wpRemember' ) ) {
89 $data['wpRemember'] = 1;
91 $login = new LoginForm( new FauxRequest( $data, true ) );
92 $login->setContext( $this->getContext() );
93 $login->execute( null );
96 } catch( PasswordError
$e ) {
97 $this->error( $e->getMessage() );
103 function doReturnTo() {
104 $titleObj = Title
::newFromText( $this->getRequest()->getVal( 'returnto' ) );
105 if ( !$titleObj instanceof Title
) {
106 $titleObj = Title
::newMainPage();
108 $this->getOutput()->redirect( $titleObj->getFullURL() );
111 function error( $msg ) {
112 $this->getOutput()->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
115 function showForm() {
116 global $wgCookieExpiration;
118 $user = $this->getUser();
119 if ( !$this->mUserName
) {
120 $this->mUserName
= $user->getName();
123 if ( !$user->isLoggedIn() ) {
124 $rememberMe = '<tr>' .
126 '<td class="mw-input">' .
128 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
129 'wpRemember', 'wpRemember',
130 $this->getRequest()->getCheck( 'wpRemember' ) ) .
133 $submitMsg = 'resetpass_submit';
134 $oldpassMsg = 'resetpass-temp-password';
136 $oldpassMsg = 'oldpassword';
137 $submitMsg = 'resetpass-submit-loggedin';
139 $this->getOutput()->addHTML(
140 Xml
::fieldset( $this->msg( 'resetpass_header' )->text() ) .
141 Xml
::openElement( 'form',
144 'action' => $this->getTitle()->getLocalUrl(),
145 'id' => 'mw-resetpass-form' ) ) . "\n" .
146 Html
::hidden( 'token', $user->getEditToken() ) . "\n" .
147 Html
::hidden( 'wpName', $this->mUserName
) . "\n" .
148 Html
::hidden( 'wpDomain', $this->mDomain
) . "\n" .
149 Html
::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
150 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
151 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
152 $this->pretty( array(
153 array( 'wpName', 'username', 'text', $this->mUserName
),
154 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
155 array( 'wpNewPassword', 'newpassword', 'password', null ),
156 array( 'wpRetype', 'retypenew', 'password', null ),
161 '<td class="mw-input">' .
162 Xml
::submitButton( $this->msg( $submitMsg )->text() ) .
163 Xml
::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
166 Xml
::closeElement( 'table' ) .
167 Xml
::closeElement( 'form' ) .
168 Xml
::closeElement( 'fieldset' ) . "\n"
172 function pretty( $fields ) {
174 foreach ( $fields as $list ) {
175 list( $name, $label, $type, $value ) = $list;
176 if( $type == 'text' ) {
177 $field = htmlspecialchars( $value );
179 $attribs = array( 'id' => $name );
180 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
181 $attribs = array_merge( $attribs,
182 User
::passwordChangeInputAttribs() );
184 if ( $name == 'wpPassword' ) {
185 $attribs[] = 'autofocus';
187 $field = Html
::input( $name, $value, $type, $attribs );
190 $out .= "\t<td class='mw-label'>";
191 if ( $type != 'text' )
192 $out .= Xml
::label( $this->msg( $label )->text(), $name );
194 $out .= $this->msg( $label )->escaped();
196 $out .= "\t<td class='mw-input'>";
205 * @throws PasswordError when cannot set the new password because requirements not met.
207 protected function attemptReset( $newpass, $retype ) {
208 $user = User
::newFromName( $this->mUserName
);
209 if( !$user ||
$user->isAnon() ) {
210 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName
)->text() );
213 if( $newpass !== $retype ) {
214 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
215 throw new PasswordError( $this->msg( 'badretype' )->text() );
218 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
219 if ( $throttleCount === true ) {
220 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
223 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
224 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
225 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
228 // Please reset throttle for successful logins, thanks!
229 if ( $throttleCount ) {
230 LoginForm
::clearLoginThrottle( $this->mUserName
);
234 $user->setPassword( $this->mNewpass
);
235 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
236 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
237 } catch( PasswordError
$e ) {
238 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
239 throw new PasswordError( $e->getMessage() );
243 $user->saveSettings();