Postgres: make sure ar_len is added when updating, alpha stuff in updaters.inc
[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 *
23 * @addtogroup SpecialPage
24 */
25
26 class LoginForm {
27
28 const SUCCESS = 0;
29 const NO_NAME = 1;
30 const ILLEGAL = 2;
31 const WRONG_PLUGIN_PASS = 3;
32 const NOT_EXISTS = 4;
33 const WRONG_PASS = 5;
34 const EMPTY_PASS = 6;
35 const RESET_PASS = 7;
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 if (!$wgUser->isAllowedToCreateAccount()) {
233 $this->userNotPrivilegedMessage();
234 return false;
235 }
236
237 $ip = wfGetIP();
238 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
239 $wgUser->inSorbsBlacklist( $ip ) )
240 {
241 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
242 return;
243 }
244
245 $name = trim( $this->mName );
246 $u = User::newFromName( $name, 'creatable' );
247 if ( is_null( $u ) ) {
248 $this->mainLoginForm( wfMsg( 'noname' ) );
249 return false;
250 }
251
252 if ( 0 != $u->idForName() ) {
253 $this->mainLoginForm( wfMsg( 'userexists' ) );
254 return false;
255 }
256
257 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
258 $this->mainLoginForm( wfMsg( 'badretype' ) );
259 return false;
260 }
261
262 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
263 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
264 return false;
265 }
266
267 $abortError = '';
268 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
269 // Hook point to add extra creation throttles and blocks
270 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
271 $this->mainLoginForm( $abortError );
272 return false;
273 }
274
275 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
276 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
277 $value = $wgMemc->incr( $key );
278 if ( !$value ) {
279 $wgMemc->set( $key, 1, 86400 );
280 }
281 if ( $value > $wgAccountCreationThrottle ) {
282 $this->throttleHit( $wgAccountCreationThrottle );
283 return false;
284 }
285 }
286
287 if( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
288 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
289 return false;
290 }
291
292 return $this->initUser( $u );
293 }
294
295 /**
296 * Actually add a user to the database.
297 * Give it a User object that has been initialised with a name.
298 *
299 * @param $u User object.
300 * @return User object.
301 * @private
302 */
303 function initUser( $u ) {
304 global $wgAuth;
305
306 $u->addToDatabase();
307
308 if ( $wgAuth->allowPasswordChange() ) {
309 $u->setPassword( $this->mPassword );
310 }
311
312 $u->setEmail( $this->mEmail );
313 $u->setRealName( $this->mRealName );
314 $u->setToken();
315
316 $wgAuth->initUser( $u );
317
318 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
319 $u->saveSettings();
320
321 # Update user count
322 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
323 $ssUpdate->doUpdate();
324
325 return $u;
326 }
327
328 /**
329 * Internally authenticate the login request.
330 *
331 * This may create a local account as a side effect if the
332 * authentication plugin allows transparent local account
333 * creation.
334 *
335 * @public
336 */
337 function authenticateUserData() {
338 global $wgUser, $wgAuth;
339 if ( '' == $this->mName ) {
340 return self::NO_NAME;
341 }
342 $u = User::newFromName( $this->mName );
343 if( is_null( $u ) || !User::isUsableName( $u->getName() ) ) {
344 return self::ILLEGAL;
345 }
346 if ( 0 == $u->getID() ) {
347 global $wgAuth;
348 /**
349 * If the external authentication plugin allows it,
350 * automatically create a new account for users that
351 * are externally defined but have not yet logged in.
352 */
353 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
354 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
355 $u = $this->initUser( $u );
356 } else {
357 return self::WRONG_PLUGIN_PASS;
358 }
359 } else {
360 return self::NOT_EXISTS;
361 }
362 } else {
363 $u->load();
364 }
365
366 if (!$u->checkPassword( $this->mPassword )) {
367 if( $u->checkTemporaryPassword( $this->mPassword ) ) {
368 // The e-mailed temporary password should not be used
369 // for actual logins; that's a very sloppy habit,
370 // and insecure if an attacker has a few seconds to
371 // click "search" on someone's open mail reader.
372 //
373 // Allow it to be used only to reset the password
374 // a single time to a new value, which won't be in
375 // the user's e-mail archives.
376 //
377 // For backwards compatibility, we'll still recognize
378 // it at the login form to minimize surprises for
379 // people who have been logging in with a temporary
380 // password for some time.
381 //
382 // As a side-effect, we can authenticate the user's
383 // e-mail address if it's not already done, since
384 // the temporary password was sent via e-mail.
385 //
386 if( !$u->isEmailConfirmed() ) {
387 $u->confirmEmail();
388 }
389
390 // At this point we just return an appropriate code
391 // indicating that the UI should show a password
392 // reset form; bot interfaces etc will probably just
393 // fail cleanly here.
394 //
395 return self::RESET_PASS;
396 } else {
397 return '' == $this->mPassword ? self::EMPTY_PASS : self::WRONG_PASS;
398 }
399 } else {
400 $wgAuth->updateUser( $u );
401 $wgUser = $u;
402
403 return self::SUCCESS;
404 }
405 }
406
407 function processLogin() {
408 global $wgUser, $wgAuth;
409
410 switch ($this->authenticateUserData())
411 {
412 case self::SUCCESS:
413 # We've verified now, update the real record
414 if( (bool)$this->mRemember != (bool)$wgUser->getOption( 'rememberpassword' ) ) {
415 $wgUser->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
416 $wgUser->saveSettings();
417 } else {
418 $wgUser->invalidateCache();
419 }
420 $wgUser->setCookies();
421
422 if( $this->hasSessionCookie() ) {
423 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
424 } else {
425 return $this->cookieRedirectCheck( 'login' );
426 }
427 break;
428
429 case self::NO_NAME:
430 case self::ILLEGAL:
431 $this->mainLoginForm( wfMsg( 'noname' ) );
432 break;
433 case self::WRONG_PLUGIN_PASS:
434 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
435 break;
436 case self::NOT_EXISTS:
437 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName ) ) );
438 break;
439 case self::WRONG_PASS:
440 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
441 break;
442 case self::EMPTY_PASS:
443 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
444 break;
445 case self::RESET_PASS:
446 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
447 break;
448 default:
449 wfDebugDieBacktrace( "Unhandled case value" );
450 }
451 }
452
453 function resetLoginForm( $error ) {
454 global $wgOut;
455 $wgOut->addWikiText( "<div class=\"errorbox\">$error</div>" );
456 $reset = new PasswordResetForm( $this->mName, $this->mPassword );
457 $reset->execute();
458 }
459
460 /**
461 * @private
462 */
463 function mailPassword() {
464 global $wgUser, $wgOut, $wgAuth;
465
466 if( !$wgAuth->allowPasswordChange() ) {
467 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
468 return;
469 }
470
471 # Check against blocked IPs
472 # fixme -- should we not?
473 if( $wgUser->isBlocked() ) {
474 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
475 return;
476 }
477
478 # Check against the rate limiter
479 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
480 $wgOut->rateLimited();
481 return;
482 }
483
484 if ( '' == $this->mName ) {
485 $this->mainLoginForm( wfMsg( 'noname' ) );
486 return;
487 }
488 $u = User::newFromName( $this->mName );
489 if( is_null( $u ) ) {
490 $this->mainLoginForm( wfMsg( 'noname' ) );
491 return;
492 }
493 if ( 0 == $u->getID() ) {
494 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
495 return;
496 }
497
498 # Check against password throttle
499 if ( $u->isPasswordReminderThrottled() ) {
500 global $wgPasswordReminderResendTime;
501 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
502 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
503 round( $wgPasswordReminderResendTime, 3 ) ) );
504 return;
505 }
506
507 $result = $this->mailPasswordInternal( $u, true );
508 if( WikiError::isError( $result ) ) {
509 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
510 } else {
511 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
512 }
513 }
514
515
516 /**
517 * @return mixed true on success, WikiError on failure
518 * @private
519 */
520 function mailPasswordInternal( $u, $throttle = true ) {
521 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
522 global $wgServer, $wgScript;
523
524 if ( '' == $u->getEmail() ) {
525 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
526 }
527
528 $np = $u->randomPassword();
529 $u->setNewpassword( $np, $throttle );
530
531 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
532
533 $u->saveSettings();
534
535 $ip = wfGetIP();
536 if ( '' == $ip ) { $ip = '(Unknown)'; }
537
538 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
539
540 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
541 return $result;
542 }
543
544
545 /**
546 * @param string $msg Message that will be shown on success
547 * @param bool $auto Toggle auto-redirect to main page; default true
548 * @private
549 */
550 function successfulLogin( $msg, $auto = true ) {
551 global $wgUser;
552 global $wgOut;
553
554 # Run any hooks; ignore results
555
556 wfRunHooks('UserLoginComplete', array(&$wgUser));
557
558 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
559 $wgOut->setRobotpolicy( 'noindex,nofollow' );
560 $wgOut->setArticleRelated( false );
561 $wgOut->addWikiText( $msg );
562 if ( !empty( $this->mReturnTo ) ) {
563 $wgOut->returnToMain( $auto, $this->mReturnTo );
564 } else {
565 $wgOut->returnToMain( $auto );
566 }
567 }
568
569 /** */
570 function userNotPrivilegedMessage() {
571 global $wgOut;
572
573 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
574 $wgOut->setRobotpolicy( 'noindex,nofollow' );
575 $wgOut->setArticleRelated( false );
576
577 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
578
579 $wgOut->returnToMain( false );
580 }
581
582 /** */
583 function userBlockedMessage() {
584 global $wgOut;
585
586 # Let's be nice about this, it's likely that this feature will be used
587 # for blocking large numbers of innocent people, e.g. range blocks on
588 # schools. Don't blame it on the user. There's a small chance that it
589 # really is the user's fault, i.e. the username is blocked and they
590 # haven't bothered to log out before trying to create an account to
591 # evade it, but we'll leave that to their guilty conscience to figure
592 # out.
593
594 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
595 $wgOut->setRobotpolicy( 'noindex,nofollow' );
596 $wgOut->setArticleRelated( false );
597
598 $ip = wfGetIP();
599 $wgOut->addWikiText( wfMsg( 'cantcreateaccounttext', $ip ) );
600 $wgOut->returnToMain( false );
601 }
602
603 /**
604 * @private
605 */
606 function mainLoginForm( $msg, $msgtype = 'error' ) {
607 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
608 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
609 global $wgAuth;
610
611 if ( $this->mType == 'signup' ) {
612 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
613 $this->userNotPrivilegedMessage();
614 return;
615 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
616 $this->userBlockedMessage();
617 return;
618 }
619 }
620
621 if ( '' == $this->mName ) {
622 if ( $wgUser->isLoggedIn() ) {
623 $this->mName = $wgUser->getName();
624 } else {
625 $this->mName = isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ? $_COOKIE[$wgCookiePrefix.'UserName'] : null;
626 }
627 }
628
629 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
630
631 if ( $this->mType == 'signup' ) {
632 $template = new UsercreateTemplate();
633 $q = 'action=submitlogin&type=signup';
634 $linkq = 'type=login';
635 $linkmsg = 'gotaccount';
636 } else {
637 $template = new UserloginTemplate();
638 $q = 'action=submitlogin&type=login';
639 $linkq = 'type=signup';
640 $linkmsg = 'nologin';
641 }
642
643 if ( !empty( $this->mReturnTo ) ) {
644 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
645 $q .= $returnto;
646 $linkq .= $returnto;
647 }
648
649 # Pass any language selection on to the mode switch link
650 if( $wgLoginLanguageSelector && $this->mLanguage )
651 $linkq .= '&uselang=' . $this->mLanguage;
652
653 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
654 $link .= wfMsgHtml( $linkmsg . 'link' );
655 $link .= '</a>';
656
657 # Don't show a "create account" link if the user can't
658 if( $this->showCreateOrLoginLink( $wgUser ) )
659 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
660 else
661 $template->set( 'link', '' );
662
663 $template->set( 'header', '' );
664 $template->set( 'name', $this->mName );
665 $template->set( 'password', $this->mPassword );
666 $template->set( 'retype', $this->mRetype );
667 $template->set( 'email', $this->mEmail );
668 $template->set( 'realname', $this->mRealName );
669 $template->set( 'domain', $this->mDomain );
670
671 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
672 $template->set( 'message', $msg );
673 $template->set( 'messagetype', $msgtype );
674 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
675 $template->set( 'userealname', $wgAllowRealName );
676 $template->set( 'useemail', $wgEnableEmail );
677 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
678 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
679
680 # Prepare language selection links as needed
681 if( $wgLoginLanguageSelector ) {
682 $template->set( 'languages', $this->makeLanguageSelector() );
683 if( $this->mLanguage )
684 $template->set( 'uselang', $this->mLanguage );
685 }
686
687 // Give authentication and captcha plugins a chance to modify the form
688 $wgAuth->modifyUITemplate( $template );
689 if ( $this->mType == 'signup' ) {
690 wfRunHooks( 'UserCreateForm', array( &$template ) );
691 } else {
692 wfRunHooks( 'UserLoginForm', array( &$template ) );
693 }
694
695 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
696 $wgOut->setRobotpolicy( 'noindex,nofollow' );
697 $wgOut->setArticleRelated( false );
698 $wgOut->addTemplate( $template );
699 }
700
701 /**
702 * @private
703 */
704 function showCreateOrLoginLink( &$user ) {
705 if( $this->mType == 'signup' ) {
706 return( true );
707 } elseif( $user->isAllowed( 'createaccount' ) ) {
708 return( true );
709 } else {
710 return( false );
711 }
712 }
713
714 /**
715 * Check if a session cookie is present.
716 *
717 * This will not pick up a cookie set during _this_ request, but is
718 * meant to ensure that the client is returning the cookie which was
719 * set on a previous pass through the system.
720 *
721 * @private
722 */
723 function hasSessionCookie() {
724 global $wgDisableCookieCheck, $wgRequest;
725 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
726 }
727
728 /**
729 * @private
730 */
731 function cookieRedirectCheck( $type ) {
732 global $wgOut;
733
734 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
735 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
736
737 return $wgOut->redirect( $check );
738 }
739
740 /**
741 * @private
742 */
743 function onCookieRedirectCheck( $type ) {
744 global $wgUser;
745
746 if ( !$this->hasSessionCookie() ) {
747 if ( $type == 'new' ) {
748 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
749 } else if ( $type == 'login' ) {
750 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
751 } else {
752 # shouldn't happen
753 return $this->mainLoginForm( wfMsg( 'error' ) );
754 }
755 } else {
756 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
757 }
758 }
759
760 /**
761 * @private
762 */
763 function throttleHit( $limit ) {
764 global $wgOut;
765
766 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
767 }
768
769 /**
770 * Produce a bar of links which allow the user to select another language
771 * during login/registration but retain "returnto"
772 *
773 * @return string
774 */
775 function makeLanguageSelector() {
776 $msg = wfMsgForContent( 'loginlanguagelinks' );
777 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
778 $langs = explode( "\n", $msg );
779 $links = array();
780 foreach( $langs as $lang ) {
781 $lang = trim( $lang, '* ' );
782 $parts = explode( '|', $lang );
783 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
784 }
785 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
786 } else {
787 return '';
788 }
789 }
790
791 /**
792 * Create a language selector link for a particular language
793 * Links back to this page preserving type and returnto
794 *
795 * @param $text Link text
796 * @param $lang Language code
797 */
798 function makeLanguageSelectorLink( $text, $lang ) {
799 global $wgUser;
800 $self = SpecialPage::getTitleFor( 'Userlogin' );
801 $attr[] = 'uselang=' . $lang;
802 if( $this->mType == 'signup' )
803 $attr[] = 'type=signup';
804 if( $this->mReturnTo )
805 $attr[] = 'returnto=' . $this->mReturnTo;
806 $skin = $wgUser->getSkin();
807 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
808 }
809 }
810 ?>