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