Whitelist the <wbr> element.
[mediawiki.git] / includes / specials / SpecialPasswordReset.php
blob69c40569d0395d75af4aea7baf31973444798fbb
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 {
30 /**
31 * @var Message
33 private $email;
35 /**
36 * @var User
38 private $firstUser;
40 /**
41 * @var Status
43 private $result;
45 public function __construct() {
46 parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
49 public function userCanExecute( User $user ) {
50 return $this->canChangePassword( $user ) === true && parent::userCanExecute( $user );
53 public function checkExecutePermissions( User $user ) {
54 $error = $this->canChangePassword( $user );
55 if ( is_string( $error ) ) {
56 throw new ErrorPageError( 'internalerror', $error );
57 } elseif ( !$error ) {
58 throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
61 return parent::checkExecutePermissions( $user );
64 protected function getFormFields() {
65 global $wgPasswordResetRoutes, $wgAuth;
66 $a = array();
67 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
68 $a['Username'] = array(
69 'type' => 'text',
70 '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 global $wgPasswordResetRoutes;
108 $i = 0;
109 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
110 $i++;
112 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
113 $i++;
115 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
116 $i++;
119 $message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
121 $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
122 $form->setSubmitTextMsg( 'mailmypassword' );
126 * Process the form. At this point we know that the user passes all the criteria in
127 * userCanExecute(), and if the data array contains 'Username', etc, then Username
128 * resets are allowed.
129 * @param $data array
130 * @throws MWException
131 * @throws ThrottledError|PermissionsError
132 * @return Bool|Array
134 public function onSubmit( array $data ) {
135 global $wgAuth;
137 if ( isset( $data['Domain'] ) ) {
138 if ( $wgAuth->validDomain( $data['Domain'] ) ) {
139 $wgAuth->setDomain( $data['Domain'] );
140 } else {
141 $wgAuth->setDomain( 'invaliddomain' );
145 if ( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ) {
146 // The user knows they don't have the passwordreset permission,
147 // but they tried to spoof the form. That's naughty
148 throw new PermissionsError( 'passwordreset' );
152 * @var $firstUser User
153 * @var $users User[]
156 if ( isset( $data['Username'] ) && $data['Username'] !== '' ) {
157 $method = 'username';
158 $users = array( User::newFromName( $data['Username'] ) );
159 } elseif ( isset( $data['Email'] )
160 && $data['Email'] !== ''
161 && Sanitizer::validateEmail( $data['Email'] )
163 $method = 'email';
164 $res = wfGetDB( DB_SLAVE )->select(
165 'user',
166 User::selectFields(),
167 array( 'user_email' => $data['Email'] ),
168 __METHOD__
171 if ( $res ) {
172 $users = array();
174 foreach ( $res as $row ) {
175 $users[] = User::newFromRow( $row );
177 } else {
178 // Some sort of database error, probably unreachable
179 throw new MWException( 'Unknown database error in ' . __METHOD__ );
181 } else {
182 // The user didn't supply any data
183 return false;
186 // Check for hooks (captcha etc), and allow them to modify the users list
187 $error = array();
188 if ( !wfRunHooks( 'SpecialPasswordResetOnSubmit', array( &$users, $data, &$error ) ) ) {
189 return array( $error );
192 if ( count( $users ) == 0 ) {
193 if ( $method == 'email' ) {
194 // Don't reveal whether or not an email address is in use
195 return true;
196 } else {
197 return array( 'noname' );
201 $firstUser = $users[0];
203 if ( !$firstUser instanceof User || !$firstUser->getID() ) {
204 return array( array( 'nosuchuser', $data['Username'] ) );
207 // Check against the rate limiter
208 if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
209 throw new ThrottledError;
212 // Check against password throttle
213 foreach ( $users as $user ) {
214 if ( $user->isPasswordReminderThrottled() ) {
215 global $wgPasswordReminderResendTime;
217 # Round the time in hours to 3 d.p., in case someone is specifying
218 # minutes or seconds.
219 return array( array(
220 'throttled-mailpassword',
221 round( $wgPasswordReminderResendTime, 3 )
222 ) );
226 global $wgNewPasswordExpiry;
228 // All the users will have the same email address
229 if ( $firstUser->getEmail() == '' ) {
230 // This won't be reachable from the email route, so safe to expose the username
231 return array( array( 'noemail', $firstUser->getName() ) );
234 // We need to have a valid IP address for the hook, but per bug 18347, we should
235 // send the user's name if they're logged in.
236 $ip = $this->getRequest()->getIP();
237 if ( !$ip ) {
238 return array( 'badipaddress' );
240 $caller = $this->getUser();
241 wfRunHooks( 'User::mailPasswordInternal', array( &$caller, &$ip, &$firstUser ) );
242 $username = $caller->getName();
243 $msg = IP::isValid( $username )
244 ? 'passwordreset-emailtext-ip'
245 : 'passwordreset-emailtext-user';
247 // Send in the user's language; which should hopefully be the same
248 $userLanguage = $firstUser->getOption( 'language' );
250 $passwords = array();
251 foreach ( $users as $user ) {
252 $password = $user->randomPassword();
253 $user->setNewpassword( $password );
254 $user->saveSettings();
255 $passwords[] = $this->msg( 'passwordreset-emailelement', $user->getName(), $password )
256 ->inLanguage( $userLanguage )->text(); // We'll escape the whole thing later
258 $passwordBlock = implode( "\n\n", $passwords );
260 $this->email = $this->msg( $msg )->inLanguage( $userLanguage );
261 $this->email->params(
262 $username,
263 $passwordBlock,
264 count( $passwords ),
265 '<' . Title::newMainPage()->getCanonicalURL() . '>',
266 round( $wgNewPasswordExpiry / 86400 )
269 $title = $this->msg( 'passwordreset-emailtitle' );
271 $this->result = $firstUser->sendMail( $title->escaped(), $this->email->text() );
273 if ( isset( $data['Capture'] ) && $data['Capture'] ) {
274 // Save the user, will be used if an error occurs when sending the email
275 $this->firstUser = $firstUser;
276 } else {
277 // Blank the email if the user is not supposed to see it
278 $this->email = null;
281 if ( $this->result->isGood() ) {
282 return true;
283 } elseif ( isset( $data['Capture'] ) && $data['Capture'] ) {
284 // The email didn't send, but maybe they knew that and that's why they captured it
285 return true;
286 } else {
287 // @todo FIXME: The email wasn't sent, but we have already set
288 // the password throttle timestamp, so they won't be able to try
289 // again until it expires... :(
290 return array( array( 'mailerror', $this->result->getMessage() ) );
294 public function onSuccess() {
295 if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ) {
296 // @todo Logging
298 if ( $this->result->isGood() ) {
299 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
300 } else {
301 $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture',
302 $this->result->getMessage(), $this->firstUser->getName() );
305 $this->getOutput()->addHTML( Html::rawElement( 'pre', array(), $this->email->escaped() ) );
308 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
309 $this->getOutput()->returnToMain();
312 protected function canChangePassword( User $user ) {
313 global $wgPasswordResetRoutes, $wgEnableEmail, $wgAuth;
315 // Maybe password resets are disabled, or there are no allowable routes
316 if ( !is_array( $wgPasswordResetRoutes ) ||
317 !in_array( true, array_values( $wgPasswordResetRoutes ) )
319 return 'passwordreset-disabled';
322 // Maybe the external auth plugin won't allow local password changes
323 if ( !$wgAuth->allowPasswordChange() ) {
324 return 'resetpass_forbidden';
327 // Maybe email features have been disabled
328 if ( !$wgEnableEmail ) {
329 return 'passwordreset-emaildisabled';
332 // Maybe the user is blocked (check this here rather than relying on the parent
333 // method as we have a more specific error message to use here
334 if ( $user->isBlocked() ) {
335 return 'blocked-mailpassword';
338 return true;
342 * Hide the password reset page if resets are disabled.
343 * @return Bool
345 function isListed() {
346 if ( $this->canChangePassword( $this->getUser() ) === true ) {
347 return parent::isListed();
350 return false;
353 protected function getGroupName() {
354 return 'users';