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() );
61 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
67 $this->checkReadOnly();
69 if ( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
71 $this->mDomain
= $wgAuth->getDomain();
72 if ( !$wgAuth->allowPasswordChange() ) {
73 $this->error( $this->msg( 'resetpass_forbidden' )->text() );
78 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
80 if ( $user->isLoggedIn() ) {
83 LoginForm
::setLoginToken();
84 $token = LoginForm
::getLoginToken();
86 'action' => 'submitlogin',
87 'wpName' => $this->mUserName
,
88 'wpDomain' => $this->mDomain
,
89 'wpLoginToken' => $token,
90 'wpPassword' => $request->getVal( 'wpNewPassword' ),
91 ) +
$request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
92 $login = new LoginForm( new FauxRequest( $data, true ) );
93 $login->setContext( $this->getContext() );
94 $login->execute( null );
98 } catch ( PasswordError
$e ) {
99 $this->error( $e->getMessage() );
105 function doReturnTo() {
106 $request = $this->getRequest();
107 $titleObj = Title
::newFromText( $request->getVal( 'returnto' ) );
108 if ( !$titleObj instanceof Title
) {
109 $titleObj = Title
::newMainPage();
111 $query = $request->getVal( 'returntoquery' );
112 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
118 function error( $msg ) {
119 $this->getOutput()->addHTML( Xml
::element( 'p', array( 'class' => 'error' ), $msg ) );
122 function showForm() {
123 global $wgCookieExpiration;
125 $user = $this->getUser();
126 if ( !$this->mUserName
) {
127 $this->mUserName
= $user->getName();
130 if ( !$user->isLoggedIn() ) {
131 $rememberMe = '<tr>' .
133 '<td class="mw-input">' .
135 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
136 'wpRemember', 'wpRemember',
137 $this->getRequest()->getCheck( 'wpRemember' ) ) .
140 $submitMsg = 'resetpass_submit';
141 $oldpassMsg = 'resetpass-temp-password';
143 $oldpassMsg = 'oldpassword';
144 $submitMsg = 'resetpass-submit-loggedin';
146 $extraFields = array();
147 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
148 $prettyFields = array(
149 array( 'wpName', 'username', 'text', $this->mUserName
),
150 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass
),
151 array( 'wpNewPassword', 'newpassword', 'password', null ),
152 array( 'wpRetype', 'retypenew', 'password', null ),
154 $prettyFields = array_merge( $prettyFields, $extraFields );
155 $hiddenFields = array(
156 'token' => $user->getEditToken(),
157 'wpName' => $this->mUserName
,
158 'wpDomain' => $this->mDomain
,
159 ) +
$this->getRequest()->getValues( 'returnto', 'returntoquery' );
160 $hiddenFieldsStr = '';
161 foreach ( $hiddenFields as $fieldname => $fieldvalue ) {
162 $hiddenFieldsStr .= Html
::hidden( $fieldname, $fieldvalue ) . "\n";
164 $this->getOutput()->addHTML(
165 Xml
::fieldset( $this->msg( 'resetpass_header' )->text() ) .
166 Xml
::openElement( 'form',
169 'action' => $this->getTitle()->getLocalURL(),
170 'id' => 'mw-resetpass-form' ) ) . "\n" .
172 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
173 Xml
::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
174 $this->pretty( $prettyFields ) . "\n" .
178 '<td class="mw-input">' .
179 Xml
::submitButton( $this->msg( $submitMsg )->text() ) .
180 Xml
::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
183 Xml
::closeElement( 'table' ) .
184 Xml
::closeElement( 'form' ) .
185 Xml
::closeElement( 'fieldset' ) . "\n"
190 * @param $fields array
193 function pretty( $fields ) {
195 foreach ( $fields as $list ) {
196 list( $name, $label, $type, $value ) = $list;
197 if ( $type == 'text' ) {
198 $field = htmlspecialchars( $value );
200 $attribs = array( 'id' => $name );
201 if ( $name == 'wpNewPassword' ||
$name == 'wpRetype' ) {
202 $attribs = array_merge( $attribs,
203 User
::passwordChangeInputAttribs() );
205 if ( $name == 'wpPassword' ) {
206 $attribs[] = 'autofocus';
208 $field = Html
::input( $name, $value, $type, $attribs );
211 $out .= "\t<td class='mw-label'>";
213 if ( $type != 'text' ) {
214 $out .= Xml
::label( $this->msg( $label )->text(), $name );
216 $out .= $this->msg( $label )->escaped();
220 $out .= "\t<td class='mw-input'>";
230 * @throws PasswordError when cannot set the new password because requirements not met.
232 protected function attemptReset( $newpass, $retype ) {
233 $isSelf = ( $this->mUserName
=== $this->getUser()->getName() );
235 $user = $this->getUser();
237 $user = User
::newFromName( $this->mUserName
);
240 if ( !$user ||
$user->isAnon() ) {
241 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName
)->text() );
244 if ( $newpass !== $retype ) {
245 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
246 throw new PasswordError( $this->msg( 'badretype' )->text() );
249 $throttleCount = LoginForm
::incLoginThrottle( $this->mUserName
);
250 if ( $throttleCount === true ) {
251 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
254 $abortMsg = 'resetpass-abort-generic';
255 if ( !wfRunHooks( 'AbortChangePassword', array( $user, $this->mOldpass
, $newpass, &$abortMsg ) ) ) {
256 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
257 throw new PasswordError( $this->msg( $abortMsg )->text() );
260 if ( !$user->checkTemporaryPassword( $this->mOldpass
) && !$user->checkPassword( $this->mOldpass
) ) {
261 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
262 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
265 // Please reset throttle for successful logins, thanks!
266 if ( $throttleCount ) {
267 LoginForm
::clearLoginThrottle( $this->mUserName
);
271 $user->setPassword( $this->mNewpass
);
272 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
273 $this->mNewpass
= $this->mOldpass
= $this->mRetype
= '';
274 } catch ( PasswordError
$e ) {
275 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
276 throw new PasswordError( $e->getMessage() );
280 // This is needed to keep the user connected since
281 // changing the password also modifies the user's token.
285 $user->saveSettings();
288 protected function getGroupName() {