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 * @var string $method Identifies which password reset field was specified by the user.
50 public function __construct() {
51 parent
::__construct( 'PasswordReset', 'editmyprivateinfo' );
54 public function userCanExecute( User
$user ) {
55 return $this->canChangePassword( $user ) === true && parent
::userCanExecute( $user );
58 public function checkExecutePermissions( User
$user ) {
59 $error = $this->canChangePassword( $user );
60 if ( is_string( $error ) ) {
61 throw new ErrorPageError( 'internalerror', $error );
62 } elseif ( !$error ) {
63 throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
66 parent
::checkExecutePermissions( $user );
69 protected function getFormFields() {
71 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
73 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
74 $a['Username'] = array(
76 'label-message' => 'passwordreset-username',
79 if ( $this->getUser()->isLoggedIn() ) {
80 $a['Username']['default'] = $this->getUser()->getName();
84 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
87 'label-message' => 'passwordreset-email',
91 if ( isset( $resetRoutes['domain'] ) && $resetRoutes['domain'] ) {
92 $domains = $wgAuth->domainList();
95 'options' => $domains,
96 'label-message' => 'passwordreset-domain',
100 if ( $this->getUser()->isAllowed( 'passwordreset' ) ) {
101 $a['Capture'] = array(
103 'label-message' => 'passwordreset-capture',
104 'help-message' => 'passwordreset-capture-help',
111 protected function getDisplayFormat() {
115 public function alterForm( HTMLForm
$form ) {
116 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
118 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
121 if ( isset( $resetRoutes['username'] ) && $resetRoutes['username'] ) {
124 if ( isset( $resetRoutes['email'] ) && $resetRoutes['email'] ) {
127 if ( isset( $resetRoutes['domain'] ) && $resetRoutes['domain'] ) {
131 $message = ( $i > 1 ) ?
'passwordreset-text-many' : 'passwordreset-text-one';
133 $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
134 $form->setSubmitTextMsg( 'mailmypassword' );
138 * Process the form. At this point we know that the user passes all the criteria in
139 * userCanExecute(), and if the data array contains 'Username', etc, then Username
140 * resets are allowed.
142 * @throws MWException
143 * @throws ThrottledError|PermissionsError
146 public function onSubmit( array $data ) {
147 global $wgAuth, $wgMinimalPasswordLength;
149 if ( isset( $data['Domain'] ) ) {
150 if ( $wgAuth->validDomain( $data['Domain'] ) ) {
151 $wgAuth->setDomain( $data['Domain'] );
153 $wgAuth->setDomain( 'invaliddomain' );
157 if ( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ) {
158 // The user knows they don't have the passwordreset permission,
159 // but they tried to spoof the form. That's naughty
160 throw new PermissionsError( 'passwordreset' );
164 * @var $firstUser User
168 if ( isset( $data['Username'] ) && $data['Username'] !== '' ) {
169 $method = 'username';
170 $users = array( User
::newFromName( $data['Username'] ) );
171 } elseif ( isset( $data['Email'] )
172 && $data['Email'] !== ''
173 && Sanitizer
::validateEmail( $data['Email'] )
176 $res = wfGetDB( DB_SLAVE
)->select(
178 User
::selectFields(),
179 array( 'user_email' => $data['Email'] ),
186 foreach ( $res as $row ) {
187 $users[] = User
::newFromRow( $row );
190 // Some sort of database error, probably unreachable
191 throw new MWException( 'Unknown database error in ' . __METHOD__
);
194 // The user didn't supply any data
198 // Check for hooks (captcha etc), and allow them to modify the users list
200 if ( !Hooks
::run( 'SpecialPasswordResetOnSubmit', array( &$users, $data, &$error ) ) ) {
201 return array( $error );
204 $this->method
= $method;
206 if ( count( $users ) == 0 ) {
207 if ( $method == 'email' ) {
208 // Don't reveal whether or not an email address is in use
211 return array( 'noname' );
215 $firstUser = $users[0];
217 if ( !$firstUser instanceof User ||
!$firstUser->getID() ) {
218 // Don't parse username as wikitext (bug 65501)
219 return array( array( 'nosuchuser', wfEscapeWikiText( $data['Username'] ) ) );
222 // Check against the rate limiter
223 if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
224 throw new ThrottledError
;
227 // Check against password throttle
228 foreach ( $users as $user ) {
229 if ( $user->isPasswordReminderThrottled() ) {
231 # Round the time in hours to 3 d.p., in case someone is specifying
232 # minutes or seconds.
234 'throttled-mailpassword',
235 round( $this->getConfig()->get( 'PasswordReminderResendTime' ), 3 )
240 // All the users will have the same email address
241 if ( $firstUser->getEmail() == '' ) {
242 // This won't be reachable from the email route, so safe to expose the username
243 return array( array( 'noemail', wfEscapeWikiText( $firstUser->getName() ) ) );
246 // We need to have a valid IP address for the hook, but per bug 18347, we should
247 // send the user's name if they're logged in.
248 $ip = $this->getRequest()->getIP();
250 return array( 'badipaddress' );
252 $caller = $this->getUser();
253 Hooks
::run( 'User::mailPasswordInternal', array( &$caller, &$ip, &$firstUser ) );
254 $username = $caller->getName();
255 $msg = IP
::isValid( $username )
256 ?
'passwordreset-emailtext-ip'
257 : 'passwordreset-emailtext-user';
259 // Send in the user's language; which should hopefully be the same
260 $userLanguage = $firstUser->getOption( 'language' );
262 $passwords = array();
263 foreach ( $users as $user ) {
264 $password = PasswordFactory
::generateRandomPasswordString( $wgMinimalPasswordLength );
265 $user->setNewpassword( $password );
266 $user->saveSettings();
267 $passwords[] = $this->msg( 'passwordreset-emailelement', $user->getName(), $password )
268 ->inLanguage( $userLanguage )->text(); // We'll escape the whole thing later
270 $passwordBlock = implode( "\n\n", $passwords );
272 $this->email
= $this->msg( $msg )->inLanguage( $userLanguage );
273 $this->email
->params(
277 '<' . Title
::newMainPage()->getCanonicalURL() . '>',
278 round( $this->getConfig()->get( 'NewPasswordExpiry' ) / 86400 )
281 $title = $this->msg( 'passwordreset-emailtitle' )->inLanguage( $userLanguage );
283 $this->result
= $firstUser->sendMail( $title->text(), $this->email
->text() );
285 if ( isset( $data['Capture'] ) && $data['Capture'] ) {
286 // Save the user, will be used if an error occurs when sending the email
287 $this->firstUser
= $firstUser;
289 // Blank the email if the user is not supposed to see it
293 if ( $this->result
->isGood() ) {
295 } elseif ( isset( $data['Capture'] ) && $data['Capture'] ) {
296 // The email didn't send, but maybe they knew that and that's why they captured it
299 // @todo FIXME: The email wasn't sent, but we have already set
300 // the password throttle timestamp, so they won't be able to try
301 // again until it expires... :(
302 return array( array( 'mailerror', $this->result
->getMessage() ) );
306 public function onSuccess() {
307 if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email
!= null ) {
310 if ( $this->result
->isGood() ) {
311 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
313 $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture',
314 $this->result
->getMessage(), $this->firstUser
->getName() );
317 $this->getOutput()->addHTML( Html
::rawElement( 'pre', array(), $this->email
->escaped() ) );
320 if ( $this->method
=== 'email' ) {
321 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentemail' );
323 $this->getOutput()->addWikiMsg( 'passwordreset-emailsentusername' );
326 $this->getOutput()->returnToMain();
329 protected function canChangePassword( User
$user ) {
331 $resetRoutes = $this->getConfig()->get( 'PasswordResetRoutes' );
333 // Maybe password resets are disabled, or there are no allowable routes
334 if ( !is_array( $resetRoutes ) ||
335 !in_array( true, array_values( $resetRoutes ) )
337 return 'passwordreset-disabled';
340 // Maybe the external auth plugin won't allow local password changes
341 if ( !$wgAuth->allowPasswordChange() ) {
342 return 'resetpass_forbidden';
345 // Maybe email features have been disabled
346 if ( !$this->getConfig()->get( 'EnableEmail' ) ) {
347 return 'passwordreset-emaildisabled';
350 // Maybe the user is blocked (check this here rather than relying on the parent
351 // method as we have a more specific error message to use here
352 if ( $user->isBlocked() ) {
353 return 'blocked-mailpassword';
360 * Hide the password reset page if resets are disabled.
363 function isListed() {
364 if ( $this->canChangePassword( $this->getUser() ) === true ) {
365 return parent
::isListed();
371 protected function getGroupName() {