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