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