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