Stop profiling before ending the function
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * constructor
9 */
10 function wfSpecialUserlogin( $par = '' ) {
11 global $wgRequest;
12 if( session_id() == '' ) {
13 wfSetupSession();
14 }
15
16 $form = new LoginForm( $wgRequest, $par );
17 $form->execute();
18 }
19
20 /**
21 * implements Special:Login
22 * @addtogroup SpecialPage
23 */
24 class LoginForm {
25
26 const SUCCESS = 0;
27 const NO_NAME = 1;
28 const ILLEGAL = 2;
29 const WRONG_PLUGIN_PASS = 3;
30 const NOT_EXISTS = 4;
31 const WRONG_PASS = 5;
32 const EMPTY_PASS = 6;
33 const RESET_PASS = 7;
34 const ABORTED = 8;
35
36 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
37 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
38 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
39
40 /**
41 * Constructor
42 * @param WebRequest $request A WebRequest object passed by reference
43 */
44 function LoginForm( &$request, $par = '' ) {
45 global $wgLang, $wgAllowRealName, $wgEnableEmail;
46 global $wgAuth;
47
48 $this->mType = ( $par == 'signup' ) ? $par : $request->getText( 'type' ); # Check for [[Special:Userlogin/signup]]
49 $this->mName = $request->getText( 'wpName' );
50 $this->mPassword = $request->getText( 'wpPassword' );
51 $this->mRetype = $request->getText( 'wpRetype' );
52 $this->mDomain = $request->getText( 'wpDomain' );
53 $this->mReturnTo = $request->getVal( 'returnto' );
54 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
55 $this->mPosted = $request->wasPosted();
56 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
57 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
58 && $wgEnableEmail;
59 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
60 && $wgEnableEmail;
61 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
62 $this->mAction = $request->getVal( 'action' );
63 $this->mRemember = $request->getCheck( 'wpRemember' );
64 $this->mLanguage = $request->getText( 'uselang' );
65
66 if( $wgEnableEmail ) {
67 $this->mEmail = $request->getText( 'wpEmail' );
68 } else {
69 $this->mEmail = '';
70 }
71 if( $wgAllowRealName ) {
72 $this->mRealName = $request->getText( 'wpRealName' );
73 } else {
74 $this->mRealName = '';
75 }
76
77 if( !$wgAuth->validDomain( $this->mDomain ) ) {
78 $this->mDomain = 'invaliddomain';
79 }
80 $wgAuth->setDomain( $this->mDomain );
81
82 # When switching accounts, it sucks to get automatically logged out
83 if( $this->mReturnTo == $wgLang->specialPage( 'Userlogout' ) ) {
84 $this->mReturnTo = '';
85 }
86 }
87
88 function execute() {
89 if ( !is_null( $this->mCookieCheck ) ) {
90 $this->onCookieRedirectCheck( $this->mCookieCheck );
91 return;
92 } else if( $this->mPosted ) {
93 if( $this->mCreateaccount ) {
94 return $this->addNewAccount();
95 } else if ( $this->mCreateaccountMail ) {
96 return $this->addNewAccountMailPassword();
97 } else if ( $this->mMailmypassword ) {
98 return $this->mailPassword();
99 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
100 return $this->processLogin();
101 }
102 }
103 $this->mainLoginForm( '' );
104 }
105
106 /**
107 * @private
108 */
109 function addNewAccountMailPassword() {
110 global $wgOut;
111
112 if ('' == $this->mEmail) {
113 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
114 return;
115 }
116
117 $u = $this->addNewaccountInternal();
118
119 if ($u == NULL) {
120 return;
121 }
122
123 // Wipe the initial password and mail a temporary one
124 $u->setPassword( null );
125 $u->saveSettings();
126 $result = $this->mailPasswordInternal( $u, false, 'createaccount-title', 'createaccount-text' );
127
128 wfRunHooks( 'AddNewAccount', array( $u, true ) );
129
130 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
131 $wgOut->setRobotpolicy( 'noindex,nofollow' );
132 $wgOut->setArticleRelated( false );
133
134 if( WikiError::isError( $result ) ) {
135 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
136 } else {
137 $wgOut->addWikiMsg( 'accmailtext', $u->getName(), $u->getEmail() );
138 $wgOut->returnToMain( false );
139 }
140 $u = 0;
141 }
142
143
144 /**
145 * @private
146 */
147 function addNewAccount() {
148 global $wgUser, $wgEmailAuthentication;
149
150 # Create the account and abort if there's a problem doing so
151 $u = $this->addNewAccountInternal();
152 if( $u == NULL )
153 return;
154
155 # If we showed up language selection links, and one was in use, be
156 # smart (and sensible) and save that language as the user's preference
157 global $wgLoginLanguageSelector;
158 if( $wgLoginLanguageSelector && $this->mLanguage )
159 $u->setOption( 'language', $this->mLanguage );
160
161 # Send out an email authentication message if needed
162 if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) ) {
163 global $wgOut;
164 $error = $u->sendConfirmationMail();
165 if( WikiError::isError( $error ) ) {
166 $wgOut->addWikiMsg( 'confirmemail_sendfailed', $error->getMessage() );
167 } else {
168 $wgOut->addWikiMsg( 'confirmemail_oncreate' );
169 }
170 }
171
172 # Save settings (including confirmation token)
173 $u->saveSettings();
174
175 # If not logged in, assume the new account as the current one and set session cookies
176 # then show a "welcome" message or a "need cookies" message as needed
177 if( $wgUser->isAnon() ) {
178 $wgUser = $u;
179 $wgUser->setCookies();
180 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
181 if( $this->hasSessionCookie() ) {
182 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
183 } else {
184 return $this->cookieRedirectCheck( 'new' );
185 }
186 } else {
187 # Confirm that the account was created
188 global $wgOut;
189 $self = SpecialPage::getTitleFor( 'Userlogin' );
190 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
191 $wgOut->setArticleRelated( false );
192 $wgOut->setRobotPolicy( 'noindex,nofollow' );
193 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
194 $wgOut->returnToMain( false, $self );
195 wfRunHooks( 'AddNewAccount', array( $u ) );
196 return true;
197 }
198 }
199
200 /**
201 * @private
202 */
203 function addNewAccountInternal() {
204 global $wgUser, $wgOut;
205 global $wgEnableSorbs, $wgProxyWhitelist;
206 global $wgMemc, $wgAccountCreationThrottle;
207 global $wgAuth, $wgMinimalPasswordLength;
208 global $wgEmailConfirmToEdit;
209
210 // If the user passes an invalid domain, something is fishy
211 if( !$wgAuth->validDomain( $this->mDomain ) ) {
212 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
213 return false;
214 }
215
216 // If we are not allowing users to login locally, we should
217 // be checking to see if the user is actually able to
218 // authenticate to the authentication server before they
219 // create an account (otherwise, they can create a local account
220 // and login as any domain user). We only need to check this for
221 // domains that aren't local.
222 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
223 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
224 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
225 return false;
226 }
227 }
228
229 if ( wfReadOnly() ) {
230 $wgOut->readOnlyPage();
231 return false;
232 }
233
234 # Check permissions
235 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
236 $this->userNotPrivilegedMessage();
237 return false;
238 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
239 $this->userBlockedMessage();
240 return false;
241 }
242
243 $ip = wfGetIP();
244 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
245 $wgUser->inSorbsBlacklist( $ip ) )
246 {
247 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
248 return;
249 }
250
251 # Now create a dummy user ($u) and check if it is valid
252 $name = trim( $this->mName );
253 $u = User::newFromName( $name, 'creatable' );
254 if ( is_null( $u ) ) {
255 $this->mainLoginForm( wfMsg( 'noname' ) );
256 return false;
257 }
258
259 if ( 0 != $u->idForName() ) {
260 $this->mainLoginForm( wfMsg( 'userexists' ) );
261 return false;
262 }
263
264 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
265 $this->mainLoginForm( wfMsg( 'badretype' ) );
266 return false;
267 }
268
269 # check for minimal password length
270 if ( !$u->isValidPassword( $this->mPassword ) ) {
271 if ( !$this->mCreateaccountMail ) {
272 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
273 return false;
274 } else {
275 # do not force a password for account creation by email
276 # set invalid password, it will be replaced later by a random generated password
277 $this->mPassword = null;
278 }
279 }
280
281 # if you need a confirmed email address to edit, then obviously you need an email address.
282 if ( $wgEmailConfirmToEdit && empty( $this->mEmail ) ) {
283 $this->mainLoginForm( wfMsg( 'noemailtitle' ) );
284 return false;
285 }
286
287 if( !empty( $this->mEmail ) && !User::isValidEmailAddr( $this->mEmail ) ) {
288 $this->mainLoginForm( wfMsg( 'invalidemailaddress' ) );
289 return false;
290 }
291
292 # Set some additional data so the AbortNewAccount hook can be
293 # used for more than just username validation
294 $u->setEmail( $this->mEmail );
295 $u->setRealName( $this->mRealName );
296
297 $abortError = '';
298 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
299 // Hook point to add extra creation throttles and blocks
300 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
301 $this->mainLoginForm( $abortError );
302 return false;
303 }
304
305 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
306 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
307 $value = $wgMemc->incr( $key );
308 if ( !$value ) {
309 $wgMemc->set( $key, 1, 86400 );
310 }
311 if ( $value > $wgAccountCreationThrottle ) {
312 $this->throttleHit( $wgAccountCreationThrottle );
313 return false;
314 }
315 }
316
317 if( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
318 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
319 return false;
320 }
321
322 return $this->initUser( $u, false );
323 }
324
325 /**
326 * Actually add a user to the database.
327 * Give it a User object that has been initialised with a name.
328 *
329 * @param $u User object.
330 * @param $autocreate boolean -- true if this is an autocreation via auth plugin
331 * @return User object.
332 * @private
333 */
334 function initUser( $u, $autocreate ) {
335 global $wgAuth;
336
337 $u->addToDatabase();
338
339 if ( $wgAuth->allowPasswordChange() ) {
340 $u->setPassword( $this->mPassword );
341 }
342
343 $u->setEmail( $this->mEmail );
344 $u->setRealName( $this->mRealName );
345 $u->setToken();
346
347 $wgAuth->initUser( $u, $autocreate );
348
349 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
350 $u->saveSettings();
351
352 # Update user count
353 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
354 $ssUpdate->doUpdate();
355
356 return $u;
357 }
358
359 /**
360 * Internally authenticate the login request.
361 *
362 * This may create a local account as a side effect if the
363 * authentication plugin allows transparent local account
364 * creation.
365 *
366 * @public
367 */
368 function authenticateUserData() {
369 global $wgUser, $wgAuth;
370 if ( '' == $this->mName ) {
371 return self::NO_NAME;
372 }
373 $u = User::newFromName( $this->mName );
374 if( is_null( $u ) || !User::isUsableName( $u->getName() ) ) {
375 return self::ILLEGAL;
376 }
377 if ( 0 == $u->getID() ) {
378 global $wgAuth;
379 /**
380 * If the external authentication plugin allows it,
381 * automatically create a new account for users that
382 * are externally defined but have not yet logged in.
383 */
384 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
385 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
386 $u = $this->initUser( $u, true );
387 } else {
388 return self::WRONG_PLUGIN_PASS;
389 }
390 } else {
391 return self::NOT_EXISTS;
392 }
393 } else {
394 $u->load();
395 }
396
397 // Give general extensions, such as a captcha, a chance to abort logins
398 $abort = self::ABORTED;
399 if( !wfRunHooks( 'AbortLogin', array( $u, $this->mPassword, &$abort ) ) ) {
400 return $abort;
401 }
402
403 if (!$u->checkPassword( $this->mPassword )) {
404 if( $u->checkTemporaryPassword( $this->mPassword ) ) {
405 // The e-mailed temporary password should not be used
406 // for actual logins; that's a very sloppy habit,
407 // and insecure if an attacker has a few seconds to
408 // click "search" on someone's open mail reader.
409 //
410 // Allow it to be used only to reset the password
411 // a single time to a new value, which won't be in
412 // the user's e-mail archives.
413 //
414 // For backwards compatibility, we'll still recognize
415 // it at the login form to minimize surprises for
416 // people who have been logging in with a temporary
417 // password for some time.
418 //
419 // As a side-effect, we can authenticate the user's
420 // e-mail address if it's not already done, since
421 // the temporary password was sent via e-mail.
422 //
423 if( !$u->isEmailConfirmed() ) {
424 $u->confirmEmail();
425 $u->saveSettings();
426 }
427
428 // At this point we just return an appropriate code
429 // indicating that the UI should show a password
430 // reset form; bot interfaces etc will probably just
431 // fail cleanly here.
432 //
433 $retval = self::RESET_PASS;
434 } else {
435 $retval = '' == $this->mPassword ? self::EMPTY_PASS : self::WRONG_PASS;
436 }
437 } else {
438 $wgAuth->updateUser( $u );
439 $wgUser = $u;
440
441 $retval = self::SUCCESS;
442 }
443 wfRunHooks( 'LoginAuthenticateAudit', array( $u, $this->mPassword, $retval ) );
444 return $retval;
445 }
446
447 function processLogin() {
448 global $wgUser, $wgAuth;
449
450 switch ($this->authenticateUserData())
451 {
452 case self::SUCCESS:
453 # We've verified now, update the real record
454 if( (bool)$this->mRemember != (bool)$wgUser->getOption( 'rememberpassword' ) ) {
455 $wgUser->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
456 $wgUser->saveSettings();
457 } else {
458 $wgUser->invalidateCache();
459 }
460 $wgUser->setCookies();
461
462 if( $this->hasSessionCookie() ) {
463 /* Replace the language object to provide user interface in correct
464 * language immediately on this first page load.
465 */
466 global $wgLang, $wgRequest;
467 $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
468 $wgLang = Language::factory( $code );
469 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
470 } else {
471 return $this->cookieRedirectCheck( 'login' );
472 }
473 break;
474
475 case self::NO_NAME:
476 case self::ILLEGAL:
477 $this->mainLoginForm( wfMsg( 'noname' ) );
478 break;
479 case self::WRONG_PLUGIN_PASS:
480 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
481 break;
482 case self::NOT_EXISTS:
483 if( $wgUser->isAllowed( 'createaccount' ) ){
484 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName ) ) );
485 } else {
486 $this->mainLoginForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->mName ) ) );
487 }
488 break;
489 case self::WRONG_PASS:
490 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
491 break;
492 case self::EMPTY_PASS:
493 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
494 break;
495 case self::RESET_PASS:
496 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
497 break;
498 default:
499 throw new MWException( "Unhandled case value" );
500 }
501 }
502
503 function resetLoginForm( $error ) {
504 global $wgOut;
505 $wgOut->addWikiText( "<div class=\"errorbox\">$error</div>" );
506 $reset = new PasswordResetForm( $this->mName, $this->mPassword );
507 $reset->execute( null );
508 }
509
510 /**
511 * @private
512 */
513 function mailPassword() {
514 global $wgUser, $wgOut, $wgAuth;
515
516 if( !$wgAuth->allowPasswordChange() ) {
517 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
518 return;
519 }
520
521 # Check against blocked IPs
522 # fixme -- should we not?
523 if( $wgUser->isBlocked() ) {
524 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
525 return;
526 }
527
528 # Check against the rate limiter
529 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
530 $wgOut->rateLimited();
531 return;
532 }
533
534 if ( '' == $this->mName ) {
535 $this->mainLoginForm( wfMsg( 'noname' ) );
536 return;
537 }
538 $u = User::newFromName( $this->mName );
539 if( is_null( $u ) ) {
540 $this->mainLoginForm( wfMsg( 'noname' ) );
541 return;
542 }
543 if ( 0 == $u->getID() ) {
544 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
545 return;
546 }
547
548 # Check against password throttle
549 if ( $u->isPasswordReminderThrottled() ) {
550 global $wgPasswordReminderResendTime;
551 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
552 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
553 round( $wgPasswordReminderResendTime, 3 ) ) );
554 return;
555 }
556
557 $result = $this->mailPasswordInternal( $u, true, 'passwordremindertitle', 'passwordremindertext' );
558 if( WikiError::isError( $result ) ) {
559 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
560 } else {
561 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
562 }
563 }
564
565
566 /**
567 * @param object user
568 * @param bool throttle
569 * @param string message name of email title
570 * @param string message name of email text
571 * @return mixed true on success, WikiError on failure
572 * @private
573 */
574 function mailPasswordInternal( $u, $throttle = true, $emailTitle = 'passwordremindertitle', $emailText = 'passwordremindertext' ) {
575 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
576 global $wgServer, $wgScript;
577
578 if ( '' == $u->getEmail() ) {
579 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
580 }
581
582 $np = $u->randomPassword();
583 $u->setNewpassword( $np, $throttle );
584 $u->saveSettings();
585
586 $ip = wfGetIP();
587 if ( '' == $ip ) { $ip = '(Unknown)'; }
588
589 $m = wfMsg( $emailText, $ip, $u->getName(), $np, $wgServer . $wgScript );
590 $result = $u->sendMail( wfMsg( $emailTitle ), $m );
591
592 return $result;
593 }
594
595
596 /**
597 * @param string $msg Message that will be shown on success
598 * @param bool $auto Toggle auto-redirect to main page; default true
599 * @private
600 */
601 function successfulLogin( $msg, $auto = true ) {
602 global $wgUser;
603 global $wgOut;
604
605 # Run any hooks; ignore results
606
607 $injected_html = '';
608 wfRunHooks('UserLoginComplete', array(&$wgUser, &$injected_html));
609
610 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
611 $wgOut->setRobotpolicy( 'noindex,nofollow' );
612 $wgOut->setArticleRelated( false );
613 $wgOut->addWikiText( $msg );
614 $wgOut->addHtml( $injected_html );
615 if ( !empty( $this->mReturnTo ) ) {
616 $wgOut->returnToMain( $auto, $this->mReturnTo );
617 } else {
618 $wgOut->returnToMain( $auto );
619 }
620 }
621
622 /** */
623 function userNotPrivilegedMessage() {
624 global $wgOut;
625
626 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
627 $wgOut->setRobotpolicy( 'noindex,nofollow' );
628 $wgOut->setArticleRelated( false );
629
630 $wgOut->addWikiMsg( 'whitelistacctext' );
631
632 $wgOut->returnToMain( false );
633 }
634
635 /** */
636 function userBlockedMessage() {
637 global $wgOut, $wgUser;
638
639 # Let's be nice about this, it's likely that this feature will be used
640 # for blocking large numbers of innocent people, e.g. range blocks on
641 # schools. Don't blame it on the user. There's a small chance that it
642 # really is the user's fault, i.e. the username is blocked and they
643 # haven't bothered to log out before trying to create an account to
644 # evade it, but we'll leave that to their guilty conscience to figure
645 # out.
646
647 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
648 $wgOut->setRobotpolicy( 'noindex,nofollow' );
649 $wgOut->setArticleRelated( false );
650
651 $ip = wfGetIP();
652 $blocker = User::whoIs( $wgUser->mBlock->mBy );
653 $block_reason = $wgUser->mBlock->mReason;
654
655 $wgOut->addWikiMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker );
656 $wgOut->returnToMain( false );
657 }
658
659 /**
660 * @private
661 */
662 function mainLoginForm( $msg, $msgtype = 'error' ) {
663 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
664 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
665 global $wgAuth, $wgEmailConfirmToEdit;
666
667 if ( $this->mType == 'signup' ) {
668 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
669 $this->userNotPrivilegedMessage();
670 return;
671 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
672 $this->userBlockedMessage();
673 return;
674 }
675 }
676
677 if ( '' == $this->mName ) {
678 if ( $wgUser->isLoggedIn() ) {
679 $this->mName = $wgUser->getName();
680 } else {
681 $this->mName = isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ? $_COOKIE[$wgCookiePrefix.'UserName'] : null;
682 }
683 }
684
685 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
686
687 if ( $this->mType == 'signup' ) {
688 $template = new UsercreateTemplate();
689 $q = 'action=submitlogin&type=signup';
690 $linkq = 'type=login';
691 $linkmsg = 'gotaccount';
692 } else {
693 $template = new UserloginTemplate();
694 $q = 'action=submitlogin&type=login';
695 $linkq = 'type=signup';
696 $linkmsg = 'nologin';
697 }
698
699 if ( !empty( $this->mReturnTo ) ) {
700 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
701 $q .= $returnto;
702 $linkq .= $returnto;
703 }
704
705 # Pass any language selection on to the mode switch link
706 if( $wgLoginLanguageSelector && $this->mLanguage )
707 $linkq .= '&uselang=' . $this->mLanguage;
708
709 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
710 $link .= wfMsgHtml( $linkmsg . 'link' ); # Calling either 'gotaccountlink' or 'nologinlink'
711 $link .= '</a>';
712
713 # Don't show a "create account" link if the user can't
714 if( $this->showCreateOrLoginLink( $wgUser ) )
715 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
716 else
717 $template->set( 'link', '' );
718
719 $template->set( 'header', '' );
720 $template->set( 'name', $this->mName );
721 $template->set( 'password', $this->mPassword );
722 $template->set( 'retype', $this->mRetype );
723 $template->set( 'email', $this->mEmail );
724 $template->set( 'realname', $this->mRealName );
725 $template->set( 'domain', $this->mDomain );
726
727 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
728 $template->set( 'message', $msg );
729 $template->set( 'messagetype', $msgtype );
730 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
731 $template->set( 'userealname', $wgAllowRealName );
732 $template->set( 'useemail', $wgEnableEmail );
733 $template->set( 'emailrequired', $wgEmailConfirmToEdit );
734 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
735 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
736
737 # Prepare language selection links as needed
738 if( $wgLoginLanguageSelector ) {
739 $template->set( 'languages', $this->makeLanguageSelector() );
740 if( $this->mLanguage )
741 $template->set( 'uselang', $this->mLanguage );
742 }
743
744 // Give authentication and captcha plugins a chance to modify the form
745 $wgAuth->modifyUITemplate( $template );
746 if ( $this->mType == 'signup' ) {
747 wfRunHooks( 'UserCreateForm', array( &$template ) );
748 } else {
749 wfRunHooks( 'UserLoginForm', array( &$template ) );
750 }
751
752 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
753 $wgOut->setRobotpolicy( 'noindex,nofollow' );
754 $wgOut->setArticleRelated( false );
755 $wgOut->disallowUserJs(); // just in case...
756 $wgOut->addTemplate( $template );
757 }
758
759 /**
760 * @private
761 */
762 function showCreateOrLoginLink( &$user ) {
763 if( $this->mType == 'signup' ) {
764 return( true );
765 } elseif( $user->isAllowed( 'createaccount' ) ) {
766 return( true );
767 } else {
768 return( false );
769 }
770 }
771
772 /**
773 * Check if a session cookie is present.
774 *
775 * This will not pick up a cookie set during _this_ request, but is
776 * meant to ensure that the client is returning the cookie which was
777 * set on a previous pass through the system.
778 *
779 * @private
780 */
781 function hasSessionCookie() {
782 global $wgDisableCookieCheck, $wgRequest;
783 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
784 }
785
786 /**
787 * @private
788 */
789 function cookieRedirectCheck( $type ) {
790 global $wgOut;
791
792 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
793 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
794
795 return $wgOut->redirect( $check );
796 }
797
798 /**
799 * @private
800 */
801 function onCookieRedirectCheck( $type ) {
802 global $wgUser;
803
804 if ( !$this->hasSessionCookie() ) {
805 if ( $type == 'new' ) {
806 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
807 } else if ( $type == 'login' ) {
808 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
809 } else {
810 # shouldn't happen
811 return $this->mainLoginForm( wfMsg( 'error' ) );
812 }
813 } else {
814 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
815 }
816 }
817
818 /**
819 * @private
820 */
821 function throttleHit( $limit ) {
822 global $wgOut;
823
824 $wgOut->addWikiMsg( 'acct_creation_throttle_hit', $limit );
825 }
826
827 /**
828 * Produce a bar of links which allow the user to select another language
829 * during login/registration but retain "returnto"
830 *
831 * @return string
832 */
833 function makeLanguageSelector() {
834 $msg = wfMsgForContent( 'loginlanguagelinks' );
835 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
836 $langs = explode( "\n", $msg );
837 $links = array();
838 foreach( $langs as $lang ) {
839 $lang = trim( $lang, '* ' );
840 $parts = explode( '|', $lang );
841 if (count($parts) >= 2) {
842 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
843 }
844 }
845 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
846 } else {
847 return '';
848 }
849 }
850
851 /**
852 * Create a language selector link for a particular language
853 * Links back to this page preserving type and returnto
854 *
855 * @param $text Link text
856 * @param $lang Language code
857 */
858 function makeLanguageSelectorLink( $text, $lang ) {
859 global $wgUser;
860 $self = SpecialPage::getTitleFor( 'Userlogin' );
861 $attr[] = 'uselang=' . $lang;
862 if( $this->mType == 'signup' )
863 $attr[] = 'type=signup';
864 if( $this->mReturnTo )
865 $attr[] = 'returnto=' . $this->mReturnTo;
866 $skin = $wgUser->getSkin();
867 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
868 }
869 }