summaryrefslogtreecommitdiff
path: root/includes/specials/SpecialPasswordReset.php
blob: d9faaccafb7c951c68fee0829b5483080ba2f3bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/**
 * Implements Special:PasswordReset
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup SpecialPage
 */

/**
 * Special page for requesting a password reset email
 *
 * @ingroup SpecialPage
 */
class SpecialPasswordReset extends FormSpecialPage {
	/**
	 * @var Message
	 */
	private $email;

	/**
	 * @var User
	 */
	private $firstUser;

	/**
	 * @var Status
	 */
	private $result;

	public function __construct() {
		parent::__construct( 'PasswordReset', 'editmyprivateinfo' );
	}

	public function userCanExecute( User $user ) {
		return $this->canChangePassword( $user ) === true && parent::userCanExecute( $user );
	}

	public function checkExecutePermissions( User $user ) {
		$error = $this->canChangePassword( $user );
		if ( is_string( $error ) ) {
			throw new ErrorPageError( 'internalerror', $error );
		} elseif ( !$error ) {
			throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
		}

		return parent::checkExecutePermissions( $user );
	}

	protected function getFormFields() {
		global $wgPasswordResetRoutes, $wgAuth;
		$a = array();
		if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
			$a['Username'] = array(
				'type' => 'text',
				'label-message' => 'passwordreset-username',
			);

			if ( $this->getUser()->isLoggedIn() ) {
				$a['Username']['default'] = $this->getUser()->getName();
			}
		}

