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
{
41 public function __construct() {
42 parent
::__construct( 'PasswordReset' );
45 public function userCanExecute( User
$user ) {
46 return $this->canChangePassword( $user ) === true && parent
::userCanExecute( $user );
49 public function checkExecutePermissions( User
$user ) {
50 $error = $this->canChangePassword( $user );
51 if ( is_string( $error ) ) {
52 throw new ErrorPageError( 'internalerror', $error );
53 } elseif ( !$error ) {
54 throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
57 return parent
::checkExecutePermissions( $user );
60 protected function getFormFields() {
61 global $wgPasswordResetRoutes, $wgAuth;
63 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
64 $a['Username'] = array(
66 'label-message' => 'passwordreset-username',
68 if( $this->getUser()->isLoggedIn() ) {
69 $a['Username']['default'] = $this->getUser()->getName();
73 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
76 'label-message' => 'passwordreset-email',
80 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
81 $domains = $wgAuth->domainList();
84 'options' => $domains,
85 'label-message' => 'passwordreset-domain',
89 if( $this->getUser()->isAllowed( 'passwordreset' ) ){
90 $a['Capture'] = array(
92 'label-message' => 'passwordreset-capture',
93 'help-message' => 'passwordreset-capture-help',
100 public function alterForm( HTMLForm
$form ) {
101 $form->setSubmitTextMsg( 'mailmypassword' );
104 protected function preText() {
105 global $wgPasswordResetRoutes;
107 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
110 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
113 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
116 return $this->msg( 'passwordreset-pretext', $i )->parseAsBlock();
120 * Process the form. At this point we know that the user passes all the criteria in
121 * userCanExecute(), and if the data array contains 'Username', etc, then Username
122 * resets are allowed.
126 public function onSubmit( array $data ) {
129 if ( isset( $data['Domain'] ) ) {
130 if ( $wgAuth->validDomain( $data['Domain'] ) ) {
131 $wgAuth->setDomain( $data['Domain'] );
133 $wgAuth->setDomain( 'invaliddomain' );
137 if( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ){
138 // The user knows they don't have the passwordreset permission, but they tried to spoof the form. That's naughty
139 throw new PermissionsError( 'passwordreset' );
143 * @var $firstUser User
147 if ( isset( $data['Username'] ) && $data['Username'] !== '' ) {
148 $method = 'username';
149 $users = array( User
::newFromName( $data['Username'] ) );
150 } elseif ( isset( $data['Email'] )
151 && $data['Email'] !== ''
152 && Sanitizer
::validateEmail( $data['Email'] ) )
155 $res = wfGetDB( DB_SLAVE
)->select(
158 array( 'user_email' => $data['Email'] ),
163 foreach( $res as $row ){
164 $users[] = User
::newFromRow( $row );
167 // Some sort of database error, probably unreachable
168 throw new MWException( 'Unknown database error in ' . __METHOD__
);
171 // The user didn't supply any data
175 // Check for hooks (captcha etc), and allow them to modify the users list
177 if ( !wfRunHooks( 'SpecialPasswordResetOnSubmit', array( &$users, $data, &$error ) ) ) {
178 return array( $error );
181 if( count( $users ) == 0 ){
182 if( $method == 'email' ){
183 // Don't reveal whether or not an email address is in use
186 return array( 'noname' );
190 $firstUser = $users[0];
192 if ( !$firstUser instanceof User ||
!$firstUser->getID() ) {
193 return array( array( 'nosuchuser', $data['Username'] ) );
196 // Check against the rate limiter
197 if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
198 throw new ThrottledError
;
201 // Check against password throttle
202 foreach ( $users as $user ) {
203 if ( $user->isPasswordReminderThrottled() ) {
204 global $wgPasswordReminderResendTime;
205 # Round the time in hours to 3 d.p., in case someone is specifying
206 # minutes or seconds.
207 return array( array( 'throttled-mailpassword', round( $wgPasswordReminderResendTime, 3 ) ) );
211 global $wgNewPasswordExpiry;
213 // All the users will have the same email address
214 if ( $firstUser->getEmail() == '' ) {
215 // This won't be reachable from the email route, so safe to expose the username
216 return array( array( 'noemail', $firstUser->getName() ) );
219 // We need to have a valid IP address for the hook, but per bug 18347, we should
220 // send the user's name if they're logged in.
221 $ip = $this->getRequest()->getIP();
223 return array( 'badipaddress' );
225 $caller = $this->getUser();
226 wfRunHooks( 'User::mailPasswordInternal', array( &$caller, &$ip, &$firstUser ) );
227 $username = $caller->getName();
228 $msg = IP
::isValid( $username )
229 ?
'passwordreset-emailtext-ip'
230 : 'passwordreset-emailtext-user';
232 $passwords = array();
233 foreach ( $users as $user ) {
234 $password = $user->randomPassword();
235 $user->setNewpassword( $password );
236 $user->saveSettings();
237 $passwords[] = $this->msg( 'passwordreset-emailelement', $user->getName(), $password )->plain(); // We'll escape the whole thing later
239 $passwordBlock = implode( "\n\n", $passwords );
241 // Send in the user's language; which should hopefully be the same
242 $userLanguage = $firstUser->getOption( 'language' );
244 $this->email
= $this->msg( $msg )->inLanguage( $userLanguage );
245 $this->email
->params(
249 Title
::newMainPage()->getCanonicalUrl(),
250 round( $wgNewPasswordExpiry / 86400 )
253 $title = $this->msg( 'passwordreset-emailtitle' );
255 $this->result
= $firstUser->sendMail( $title->escaped(), $this->email
->escaped() );
257 // Blank the email if the user is not supposed to see it
258 if( !isset( $data['Capture'] ) ||
!$data['Capture'] ) {
262 if ( $this->result
->isGood() ) {
264 } elseif( isset( $data['Capture'] ) && $data['Capture'] ){
265 // The email didn't send, but maybe they knew that and that's why they captured it
268 // @todo FIXME: The email didn't send, but we have already set the password throttle
269 // timestamp, so they won't be able to try again until it expires... :(
270 return array( array( 'mailerror', $this->result
->getMessage() ) );
274 public function onSuccess() {
275 if( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email
!= null ){
278 if( $this->result
->isGood() ){
279 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
281 $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture', $this->result
->getMessage() );
284 $this->getOutput()->addHTML( Html
::rawElement( 'pre', array(), $this->email
->escaped() ) );
287 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
288 $this->getOutput()->returnToMain();
291 protected function canChangePassword( User
$user ) {
292 global $wgPasswordResetRoutes, $wgAuth;
294 // Maybe password resets are disabled, or there are no allowable routes
295 if ( !is_array( $wgPasswordResetRoutes ) ||
296 !in_array( true, array_values( $wgPasswordResetRoutes ) ) )
298 return 'passwordreset-disabled';
301 // Maybe the external auth plugin won't allow local password changes
302 if ( !$wgAuth->allowPasswordChange() ) {
303 return 'resetpass_forbidden';
306 // Maybe the user is blocked (check this here rather than relying on the parent
307 // method as we have a more specific error message to use here
308 if ( $user->isBlocked() ) {
309 return 'blocked-mailpassword';
316 * Hide the password reset page if resets are disabled.
319 function isListed() {
320 if ( $this->canChangePassword( $this->getUser() ) === true ) {
321 return parent
::isListed();