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