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