Display "Printable version" links in toolbox on special pages
[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 /**
32 * @var Message
33 */
34 private $email;
35
36 /**
37 * @var User
38 */
39 private $firstUser;
40
41 /**
42 * @var Status
43 */
44 private $result;
45
46 public function __construct() {
47 parent::__construct( 'PasswordReset' );
48 }
49
50 public function userCanExecute( User $user ) {
51 return $this->canChangePassword( $user ) === true && parent::userCanExecute( $user );
52 }
53
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' );
60 }
61
62 return parent::checkExecutePermissions( $user );
63 }
64
65 protected function getFormFields() {
66 global $wgPasswordResetRoutes, $wgAuth;
67 $a = array();
68 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
69 $a['Username'] = array(
70 'type' => 'text',
71 'label-message' => 'passwordreset-username',
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 $form->setSubmitTextMsg( 'mailmypassword' );
107 }
108
109 protected function preText() {
110 global $wgPasswordResetRoutes;
111 $i = 0;
112 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
113 $i++;
114 }
115 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
116 $i++;
117 }
118 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
119 $i++;
120 }
121 return $this->msg( 'passwordreset-pretext', $i )->parseAsBlock();
122 }
123
124 /**
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.
128 * @param $data array
129 * @throws MWException
130 * @throws ThrottledError|PermissionsError
131 * @return Bool|Array
132 */
133 public function onSubmit( array $data ) {
134 global $wgAuth;
135
136 if ( isset( $data['Domain'] ) ) {
137 if ( $wgAuth->validDomain( $data['Domain'] ) ) {
138 $wgAuth->setDomain( $data['Domain'] );
139 } else {
140 $wgAuth->setDomain( 'invaliddomain' );
141 }
142 }
143
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' );
147 }
148
149 /**
150 * @var $firstUser User
151 * @var $users User[]
152 */
153
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'] ) )
160 {
161 $method = 'email';
162 $res = wfGetDB( DB_SLAVE )->select(
163 'user',
164 User::selectFields(),
165 array( 'user_email' => $data['Email'] ),
166 __METHOD__
167 );
168 if ( $res ) {
169 $users = array();
170 foreach ( $res as $row ) {
171 $users[] = User::newFromRow( $row );
172 }
173 } else {
174 // Some sort of database error, probably unreachable
175 throw new MWException( 'Unknown database error in ' . __METHOD__ );
176 }
177 } else {
178 // The user didn't supply any data
179 return false;
180 }
181
182 // Check for hooks (captcha etc), and allow them to modify the users list
183 $error = array();
184 if ( !wfRunHooks( 'SpecialPasswordResetOnSubmit', array( &$users, $data, &$error ) ) ) {
185 return array( $error );
186 }
187
188 if ( count( $users ) == 0 ) {
189 if ( $method == 'email' ) {
190 // Don't reveal whether or not an email address is in use
191 return true;
192 } else {
193 return array( 'noname' );
194 }
195 }
196
197 $firstUser = $users[0];
198
199 if ( !$firstUser instanceof User || !$firstUser->getID() ) {
200 return array( array( 'nosuchuser', $data['Username'] ) );
201 }
202
203 // Check against the rate limiter
204 if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
205 throw new ThrottledError;
206 }
207
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 ) ) );
215 }
216 }
217
218 global $wgNewPasswordExpiry;
219
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() ) );
224 }
225
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();
229 if ( !$ip ) {
230 return array( 'badipaddress' );
231 }
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';
238
239 // Send in the user's language; which should hopefully be the same
240 $userLanguage = $firstUser->getOption( 'language' );
241
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
249 }
250 $passwordBlock = implode( "\n\n", $passwords );
251
252 $this->email = $this->msg( $msg )->inLanguage( $userLanguage );
253 $this->email->params(
254 $username,
255 $passwordBlock,
256 count( $passwords ),
257 '<' . Title::newMainPage()->getCanonicalURL() . '>',
258 round( $wgNewPasswordExpiry / 86400 )
259 );
260
261 $title = $this->msg( 'passwordreset-emailtitle' );
262
263 $this->result = $firstUser->sendMail( $title->escaped(), $this->email->text() );
264
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;
268 } else {
269 // Blank the email if the user is not supposed to see it
270 $this->email = null;
271 }
272
273 if ( $this->result->isGood() ) {
274 return true;
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
277 return true;
278 } else {
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() ) );
282 }
283 }
284
285 public function onSuccess() {
286 if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ) {
287 // @todo: Logging
288
289 if ( $this->result->isGood() ) {
290 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
291 } else {
292 $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture',
293 $this->result->getMessage(), $this->firstUser->getName() );
294 }
295
296 $this->getOutput()->addHTML( Html::rawElement( 'pre', array(), $this->email->escaped() ) );
297 }
298
299 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
300 $this->getOutput()->returnToMain();
301 }
302
303 protected function canChangePassword( User $user ) {
304 global $wgPasswordResetRoutes, $wgEnableEmail, $wgAuth;
305
306 // Maybe password resets are disabled, or there are no allowable routes
307 if ( !is_array( $wgPasswordResetRoutes ) ||
308 !in_array( true, array_values( $wgPasswordResetRoutes ) ) )
309 {
310 return 'passwordreset-disabled';
311 }
312
313 // Maybe the external auth plugin won't allow local password changes
314 if ( !$wgAuth->allowPasswordChange() ) {
315 return 'resetpass_forbidden';
316 }
317
318 // Maybe email features have been disabled
319 if ( !$wgEnableEmail ) {
320 return 'passwordreset-emaildisabled';
321 }
322
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';
327 }
328
329 return true;
330 }
331
332 /**
333 * Hide the password reset page if resets are disabled.
334 * @return Bool
335 */
336 function isListed() {
337 if ( $this->canChangePassword( $this->getUser() ) === true ) {
338 return parent::isListed();
339 }
340
341 return false;
342 }
343
344 protected function getGroupName() {
345 return 'users';
346 }
347 }