Merge "Whitelist the <wbr> element."
[mediawiki.git] / includes / specials / SpecialChangePassword.php
blobd297a2fc2cd494eac98edce829342ee270c0d841
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 {
31 protected $mUserName, $mOldpass, $mNewpass, $mRetype, $mDomain;
33 public function __construct() {
34 parent::__construct( 'ChangePassword', 'editmyprivateinfo' );
37 /**
38 * Main execution point
40 function execute( $par ) {
41 global $wgAuth;
43 $this->setHeaders();
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() );
58 return;
61 if ( $request->wasPosted() && $request->getBool( 'wpCancel' ) ) {
62 $this->doReturnTo();
64 return;
67 $this->checkReadOnly();
68 $this->checkPermissions();
70 if ( $request->wasPosted() && $user->matchEditToken( $request->getVal( 'token' ) ) ) {
71 try {
72 $this->mDomain = $wgAuth->getDomain();
73 if ( !$wgAuth->allowPasswordChange() ) {
74 $this->error( $this->msg( 'resetpass_forbidden' )->text() );
76 return;
79 $this->attemptReset( $this->mNewpass, $this->mRetype );
81 if ( $user->isLoggedIn() ) {
82 $this->doReturnTo();
83 } else {
84 LoginForm::setLoginToken();
85 $token = LoginForm::getLoginToken();
86 $data = array(
87 'action' => 'submitlogin',
88 'wpName' => $this->mUserName,
89 'wpDomain' => $this->mDomain,
90 'wpLoginToken' => $token,
91 'wpPassword' => $request->getVal( 'wpNewPassword' ),
92 ) + $request->getValues( 'wpRemember', 'returnto', 'returntoquery' );
93 $login = new LoginForm( new FauxRequest( $data, true ) );
94 $login->setContext( $this->getContext() );
95 $login->execute( null );
98 return;
99 } catch ( PasswordError $e ) {
100 $this->error( $e->getMessage() );
103 $this->showForm();
106 function doReturnTo() {
107 $request = $this->getRequest();
108 $titleObj = Title::newFromText( $request->getVal( 'returnto' ) );
109 if ( !$titleObj instanceof Title ) {
110 $titleObj = Title::newMainPage();
112 $query = $request->getVal( 'returntoquery' );
113 $this->getOutput()->redirect( $titleObj->getFullURL( $query ) );
117 * @param $msg string
119 function error( $msg ) {
120 $this->getOutput()->addHTML( Xml::element( 'p', array( 'class' => 'error' ), $msg ) );
123 function showForm() {
124 global $wgCookieExpiration;
126 $user = $this->getUser();
127 if ( !$this->mUserName ) {
128 $this->mUserName = $user->getName();
130 $rememberMe = '';
131 if ( !$user->isLoggedIn() ) {
132 $rememberMe = '<tr>' .
133 '<td></td>' .
134 '<td class="mw-input">' .
135 Xml::checkLabel(
136 $this->msg( 'remembermypassword' )->numParams( ceil( $wgCookieExpiration / ( 3600 * 24 ) ) )->text(),
137 'wpRemember', 'wpRemember',
138 $this->getRequest()->getCheck( 'wpRemember' ) ) .
139 '</td>' .
140 '</tr>';
141 $submitMsg = 'resetpass_submit';
142 $oldpassMsg = 'resetpass-temp-password';
143 } else {
144 $oldpassMsg = 'oldpassword';
145 $submitMsg = 'resetpass-submit-loggedin';
147 $extraFields = array();
148 wfRunHooks( 'ChangePasswordForm', array( &$extraFields ) );
149 $prettyFields = array(
150 array( 'wpName', 'username', 'text', $this->mUserName ),
151 array( 'wpPassword', $oldpassMsg, 'password', $this->mOldpass ),
152 array( 'wpNewPassword', 'newpassword', 'password', null ),
153 array( 'wpRetype', 'retypenew', 'password', null ),
155 $prettyFields = array_merge( $prettyFields, $extraFields );
156 $hiddenFields = array(
157 'token' => $user->getEditToken(),
158 'wpName' => $this->mUserName,
159 'wpDomain' => $this->mDomain,
160 ) + $this->getRequest()->getValues( 'returnto', 'returntoquery' );
161 $hiddenFieldsStr = '';
162 foreach ( $hiddenFields as $fieldname => $fieldvalue ) {
163 $hiddenFieldsStr .= Html::hidden( $fieldname, $fieldvalue ) . "\n";
165 $this->getOutput()->addHTML(
166 Xml::fieldset( $this->msg( 'resetpass_header' )->text() ) .
167 Xml::openElement( 'form',
168 array(
169 'method' => 'post',
170 'action' => $this->getTitle()->getLocalURL(),
171 'id' => 'mw-resetpass-form' ) ) . "\n" .
172 $hiddenFieldsStr .
173 $this->msg( 'resetpass_text' )->parseAsBlock() . "\n" .
174 Xml::openElement( 'table', array( 'id' => 'mw-resetpass-table' ) ) . "\n" .
175 $this->pretty( $prettyFields ) . "\n" .
176 $rememberMe .
177 "<tr>\n" .
178 "<td></td>\n" .
179 '<td class="mw-input">' .
180 Xml::submitButton( $this->msg( $submitMsg )->text() ) .
181 Xml::submitButton( $this->msg( 'resetpass-submit-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
182 "</td>\n" .
183 "</tr>\n" .
184 Xml::closeElement( 'table' ) .
185 Xml::closeElement( 'form' ) .
186 Xml::closeElement( 'fieldset' ) . "\n"
191 * @param $fields array
192 * @return string
194 function pretty( $fields ) {
195 $out = '';
196 foreach ( $fields as $list ) {
197 list( $name, $label, $type, $value ) = $list;
198 if ( $type == 'text' ) {
199 $field = htmlspecialchars( $value );
200 } else {
201 $attribs = array( 'id' => $name );
202 if ( $name == 'wpNewPassword' || $name == 'wpRetype' ) {
203 $attribs = array_merge( $attribs,
204 User::passwordChangeInputAttribs() );
206 if ( $name == 'wpPassword' ) {
207 $attribs[] = 'autofocus';
209 $field = Html::input( $name, $value, $type, $attribs );
211 $out .= "<tr>\n";
212 $out .= "\t<td class='mw-label'>";
214 if ( $type != 'text' ) {
215 $out .= Xml::label( $this->msg( $label )->text(), $name );
216 } else {
217 $out .= $this->msg( $label )->escaped();
220 $out .= "</td>\n";
221 $out .= "\t<td class='mw-input'>";
222 $out .= $field;
223 $out .= "</td>\n";
224 $out .= "</tr>";
227 return $out;
231 * @throws PasswordError when cannot set the new password because requirements not met.
233 protected function attemptReset( $newpass, $retype ) {
234 $isSelf = ( $this->mUserName === $this->getUser()->getName() );
235 if ( $isSelf ) {
236 $user = $this->getUser();
237 } else {
238 $user = User::newFromName( $this->mUserName );
241 if ( !$user || $user->isAnon() ) {
242 throw new PasswordError( $this->msg( 'nosuchusershort', $this->mUserName )->text() );
245 if ( $newpass !== $retype ) {
246 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'badretype' ) );
247 throw new PasswordError( $this->msg( 'badretype' )->text() );
250 $throttleCount = LoginForm::incLoginThrottle( $this->mUserName );
251 if ( $throttleCount === true ) {
252 throw new PasswordError( $this->msg( 'login-throttled' )->text() );
255 $abortMsg = 'resetpass-abort-generic';
256 if ( !wfRunHooks( 'AbortChangePassword', array( $user, $this->mOldpass, $newpass, &$abortMsg ) ) ) {
257 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'abortreset' ) );
258 throw new PasswordError( $this->msg( $abortMsg )->text() );
261 if ( !$user->checkTemporaryPassword( $this->mOldpass ) && !$user->checkPassword( $this->mOldpass ) ) {
262 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'wrongpassword' ) );
263 throw new PasswordError( $this->msg( 'resetpass-wrong-oldpass' )->text() );
266 // Please reset throttle for successful logins, thanks!
267 if ( $throttleCount ) {
268 LoginForm::clearLoginThrottle( $this->mUserName );
271 try {
272 $user->setPassword( $this->mNewpass );
273 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'success' ) );
274 $this->mNewpass = $this->mOldpass = $this->mRetype = '';
275 } catch ( PasswordError $e ) {
276 wfRunHooks( 'PrefsPasswordAudit', array( $user, $newpass, 'error' ) );
277 throw new PasswordError( $e->getMessage() );
280 if ( $isSelf ) {
281 // This is needed to keep the user connected since
282 // changing the password also modifies the user's token.
283 $user->setCookies();
286 $user->saveSettings();
289 protected function getGroupName() {
290 return 'users';