Merge "Follow-up 42333412833a - Fix behaviour $wgVerifyMimeType = false;"
[lhc/web/wiklou.git] / includes / specials / SpecialPasswordReset.php
1 <?php
2 /**
3 * Implements Special:PasswordReset
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special page for requesting a password reset email
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialPasswordReset extends FormSpecialPage {
30 /**
31 * @var Message
32 */
33 private $email;
34
35 /**
36 * @var User
37 */
38 private $firstUser;
39
40 /**
41 * @var Status
42 */
43 private $result;
44
45 public function __construct() {
46 parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
47 }
48
49 public function userCanExecute( User $user ) {
50 return $this->canChangePassword( $user ) === true && parent::userCanExecute( $user );
51 }
52
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' );
59 }
60
61 return parent::checkExecutePermissions( $user );
62 }
63
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',
71 );
72
73 if ( $this->getUser()->isLoggedIn() ) {
74 $a['Username']['default'] = $this->getUser()->getName();
75 }
76 }
77
78 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
79 $a['Email'] = array(
80 'type' => 'email',
81 'label-message' => 'passwordreset-email',
82 );
83 }
84
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',
91 );
92 }
93
94 if ( $this->getUser()->isAllowed( 'passwordreset' ) ) {
95 $a['Capture'] = array(
96 'type' => 'check',
97 'label-message' => 'passwordreset-capture',
98 'help-message' => 'passwordreset-capture-help',
99 );
100 }
101
102 return $a;
103 }
104
105 public function alterForm( HTMLForm $form ) {
106 global $wgPasswordResetRoutes;
107
108 $i = 0;
109 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
110 $i++;
111 }
112 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
113 $i++;
114 }
115 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
116 $i++;
117 }
118
119 $message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';
120
121 $form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
122 $form->setSubmitTextMsg( 'mailmypassword' );
123 }
124
125 /**
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
133 */
134 public function onSubmit( array $data ) {
135 global $wgAuth;
136
137 if ( isset( $data['Domain'] ) ) {
138 if ( $wgAuth->validDomain( $data['Domain'] ) ) {
139 $wgAuth->setDomain( $data['Domain'] );
140 } else {
141 $wgAuth->setDomain( 'invaliddomain' );
142 }
143 }
144
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' );
149 }
150
151 /**
152 * @var $firstUser User
153 * @var $users User[]
154 */
155
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'] )
162 ) {
163 $method = 'email';
164 $res = wfGetDB( DB_SLAVE )->select(
165 'user',
166 User::selectFields(),
167 array( 'user_email' => $data['Email'] ),
168 __METHOD__
169 );
170
171 if ( $res ) {
172 $users = array();
173
174 foreach ( $res as $row ) {
175 $users[] = User::newFromRow( $row );
176 }
177 } else {
178 // Some sort of database error, probably unreachable
179 throw new MWException( 'Unknown database error in ' . __METHOD__ );
180 }
181 } else {
182 // The user didn't supply any data
183 return false;
184 }
185
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 );
190 }
191
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' );
198 }
199 }
200
201 $firstUser = $users[0];
202
203 if ( !$firstUser instanceof User || !$firstUser->getID() ) {
204 return array( array( 'nosuchuser', $data['Username'] ) );
205 }
206
207 // Check against the rate limiter
208 if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
209 throw new ThrottledError;
210 }
211
212 // Check against password throttle
213 foreach ( $users as $user ) {
214 if ( $user->isPasswordReminderThrottled() ) {
215 global $wgPasswordReminderResendTime;
216
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 ) );
223 }
224 }
225
226 global $wgNewPasswordExpiry;
227
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() ) );
232 }
233
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' );
239 }
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';
246
247 // Send in the user's language; which should hopefully be the same
248 $userLanguage = $firstUser->getOption( 'language' );
249
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
257 }
258 $passwordBlock = implode( "\n\n", $passwords );
259
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 )
267 );
268
269 $title = $this->msg( 'passwordreset-emailtitle' );
270
271 $this->result = $firstUser->sendMail( $title->escaped(), $this->email->text() );
272
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;
279 }
280
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() ) );
291 }
292 }
293
294 public function onSuccess() {
295 if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ) {
296 // @todo Logging
297
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() );
303 }
304
305 $this->getOutput()->addHTML( Html::rawElement( 'pre', array(), $this->email->escaped() ) );
306 }
307
308 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
309 $this->getOutput()->returnToMain();
310 }
311
312 protected function canChangePassword( User $user ) {
313 global $wgPasswordResetRoutes, $wgEnableEmail, $wgAuth;
314
315 // Maybe password resets are disabled, or there are no allowable routes
316 if ( !is_array( $wgPasswordResetRoutes ) ||
317 !in_array( true, array_values( $wgPasswordResetRoutes ) )
318 ) {
319 return 'passwordreset-disabled';
320 }
321
322 // Maybe the external auth plugin won't allow local password changes
323 if ( !$wgAuth->allowPasswordChange() ) {
324 return 'resetpass_forbidden';
325 }
326
327 // Maybe email features have been disabled
328 if ( !$wgEnableEmail ) {
329 return 'passwordreset-emaildisabled';
330 }
331
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';
336 }
337
338 return true;
339 }
340
341 /**
342 * Hide the password reset page if resets are disabled.
343 * @return Bool
344 */
345 function isListed() {
346 if ( $this->canChangePassword( $this->getUser() ) === true ) {
347 return parent::isListed();
348 }
349
350 return false;
351 }
352
353 protected function getGroupName() {
354 return 'users';
355 }
356 }