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