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