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 ) {
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' );
49 $this->mDomain
= $wgRequest->getVal( 'wpDomain' );
52 $this->outputHeader();
53 $wgOut->disallowUserJs();
55 if( !$wgRequest->wasPosted() && !$wgUser->isLoggedIn() ) {
56 $this->error( wfMsg( 'resetpass-no-info' ) );
60 if( $wgRequest->wasPosted() && $wgRequest->getBool( 'wpCancel' ) ) {
65 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $wgRequest->getVal( 'token' ) ) ) {
67 $wgAuth->setDomain( $this->mDomain
);
68 if( !$wgAuth->allowPasswordChange() ) {
69 $this->error( wfMsg( 'resetpass_forbidden' ) );
73 $this->attemptReset( $this->mNewpass
, $this->mRetype
);
74 $wgOut->addWikiMsg( 'resetpass_success' );
75 if( !$wgUser->isLoggedIn() ) {
76 LoginForm
::setLoginToken();
77 $token = LoginForm
::getLoginToken();
79 'action' => 'submitlogin',
80 'wpName' => $this->mUserName
,
81 'wpDomain' => $this->mDomain
,
82 'wpLoginToken' => $token,
83 'wpPassword' => $this->mNewpass
,
84 'returnto' => $wgRequest->getVal( 'returnto' ),
86 if( $wgRequest->getCheck( 'wpRemember' ) ) {
87 $data['wpRemember'] = 1;
89 $login = new LoginForm( new FauxRequest( $data, true ) );
90 $login->execute( null );
93 } catch( PasswordError
$e ) {
94 $this->error( $e->getMessage() );
100 function doReturnTo() {
101 global $wgRequest, $wgOut;
102 $titleObj = Title
::newFromText( $wgRequest->getVal( 'returnto' ) );
103 if ( !$titleObj instanceof Title
) {
104 $titleObj = Title
::newMainPage();
106 $wgOut->redirect( $titleObj->getFullURL() );
109 function error( $msg ) {
111 $wgOut->addHTML( Xml
::element('p', array( 'class' => 'error' ), $msg ) );
114 function showForm() {
115 global $wgOut, $wgUser, $wgRequest;
117 $self = $this->getTitle();
118 if ( !$this->mUserName
) {
119 $this->mUserName
= $wgUser->getName();
122 if ( !$wgUser->isLoggedIn() ) {
123 global $wgCookieExpiration, $wgLang;
124 $rememberMe = '<tr>' .
126 '<td class="mw-input">' .
128 wfMsgExt( 'remembermypassword', 'parsemag', $wgLang->formatNum( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) ) ),
129 'wpRemember', 'wpRemember',
130 $wgRequest->getCheck( 'wpRemember' ) ) .
133 $submitMsg = 'resetpass_submit';
134 $oldpassMsg = 'resetpass-temp-password';
136 $oldpassMsg = 'oldpassword';
137 $submitMsg = 'resetpass-submit-loggedin';
140 Xml
::fieldset( wfMsg( 'resetpass_header' ) ) .
141 Xml
::openElement( 'form',
144 'action' => $self->getLocalUrl(),
145 'id' => 'mw-resetpass-form' ) ) . "\n" .
146 Html
::hidden( 'token', $wgUser->editToken() ) . "\n" .
147 Html
::hidden( 'wpName', $this->mUserName
) . "\n" .
148 Html
::hidden( 'wpDomain', $this->mDomain
) . "\n" .
149 Html
::hidden( 'returnto', $wgRequest->getVal( 'returnto' ) ) . "\n" .
150 wfMsgExt( 'resetpass_text', array( 'parse' ) ) . "\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( wfMsg( $submitMsg ) ) .
163 Xml
::submitButton( wfMsg( 'resetpass-submit-cancel' ), 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( wfMsg( $label ), $name );
194 $out .= wfMsgHtml( $label );
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( 'no such user' );
213 if( $newpass !== $retype ) {
214 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
215 throw new PasswordError( wfMsg( 'badretype' ) );
218 if( !$user->checkTemporaryPassword($this->mOldpass
) && !$user->checkPassword($this->mOldpass
) ) {
219 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
220 throw new PasswordError( wfMsg( 'resetpass-wrong-oldpass' ) );
224 $user->setPassword( $this->mNewpass
);
225 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
226 $this->mNewpass
= $this->mOldpass
= $this->mRetypePass
= '';
227 } catch( PasswordError
$e ) {
228 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
229 throw new PasswordError( $e->getMessage() );
233 $user->saveSettings();