Merge "Fix test for view language for non-wikitext in main NS."
[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 Status
38 */
39 private $result;
40
41 public function __construct() {
42 parent::__construct( 'PasswordReset' );
43 }
44
45 public function userCanExecute( User $user ) {
46 return $this->canChangePassword( $user ) === true && parent::userCanExecute( $user );
47 }
48
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' );
55 }
56
57 return parent::checkExecutePermissions( $user );
58 }
59
60 protected function getFormFields() {
61 global $wgPasswordResetRoutes, $wgAuth;
62 $a = array();
63 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
64 $a['Username'] = array(
65 'type' => 'text',
66 'label-message' => 'passwordreset-username',
67 );
68 if( $this->getUser()->isLoggedIn() ) {
69 $a['Username']['default'] = $this->getUser()->getName();
70 }
71 }
72
73 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
74 $a['Email'] = array(
75 'type' => 'email',
76 'label-message' => 'passwordreset-email',
77 );
78 }
79
80 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
81 $domains = $wgAuth->domainList();
82 $a['Domain'] = array(
83 'type' => 'select',
84 'options' => $domains,
85 'label-message' => 'passwordreset-domain',
86 );
87 }
88
89 if( $this->getUser()->isAllowed( 'passwordreset' ) ){
90 $a['Capture'] = array(
91 'type' => 'check',
92 'label-message' => 'passwordreset-capture',
93 'help-message' => 'passwordreset-capture-help',
94 );
95 }
96
97 return $a;
98 }
99
100 public function alterForm( HTMLForm $form ) {
101 $form->setSubmitTextMsg( 'mailmypassword' );
102 }
103
104 protected function preText() {
105 global $wgPasswordResetRoutes;
106 $i = 0;
107 if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
108 $i++;
109 }
110 if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
111 $i++;
112 }
113 if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
114 $i++;
115 }
116 return $this->msg( 'passwordreset-pretext', $i )->parseAsBlock();
117 }
118
119 /**
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.
123 * @param $data array
124 * @throws MWException
125 * @throws ThrottledError|PermissionsError
126 * @return Bool|Array
127 */
128 public function onSubmit( array $data ) {
129 global $wgAuth;
130
131 if ( isset( $data['Domain'] ) ) {
132 if ( $wgAuth->validDomain( $data['Domain'] ) ) {
133 $wgAuth->setDomain( $data['Domain'] );
134 } else {
135 $wgAuth->setDomain( 'invaliddomain' );
136 }
137 }
138
139 if( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ){
140 // The user knows they don't have the passwordreset permission, but they tried to spoof the form. That's naughty
141 throw new PermissionsError( 'passwordreset' );
142 }
143
144 /**
145 * @var $firstUser User
146 * @var $users User[]
147 */
148
149 if ( isset( $data['Username'] ) && $data['Username'] !== '' ) {
150 $method = 'username';
151 $users = array( User::newFromName( $data['Username'] ) );
152 } elseif ( isset( $data['Email'] )
153 && $data['Email'] !== ''
154 && Sanitizer::validateEmail( $data['Email'] ) )
155 {
156 $method = 'email';
157 $res = wfGetDB( DB_SLAVE )->select(
158 'user',
159 User::selectFields(),
160 array( 'user_email' => $data['Email'] ),
161 __METHOD__
162 );
163 if ( $res ) {
164 $users = array();
165 foreach( $res as $row ){
166 $users[] = User::newFromRow( $row );
167 }
168 } else {
169 // Some sort of database error, probably unreachable
170 throw new MWException( 'Unknown database error in ' . __METHOD__ );
171 }
172 } else {
173 // The user didn't supply any data
174 return false;
175 }
176
177 // Check for hooks (captcha etc), and allow them to modify the users list
178 $error = array();
179 if ( !wfRunHooks( 'SpecialPasswordResetOnSubmit', array( &$users, $data, &$error ) ) ) {
180 return array( $error );
181 }
182
183 if( count( $users ) == 0 ){
184 if( $method == 'email' ){
185 // Don't reveal whether or not an email address is in use
186 return true;
187 } else {
188 return array( 'noname' );
189 }
190 }
191
192 $firstUser = $users[0];
193
194 if ( !$firstUser instanceof User || !$firstUser->getID() ) {
195 return array( array( 'nosuchuser', $data['Username'] ) );
196 }
197
198 // Check against the rate limiter
199 if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
200 throw new ThrottledError;
201 }
202
203 // Check against password throttle
204 foreach ( $users as $user ) {
205 if ( $user->isPasswordReminderThrottled() ) {
206 global $wgPasswordReminderResendTime;
207 # Round the time in hours to 3 d.p., in case someone is specifying
208 # minutes or seconds.
209 return array( array( 'throttled-mailpassword', round( $wgPasswordReminderResendTime, 3 ) ) );
210 }
211 }
212
213 global $wgNewPasswordExpiry;
214
215 // All the users will have the same email address
216 if ( $firstUser->getEmail() == '' ) {
217 // This won't be reachable from the email route, so safe to expose the username
218 return array( array( 'noemail', $firstUser->getName() ) );
219 }
220
221 // We need to have a valid IP address for the hook, but per bug 18347, we should
222 // send the user's name if they're logged in.
223 $ip = $this->getRequest()->getIP();
224 if ( !$ip ) {
225 return array( 'badipaddress' );
226 }
227 $caller = $this->getUser();
228 wfRunHooks( 'User::mailPasswordInternal', array( &$caller, &$ip, &$firstUser ) );
229 $username = $caller->getName();
230 $msg = IP::isValid( $username )
231 ? 'passwordreset-emailtext-ip'
232 : 'passwordreset-emailtext-user';
233
234 // Send in the user's language; which should hopefully be the same
235 $userLanguage = $firstUser->getOption( 'language' );
236
237 $passwords = array();
238 foreach ( $users as $user ) {
239 $password = $user->randomPassword();
240 $user->setNewpassword( $password );
241 $user->saveSettings();
242 $passwords[] = $this->msg( 'passwordreset-emailelement', $user->getName(), $password
243 )->inLanguage( $userLanguage )->text(); // We'll escape the whole thing later
244 }
245 $passwordBlock = implode( "\n\n", $passwords );
246
247 $this->email = $this->msg( $msg )->inLanguage( $userLanguage );
248 $this->email->params(
249 $username,
250 $passwordBlock,
251 count( $passwords ),
252 '<' . Title::newMainPage()->getCanonicalUrl() . '>',
253 round( $wgNewPasswordExpiry / 86400 )
254 );
255
256 $title = $this->msg( 'passwordreset-emailtitle' );
257
258 $this->result = $firstUser->sendMail( $title->escaped(), $this->email->text() );
259
260 // Blank the email if the user is not supposed to see it
261 if( !isset( $data['Capture'] ) || !$data['Capture'] ) {
262 $this->email = null;
263 }
264
265 if ( $this->result->isGood() ) {
266 return true;
267 } elseif( isset( $data['Capture'] ) && $data['Capture'] ){
268 // The email didn't send, but maybe they knew that and that's why they captured it
269 return true;
270 } else {
271 // @todo FIXME: The email didn't send, but we have already set the password throttle
272 // timestamp, so they won't be able to try again until it expires... :(
273 return array( array( 'mailerror', $this->result->getMessage() ) );
274 }
275 }
276
277 public function onSuccess() {
278 if( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ){
279 // @todo: Logging
280
281 if( $this->result->isGood() ){
282 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
283 } else {
284 $this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture', $this->result->getMessage() );
285 }
286
287 $this->getOutput()->addHTML( Html::rawElement( 'pre', array(), $this->email->escaped() ) );
288 }
289
290 $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
291 $this->getOutput()->returnToMain();
292 }
293
294 protected function canChangePassword( User $user ) {
295 global $wgPasswordResetRoutes, $wgAuth;
296
297 // Maybe password resets are disabled, or there are no allowable routes
298 if ( !is_array( $wgPasswordResetRoutes ) ||
299 !in_array( true, array_values( $wgPasswordResetRoutes ) ) )
300 {
301 return 'passwordreset-disabled';
302 }
303
304 // Maybe the external auth plugin won't allow local password changes
305 if ( !$wgAuth->allowPasswordChange() ) {
306 return 'resetpass_forbidden';
307 }
308
309 // Maybe the user is blocked (check this here rather than relying on the parent
310 // method as we have a more specific error message to use here
311 if ( $user->isBlocked() ) {
312 return 'blocked-mailpassword';
313 }
314
315 return true;
316 }
317
318 /**
319 * Hide the password reset page if resets are disabled.
320 * @return Bool
321 */
322 function isListed() {
323 if ( $this->canChangePassword( $this->getUser() ) === true ) {
324 return parent::isListed();
325 }
326
327 return false;
328 }
329 }