		if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
			$a['Email'] = array(
				'type' => 'email',
				'label-message' => 'passwordreset-email',
			);
		}

		if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
			$domains = $wgAuth->domainList();
			$a['Domain'] = array(
				'type' => 'select',
				'options' => $domains,
				'label-message' => 'passwordreset-domain',
			);
		}

		if ( $this->getUser()->isAllowed( 'passwordreset' ) ) {
			$a['Capture'] = array(
				'type' => 'check',
				'label-message' => 'passwordreset-capture',
				'help-message' => 'passwordreset-capture-help',
			);
		}

		return $a;
	}

	public function alterForm( HTMLForm $form ) {
		global $wgPasswordResetRoutes;

		$form->setDisplayFormat( 'vform' );
		// Turn the old-school line around the form off.
		// XXX This wouldn't be necessary here if we could set the format of
		// the HTMLForm to 'vform' at its creation, but there's no way to do so
		// from a FormSpecialPage class.
		$form->setWrapperLegend( false );

		$i = 0;
		if ( isset( $wgPasswordResetRoutes['username'] ) && $wgPasswordResetRoutes['username'] ) {
			$i++;
		}
		if ( isset( $wgPasswordResetRoutes['email'] ) && $wgPasswordResetRoutes['email'] ) {
			$i++;
		}
		if ( isset( $wgPasswordResetRoutes['domain'] ) && $wgPasswordResetRoutes['domain'] ) {
			$i++;
		}

		$message = ( $i > 1 ) ? 'passwordreset-text-many' : 'passwordreset-text-one';

		$form->setHeaderText( $this->msg( $message, $i )->parseAsBlock() );
		$form->setSubmitTextMsg( 'mailmypassword' );
	}

	/**
	 * Process the form.  At this point we know that the user passes all the criteria in
	 * userCanExecute(), and if the data array contains 'Username', etc, then Username
	 * resets are allowed.
	 * @param $data array
	 * @throws MWException
	 * @throws ThrottledError|PermissionsError
	 * @return Bool|Array
	 */
	public function onSubmit( array $data ) {
		global $wgAuth;

		if ( isset( $data['Domain'] ) ) {
			if ( $wgAuth->validDomain( $data['Domain'] ) ) {
				$wgAuth->setDomain( $data['Domain'] );
			} else {
				$wgAuth->setDomain( 'invaliddomain' );
			}
		}

		if ( isset( $data['Capture'] ) && !$this->getUser()->isAllowed( 'passwordreset' ) ) {
			// The user knows they don't have the passwordreset permission,
			// but they tried to spoof the form. That's naughty
			throw new PermissionsError( 'passwordreset' );
		}

		/**
		 * @var $firstUser User
		 * @var $users User[]
		 */

		if ( isset( $data['Username'] ) && $data['Username'] !== '' ) {
			$method = 'username';
			$users = array( User::newFromName( $data['Username'] ) );
		} elseif ( isset( $data['Email'] )
			&& $data['Email'] !== ''
			&& Sanitizer::validateEmail( $data['Email'] )
		) {
			$method = 'email';
			$res = wfGetDB( DB_SLAVE )->select(
				'user',
				User::selectFields(),
				array( 'user_email' => $data['Email'] ),
				__METHOD__
			);

			if ( $res ) {
				$users = array();

				foreach ( $res as $row ) {
					$users[] = User::newFromRow( $row );
				}
			} else {
				// Some sort of database error, probably unreachable
				throw new MWException( 'Unknown database error in ' . __METHOD__ );
			}
		} else {
			// The user didn't supply any data
			return false;
		}

		// Check for hooks (captcha etc), and allow them to modify the users list
		$error = array();
		if ( !wfRunHooks( 'SpecialPasswordResetOnSubmit', array( &$users, $data, &$error ) ) ) {
			return array( $error );
		}

		if ( count( $users ) == 0 ) {
			if ( $method == 'email' ) {
				// Don't reveal whether or not an email address is in use
				return true;
			} else {
				return array( 'noname' );
			}
		}

		$firstUser = $users[0];

		if ( !$firstUser instanceof User || !$firstUser->getID() ) {
			// Don't parse username as wikitext (bug 65501)
			return array( array( 'nosuchuser', wfEscapeWikiText( $data['Username'] ) ) );
		}

		// Check against the rate limiter
		if ( $this->getUser()->pingLimiter( 'mailpassword' ) ) {
			throw new ThrottledError;
		}

		// Check against password throttle
		foreach ( $users as $user ) {
			if ( $user->isPasswordReminderThrottled() ) {
				global $wgPasswordReminderResendTime;

				# Round the time in hours to 3 d.p., in case someone is specifying
				# minutes or seconds.
				return array( array(
					'throttled-mailpassword',
					round( $wgPasswordReminderResendTime, 3 )
				) );
			}
		}

		global $wgNewPasswordExpiry;

		// All the users will have the same email address
		if ( $firstUser->getEmail() == '' ) {
			// This won't be reachable from the email route, so safe to expose the username
			return array( array( 'noemail', wfEscapeWikiText( $firstUser->getName() ) ) );
		}

		// We need to have a valid IP address for the hook, but per bug 18347, we should
		// send the user's name if they're logged in.
		$ip = $this->getRequest()->getIP();
		if ( !$ip ) {
			return array( 'badipaddress' );
		}
		$caller = $this->getUser();
		wfRunHooks( 'User::mailPasswordInternal', array( &$caller, &$ip, &$firstUser ) );
		$username = $caller->getName();
		$msg = IP::isValid( $username )
			? 'passwordreset-emailtext-ip'
			: 'passwordreset-emailtext-user';

		// Send in the user's language; which should hopefully be the same
		$userLanguage = $firstUser->getOption( 'language' );

		$passwords = array();
		foreach ( $users as $user ) {
			$password = $user->randomPassword();
			$user->setNewpassword( $password );
			$user->saveSettings();
			$passwords[] = $this->msg( 'passwordreset-emailelement', $user->getName(), $password )
				->inLanguage( $userLanguage )->text(); // We'll escape the whole thing later
		}
		$passwordBlock = implode( "\n\n", $passwords );

		$this->email = $this->msg( $msg )->inLanguage( $userLanguage );
		$this->email->params(
			$username,
			$passwordBlock,
			count( $passwords ),
			'<' . Title::newMainPage()->getCanonicalURL() . '>',
			round( $wgNewPasswordExpiry / 86400 )
		);

		$title = $this->msg( 'passwordreset-emailtitle' );

		$this->result = $firstUser->sendMail( $title->escaped(), $this->email->text() );

		if ( isset( $data['Capture'] ) && $data['Capture'] ) {
			// Save the user, will be used if an error occurs when sending the email
			$this->firstUser = $firstUser;
		} else {
			// Blank the email if the user is not supposed to see it
			$this->email = null;
		}

		if ( $this->result->isGood() ) {
			return true;
		} elseif ( isset( $data['Capture'] ) && $data['Capture'] ) {
			// The email didn't send, but maybe they knew that and that's why they captured it
			return true;
		} else {
			// @todo FIXME: The email wasn't sent, but we have already set
			// the password throttle timestamp, so they won't be able to try
			// again until it expires...  :(
			return array( array( 'mailerror', $this->result->getMessage() ) );
		}
	}

	public function onSuccess() {
		if ( $this->getUser()->isAllowed( 'passwordreset' ) && $this->email != null ) {
			// @todo Logging

			if ( $this->result->isGood() ) {
				$this->getOutput()->addWikiMsg( 'passwordreset-emailsent-capture' );
			} else {
				$this->getOutput()->addWikiMsg( 'passwordreset-emailerror-capture',
					$this->result->getMessage(), $this->firstUser->getName() );
			}

			$this->getOutput()->addHTML( Html::rawElement( 'pre', array(), $this->email->escaped() ) );
		}

		$this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
		$this->getOutput()->returnToMain();
	}

	protected function canChangePassword( User $user ) {
		global $wgPasswordResetRoutes, $wgEnableEmail, $wgAuth;

		// Maybe password resets are disabled, or there are no allowable routes
		if ( !is_array( $wgPasswordResetRoutes ) ||
			!in_array( true, array_values( $wgPasswordResetRoutes ) )
		) {
			return 'passwordreset-disabled';
		}

		// Maybe the external auth plugin won't allow local password changes
		if ( !$wgAuth->allowPasswordChange() ) {
			return 'resetpass_forbidden';
		}

		// Maybe email features have been disabled
		if ( !$wgEnableEmail ) {
			return 'passwordreset-emaildisabled';
		}

		// Maybe the user is blocked (check this here rather than relying on the parent
		// method as we have a more specific error message to use here
		if ( $user->isBlocked() ) {
			return 'blocked-mailpassword';
		}

		return true;
	}

	/**
	 * Hide the password reset page if resets are disabled.
	 * @return Bool
	 */
	function isListed() {
		if ( $this->canChangePassword( $this->getUser() ) === true ) {
			return parent::isListed();
		}

		return false;
	}

	protected function getGroupName() {
		return 'users';
	}
}