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
{
31 protected $mUserName, $mOldpass, $mNewpass, $mRetype, $mDomain;
33 public function __construct() {
34 parent
::__construct( 'ChangePassword' );
38 * Main execution point
40 function execute( $par ) {
44 $this->outputHeader();
45 $this->getOutput()->disallowUserJs();
47 $request = $this->getRequest();
48 $this->mUserName
= trim( $request->getVal( 'wpName' ) );
49 $this->mOldpass
= $request->getVal( 'wpPassword' );
50 $this->mNewpass
= $request->getVal( 'wpNewPassword' );
51 $this->mRetype
= $request->getVal( 'wpRetype' );
52 $this->mDomain
= $request->getVal( 'wpDomain' );
54 $user = $this->getUser();
55 if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
56 $this->error( $this->msg( 'resetpass-no-info' )->text() );
60 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
65 $this->checkReadOnly();
67 if ( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
69 $this->mDomain
= $wgAuth->getDomain();
70 if ( !$wgAuth->allowPasswordChange() ) {
71 $this->error( $this->msg( 'resetpass_forbidden' )->text() );
75 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
77 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' => $request->getVal( 'wpNewPassword' ),
88 ) +
$request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
89 $login = new LoginForm( new FauxRequest( $data, true ) );
90 $login->setContext( $this->getContext() );
91 $login->execute( null );
94 } catch ( PasswordError
$e ) {
95 $this->error( $e->getMessage() );
101 function doReturnTo() {
102 $request = $this->getRequest();
103 $titleObj = Title
::newFromText( $request->getVal( 'returnto' ) );
104 if ( !$titleObj instanceof Title
) {
105 $titleObj = Title
::newMainPage();
107 $query = $request->getVal( 'returntoquery' );
108 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
114 function error( $msg ) {
115 $this->getOutput()->addHTML( Xml
::element( 'p', array( 'class' => 'error' ), $msg ) );
118 function showForm() {
119 global $wgCookieExpiration;
121 $user = $this->getUser();
122 if ( !$this->mUserName
) {
123 $this->mUserName
= $user->getName();
126 if ( !$user->isLoggedIn() ) {
127 $rememberMe = '<tr>' .
129 '<td class="mw-input">' .
131 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
132 'wpRemember', 'wpRemember',
133 $this->getRequest()->getCheck( 'wpRemember' ) ) .
136 $submitMsg = 'resetpass_submit';
137 $oldpassMsg = 'resetpass-temp-password';
139 $oldpassMsg = 'oldpassword';
140 $submitMsg = 'resetpass-submit-loggedin';
142 $extraFields = array();
143 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
144 $prettyFields = array(
145 array( 'wpName', 'username', 'text', $this->mUserName
),
146 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
147 array( 'wpNewPassword', 'newpassword', 'password', null ),
148 array( 'wpRetype', 'retypenew', 'password', null ),
150 $prettyFields = array_merge( $prettyFields, $extraFields );
151 $hiddenFields = array(
152 'token' => $user->getEditToken(),
153 'wpName' => $this->mUserName
,
154 'wpDomain' => $this->mDomain
,
155 ) +
$this->getRequest()->getValues( 'returnto', 'returntoquery' );
156 $hiddenFieldsStr = '';
157 foreach ( $hiddenFields as $fieldname => $fieldvalue ) {
158 $hiddenFieldsStr .= Html
::hidden( $fieldname, $fieldvalue ) . "\n";
160 $this->getOutput()->addHTML(
161 Xml
::fieldset( $this->msg( 'resetpass_header' )->text() ) .
162 Xml
::openElement( 'form',
165 'action' => $this->getTitle()->getLocalURL(),
166 'id' => 'mw-resetpass-form' ) ) . "\n" .
168 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
169 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
170 $this->pretty( $prettyFields ) . "\n" .
174 '<td class="mw-input">' .
175 Xml
::submitButton( $this->msg( $submitMsg )->text() ) .
176 Xml
::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
179 Xml
::closeElement( 'table' ) .
180 Xml
::closeElement( 'form' ) .
181 Xml
::closeElement( 'fieldset' ) . "\n"
186 * @param $fields array
189 function pretty( $fields ) {
191 foreach ( $fields as $list ) {
192 list( $name, $label, $type, $value ) = $list;
193 if ( $type == 'text' ) {
194 $field = htmlspecialchars( $value );
196 $attribs = array( 'id' => $name );
197 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
198 $attribs = array_merge( $attribs,
199 User
::passwordChangeInputAttribs() );
201 if ( $name == 'wpPassword' ) {
202 $attribs[] = 'autofocus';
204 $field = Html
::input( $name, $value, $type, $attribs );
207 $out .= "\t<td class='mw-label'>";
209 if ( $type != 'text' ) {
210 $out .= Xml
::label( $this->msg( $label )->text(), $name );
212 $out .= $this->msg( $label )->escaped();
216 $out .= "\t<td class='mw-input'>";
225 * @throws PasswordError when cannot set the new password because requirements not met.
227 protected function attemptReset( $newpass, $retype ) {
228 $isSelf = ( $this->mUserName
=== $this->getUser()->getName() );
230 $user = $this->getUser();
232 $user = User
::newFromName( $this->mUserName
);
235 if ( !$user ||
$user->isAnon() ) {
236 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName
)->text() );
239 if ( $newpass !== $retype ) {
240 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
241 throw new PasswordError( $this->msg( 'badretype' )->text() );
244 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
245 if ( $throttleCount === true ) {
246 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
249 if ( !$user->checkTemporaryPassword( $this->mOldpass
) && !$user->checkPassword( $this->mOldpass
) ) {
250 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
251 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
254 // Please reset throttle for successful logins, thanks!
255 if ( $throttleCount ) {
256 LoginForm
::clearLoginThrottle( $this->mUserName
);
260 $user->setPassword( $this->mNewpass
);
261 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
262 $this->mNewpass
= $this->mOldpass
= $this->mRetype
= '';
263 } catch ( PasswordError
$e ) {
264 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
265 throw new PasswordError( $e->getMessage() );
269 // This is needed to keep the user connected since
270 // changing the password also modifies the user's token.
274 $user->saveSettings();
277 protected function getGroupName() {