10 function wfSpecialUserlogin( $par = '' ) {
12 if( session_id() == '' ) {
16 $form = new LoginForm( $wgRequest, $par );
21 * implements Special:Login
22 * @ingroup SpecialPage
29 const WRONG_PLUGIN_PASS = 3;
35 const CREATE_BLOCKED = 9;
37 const USER_BLOCKED = 11;
38 const NEED_TOKEN = 12;
39 const WRONG_TOKEN = 13;
41 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
42 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
43 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage, $mSkipCookieCheck;
48 * @param WebRequest $request A WebRequest object passed by reference
50 function LoginForm( &$request, $par = '' ) {
51 global $wgLang, $wgAllowRealName, $wgEnableEmail;
52 global $wgAuth, $wgRedirectOnLogin;
54 $this->mType = ( $par == 'signup' ) ? $par : $request->getText( 'type' ); # Check for [[Special:Userlogin/signup]]
55 $this->mName = $request->getText( 'wpName' );
56 $this->mPassword = $request->getText( 'wpPassword' );
57 $this->mRetype = $request->getText( 'wpRetype' );
58 $this->mDomain = $request->getText( 'wpDomain' );
59 $this->mReturnTo = $request->getVal( 'returnto' );
60 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
61 $this->mPosted = $request->wasPosted();
62 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
63 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
65 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
67 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
68 $this->mAction = $request->getVal( 'action' );
69 $this->mRemember = $request->getCheck( 'wpRemember' );
70 $this->mLanguage = $request->getText( 'uselang' );
71 $this->mSkipCookieCheck = $request->getCheck( 'wpSkipCookieCheck' );
72 $this->mToken = ($this->mType == 'signup' ) ? $request->getVal( 'wpCreateaccountToken' ) : $request->getVal( 'wpLoginToken' );
74 if ( $wgRedirectOnLogin ) {
75 $this->mReturnTo = $wgRedirectOnLogin;
78 if( $wgEnableEmail ) {
79 $this->mEmail = $request->getText( 'wpEmail' );
83 if( $wgAllowRealName ) {
84 $this->mRealName = $request->getText( 'wpRealName' );
86 $this->mRealName = '';
89 if( !$wgAuth->validDomain( $this->mDomain ) ) {
90 $this->mDomain = 'invaliddomain';
92 $wgAuth->setDomain( $this->mDomain );
94 # When switching accounts, it sucks to get automatically logged out
95 if( $this->mReturnTo == $wgLang->specialPage( 'Userlogout' ) ) {
96 $this->mReturnTo = '';
101 if ( !is_null( $this->mCookieCheck ) ) {
102 $this->onCookieRedirectCheck( $this->mCookieCheck );
104 } else if( $this->mPosted ) {
105 if( $this->mCreateaccount ) {
106 return $this->addNewAccount();
107 } else if ( $this->mCreateaccountMail ) {
108 return $this->addNewAccountMailPassword();
109 } else if ( $this->mMailmypassword ) {
110 return $this->mailPassword();
111 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
112 return $this->processLogin();
115 $this->mainLoginForm( '' );
121 function addNewAccountMailPassword() {
124 if ('' == $this->mEmail) {
125 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
129 $u = $this->addNewaccountInternal();
135 // Wipe the initial password and mail a temporary one
136 $u->setPassword( null );
138 $result = $this->mailPasswordInternal( $u, false, 'createaccount-title', 'createaccount-text' );
140 wfRunHooks( 'AddNewAccount', array( $u, true ) );
141 $u->addNewUserLogEntry();
143 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
144 $wgOut->setRobotPolicy( 'noindex,nofollow' );
145 $wgOut->setArticleRelated( false );
147 if( WikiError::isError( $result ) ) {
148 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
150 $wgOut->addWikiMsg( 'accmailtext', $u->getName(), $u->getEmail() );
151 $wgOut->returnToMain( false );
160 function addNewAccount() {
161 global $wgUser, $wgEmailAuthentication;
163 # Create the account and abort if there's a problem doing so
164 $u = $this->addNewAccountInternal();
168 # If we showed up language selection links, and one was in use, be
169 # smart (and sensible) and save that language as the user's preference
170 global $wgLoginLanguageSelector;
171 if( $wgLoginLanguageSelector && $this->mLanguage )
172 $u->setOption( 'language', $this->mLanguage );
174 # Send out an email authentication message if needed
175 if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) ) {
177 $error = $u->sendConfirmationMail();
178 if( WikiError::isError( $error ) ) {
179 $wgOut->addWikiMsg( 'confirmemail_sendfailed', $error->getMessage() );
181 $wgOut->addWikiMsg( 'confirmemail_oncreate' );
185 # Save settings (including confirmation token)
188 # If not logged in, assume the new account as the current one and set
189 # session cookies then show a "welcome" message or a "need cookies"
191 if( $wgUser->isAnon() ) {
193 $wgUser->setCookies();
194 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
195 $wgUser->addNewUserLogEntry();
196 if( $this->hasSessionCookie() ) {
197 return $this->successfulCreation();
199 return $this->cookieRedirectCheck( 'new' );
202 # Confirm that the account was created
204 $self = SpecialPage::getTitleFor( 'Userlogin' );
205 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
206 $wgOut->setArticleRelated( false );
207 $wgOut->setRobotPolicy( 'noindex,nofollow' );
208 $wgOut->addHTML( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
209 $wgOut->returnToMain( false, $self );
210 wfRunHooks( 'AddNewAccount', array( $u ) );
211 $u->addNewUserLogEntry();
219 function addNewAccountInternal() {
220 global $wgUser, $wgOut;
221 global $wgEnableSorbs, $wgProxyWhitelist;
222 global $wgMemc, $wgAccountCreationThrottle;
223 global $wgAuth, $wgMinimalPasswordLength;
224 global $wgEmailConfirmToEdit;
226 // If the user passes an invalid domain, something is fishy
227 if( !$wgAuth->validDomain( $this->mDomain ) ) {
228 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
232 // If we are not allowing users to login locally, we should be checking
233 // to see if the user is actually able to authenticate to the authenti-
234 // cation server before they create an account (otherwise, they can
235 // create a local account and login as any domain user). We only need
236 // to check this for domains that aren't local.
237 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
238 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
239 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
244 if ( wfReadOnly() ) {
245 $wgOut->readOnlyPage();
249 # Request forgery checks.
250 if ( !self::getCreateaccountToken() ) {
251 self::setCreateaccountToken();
252 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
256 # The user didn't pass a createaccount token
257 if ( !$this->mToken ) {
258 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
262 # Validate the createaccount token
263 if ( $this->mToken !== self::getCreateaccountToken() ) {
264 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
269 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
270 $this->userNotPrivilegedMessage();
272 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
273 $this->userBlockedMessage();
278 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
279 $wgUser->inSorbsBlacklist( $ip ) )
281 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
285 # Now create a dummy user ($u) and check if it is valid
286 $name = trim( $this->mName );
287 $u = User::newFromName( $name, 'creatable' );
288 if ( is_null( $u ) ) {
289 $this->mainLoginForm( wfMsg( 'noname' ) );
293 if ( 0 != $u->idForName() ) {
294 $this->mainLoginForm( wfMsg( 'userexists' ) );
298 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
299 $this->mainLoginForm( wfMsg( 'badretype' ) );
303 # check for minimal password length
304 if ( !$u->isValidPassword( $this->mPassword ) ) {
305 if ( !$this->mCreateaccountMail ) {
306 $this->mainLoginForm( wfMsgExt( 'passwordtooshort', array( 'parsemag' ), $wgMinimalPasswordLength ) );
309 # do not force a password for account creation by email
310 # set invalid password, it will be replaced later by a random generated password
311 $this->mPassword = null;
315 # if you need a confirmed email address to edit, then obviously you
316 # need an email address.
317 if ( $wgEmailConfirmToEdit && empty( $this->mEmail ) ) {
318 $this->mainLoginForm( wfMsg( 'noemailtitle' ) );
322 if( !empty( $this->mEmail ) && !User::isValidEmailAddr( $this->mEmail ) ) {
323 $this->mainLoginForm( wfMsg( 'invalidemailaddress' ) );
327 # Set some additional data so the AbortNewAccount hook can be used for
328 # more than just username validation
329 $u->setEmail( $this->mEmail );
330 $u->setRealName( $this->mRealName );
333 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
334 // Hook point to add extra creation throttles and blocks
335 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
336 $this->mainLoginForm( $abortError );
340 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
341 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
342 $value = $wgMemc->get( $key );
344 $wgMemc->set( $key, 0, 86400 );
346 if ( $value >= $wgAccountCreationThrottle ) {
347 $this->throttleHit( $wgAccountCreationThrottle );
350 $wgMemc->incr( $key );
353 if( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
354 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
358 self::clearCreateaccountToken();
359 return $this->initUser( $u, false );
363 * Actually add a user to the database.
364 * Give it a User object that has been initialised with a name.
366 * @param $u User object.
367 * @param $autocreate boolean -- true if this is an autocreation via auth plugin
368 * @return User object.
371 function initUser( $u, $autocreate ) {
376 if ( $wgAuth->allowPasswordChange() ) {
377 $u->setPassword( $this->mPassword );
380 $u->setEmail( $this->mEmail );
381 $u->setRealName( $this->mRealName );
384 $wgAuth->initUser( $u, $autocreate );
386 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
390 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
391 $ssUpdate->doUpdate();
397 * Internally authenticate the login request.
399 * This may create a local account as a side effect if the
400 * authentication plugin allows transparent local account
405 function authenticateUserData() {
406 global $wgUser, $wgAuth;
407 if ( '' == $this->mName ) {
408 return self::NO_NAME;
411 // We require a login token to prevent login CSRF
412 // Handle part of this before incrementing the throttle so
413 // token-less login attempts don't count towards the throttle
414 // but wrong-token attempts do.
416 // If the user doesn't have a login token yet, set one.
417 if ( !self::getLoginToken() ) {
418 self::setLoginToken();
419 return self::NEED_TOKEN;
421 // If the user didn't pass a login token, tell them we need one
422 if ( !$this->mToken ) {
423 return self::NEED_TOKEN;
426 global $wgPasswordAttemptThrottle;
429 if ( is_array($wgPasswordAttemptThrottle) ) {
430 $throttleKey = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mName ) );
431 $count = $wgPasswordAttemptThrottle['count'];
432 $period = $wgPasswordAttemptThrottle['seconds'];
435 $throttleCount = $wgMemc->get($throttleKey);
436 if ( !$throttleCount ) {
437 $wgMemc->add( $throttleKey, 1, $period ); // start counter
438 } else if ( $throttleCount < $count ) {
439 $wgMemc->incr($throttleKey);
440 } else if ( $throttleCount >= $count ) {
441 return self::THROTTLED;
445 // Validate the login token
446 if ( $this->mToken !== self::getLoginToken() ) {
447 return self::WRONG_TOKEN;
450 // Load $wgUser now, and check to see if we're logging in as the same
451 // name. This is necessary because loading $wgUser (say by calling
452 // getName()) calls the UserLoadFromSession hook, which potentially
453 // creates the user in the database. Until we load $wgUser, checking
454 // for user existence using User::newFromName($name)->getId() below
455 // will effectively be using stale data.
456 if ( $wgUser->getName() === $this->mName ) {
457 wfDebug( __METHOD__.": already logged in as {$this->mName}\n" );
458 return self::SUCCESS;
460 $u = User::newFromName( $this->mName );
461 if( is_null( $u ) || !User::isUsableName( $u->getName() ) ) {
462 return self::ILLEGAL;
465 $isAutoCreated = false;
466 if ( 0 == $u->getID() ) {
467 $status = $this->attemptAutoCreate( $u );
468 if ( $status !== self::SUCCESS ) {
471 $isAutoCreated = true;
477 // Give general extensions, such as a captcha, a chance to abort logins
478 $abort = self::ABORTED;
479 if( !wfRunHooks( 'AbortLogin', array( $u, $this->mPassword, &$abort ) ) ) {
483 if (!$u->checkPassword( $this->mPassword )) {
484 if( $u->checkTemporaryPassword( $this->mPassword ) ) {
485 // The e-mailed temporary password should not be used for actu-
486 // al logins; that's a very sloppy habit, and insecure if an
487 // attacker has a few seconds to click "search" on someone's o-
490 // Allow it to be used only to reset the password a single time
491 // to a new value, which won't be in the user's e-mail ar-
494 // For backwards compatibility, we'll still recognize it at the
495 // login form to minimize surprises for people who have been
496 // logging in with a temporary password for some time.
498 // As a side-effect, we can authenticate the user's e-mail ad-
499 // dress if it's not already done, since the temporary password
500 // was sent via e-mail.
501 if( !$u->isEmailConfirmed() ) {
506 // At this point we just return an appropriate code/ indicating
507 // that the UI should show a password reset form; bot inter-
508 // faces etc will probably just fail cleanly here.
509 $retval = self::RESET_PASS;
511 $retval = '' == $this->mPassword ? self::EMPTY_PASS : self::WRONG_PASS;
514 $wgAuth->updateUser( $u );
517 // Please reset throttle for successful logins, thanks!
519 $wgMemc->delete($throttleKey);
522 if ( $isAutoCreated ) {
523 // Must be run after $wgUser is set, for correct new user log
524 wfRunHooks( 'AuthPluginAutoCreate', array( $wgUser ) );
527 $retval = self::SUCCESS;
529 wfRunHooks( 'LoginAuthenticateAudit', array( $u, $this->mPassword, $retval ) );
534 * Attempt to automatically create a user on login. Only succeeds if there
535 * is an external authentication method which allows it.
536 * @return integer Status code
538 function attemptAutoCreate( $user ) {
539 global $wgAuth, $wgUser;
541 * If the external authentication plugin allows it, automatically cre-
542 * ate a new account for users that are externally defined but have not
545 if ( !$wgAuth->autoCreate() ) {
546 return self::NOT_EXISTS;
548 if ( !$wgAuth->userExists( $user->getName() ) ) {
549 wfDebug( __METHOD__.": user does not exist\n" );
550 return self::NOT_EXISTS;
552 if ( !$wgAuth->authenticate( $user->getName(), $this->mPassword ) ) {
553 wfDebug( __METHOD__.": \$wgAuth->authenticate() returned false, aborting\n" );
554 return self::WRONG_PLUGIN_PASS;
556 if ( $wgUser->isBlockedFromCreateAccount() ) {
557 wfDebug( __METHOD__.": user is blocked from account creation\n" );
558 return self::CREATE_BLOCKED;
561 wfDebug( __METHOD__.": creating account\n" );
562 $user = $this->initUser( $user, true );
563 return self::SUCCESS;
566 function processLogin() {
567 global $wgUser, $wgAuth;
569 switch ($this->authenticateUserData())
572 # We've verified now, update the real record
573 if( (bool)$this->mRemember != (bool)$wgUser->getOption( 'rememberpassword' ) ) {
574 $wgUser->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
575 $wgUser->saveSettings();
577 $wgUser->invalidateCache();
579 $wgUser->setCookies();
580 self::clearLoginToken();
582 // Reset the throttle
583 $key = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mName ) );
585 $wgMemc->delete( $key );
587 if( $this->hasSessionCookie() || $this->mSkipCookieCheck ) {
588 /* Replace the language object to provide user interface in
589 * correct language immediately on this first page load.
591 global $wgLang, $wgRequest;
592 $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
593 $wgLang = Language::factory( $code );
594 return $this->successfulLogin();
596 return $this->cookieRedirectCheck( 'login' );
600 case self::NEED_TOKEN:
601 case self::WRONG_TOKEN:
602 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
606 $this->mainLoginForm( wfMsg( 'noname' ) );
608 case self::WRONG_PLUGIN_PASS:
609 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
611 case self::NOT_EXISTS:
612 if( $wgUser->isAllowed( 'createaccount' ) ){
613 $this->mainLoginForm( wfMsgWikiHtml( 'nosuchuser', htmlspecialchars( $this->mName ) ) );
615 $this->mainLoginForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->mName ) ) );
618 case self::WRONG_PASS:
619 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
621 case self::EMPTY_PASS:
622 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
624 case self::RESET_PASS:
625 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
627 case self::CREATE_BLOCKED:
628 $this->userBlockedMessage();
630 case self::THROTTLED:
631 $this->mainLoginForm( wfMsg( 'login-throttled' ) );
634 throw new MWException( "Unhandled case value" );
638 function resetLoginForm( $error ) {
640 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $error ) );
641 $reset = new SpecialResetpass();
642 $reset->execute( null );
648 function mailPassword() {
649 global $wgUser, $wgOut, $wgAuth;
651 if ( wfReadOnly() ) {
652 $wgOut->readOnlyPage();
656 if( !$wgAuth->allowPasswordChange() ) {
657 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
661 # Check against blocked IPs so blocked users can't flood admins
662 # with password resets
663 if( $wgUser->isBlocked() ) {
664 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
668 # If the user doesn't have a login token yet, set one.
669 if ( !self::getLoginToken() ) {
670 self::setLoginToken();
671 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
675 # If the user didn't pass a login token, tell them we need one
676 if ( !$this->mToken ) {
677 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
681 # Check against the rate limiter
682 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
683 $wgOut->rateLimited();
687 if ( '' == $this->mName ) {
688 $this->mainLoginForm( wfMsg( 'noname' ) );
691 $u = User::newFromName( $this->mName );
692 if( is_null( $u ) ) {
693 $this->mainLoginForm( wfMsg( 'noname' ) );
696 if ( 0 == $u->getID() ) {
697 $this->mainLoginForm( wfMsgWikiHtml( 'nosuchuser', htmlspecialchars( $u->getName() ) ) );
701 # Validate the login token
702 if ( $this->mToken !== self::getLoginToken() ) {
703 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
707 # Check against password throttle
708 if ( $u->isPasswordReminderThrottled() ) {
709 global $wgPasswordReminderResendTime;
710 # Round the time in hours to 3 d.p., in case someone is specifying
711 # minutes or seconds.
712 $this->mainLoginForm( wfMsgExt( 'throttled-mailpassword', array( 'parsemag' ),
713 round( $wgPasswordReminderResendTime, 3 ) ) );
717 $result = $this->mailPasswordInternal( $u, true, 'passwordremindertitle', 'passwordremindertext' );
718 if( WikiError::isError( $result ) ) {
719 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
721 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
722 self::clearLoginToken();
729 * @param bool throttle
730 * @param string message name of email title
731 * @param string message name of email text
732 * @return mixed true on success, WikiError on failure
735 function mailPasswordInternal( $u, $throttle = true, $emailTitle = 'passwordremindertitle', $emailText = 'passwordremindertext' ) {
736 global $wgServer, $wgScript, $wgUser, $wgNewPasswordExpiry;
738 if ( '' == $u->getEmail() ) {
739 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
743 return new WikiError( wfMsg( 'badipaddress' ) );
746 wfRunHooks( 'User::mailPasswordInternal', array(&$wgUser, &$ip, &$u) );
748 $np = $u->randomPassword();
749 $u->setNewpassword( $np, $throttle );
752 $m = wfMsgExt( $emailText, array( 'parsemag' ), $ip, $u->getName(), $np,
753 $wgServer . $wgScript, round( $wgNewPasswordExpiry / 86400 ) );
754 $result = $u->sendMail( wfMsg( $emailTitle ), $m );
761 * Run any hooks registered for logins, then HTTP redirect to
762 * $this->mReturnTo (or Main Page if that's undefined). Formerly we had a
763 * nice message here, but that's really not as useful as just being sent to
764 * wherever you logged in from. It should be clear that the action was
765 * successful, given the lack of error messages plus the appearance of your
766 * name in the upper right.
770 function successfulLogin() {
771 global $wgUser, $wgOut;
773 # Run any hooks; display injected HTML if any, else redirect
775 wfRunHooks('UserLoginComplete', array(&$wgUser, &$injected_html));
777 if( $injected_html !== '' ) {
778 $this->displaySuccessfulLogin( 'loginsuccess', $injected_html );
780 $titleObj = Title::newFromText( $this->mReturnTo );
781 if ( !$titleObj instanceof Title ) {
782 $titleObj = Title::newMainPage();
785 $wgOut->redirect( $titleObj->getFullURL() );
790 * Run any hooks registered for logins, then display a message welcoming
795 function successfulCreation() {
796 global $wgUser, $wgOut;
798 # Run any hooks; display injected HTML
800 wfRunHooks('UserLoginComplete', array(&$wgUser, &$injected_html));
802 $this->displaySuccessfulLogin( 'welcomecreation', $injected_html );
806 * Display a "login successful" page.
808 private function displaySuccessfulLogin( $msgname, $injected_html ) {
809 global $wgOut, $wgUser;
811 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
812 $wgOut->setRobotPolicy( 'noindex,nofollow' );
813 $wgOut->setArticleRelated( false );
814 $wgOut->addWikiMsg( $msgname, $wgUser->getName() );
815 $wgOut->addHTML( $injected_html );
817 if ( !empty( $this->mReturnTo ) ) {
818 $wgOut->returnToMain( null, $this->mReturnTo );
820 $wgOut->returnToMain( null );
825 function userNotPrivilegedMessage($errors) {
828 $wgOut->setPageTitle( wfMsg( 'permissionserrors' ) );
829 $wgOut->setRobotPolicy( 'noindex,nofollow' );
830 $wgOut->setArticleRelated( false );
832 $wgOut->addWikitext( $wgOut->formatPermissionsErrorMessage( $errors, 'createaccount' ) );
833 // Stuff that might want to be added at the end. For example, instruc-
835 $wgOut->addWikiMsg( 'cantcreateaccount-nonblock-text' );
837 $wgOut->returnToMain( false );
841 function userBlockedMessage() {
842 global $wgOut, $wgUser;
844 # Let's be nice about this, it's likely that this feature will be used
845 # for blocking large numbers of innocent people, e.g. range blocks on
846 # schools. Don't blame it on the user. There's a small chance that it
847 # really is the user's fault, i.e. the username is blocked and they
848 # haven't bothered to log out before trying to create an account to
849 # evade it, but we'll leave that to their guilty conscience to figure
852 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
853 $wgOut->setRobotPolicy( 'noindex,nofollow' );
854 $wgOut->setArticleRelated( false );
857 $blocker = User::whoIs( $wgUser->mBlock->mBy );
858 $block_reason = $wgUser->mBlock->mReason;
860 if ( strval( $block_reason ) === '' ) {
861 $block_reason = wfMsg( 'blockednoreason' );
863 $wgOut->addWikiMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker );
864 $wgOut->returnToMain( false );
870 function mainLoginForm( $msg, $msgtype = 'error' ) {
871 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
872 global $wgCookiePrefix, $wgLoginLanguageSelector;
873 global $wgAuth, $wgEmailConfirmToEdit, $wgCookieExpiration;
875 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
877 if ( $this->mType == 'signup' ) {
878 // Block signup here if in readonly. Keeps user from
879 // going through the process (filling out data, etc)
880 // and being informed later.
881 if ( wfReadOnly() ) {
882 $wgOut->readOnlyPage();
884 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
885 $this->userBlockedMessage();
887 } elseif ( count( $permErrors = $titleObj->getUserPermissionsErrors( 'createaccount', $wgUser, true ) )>0 ) {
888 $wgOut->showPermissionsErrorPage( $permErrors, 'createaccount' );
893 if ( '' == $this->mName ) {
894 if ( $wgUser->isLoggedIn() ) {
895 $this->mName = $wgUser->getName();
897 $this->mName = isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ? $_COOKIE[$wgCookiePrefix.'UserName'] : null;
901 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
903 if ( $this->mType == 'signup' ) {
904 $template = new UsercreateTemplate();
905 $q = 'action=submitlogin&type=signup';
906 $linkq = 'type=login';
907 $linkmsg = 'gotaccount';
909 $template = new UserloginTemplate();
910 $q = 'action=submitlogin&type=login';
911 $linkq = 'type=signup';
912 $linkmsg = 'nologin';
915 if ( !empty( $this->mReturnTo ) ) {
916 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
921 # Pass any language selection on to the mode switch link
922 if( $wgLoginLanguageSelector && $this->mLanguage )
923 $linkq .= '&uselang=' . $this->mLanguage;
925 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
926 $link .= wfMsgHtml( $linkmsg . 'link' ); # Calling either 'gotaccountlink' or 'nologinlink'
929 # Don't show a "create account" link if the user can't
930 if( $this->showCreateOrLoginLink( $wgUser ) )
931 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
933 $template->set( 'link', '' );
935 $template->set( 'header', '' );
936 $template->set( 'name', $this->mName );
937 $template->set( 'password', $this->mPassword );
938 $template->set( 'retype', $this->mRetype );
939 $template->set( 'email', $this->mEmail );
940 $template->set( 'realname', $this->mRealName );
941 $template->set( 'domain', $this->mDomain );
943 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
944 $template->set( 'message', $msg );
945 $template->set( 'messagetype', $msgtype );
946 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
947 $template->set( 'userealname', $wgAllowRealName );
948 $template->set( 'useemail', $wgEnableEmail );
949 $template->set( 'emailrequired', $wgEmailConfirmToEdit );
950 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
951 $template->set( 'canremember', ( $wgCookieExpiration > 0 ) );
952 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
954 if ( $this->mType == 'signup' ) {
955 if ( !self::getCreateaccountToken() ) {
956 self::setCreateaccountToken();
958 $template->set( 'token', self::getCreateaccountToken() );
960 if ( !self::getLoginToken() ) {
961 self::setLoginToken();
963 $template->set( 'token', self::getLoginToken() );
966 # Prepare language selection links as needed
967 if( $wgLoginLanguageSelector ) {
968 $template->set( 'languages', $this->makeLanguageSelector() );
969 if( $this->mLanguage )
970 $template->set( 'uselang', $this->mLanguage );
973 // Give authentication and captcha plugins a chance to modify the form
974 $wgAuth->modifyUITemplate( $template );
975 if ( $this->mType == 'signup' ) {
976 wfRunHooks( 'UserCreateForm', array( &$template ) );
978 wfRunHooks( 'UserLoginForm', array( &$template ) );
981 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
982 $wgOut->setRobotPolicy( 'noindex,nofollow' );
983 $wgOut->setArticleRelated( false );
984 $wgOut->disallowUserJs(); // just in case...
985 $wgOut->addTemplate( $template );
991 function showCreateOrLoginLink( &$user ) {
992 if( $this->mType == 'signup' ) {
994 } elseif( $user->isAllowed( 'createaccount' ) ) {
1002 * Check if a session cookie is present.
1004 * This will not pick up a cookie set during _this_ request, but is meant
1005 * to ensure that the client is returning the cookie which was set on a
1006 * previous pass through the system.
1010 function hasSessionCookie() {
1011 global $wgDisableCookieCheck, $wgRequest;
1012 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
1016 * Get the login token from the current session
1018 public static function getLoginToken() {
1020 return $wgRequest->getSessionData( 'wsLoginToken' );
1024 * Randomly generate a new login token and attach it to the current session
1026 public static function setLoginToken() {
1028 // Use User::generateToken() instead of $user->editToken()
1029 // because the latter reuses $_SESSION['wsEditToken']
1030 $wgRequest->setSessionData( 'wsLoginToken', User::generateToken() );
1034 * Remove any login token attached to the current session
1036 public static function clearLoginToken() {
1038 $wgRequest->setSessionData( 'wsLoginToken', null );
1042 * Get the createaccount token from the current session
1044 public static function getCreateaccountToken() {
1046 return $wgRequest->getSessionData( 'wsCreateaccountToken' );
1050 * Randomly generate a new createaccount token and attach it to the current session
1052 public static function setCreateaccountToken() {
1054 $wgRequest->setSessionData( 'wsCreateaccountToken', User::generateToken() );
1058 * Remove any createaccount token attached to the current session
1060 public static function clearCreateaccountToken() {
1062 $wgRequest->setSessionData( 'wsCreateaccountToken', null );
1068 function cookieRedirectCheck( $type ) {
1071 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
1072 $query = array( 'wpCookieCheck' => $type );
1073 if ( $this->mReturnTo ) $query['returnto'] = $this->mReturnTo;
1074 $check = $titleObj->getFullURL( $query );
1076 return $wgOut->redirect( $check );
1082 function onCookieRedirectCheck( $type ) {
1085 if ( !$this->hasSessionCookie() ) {
1086 if ( $type == 'new' ) {
1087 return $this->mainLoginForm( wfMsgExt( 'nocookiesnew', array( 'parseinline' ) ) );
1088 } else if ( $type == 'login' ) {
1089 return $this->mainLoginForm( wfMsgExt( 'nocookieslogin', array( 'parseinline' ) ) );
1092 return $this->mainLoginForm( wfMsg( 'error' ) );
1095 return $this->successfulLogin();
1102 function throttleHit( $limit ) {
1103 $this->mainLoginForm( wfMsgExt( 'acct_creation_throttle_hit', array( 'parseinline' ), $limit ) );
1107 * Produce a bar of links which allow the user to select another language
1108 * during login/registration but retain "returnto"
1112 function makeLanguageSelector() {
1115 $msg = wfMsgForContent( 'loginlanguagelinks' );
1116 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
1117 $langs = explode( "\n", $msg );
1119 foreach( $langs as $lang ) {
1120 $lang = trim( $lang, '* ' );
1121 $parts = explode( '|', $lang );
1122 if (count($parts) >= 2) {
1123 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
1126 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', $wgLang->pipeList( $links ) ) : '';
1133 * Create a language selector link for a particular language
1134 * Links back to this page preserving type and returnto
1136 * @param $text Link text
1137 * @param $lang Language code
1139 function makeLanguageSelectorLink( $text, $lang ) {
1141 $self = SpecialPage::getTitleFor( 'Userlogin' );
1142 $attr[] = 'uselang=' . $lang;
1143 if( $this->mType == 'signup' )
1144 $attr[] = 'type=signup';
1145 if( $this->mReturnTo )
1146 $attr[] = 'returnto=' . $this->mReturnTo;
1147 $skin = $wgUser->getSkin();
1148 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );