Update to r32375 / bug 11874 -- !important may have whitespace between ! and important
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * constructor
9 */
10 function wfSpecialUserlogin( $par = '' ) {
11 global $wgRequest;
12 if( session_id() == '' ) {
13 wfSetupSession();
14 }
15
16 $form = new LoginForm( $wgRequest, $par );
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, $par = '' ) {
45 global $wgLang, $wgAllowRealName, $wgEnableEmail;
46 global $wgAuth;
47
48 $this->mType = ( $par == 'signup' ) ? $par : $request->getText( 'type' ); # Check for [[Special:Userlogin/signup]]
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, true ) );
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->addWikiMsg( '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->addWikiMsg( 'confirmemail_sendfailed', $error->getMessage() );
168 } else {
169 $wgOut->addWikiMsg( '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 invalid password, it will be replaced later by a random generated password
275 $this->mPassword = null;
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 if( $wgUser->isAllowed( 'createaccount' ) ){
475 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName ) ) );
476 } else {
477 $this->mainLoginForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->mName ) ) );
478 }
479 break;
480 case self::WRONG_PASS:
481 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
482 break;
483 case self::EMPTY_PASS:
484 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
485 break;
486 case self::RESET_PASS:
487 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
488 break;
489 default:
490 throw new MWException( "Unhandled case value" );
491 }
492 }
493
494 function resetLoginForm( $error ) {
495 global $wgOut;
496 $wgOut->addWikiText( "<div class=\"errorbox\">$error</div>" );
497 $reset = new PasswordResetForm( $this->mName, $this->mPassword );
498 $reset->execute( null );
499 }
500
501 /**
502 * @private
503 */
504 function mailPassword() {
505 global $wgUser, $wgOut, $wgAuth;
506
507 if( !$wgAuth->allowPasswordChange() ) {
508 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
509 return;
510 }
511
512 # Check against blocked IPs
513 # fixme -- should we not?
514 if( $wgUser->isBlocked() ) {
515 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
516 return;
517 }
518
519 # Check against the rate limiter
520 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
521 $wgOut->rateLimited();
522 return;
523 }
524
525 if ( '' == $this->mName ) {
526 $this->mainLoginForm( wfMsg( 'noname' ) );
527 return;
528 }
529 $u = User::newFromName( $this->mName );
530 if( is_null( $u ) ) {
531 $this->mainLoginForm( wfMsg( 'noname' ) );
532 return;
533 }
534 if ( 0 == $u->getID() ) {
535 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
536 return;
537 }
538
539 # Check against password throttle
540 if ( $u->isPasswordReminderThrottled() ) {
541 global $wgPasswordReminderResendTime;
542 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
543 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
544 round( $wgPasswordReminderResendTime, 3 ) ) );
545 return;
546 }
547
548 $result = $this->mailPasswordInternal( $u, true, 'passwordremindertitle', 'passwordremindertext' );
549 if( WikiError::isError( $result ) ) {
550 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
551 } else {
552 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
553 }
554 }
555
556
557 /**
558 * @param object user
559 * @param bool throttle
560 * @param string message name of email title
561 * @param string message name of email text
562 * @return mixed true on success, WikiError on failure
563 * @private
564 */
565 function mailPasswordInternal( $u, $throttle = true, $emailTitle = 'passwordremindertitle', $emailText = 'passwordremindertext' ) {
566 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
567 global $wgServer, $wgScript;
568
569 if ( '' == $u->getEmail() ) {
570 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
571 }
572
573 $np = $u->randomPassword();
574 $u->setNewpassword( $np, $throttle );
575
576 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
577
578 $u->saveSettings();
579
580 $ip = wfGetIP();
581 if ( '' == $ip ) { $ip = '(Unknown)'; }
582
583 $m = wfMsg( $emailText, $ip, $u->getName(), $np, $wgServer . $wgScript );
584 $result = $u->sendMail( wfMsg( $emailTitle ), $m );
585
586 return $result;
587 }
588
589
590 /**
591 * @param string $msg Message that will be shown on success
592 * @param bool $auto Toggle auto-redirect to main page; default true
593 * @private
594 */
595 function successfulLogin( $msg, $auto = true ) {
596 global $wgUser;
597 global $wgOut;
598
599 # Run any hooks; ignore results
600
601 wfRunHooks('UserLoginComplete', array(&$wgUser));
602
603 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
604 $wgOut->setRobotpolicy( 'noindex,nofollow' );
605 $wgOut->setArticleRelated( false );
606 $wgOut->addWikiText( $msg );
607 if ( !empty( $this->mReturnTo ) ) {
608 $wgOut->returnToMain( $auto, $this->mReturnTo );
609 } else {
610 $wgOut->returnToMain( $auto );
611 }
612 }
613
614 /** */
615 function userNotPrivilegedMessage() {
616 global $wgOut;
617
618 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
619 $wgOut->setRobotpolicy( 'noindex,nofollow' );
620 $wgOut->setArticleRelated( false );
621
622 $wgOut->addWikiMsg( 'whitelistacctext' );
623
624 $wgOut->returnToMain( false );
625 }
626
627 /** */
628 function userBlockedMessage() {
629 global $wgOut, $wgUser;
630
631 # Let's be nice about this, it's likely that this feature will be used
632 # for blocking large numbers of innocent people, e.g. range blocks on
633 # schools. Don't blame it on the user. There's a small chance that it
634 # really is the user's fault, i.e. the username is blocked and they
635 # haven't bothered to log out before trying to create an account to
636 # evade it, but we'll leave that to their guilty conscience to figure
637 # out.
638
639 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
640 $wgOut->setRobotpolicy( 'noindex,nofollow' );
641 $wgOut->setArticleRelated( false );
642
643 $ip = wfGetIP();
644 $blocker = User::whoIs( $wgUser->mBlock->mBy );
645 $block_reason = $wgUser->mBlock->mReason;
646
647 $wgOut->addWikiMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker );
648 $wgOut->returnToMain( false );
649 }
650
651 /**
652 * @private
653 */
654 function mainLoginForm( $msg, $msgtype = 'error' ) {
655 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
656 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
657 global $wgAuth, $wgEmailConfirmToEdit;
658
659 if ( $this->mType == 'signup' ) {
660 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
661 $this->userNotPrivilegedMessage();
662 return;
663 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
664 $this->userBlockedMessage();
665 return;
666 }
667 }
668
669 if ( '' == $this->mName ) {
670 if ( $wgUser->isLoggedIn() ) {
671 $this->mName = $wgUser->getName();
672 } else {
673 $this->mName = isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ? $_COOKIE[$wgCookiePrefix.'UserName'] : null;
674 }
675 }
676
677 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
678
679 if ( $this->mType == 'signup' ) {
680 $template = new UsercreateTemplate();
681 $q = 'action=submitlogin&type=signup';
682 $linkq = 'type=login';
683 $linkmsg = 'gotaccount';
684 } else {
685 $template = new UserloginTemplate();
686 $q = 'action=submitlogin&type=login';
687 $linkq = 'type=signup';
688 $linkmsg = 'nologin';
689 }
690
691 if ( !empty( $this->mReturnTo ) ) {
692 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
693 $q .= $returnto;
694 $linkq .= $returnto;
695 }
696
697 # Pass any language selection on to the mode switch link
698 if( $wgLoginLanguageSelector && $this->mLanguage )
699 $linkq .= '&uselang=' . $this->mLanguage;
700
701 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
702 $link .= wfMsgHtml( $linkmsg . 'link' ); # Calling either 'gotaccountlink' or 'nologinlink'
703 $link .= '</a>';
704
705 # Don't show a "create account" link if the user can't
706 if( $this->showCreateOrLoginLink( $wgUser ) )
707 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
708 else
709 $template->set( 'link', '' );
710
711 $template->set( 'header', '' );
712 $template->set( 'name', $this->mName );
713 $template->set( 'password', $this->mPassword );
714 $template->set( 'retype', $this->mRetype );
715 $template->set( 'email', $this->mEmail );
716 $template->set( 'realname', $this->mRealName );
717 $template->set( 'domain', $this->mDomain );
718
719 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
720 $template->set( 'message', $msg );
721 $template->set( 'messagetype', $msgtype );
722 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
723 $template->set( 'userealname', $wgAllowRealName );
724 $template->set( 'useemail', $wgEnableEmail );
725 $template->set( 'emailrequired', $wgEmailConfirmToEdit );
726 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
727 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
728
729 # Prepare language selection links as needed
730 if( $wgLoginLanguageSelector ) {
731 $template->set( 'languages', $this->makeLanguageSelector() );
732 if( $this->mLanguage )
733 $template->set( 'uselang', $this->mLanguage );
734 }
735
736 // Give authentication and captcha plugins a chance to modify the form
737 $wgAuth->modifyUITemplate( $template );
738 if ( $this->mType == 'signup' ) {
739 wfRunHooks( 'UserCreateForm', array( &$template ) );
740 } else {
741 wfRunHooks( 'UserLoginForm', array( &$template ) );
742 }
743
744 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
745 $wgOut->setRobotpolicy( 'noindex,nofollow' );
746 $wgOut->setArticleRelated( false );
747 $wgOut->disallowUserJs(); // just in case...
748 $wgOut->addTemplate( $template );
749 }
750
751 /**
752 * @private
753 */
754 function showCreateOrLoginLink( &$user ) {
755 if( $this->mType == 'signup' ) {
756 return( true );
757 } elseif( $user->isAllowed( 'createaccount' ) ) {
758 return( true );
759 } else {
760 return( false );
761 }
762 }
763
764 /**
765 * Check if a session cookie is present.
766 *
767 * This will not pick up a cookie set during _this_ request, but is
768 * meant to ensure that the client is returning the cookie which was
769 * set on a previous pass through the system.
770 *
771 * @private
772 */
773 function hasSessionCookie() {
774 global $wgDisableCookieCheck, $wgRequest;
775 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
776 }
777
778 /**
779 * @private
780 */
781 function cookieRedirectCheck( $type ) {
782 global $wgOut;
783
784 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
785 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
786
787 return $wgOut->redirect( $check );
788 }
789
790 /**
791 * @private
792 */
793 function onCookieRedirectCheck( $type ) {
794 global $wgUser;
795
796 if ( !$this->hasSessionCookie() ) {
797 if ( $type == 'new' ) {
798 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
799 } else if ( $type == 'login' ) {
800 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
801 } else {
802 # shouldn't happen
803 return $this->mainLoginForm( wfMsg( 'error' ) );
804 }
805 } else {
806 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
807 }
808 }
809
810 /**
811 * @private
812 */
813 function throttleHit( $limit ) {
814 global $wgOut;
815
816 $wgOut->addWikiMsg( 'acct_creation_throttle_hit', $limit );
817 }
818
819 /**
820 * Produce a bar of links which allow the user to select another language
821 * during login/registration but retain "returnto"
822 *
823 * @return string
824 */
825 function makeLanguageSelector() {
826 $msg = wfMsgForContent( 'loginlanguagelinks' );
827 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
828 $langs = explode( "\n", $msg );
829 $links = array();
830 foreach( $langs as $lang ) {
831 $lang = trim( $lang, '* ' );
832 $parts = explode( '|', $lang );
833 if (count($parts) >= 2) {
834 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
835 }
836 }
837 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
838 } else {
839 return '';
840 }
841 }
842
843 /**
844 * Create a language selector link for a particular language
845 * Links back to this page preserving type and returnto
846 *
847 * @param $text Link text
848 * @param $lang Language code
849 */
850 function makeLanguageSelectorLink( $text, $lang ) {
851 global $wgUser;
852 $self = SpecialPage::getTitleFor( 'Userlogin' );
853 $attr[] = 'uselang=' . $lang;
854 if( $this->mType == 'signup' )
855 $attr[] = 'type=signup';
856 if( $this->mReturnTo )
857 $attr[] = 'returnto=' . $this->mReturnTo;
858 $skin = $wgUser->getSkin();
859 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
860 }
861 }
862
863