Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / specials / SpecialPasswordReset.php
blobdf9ed575d4132e0d9670e9765690472e1bdf1e59
1 <?php
2 /**
3 * Implements Special:PasswordReset
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 * Special page for requesting a password reset email
27 * @ingroup SpecialPage
29 class SpecialPasswordReset extends FormSpecialPage {
31 /**
32 * @var Message
34 private $email;
36 /**
37 * @var User
39 private $firstUser;
41 /**
42 * @var Status
44 private $result;
46 public function __construct() {
47 parent::__construct( 'PasswordReset' );
50 public function userCanExecute( User $user ) {
51 return $this->canChangePassword( $user ) === true && parent::userCanExecute( $user );
54 public function checkExecutePermissions( User $user ) {
55 $error = $this->canChangePassword( $user );
56 if ( is_string( $error ) ) {
57 throw new ErrorPageError( 'internalerror', $error );
58 } elseif ( !$error ) {
59 throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
62 return parent::checkExecutePermissions( $user );
65 protected function getFormFields() {
66 global $wgPasswordResetRoutes, $wgAuth;
67 $a = array();
68 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
69 $a['Username'] = array(
70 'type' => 'text',
71 'label-message' => 'passwordreset-username',
73 if ( $this->getUser()->isLoggedIn() ) {
74 $a['Username']['default'] = $this->getUser()->getName();
78 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
79 $a['Email'] = array(
80 'type' => 'email',
81 'label-message' => 'passwordreset-email',
85 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
86 $domains = $wgAuth->domainList();
87 $a['Domain'] = array(
88 'type' => 'select',
89 'options' => $domains,
90 'label-message' => 'passwordreset-domain',
94 if ( $this->getUser()->isAllowed( 'passwordreset' ) ) {
95 $a['Capture'] = array(
96 'type' => 'check',
97 'label-message' => 'passwordreset-capture',
98 'help-message' => 'passwordreset-capture-help',
102 return $a;
105 public function alterForm( HTMLForm $form ) {
106 $form->setSubmitTextMsg( 'mailmypassword' );
109 protected function preText() {
110 global $wgPasswordResetRoutes;
111 $i = 0;
112 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
113 $i++;
115 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
116 $i++;
118 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
119 $i++;
121 return $this->msg( 'passwordreset-pretext', $i )->parseAsBlock();
125 * Process the form. At this point we know that the user passes all the criteria in
126 * userCanExecute(), and if the data array contains 'Username', etc, then Username
127 * resets are allowed.
128 * @param $data array
129 * @throws MWException
130 * @throws ThrottledError|PermissionsError
131 * @return Bool|Array
133 public function onSubmit( array $data ) {
134 global $wgAuth;
136 if ( isset( $data['Domain'] ) ) {
137 if ( $wgAuth->validDomain( $data['Domain'] ) ) {
138 $wgAuth->setDomain( $data['Domain'] );
139 } else {
140 $wgAuth->setDomain( 'invaliddomain' );
144 if ( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ) {
145 // The user knows they don't have the passwordreset permission, but they tried to spoof the form. That's naughty
146 throw new PermissionsError( 'passwordreset' );
150 * @var $firstUser User
151 * @var $users User[]
154 if ( isset( $data['Username'] ) && $data['Username'] !== '' ) {
155 $method = 'username';
156 $users = array( User::newFromName( $data['Username'] ) );
157 } elseif ( isset( $data['Email'] )
158 && $data['Email'] !== ''
159 && Sanitizer::validateEmail( $data['Email'] ) )
161 $method = 'email';
162 $res = wfGetDB( DB_SLAVE )->select(
163 'user',
164 User::selectFields(),
165 array( 'user_email' => $data['Email'] ),
166 __METHOD__
168 if ( $res ) {
169 $users = array();
170 foreach ( $res as $row ) {
171 $users[] = User::newFromRow( $row );
173 } else {
174 // Some sort of database error, probably unreachable
175 throw new MWException( 'Unknown database error in ' . __METHOD__ );
177 } else {
178 // The user didn't supply any data
179 return false;
182 // Check for hooks (captcha etc), and allow them to modify the users list
183 $error = array();
184 if ( !wfRunHooks( 'SpecialPasswordResetOnSubmit', array( &$users, $data, &$error ) ) ) {
185 return array( $error );
188 if ( count( $users ) == 0 ) {
189 if ( $method == 'email' ) {
190 // Don't reveal whether or not an email address is in use
191 return true;
192 } else {
193 return array( 'noname' );
197 $firstUser = $users[0];
199 if ( !$firstUser instanceof User || !$firstUser->getID() ) {
200 return array( array( 'nosuchuser', $data['Username'] ) );
203 // Check against the rate limiter
204 if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
205 throw new ThrottledError;
208 // Check against password throttle
209 foreach ( $users as $user ) {
210 if ( $user->isPasswordReminderThrottled() ) {
211 global $wgPasswordReminderResendTime;
212 # Round the time in hours to 3 d.p., in case someone is specifying
213 # minutes or seconds.
214 return array( array( 'throttled-mailpassword', round( $wgPasswordReminderResendTime, 3 ) ) );
218 global $wgNewPasswordExpiry;
220 // All the users will have the same email address
221 if ( $firstUser->getEmail() == '' ) {
222 // This won't be reachable from the email route, so safe to expose the username
223 return array( array( 'noemail', $firstUser->getName() ) );
226 // We need to have a valid IP address for the hook, but per bug 18347, we should
227 // send the user's name if they're logged in.
228 $ip = $this->getRequest()->getIP();
229 if ( !$ip ) {
230 return array( 'badipaddress' );
232 $caller = $this->getUser();
233 wfRunHooks( 'User::mailPasswordInternal', array( &$caller, &$ip, &$firstUser ) );
234 $username = $caller->getName();
235 $msg = IP::isValid( $username )
236 ? 'passwordreset-emailtext-ip'
237 : 'passwordreset-emailtext-user';
239 // Send in the user's language; which should hopefully be the same
240 $userLanguage = $firstUser->getOption( 'language' );
242 $passwords = array();
243 foreach ( $users as $user ) {
244 $password = $user->randomPassword();
245 $user->setNewpassword( $password );
246 $user->saveSettings();
247 $passwords[] = $this->msg( 'passwordreset-emailelement', $user->getName(), $password
248 )->inLanguage( $userLanguage )->text(); // We'll escape the whole thing later
250 $passwordBlock = implode( "\n\n", $passwords );
252 $this->email = $this->msg( $msg )->inLanguage( $userLanguage );
253 $this->email->params(
254 $username,
255 $passwordBlock,
256 count( $passwords ),
257 '<' . Title::newMainPage()->getCanonicalURL() . '>',
258 round( $wgNewPasswordExpiry / 86400 )
261 $title = $this->msg( 'passwordreset-emailtitle' );
263 $this->result = $firstUser->sendMail( $title->escaped(), $this->email->text() );
265 if ( isset( $data['Capture'] ) && $data['Capture'] ) {
266 // Save the user, will be used if an error occurs when sending the email
267 $this->firstUser = $firstUser;
268 } else {
269 // Blank the email if the user is not supposed to see it
270 $this->email = null;
273 if ( $this->result->isGood() ) {
274 return true;
275 } elseif ( isset( $data['Capture'] ) && $data['Capture'] ) {
276 // The email didn't send, but maybe they knew that and that's why they captured it
277 return true;
278 } else {
279 // @todo FIXME: The email didn't send, but we have already set the password throttle
280 // timestamp, so they won't be able to try again until it expires... :(
281 return array( array( 'mailerror', $this->result->getMessage() ) );
285 public function onSuccess() {
286 if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ) {
287 // @todo: Logging
289 if ( $this->result->isGood() ) {
290 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
291 } else {
292 $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture',
293 $this->result->getMessage(), $this->firstUser->getName() );
296 $this->getOutput()->addHTML( Html::rawElement( 'pre', array(), $this->email->escaped() ) );
299 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
300 $this->getOutput()->returnToMain();
303 protected function canChangePassword( User $user ) {
304 global $wgPasswordResetRoutes, $wgEnableEmail, $wgAuth;
306 // Maybe password resets are disabled, or there are no allowable routes
307 if ( !is_array( $wgPasswordResetRoutes ) ||
308 !in_array( true, array_values( $wgPasswordResetRoutes ) ) )
310 return 'passwordreset-disabled';
313 // Maybe the external auth plugin won't allow local password changes
314 if ( !$wgAuth->allowPasswordChange() ) {
315 return 'resetpass_forbidden';
318 // Maybe email features have been disabled
319 if ( !$wgEnableEmail ) {
320 return 'passwordreset-emaildisabled';
323 // Maybe the user is blocked (check this here rather than relying on the parent
324 // method as we have a more specific error message to use here
325 if ( $user->isBlocked() ) {
326 return 'blocked-mailpassword';
329 return true;
333 * Hide the password reset page if resets are disabled.
334 * @return Bool
336 function isListed() {
337 if ( $this->canChangePassword( $this->getUser() ) === true ) {
338 return parent::isListed();
341 return false;
344 protected function getGroupName() {
345 return 'users';