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