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