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