(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / specials / SpecialChangePassword.php
blobf6482ef5b4bb022ee853fd1b4a3e50e17924d513
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Let users recover their password.
27 * @ingroup SpecialPage
29 class SpecialChangePassword extends UnlistedSpecialPage {
30 public function __construct() {
31 parent::__construct( 'ChangePassword' );
34 /**
35 * Main execution point
37 function execute( $par ) {
38 global $wgAuth;
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' );
49 $this->setHeaders();
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() );
56 return;
59 if( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
60 $this->doReturnTo();
61 return;
64 if( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
65 try {
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() );
72 return;
75 $this->attemptReset( $this->mNewpass, $this->mRetype );
76 $this->getOutput()->addWikiMsg( 'resetpass_success' );
77 if( !$user->isLoggedIn() ) {
78 LoginForm::setLoginToken();
79 $token = LoginForm::getLoginToken();
80 $data = array(
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 );
95 $this->doReturnTo();
96 } catch( PasswordError $e ) {
97 $this->error( $e->getMessage() );
100 $this->showForm();
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();
122 $rememberMe = '';
123 if ( !$user->isLoggedIn() ) {
124 $rememberMe = '<tr>' .
125 '<td></td>' .
126 '<td class="mw-input">' .
127 Xml::checkLabel(
128 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
129 'wpRemember', 'wpRemember',
130 $this->getRequest()->getCheck( 'wpRemember' ) ) .
131 '</td>' .
132 '</tr>';
133 $submitMsg = 'resetpass_submit';
134 $oldpassMsg = 'resetpass-temp-password';
135 } else {
136 $oldpassMsg = 'oldpassword';
137 $submitMsg = 'resetpass-submit-loggedin';
139 $this->getOutput()->addHTML(
140 Xml::fieldset( $this->msg( 'resetpass_header' )->text() ) .
141 Xml::openElement( 'form',
142 array(
143 'method' => 'post',
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 ),
157 ) ) . "\n" .
158 $rememberMe .
159 "<tr>\n" .
160 "<td></td>\n" .
161 '<td class="mw-input">' .
162 Xml::submitButton( $this->msg( $submitMsg )->text() ) .
163 Xml::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
164 "</td>\n" .
165 "</tr>\n" .
166 Xml::closeElement( 'table' ) .
167 Xml::closeElement( 'form' ) .
168 Xml::closeElement( 'fieldset' ) . "\n"
172 function pretty( $fields ) {
173 $out = '';
174 foreach ( $fields as $list ) {
175 list( $name, $label, $type, $value ) = $list;
176 if( $type == 'text' ) {
177 $field = htmlspecialchars( $value );
178 } else {
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 );
189 $out .= "<tr>\n";
190 $out .= "\t<td class='mw-label'>";
191 if ( $type != 'text' )
192 $out .= Xml::label( $this->msg( $label )->text(), $name );
193 else
194 $out .= $this->msg( $label )->escaped();
195 $out .= "</td>\n";
196 $out .= "\t<td class='mw-input'>";
197 $out .= $field;
198 $out .= "</td>\n";
199 $out .= "</tr>";
201 return $out;
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 );
233 try {
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() );
242 $user->setCookies();
243 $user->saveSettings();