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
21 * @ingroup SpecialPage
25 * Special page for requesting a password reset email
27 * @ingroup SpecialPage
29 class SpecialPasswordReset
extends FormSpecialPage
{
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;
68 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
69 $a['Username'] = array(
71 'label-message' => 'passwordreset-username',
73 if ( $this->getUser()->isLoggedIn() ) {
74 $a['Username']['default'] = $this->getUser()->getName();
78 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
81 'label-message' => 'passwordreset-email',
85 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
86 $domains = $wgAuth->domainList();
89 'options' => $domains,
90 'label-message' => 'passwordreset-domain',
94 if ( $this->getUser()->isAllowed( 'passwordreset' ) ) {
95 $a['Capture'] = array(
97 'label-message' => 'passwordreset-capture',
98 'help-message' => 'passwordreset-capture-help',
105 public function alterForm( HTMLForm
$form ) {
106 $form->setSubmitTextMsg( 'mailmypassword' );
109 protected function preText() {
110 global $wgPasswordResetRoutes;
112 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
115 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
118 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
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.
129 * @throws MWException
130 * @throws ThrottledError|PermissionsError
133 public function onSubmit( array $data ) {
136 if ( isset( $data['Domain'] ) ) {
137 if ( $wgAuth->validDomain( $data['Domain'] ) ) {
138 $wgAuth->setDomain( $data['Domain'] );
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
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'] ) )
162 $res = wfGetDB( DB_SLAVE
)->select(
164 User
::selectFields(),
165 array( 'user_email' => $data['Email'] ),
170 foreach ( $res as $row ) {
171 $users[] = User
::newFromRow( $row );
174 // Some sort of database error, probably unreachable
175 throw new MWException( 'Unknown database error in ' . __METHOD__
);
178 // The user didn't supply any data
182 // Check for hooks (captcha etc), and allow them to modify the users list
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
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();
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(
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;
269 // Blank the email if the user is not supposed to see it
273 if ( $this->result
->isGood() ) {
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
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 ) {
289 if ( $this->result
->isGood() ) {
290 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
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';
333 * Hide the password reset page if resets are disabled.
336 function isListed() {
337 if ( $this->canChangePassword( $this->getUser() ) === true ) {
338 return parent
::isListed();
344 protected function getGroupName() {