* (bug 1949) Profiling typo in rare error case
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * constructor
10 */
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
13 global $wgRequest;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('session.name')] ) ) {
15 User::SetupSession();
16 }
17
18 $form = new LoginForm( $wgRequest );
19 $form->execute();
20 }
21
22 /**
23 *
24 * @package MediaWiki
25 * @subpackage SpecialPage
26 */
27 class LoginForm {
28 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
29 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
30 var $mLoginattempt, $mRemember, $mEmail;
31
32 /**
33 * Constructor
34 * @param webrequest $request A webrequest object passed by reference
35 */
36 function LoginForm( &$request ) {
37 global $wgLang, $wgAllowRealName, $wgEnableEmail;
38 global $wgEmailAuthentication;
39
40 $this->mName = $request->getText( 'wpName' );
41 $this->mPassword = $request->getText( 'wpPassword' );
42 $this->mRetype = $request->getText( 'wpRetype' );
43 $this->mReturnto = $request->getVal( 'returnto' );
44 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
45 $this->mPosted = $request->wasPosted();
46 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
47 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
48 && $wgEnableEmail;
49 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
50 && $wgEnableEmail;
51 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
52 $this->mAction = $request->getVal( 'action' );
53 $this->mRemember = $request->getCheck( 'wpRemember' );
54
55 if( $wgEnableEmail ) {
56 $this->mEmail = $request->getText( 'wpEmail' );
57 } else {
58 $this->mEmail = '';
59 }
60 if( $wgAllowRealName ) {
61 $this->mRealName = $request->getText( 'wpRealName' );
62 } else {
63 $this->mRealName = '';
64 }
65
66 # When switching accounts, it sucks to get automatically logged out
67 if( $this->mReturnto == $wgLang->specialPage( 'Userlogout' ) ) {
68 $this->mReturnto = '';
69 }
70 }
71
72 function execute() {
73 if ( !is_null( $this->mCookieCheck ) ) {
74 $this->onCookieRedirectCheck( $this->mCookieCheck );
75 return;
76 } else if( $this->mPosted ) {
77 if( $this->mCreateaccount ) {
78 return $this->addNewAccount();
79 } else if ( $this->mCreateaccountMail ) {
80 return $this->addNewAccountMailPassword();
81 } else if ( $this->mMailmypassword ) {
82 return $this->mailPassword();
83 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
84 return $this->processLogin();
85 }
86 }
87 $this->mainLoginForm( '' );
88 }
89
90 /**
91 * @access private
92 */
93 function addNewAccountMailPassword() {
94 global $wgOut;
95 global $wgEmailAuthentication;
96
97 if ('' == $this->mEmail) {
98 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
99 return;
100 }
101
102 $u = $this->addNewaccountInternal();
103
104 if ($u == NULL) {
105 return;
106 }
107
108 $newadr = strtolower($this->mEmail);
109
110 # prepare for authentication and mail a temporary password to newadr
111 if ( !$u->isValidEmailAddr( $newadr ) ) {
112 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
113 }
114 $u->mEmail = $newadr; # new behaviour: set this new emailaddr from login-page into user database record
115 $u->mEmailAuthenticationtimestamp = 0; # but flag as "dirty" = unauthenticated
116
117 if ($wgEmailAuthentication) {
118 $error = $this->mailPasswordInternal( $u, true, $dummy ); # mail a temporary password to the dirty address
119 }
120
121 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
122 $wgOut->setRobotpolicy( 'noindex,nofollow' );
123 $wgOut->setArticleRelated( false );
124
125 if ($wgEmailAuthentication) {
126 if ($error === '') {
127 return $this->mainLoginForm( wfMsg( 'passwordsentforemailauthentication', $u->getName() ) );
128 } else {
129 return $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
130 }
131 # if user returns, that new email address gets authenticated in checkpassword()
132 }
133 # if ( $error === '' ) {
134 # $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
135 # $wgOut->returnToMain( false );
136 # } else {
137 # $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
138 # }
139 $u = 0;
140 }
141
142
143 /**
144 * @access private
145 */
146 function addNewAccount() {
147 global $wgUser, $wgOut;
148 global $wgEmailAuthentication;
149
150 $u = $this->addNewAccountInternal();
151
152 if ($u == NULL) {
153 return;
154 }
155
156 $newadr = strtolower($this->mEmail);
157 if ($newadr != '') { # prepare for authentication and mail a temporary password to newadr
158 if ( !$u->isValidEmailAddr( $newadr ) ) {
159 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
160 }
161 $u->mEmail = $newadr; # new behaviour: set this new emailaddr from login-page into user database record
162 $u->mEmailAuthenticationtimestamp = 0; # but flag as "dirty" = unauthenticated
163
164 if ($wgEmailAuthentication) {
165 # mail a temporary password to the dirty address
166
167 $error = $this->mailPasswordInternal( $u, true, $dummy );
168 if ($error === '') {
169 return $this->mainLoginForm( wfMsg( 'passwordsentforemailauthentication', $u->getName() ) );
170 } else {
171 return $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
172 }
173 # if user returns, that new email address gets authenticated in checkpassword()
174 }
175 }
176
177 $wgUser = $u;
178 $wgUser->setCookies();
179
180 $wgUser->saveSettings();
181
182 if( $this->hasSessionCookie() ) {
183 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
184 } else {
185 return $this->cookieRedirectCheck( 'new' );
186 }
187 }
188
189 /**
190 * @access private
191 */
192 function addNewAccountInternal() {
193 global $wgUser, $wgOut;
194 global $wgMaxNameChars;
195 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
196 global $wgMinimalPasswordLength;
197
198 if (!$wgUser->isAllowedToCreateAccount()) {
199 $this->userNotPrivilegedMessage();
200 return false;
201 }
202
203 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
204 $this->mainLoginForm( wfMsg( 'badretype' ) );
205 return false;
206 }
207
208 $name = trim( $this->mName );
209 $u = User::newFromName( $name );
210 if ( is_null( $u ) ||
211 ( '' == $name ) ||
212 $wgUser->isIP( $name ) ||
213 (strpos( $name, '/' ) !== false) ||
214 (strlen( $name ) > $wgMaxNameChars) ||
215 ucFirst($name) != $u->getName() )
216 {
217 $this->mainLoginForm( wfMsg( 'noname' ) );
218 return false;
219 }
220 if ( wfReadOnly() ) {
221 $wgOut->readOnlyPage();
222 return false;
223 }
224
225 if ( 0 != $u->idForName() ) {
226 $this->mainLoginForm( wfMsg( 'userexists' ) );
227 return false;
228 }
229
230 if ( strlen( $this->mPassword ) < $wgMinimalPasswordLength ) {
231 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
232 return false;
233 }
234
235 if ( $wgAccountCreationThrottle ) {
236 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
237 $value = $wgMemc->incr( $key );
238 if ( !$value ) {
239 $wgMemc->set( $key, 1, 86400 );
240 }
241 if ( $value > $wgAccountCreationThrottle ) {
242 $this->throttleHit( $wgAccountCreationThrottle );
243 return false;
244 }
245 }
246
247 return $this->initUser( $u );
248 }
249
250 /**
251 * Actually add a user to the database.
252 * Give it a User object that has been initialised with a name.
253 *
254 * @param User $u
255 * @return User
256 * @access private
257 */
258 function &initUser( &$u ) {
259 $u->addToDatabase();
260 $u->setPassword( $this->mPassword );
261 $u->setEmail( $this->mEmail );
262 $u->setRealName( $this->mRealName );
263 $u->setToken();
264
265 global $wgAuth;
266 $wgAuth->initUser( $u );
267
268 if ( $this->mRemember ) { $r = 1; }
269 else { $r = 0; }
270 $u->setOption( 'rememberpassword', $r );
271
272 return $u;
273 }
274
275 /**
276 * @access private
277 */
278 function processLogin() {
279 global $wgUser, $wgLang;
280 global $wgEmailAuthentication;
281
282 if ( '' == $this->mName ) {
283 $this->mainLoginForm( wfMsg( 'noname' ) );
284 return;
285 }
286 $u = User::newFromName( $this->mName );
287 if( is_null( $u ) ) {
288 $this->mainLoginForm( wfMsg( 'noname' ) );
289 return;
290 }
291 if ( 0 == $u->getID() ) {
292 global $wgAuth;
293 /**
294 * If the external authentication plugin allows it,
295 * automatically create a new account for users that
296 * are externally defined but have not yet logged in.
297 */
298 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
299 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
300 $u =& $this->initUser( $u );
301 } else {
302 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
303 return;
304 }
305 } else {
306 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
307 return;
308 }
309 } else {
310 $u->loadFromDatabase();
311 }
312
313 # store temporarily the status before the password check is performed
314 $mailmsg = '';
315 $oldadr = strtolower($u->getEmail());
316 $newadr = strtolower($this->mEmail);
317 $alreadyauthenticated = (( $u->mEmailAuthenticationtimestamp != 0 ) || ($oldadr == '')) ;
318
319 # checkPassword sets EmailAuthenticationtimestamp, if the newPassword is used
320
321 if (!$u->checkPassword( $this->mPassword )) {
322 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
323 return;
324 }
325
326 # We've verified now, update the real record
327 #
328 if ( $this->mRemember ) {
329 $r = 1;
330 } else {
331 $r = 0;
332 }
333 $u->setOption( 'rememberpassword', $r );
334
335 /* check if user with correct password has entered a new email address */
336 if (($newadr <> '') && ($newadr <> $oldadr)) { # the user supplied a new email address on the login page
337
338 # prepare for authentication and mail a temporary password to newadr
339 if ( !$u->isValidEmailAddr( $newadr ) ) {
340 return $this->mainLoginForm( wfMsg( 'invalidemailaddress', $error ) );
341 }
342 $u->mEmail = $newadr; # new behaviour: store this new emailaddr from login-page now into user database record ...
343 $u->mEmailAuthenticationtimestamp = 0; # ... but flag the address as "dirty" (unauthenticated)
344 $alreadyauthenticated = false;
345
346 if ($wgEmailAuthentication) {
347
348 # mail a temporary one-time password to the dirty address and return here to complete the user login
349 # if the user returns now or later using this temp. password, then the new email address $newadr
350 # - which is already stored in his user record - gets authenticated in checkpassword()
351
352 $error = $this->mailPasswordInternal( $u, false, $newpassword_temp);
353 $u->mNewpassword = $newpassword_temp;
354
355 # The temporary password is mailed. The user is logged-in as he entered his correct password
356 # This appears to be more intuitive than alternative 2.
357
358 if ($error === '') {
359 $mailmsg = '<br />' . wfMsg( 'passwordsentforemailauthentication', $u->getName() );
360 } else {
361 $mailmsg = '<br />' . wfMsg( 'mailerror', $error ) ;
362 }
363 }
364 }
365
366 $wgUser = $u;
367 $wgUser->setCookies();
368
369 # save all settings (incl. new email address and/or temporary password, if applicable)
370 $wgUser->saveSettings();
371
372 if ( !$wgEmailAuthentication || $alreadyauthenticated ) {
373 $authenticated = '';
374 $mailmsg = '';
375 } elseif ($u->mEmailAuthenticationtimestamp != 0) {
376 $authenticated = ' ' . wfMsg( 'emailauthenticated', $wgLang->timeanddate( $u->mEmailAuthenticationtimestamp, true ) );
377 } else {
378 $authenticated = ' ' . wfMsg( 'emailnotauthenticated' );
379 }
380
381 if( $this->hasSessionCookie() ) {
382 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) . $authenticated . $mailmsg );
383 } else {
384 return $this->cookieRedirectCheck( 'login' );
385 }
386 }
387
388 /**
389 * @access private
390 */
391 function mailPassword() {
392 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
393 global $wgCookiePath, $wgCookieDomain, $wgDBname;
394
395 if ( '' == $this->mName ) {
396 $this->mainLoginForm( wfMsg( 'noname' ) );
397 return;
398 }
399 $u = User::newFromName( $this->mName );
400 if( is_null( $u ) ) {
401 $this->mainLoginForm( wfMsg( 'noname' ) );
402 return;
403 }
404 if ( 0 == $u->getID() ) {
405 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
406 return;
407 }
408
409 $u->loadFromDatabase();
410
411 $error = $this->mailPasswordInternal( $u, true, $dummy );
412 if ($error === '') {
413 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
414 } else {
415 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
416 }
417 return;
418 }
419
420
421 /**
422 * @access private
423 */
424 function mailPasswordInternal( $u, $savesettings = true, &$newpassword_out ) {
425 global $wgPasswordSender, $wgDBname, $wgIP;
426 global $wgCookiePath, $wgCookieDomain;
427
428 if ( '' == $u->getEmail() ) {
429 return wfMsg( 'noemail', $u->getName() );
430 }
431
432 $np = $u->randomPassword();
433 $u->setNewpassword( $np );
434
435 # we want to store this new password together with other values in the calling function
436 $newpassword_out = $u->mNewpassword;
437
438 # WHY IS THIS HERE ? SHOULDN'T IT BE User::setcookie ???
439 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
440
441 if ($savesettings) {
442 $u->saveSettings();
443 }
444
445 $ip = $wgIP;
446 if ( '' == $ip ) { $ip = '(Unknown)'; }
447
448 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
449
450 require_once('UserMailer.php');
451 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindermailsubject' ), $m );
452
453 return htmlspecialchars( $error );
454 }
455
456
457 /**
458 * @param string $msg Message that will be shown on success.
459 * @access private
460 */
461 function successfulLogin( $msg ) {
462 global $wgUser;
463 global $wgOut;
464
465 # Run any hooks; ignore results
466
467 wfRunHooks('UserLoginComplete', array(&$wgUser));
468
469 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
470 $wgOut->setRobotpolicy( 'noindex,nofollow' );
471 $wgOut->setArticleRelated( false );
472 $wgOut->addWikiText( $msg );
473 $wgOut->returnToMain();
474 }
475
476 /** */
477 function userNotPrivilegedMessage() {
478 global $wgOut;
479
480 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
481 $wgOut->setRobotpolicy( 'noindex,nofollow' );
482 $wgOut->setArticleRelated( false );
483
484 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
485
486 $wgOut->returnToMain( false );
487 }
488
489 /**
490 * @access private
491 */
492 function mainLoginForm( $err ) {
493 global $wgUser, $wgOut, $wgLang;
494 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
495 global $wgEmailAuthentication;
496
497 if ( '' == $this->mName ) {
498 if ( $wgUser->isLoggedIn() ) {
499 $this->mName = $wgUser->getName();
500 } else {
501 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
502 }
503 }
504
505 $q = 'action=submitlogin';
506 if ( !empty( $this->mReturnto ) ) {
507 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
508 }
509 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
510
511 require_once( 'templates/Userlogin.php' );
512 $template =& new UserloginTemplate();
513
514 $template->set( 'name', $this->mName );
515 $template->set( 'password', $this->mPassword );
516 $template->set( 'retype', $this->mRetype );
517 $template->set( 'email', $this->mEmail );
518 $template->set( 'realname', $this->mRealName );
519
520 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
521 $template->set( 'error', $err );
522 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
523 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
524 $template->set( 'userealname', $wgAllowRealName );
525 $template->set( 'useemail', $wgEnableEmail );
526 $template->set( 'useemailauthent', $wgEmailAuthentication );
527 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
528
529 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
530 $wgOut->setRobotpolicy( 'noindex,nofollow' );
531 $wgOut->setArticleRelated( false );
532 $wgOut->addTemplate( $template );
533 }
534
535 /**
536 * @access private
537 */
538 function hasSessionCookie() {
539 global $wgDisableCookieCheck;
540 return ( $wgDisableCookieCheck ) ? true : ( '' != $_COOKIE[session_name()] );
541 }
542
543 /**
544 * @access private
545 */
546 function cookieRedirectCheck( $type ) {
547 global $wgOut, $wgLang;
548
549 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
550 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
551
552 return $wgOut->redirect( $check );
553 }
554
555 /**
556 * @access private
557 */
558 function onCookieRedirectCheck( $type ) {
559 global $wgUser;
560
561 if ( !$this->hasSessionCookie() ) {
562 if ( $type == 'new' ) {
563 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
564 } else if ( $type == 'login' ) {
565 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
566 } else {
567 # shouldn't happen
568 return $this->mainLoginForm( wfMsg( 'error' ) );
569 }
570 } else {
571 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
572 }
573 }
574
575 /**
576 * @access private
577 */
578 function throttleHit( $limit ) {
579 global $wgOut;
580
581 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
582 }
583 }
584 ?>