4 * @addtogroup SpecialPage
10 function wfSpecialUserlogin() {
11 global $wgCommandLineMode;
13 if( session_id() == '' ) {
17 $form = new LoginForm( $wgRequest );
22 * implements Special:Login
23 * @addtogroup SpecialPage
30 const WRONG_PLUGIN_PASS
= 3;
36 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
37 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
38 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
42 * @param WebRequest $request A WebRequest object passed by reference
44 function LoginForm( &$request ) {
45 global $wgLang, $wgAllowRealName, $wgEnableEmail;
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' )
59 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
61 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
62 $this->mAction
= $request->getVal( 'action' );
63 $this->mRemember
= $request->getCheck( 'wpRemember' );
64 $this->mLanguage
= $request->getText( 'uselang' );
66 if( $wgEnableEmail ) {
67 $this->mEmail
= $request->getText( 'wpEmail' );
71 if( $wgAllowRealName ) {
72 $this->mRealName
= $request->getText( 'wpRealName' );
74 $this->mRealName
= '';
77 if( !$wgAuth->validDomain( $this->mDomain
) ) {
78 $this->mDomain
= 'invaliddomain';
80 $wgAuth->setDomain( $this->mDomain
);
82 # When switching accounts, it sucks to get automatically logged out
83 if( $this->mReturnTo
== $wgLang->specialPage( 'Userlogout' ) ) {
84 $this->mReturnTo
= '';
89 if ( !is_null( $this->mCookieCheck
) ) {
90 $this->onCookieRedirectCheck( $this->mCookieCheck
);
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();
103 $this->mainLoginForm( '' );
109 function addNewAccountMailPassword() {
112 if ('' == $this->mEmail
) {
113 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
117 $u = $this->addNewaccountInternal();
123 // Wipe the initial password and mail a temporary one
124 $u->setPassword( null );
126 $result = $this->mailPasswordInternal( $u, false );
128 wfRunHooks( 'AddNewAccount', array( $u ) );
130 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
131 $wgOut->setRobotpolicy( 'noindex,nofollow' );
132 $wgOut->setArticleRelated( false );
134 if( WikiError
::isError( $result ) ) {
135 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
137 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
138 $wgOut->returnToMain( false );
147 function addNewAccount() {
148 global $wgUser, $wgEmailAuthentication;
150 # Create the account and abort if there's a problem doing so
151 $u = $this->addNewAccountInternal();
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
);
161 # Save user settings and send out an email authentication message if needed
163 if( $wgEmailAuthentication && User
::isValidEmailAddr( $u->getEmail() ) ) {
165 $error = $u->sendConfirmationMail();
166 if( WikiError
::isError( $error ) ) {
167 $wgOut->addWikiText( wfMsg( 'confirmemail_sendfailed', $error->getMessage() ) );
169 $wgOut->addWikiText( wfMsg( 'confirmemail_oncreate' ) );
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() ) {
177 $wgUser->setCookies();
178 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
179 if( $this->hasSessionCookie() ) {
180 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
182 return $this->cookieRedirectCheck( 'new' );
185 # Confirm that the account was created
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( $self->getPrefixedText() );
193 wfRunHooks( 'AddNewAccount', array( $u ) );
201 function addNewAccountInternal() {
202 global $wgUser, $wgOut;
203 global $wgEnableSorbs, $wgProxyWhitelist;
204 global $wgMemc, $wgAccountCreationThrottle;
205 global $wgAuth, $wgMinimalPasswordLength;
207 // If the user passes an invalid domain, something is fishy
208 if( !$wgAuth->validDomain( $this->mDomain
) ) {
209 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
213 // If we are not allowing users to login locally, we should
214 // be checking to see if the user is actually able to
215 // authenticate to the authentication server before they
216 // create an account (otherwise, they can create a local account
217 // and login as any domain user). We only need to check this for
218 // domains that aren't local.
219 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
220 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
221 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
226 if ( wfReadOnly() ) {
227 $wgOut->readOnlyPage();
231 if (!$wgUser->isAllowedToCreateAccount()) {
232 $this->userNotPrivilegedMessage();
237 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
238 $wgUser->inSorbsBlacklist( $ip ) )
240 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
244 $name = trim( $this->mName
);
245 $u = User
::newFromName( $name, 'creatable' );
246 if ( is_null( $u ) ) {
247 $this->mainLoginForm( wfMsg( 'noname' ) );
251 if ( 0 != $u->idForName() ) {
252 $this->mainLoginForm( wfMsg( 'userexists' ) );
256 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
257 $this->mainLoginForm( wfMsg( 'badretype' ) );
261 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
262 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
267 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
268 // Hook point to add extra creation throttles and blocks
269 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
270 $this->mainLoginForm( $abortError );
274 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
275 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
276 $value = $wgMemc->incr( $key );
278 $wgMemc->set( $key, 1, 86400 );
280 if ( $value > $wgAccountCreationThrottle ) {
281 $this->throttleHit( $wgAccountCreationThrottle );
286 if( !$wgAuth->addUser( $u, $this->mPassword
, $this->mEmail
, $this->mRealName
) ) {
287 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
291 return $this->initUser( $u );
295 * Actually add a user to the database.
296 * Give it a User object that has been initialised with a name.
298 * @param $u User object.
299 * @return User object.
302 function initUser( $u ) {
307 if ( $wgAuth->allowPasswordChange() ) {
308 $u->setPassword( $this->mPassword
);
311 $u->setEmail( $this->mEmail
);
312 $u->setRealName( $this->mRealName
);
315 $wgAuth->initUser( $u );
317 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
321 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
322 $ssUpdate->doUpdate();
328 * Internally authenticate the login request.
330 * This may create a local account as a side effect if the
331 * authentication plugin allows transparent local account
336 function authenticateUserData() {
337 global $wgUser, $wgAuth;
338 if ( '' == $this->mName
) {
339 return self
::NO_NAME
;
341 $u = User
::newFromName( $this->mName
);
342 if( is_null( $u ) ||
!User
::isUsableName( $u->getName() ) ) {
343 return self
::ILLEGAL
;
345 if ( 0 == $u->getID() ) {
348 * If the external authentication plugin allows it,
349 * automatically create a new account for users that
350 * are externally defined but have not yet logged in.
352 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
353 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
354 $u = $this->initUser( $u );
356 return self
::WRONG_PLUGIN_PASS
;
359 return self
::NOT_EXISTS
;
365 if (!$u->checkPassword( $this->mPassword
)) {
366 if( $u->checkTemporaryPassword( $this->mPassword
) ) {
367 // The e-mailed temporary password should not be used
368 // for actual logins; that's a very sloppy habit,
369 // and insecure if an attacker has a few seconds to
370 // click "search" on someone's open mail reader.
372 // Allow it to be used only to reset the password
373 // a single time to a new value, which won't be in
374 // the user's e-mail archives.
376 // For backwards compatibility, we'll still recognize
377 // it at the login form to minimize surprises for
378 // people who have been logging in with a temporary
379 // password for some time.
381 // As a side-effect, we can authenticate the user's
382 // e-mail address if it's not already done, since
383 // the temporary password was sent via e-mail.
385 if( !$u->isEmailConfirmed() ) {
389 // At this point we just return an appropriate code
390 // indicating that the UI should show a password
391 // reset form; bot interfaces etc will probably just
392 // fail cleanly here.
394 return self
::RESET_PASS
;
396 return '' == $this->mPassword ? self
::EMPTY_PASS
: self
::WRONG_PASS
;
399 $wgAuth->updateUser( $u );
402 return self
::SUCCESS
;
406 function processLogin() {
407 global $wgUser, $wgAuth;
409 switch ($this->authenticateUserData())
412 # We've verified now, update the real record
413 if( (bool)$this->mRemember
!= (bool)$wgUser->getOption( 'rememberpassword' ) ) {
414 $wgUser->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
415 $wgUser->saveSettings();
417 $wgUser->invalidateCache();
419 $wgUser->setCookies();
421 if( $this->hasSessionCookie() ) {
422 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
424 return $this->cookieRedirectCheck( 'login' );
430 $this->mainLoginForm( wfMsg( 'noname' ) );
432 case self
::WRONG_PLUGIN_PASS
:
433 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
435 case self
::NOT_EXISTS
:
436 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName
) ) );
438 case self
::WRONG_PASS
:
439 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
441 case self
::EMPTY_PASS
:
442 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
444 case self
::RESET_PASS
:
445 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
448 wfDebugDieBacktrace( "Unhandled case value" );
452 function resetLoginForm( $error ) {
454 $wgOut->addWikiText( "<div class=\"errorbox\">$error</div>" );
455 $reset = new PasswordResetForm( $this->mName
, $this->mPassword
);
462 function mailPassword() {
463 global $wgUser, $wgOut, $wgAuth;
465 if( !$wgAuth->allowPasswordChange() ) {
466 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
470 # Check against blocked IPs
471 # fixme -- should we not?
472 if( $wgUser->isBlocked() ) {
473 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
477 # Check against the rate limiter
478 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
479 $wgOut->rateLimited();
483 if ( '' == $this->mName
) {
484 $this->mainLoginForm( wfMsg( 'noname' ) );
487 $u = User
::newFromName( $this->mName
);
488 if( is_null( $u ) ) {
489 $this->mainLoginForm( wfMsg( 'noname' ) );
492 if ( 0 == $u->getID() ) {
493 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
497 # Check against password throttle
498 if ( $u->isPasswordReminderThrottled() ) {
499 global $wgPasswordReminderResendTime;
500 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
501 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
502 round( $wgPasswordReminderResendTime, 3 ) ) );
506 $result = $this->mailPasswordInternal( $u, true );
507 if( WikiError
::isError( $result ) ) {
508 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
510 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
516 * @return mixed true on success, WikiError on failure
519 function mailPasswordInternal( $u, $throttle = true ) {
520 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
521 global $wgServer, $wgScript;
523 if ( '' == $u->getEmail() ) {
524 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
527 $np = $u->randomPassword();
528 $u->setNewpassword( $np, $throttle );
530 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
535 if ( '' == $ip ) { $ip = '(Unknown)'; }
537 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
539 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
545 * @param string $msg Message that will be shown on success
546 * @param bool $auto Toggle auto-redirect to main page; default true
549 function successfulLogin( $msg, $auto = true ) {
553 # Run any hooks; ignore results
555 wfRunHooks('UserLoginComplete', array(&$wgUser));
557 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
558 $wgOut->setRobotpolicy( 'noindex,nofollow' );
559 $wgOut->setArticleRelated( false );
560 $wgOut->addWikiText( $msg );
561 if ( !empty( $this->mReturnTo
) ) {
562 $wgOut->returnToMain( $auto, $this->mReturnTo
);
564 $wgOut->returnToMain( $auto );
569 function userNotPrivilegedMessage() {
572 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
573 $wgOut->setRobotpolicy( 'noindex,nofollow' );
574 $wgOut->setArticleRelated( false );
576 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
578 $wgOut->returnToMain( false );
582 function userBlockedMessage() {
585 # Let's be nice about this, it's likely that this feature will be used
586 # for blocking large numbers of innocent people, e.g. range blocks on
587 # schools. Don't blame it on the user. There's a small chance that it
588 # really is the user's fault, i.e. the username is blocked and they
589 # haven't bothered to log out before trying to create an account to
590 # evade it, but we'll leave that to their guilty conscience to figure
593 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
594 $wgOut->setRobotpolicy( 'noindex,nofollow' );
595 $wgOut->setArticleRelated( false );
598 $wgOut->addWikiText( wfMsg( 'cantcreateaccounttext', $ip ) );
599 $wgOut->returnToMain( false );
605 function mainLoginForm( $msg, $msgtype = 'error' ) {
606 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
607 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
610 if ( $this->mType
== 'signup' ) {
611 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
612 $this->userNotPrivilegedMessage();
614 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
615 $this->userBlockedMessage();
620 if ( '' == $this->mName
) {
621 if ( $wgUser->isLoggedIn() ) {
622 $this->mName
= $wgUser->getName();
624 $this->mName
= isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ?
$_COOKIE[$wgCookiePrefix.'UserName'] : null;
628 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
630 if ( $this->mType
== 'signup' ) {
631 $template = new UsercreateTemplate();
632 $q = 'action=submitlogin&type=signup';
633 $linkq = 'type=login';
634 $linkmsg = 'gotaccount';
636 $template = new UserloginTemplate();
637 $q = 'action=submitlogin&type=login';
638 $linkq = 'type=signup';
639 $linkmsg = 'nologin';
642 if ( !empty( $this->mReturnTo
) ) {
643 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo
);
648 # Pass any language selection on to the mode switch link
649 if( $wgLoginLanguageSelector && $this->mLanguage
)
650 $linkq .= '&uselang=' . $this->mLanguage
;
652 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
653 $link .= wfMsgHtml( $linkmsg . 'link' );
656 # Don't show a "create account" link if the user can't
657 if( $this->showCreateOrLoginLink( $wgUser ) )
658 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
660 $template->set( 'link', '' );
662 $template->set( 'header', '' );
663 $template->set( 'name', $this->mName
);
664 $template->set( 'password', $this->mPassword
);
665 $template->set( 'retype', $this->mRetype
);
666 $template->set( 'email', $this->mEmail
);
667 $template->set( 'realname', $this->mRealName
);
668 $template->set( 'domain', $this->mDomain
);
670 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
671 $template->set( 'message', $msg );
672 $template->set( 'messagetype', $msgtype );
673 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
674 $template->set( 'userealname', $wgAllowRealName );
675 $template->set( 'useemail', $wgEnableEmail );
676 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
677 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
679 # Prepare language selection links as needed
680 if( $wgLoginLanguageSelector ) {
681 $template->set( 'languages', $this->makeLanguageSelector() );
682 if( $this->mLanguage
)
683 $template->set( 'uselang', $this->mLanguage
);
686 // Give authentication and captcha plugins a chance to modify the form
687 $wgAuth->modifyUITemplate( $template );
688 if ( $this->mType
== 'signup' ) {
689 wfRunHooks( 'UserCreateForm', array( &$template ) );
691 wfRunHooks( 'UserLoginForm', array( &$template ) );
694 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
695 $wgOut->setRobotpolicy( 'noindex,nofollow' );
696 $wgOut->setArticleRelated( false );
697 $wgOut->addTemplate( $template );
703 function showCreateOrLoginLink( &$user ) {
704 if( $this->mType
== 'signup' ) {
706 } elseif( $user->isAllowed( 'createaccount' ) ) {
714 * Check if a session cookie is present.
716 * This will not pick up a cookie set during _this_ request, but is
717 * meant to ensure that the client is returning the cookie which was
718 * set on a previous pass through the system.
722 function hasSessionCookie() {
723 global $wgDisableCookieCheck, $wgRequest;
724 return $wgDisableCookieCheck ?
true : $wgRequest->checkSessionCookie();
730 function cookieRedirectCheck( $type ) {
733 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
734 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
736 return $wgOut->redirect( $check );
742 function onCookieRedirectCheck( $type ) {
745 if ( !$this->hasSessionCookie() ) {
746 if ( $type == 'new' ) {
747 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
748 } else if ( $type == 'login' ) {
749 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
752 return $this->mainLoginForm( wfMsg( 'error' ) );
755 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
762 function throttleHit( $limit ) {
765 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
769 * Produce a bar of links which allow the user to select another language
770 * during login/registration but retain "returnto"
774 function makeLanguageSelector() {
775 $msg = wfMsgForContent( 'loginlanguagelinks' );
776 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
777 $langs = explode( "\n", $msg );
779 foreach( $langs as $lang ) {
780 $lang = trim( $lang, '* ' );
781 $parts = explode( '|', $lang );
782 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
784 return count( $links ) > 0 ?
wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
791 * Create a language selector link for a particular language
792 * Links back to this page preserving type and returnto
794 * @param $text Link text
795 * @param $lang Language code
797 function makeLanguageSelectorLink( $text, $lang ) {
799 $self = SpecialPage
::getTitleFor( 'Userlogin' );
800 $attr[] = 'uselang=' . $lang;
801 if( $this->mType
== 'signup' )
802 $attr[] = 'type=signup';
803 if( $this->mReturnTo
)
804 $attr[] = 'returnto=' . $this->mReturnTo
;
805 $skin = $wgUser->getSkin();
806 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );