5 * @subpackage SpecialPage
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[session_name()] ) ) {
18 $form = new LoginForm( $wgRequest );
25 * @subpackage SpecialPage
33 const WRONG_PLUGIN_PASS
= 3;
39 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
40 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
41 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
45 * @param webrequest $request A webrequest object passed by reference
47 function LoginForm( &$request ) {
48 global $wgLang, $wgAllowRealName, $wgEnableEmail;
51 $this->mType
= $request->getText( 'type' );
52 $this->mName
= $request->getText( 'wpName' );
53 $this->mPassword
= $request->getText( 'wpPassword' );
54 $this->mRetype
= $request->getText( 'wpRetype' );
55 $this->mDomain
= $request->getText( 'wpDomain' );
56 $this->mReturnTo
= $request->getVal( 'returnto' );
57 $this->mCookieCheck
= $request->getVal( 'wpCookieCheck' );
58 $this->mPosted
= $request->wasPosted();
59 $this->mCreateaccount
= $request->getCheck( 'wpCreateaccount' );
60 $this->mCreateaccountMail
= $request->getCheck( 'wpCreateaccountMail' )
62 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
64 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
65 $this->mAction
= $request->getVal( 'action' );
66 $this->mRemember
= $request->getCheck( 'wpRemember' );
67 $this->mLanguage
= $request->getText( 'uselang' );
69 if( $wgEnableEmail ) {
70 $this->mEmail
= $request->getText( 'wpEmail' );
74 if( $wgAllowRealName ) {
75 $this->mRealName
= $request->getText( 'wpRealName' );
77 $this->mRealName
= '';
80 if( !$wgAuth->validDomain( $this->mDomain
) ) {
81 $this->mDomain
= 'invaliddomain';
83 $wgAuth->setDomain( $this->mDomain
);
85 # When switching accounts, it sucks to get automatically logged out
86 if( $this->mReturnTo
== $wgLang->specialPage( 'Userlogout' ) ) {
87 $this->mReturnTo
= '';
92 if ( !is_null( $this->mCookieCheck
) ) {
93 $this->onCookieRedirectCheck( $this->mCookieCheck
);
95 } else if( $this->mPosted
) {
96 if( $this->mCreateaccount
) {
97 return $this->addNewAccount();
98 } else if ( $this->mCreateaccountMail
) {
99 return $this->addNewAccountMailPassword();
100 } else if ( $this->mMailmypassword
) {
101 return $this->mailPassword();
102 } else if ( ( 'submitlogin' == $this->mAction
) ||
$this->mLoginattempt
) {
103 return $this->processLogin();
106 $this->mainLoginForm( '' );
112 function addNewAccountMailPassword() {
115 if ('' == $this->mEmail
) {
116 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
120 $u = $this->addNewaccountInternal();
126 // Wipe the initial password and mail a temporary one
127 $u->setPassword( null );
129 $result = $this->mailPasswordInternal( $u, false );
131 wfRunHooks( 'AddNewAccount', array( $u ) );
133 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
134 $wgOut->setRobotpolicy( 'noindex,nofollow' );
135 $wgOut->setArticleRelated( false );
137 if( WikiError
::isError( $result ) ) {
138 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
140 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
141 $wgOut->returnToMain( false );
150 function addNewAccount() {
151 global $wgUser, $wgEmailAuthentication;
153 # Create the account and abort if there's a problem doing so
154 $u = $this->addNewAccountInternal();
158 # If we showed up language selection links, and one was in use, be
159 # smart (and sensible) and save that language as the user's preference
160 global $wgLoginLanguageSelector;
161 if( $wgLoginLanguageSelector && $this->mLanguage
)
162 $u->setOption( 'language', $this->mLanguage
);
164 # Save user settings and send out an email authentication message if needed
166 if( $wgEmailAuthentication && User
::isValidEmailAddr( $u->getEmail() ) ) {
168 $error = $u->sendConfirmationMail();
169 if( WikiError
::isError( $error ) ) {
170 $wgOut->addWikiText( wfMsg( 'confirmemail_sendfailed', $error->getMessage() ) );
172 $wgOut->addWikiText( wfMsg( 'confirmemail_oncreate' ) );
176 # If not logged in, assume the new account as the current one and set session cookies
177 # then show a "welcome" message or a "need cookies" message as needed
178 if( $wgUser->isAnon() ) {
180 $wgUser->setCookies();
181 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
182 if( $this->hasSessionCookie() ) {
183 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
185 return $this->cookieRedirectCheck( 'new' );
188 # Confirm that the account was created
190 $self = SpecialPage
::getTitleFor( 'Userlogin' );
191 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
192 $wgOut->setArticleRelated( false );
193 $wgOut->setRobotPolicy( 'noindex,nofollow' );
194 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
195 $wgOut->returnToMain( $self->getPrefixedText() );
196 wfRunHooks( 'AddNewAccount', array( $u ) );
204 function addNewAccountInternal() {
205 global $wgUser, $wgOut;
206 global $wgEnableSorbs, $wgProxyWhitelist;
207 global $wgMemc, $wgAccountCreationThrottle;
208 global $wgAuth, $wgMinimalPasswordLength;
210 // If the user passes an invalid domain, something is fishy
211 if( !$wgAuth->validDomain( $this->mDomain
) ) {
212 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
216 // If we are not allowing users to login locally, we should
217 // be checking to see if the user is actually able to
218 // authenticate to the authentication server before they
219 // create an account (otherwise, they can create a local account
220 // and login as any domain user). We only need to check this for
221 // domains that aren't local.
222 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
223 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
224 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
229 if ( wfReadOnly() ) {
230 $wgOut->readOnlyPage();
234 if (!$wgUser->isAllowedToCreateAccount()) {
235 $this->userNotPrivilegedMessage();
240 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
241 $wgUser->inSorbsBlacklist( $ip ) )
243 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
247 $name = trim( $this->mName
);
248 $u = User
::newFromName( $name, 'creatable' );
249 if ( is_null( $u ) ) {
250 $this->mainLoginForm( wfMsg( 'noname' ) );
254 if ( 0 != $u->idForName() ) {
255 $this->mainLoginForm( wfMsg( 'userexists' ) );
259 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
260 $this->mainLoginForm( wfMsg( 'badretype' ) );
264 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
265 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
270 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
271 // Hook point to add extra creation throttles and blocks
272 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
273 $this->mainLoginForm( $abortError );
277 if ( $wgAccountCreationThrottle ) {
278 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
279 $value = $wgMemc->incr( $key );
281 $wgMemc->set( $key, 1, 86400 );
283 if ( $value > $wgAccountCreationThrottle ) {
284 $this->throttleHit( $wgAccountCreationThrottle );
289 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
290 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
295 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
296 $ssUpdate->doUpdate();
298 return $this->initUser( $u );
302 * Actually add a user to the database.
303 * Give it a User object that has been initialised with a name.
305 * @param $u User object.
306 * @return User object.
309 function initUser( $u ) {
311 $u->setPassword( $this->mPassword
);
312 $u->setEmail( $this->mEmail
);
313 $u->setRealName( $this->mRealName
);
317 $wgAuth->initUser( $u );
319 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
326 * Internally authenticate the login request.
328 * This may create a local account as a side effect if the
329 * authentication plugin allows transparent local account
334 function authenticateUserData() {
335 global $wgUser, $wgAuth;
336 if ( '' == $this->mName
) {
337 return self
::NO_NAME
;
339 $u = User
::newFromName( $this->mName
);
340 if( is_null( $u ) ||
!User
::isUsableName( $u->getName() ) ) {
341 return self
::ILLEGAL
;
343 if ( 0 == $u->getID() ) {
346 * If the external authentication plugin allows it,
347 * automatically create a new account for users that
348 * are externally defined but have not yet logged in.
350 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
351 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
352 $u = $this->initUser( $u );
354 return self
::WRONG_PLUGIN_PASS
;
357 return self
::NOT_EXISTS
;
363 if (!$u->checkPassword( $this->mPassword
)) {
364 if( $u->checkTemporaryPassword( $this->mPassword
) ) {
365 // The e-mailed temporary password should not be used
366 // for actual logins; that's a very sloppy habit,
367 // and insecure if an attacker has a few seconds to
368 // click "search" on someone's open mail reader.
370 // Allow it to be used only to reset the password
371 // a single time to a new value, which won't be in
372 // the user's e-mail archives.
374 // For backwards compatibility, we'll still recognize
375 // it at the login form to minimize surprises for
376 // people who have been logging in with a temporary
377 // password for some time.
379 // As a side-effect, we can authenticate the user's
380 // e-mail address if it's not already done, since
381 // the temporary password was sent via e-mail.
383 if( !$u->isEmailConfirmed() ) {
387 // At this point we just return an appropriate code
388 // indicating that the UI should show a password
389 // reset form; bot interfaces etc will probably just
390 // fail cleanly here.
392 return self
::RESET_PASS
;
394 return '' == $this->mPassword ? self
::EMPTY_PASS
: self
::WRONG_PASS
;
397 $wgAuth->updateUser( $u );
400 return self
::SUCCESS
;
404 function processLogin() {
405 global $wgUser, $wgAuth;
407 switch ($this->authenticateUserData())
410 # We've verified now, update the real record
411 if( (bool)$this->mRemember
!= (bool)$wgUser->getOption( 'rememberpassword' ) ) {
412 $wgUser->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
413 $wgUser->saveSettings();
415 $wgUser->invalidateCache();
417 $wgUser->setCookies();
419 if( $this->hasSessionCookie() ) {
420 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
422 return $this->cookieRedirectCheck( 'login' );
428 $this->mainLoginForm( wfMsg( 'noname' ) );
430 case self
::WRONG_PLUGIN_PASS
:
431 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
433 case self
::NOT_EXISTS
:
434 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName
) ) );
436 case self
::WRONG_PASS
:
437 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
439 case self
::EMPTY_PASS
:
440 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
442 case self
::RESET_PASS
:
443 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
446 wfDebugDieBacktrace( "Unhandled case value" );
450 function resetLoginForm( $error ) {
452 $wgOut->addWikiText( "<div class=\"errorbox\">$error</div>" );
453 $reset = new PasswordResetForm( $this->mName
, $this->mPassword
);
460 function mailPassword() {
461 global $wgUser, $wgOut, $wgAuth;
463 if( !$wgAuth->allowPasswordChange() ) {
464 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
468 # Check against blocked IPs
469 # fixme -- should we not?
470 if( $wgUser->isBlocked() ) {
471 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
475 # Check against the rate limiter
476 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
477 $wgOut->rateLimited();
481 if ( '' == $this->mName
) {
482 $this->mainLoginForm( wfMsg( 'noname' ) );
485 $u = User
::newFromName( $this->mName
);
486 if( is_null( $u ) ) {
487 $this->mainLoginForm( wfMsg( 'noname' ) );
490 if ( 0 == $u->getID() ) {
491 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
495 # Check against password throttle
496 if ( $u->isPasswordReminderThrottled() ) {
497 global $wgPasswordReminderResendTime;
498 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
499 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
500 round( $wgPasswordReminderResendTime, 3 ) ) );
504 $result = $this->mailPasswordInternal( $u, true );
505 if( WikiError
::isError( $result ) ) {
506 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
508 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
514 * @return mixed true on success, WikiError on failure
517 function mailPasswordInternal( $u, $throttle = true ) {
518 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
519 global $wgServer, $wgScript;
521 if ( '' == $u->getEmail() ) {
522 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
525 $np = $u->randomPassword();
526 $u->setNewpassword( $np, $throttle );
528 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
533 if ( '' == $ip ) { $ip = '(Unknown)'; }
535 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
537 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
543 * @param string $msg Message that will be shown on success
544 * @param bool $auto Toggle auto-redirect to main page; default true
547 function successfulLogin( $msg, $auto = true ) {
551 # Run any hooks; ignore results
553 wfRunHooks('UserLoginComplete', array(&$wgUser));
555 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
556 $wgOut->setRobotpolicy( 'noindex,nofollow' );
557 $wgOut->setArticleRelated( false );
558 $wgOut->addWikiText( $msg );
559 if ( !empty( $this->mReturnTo
) ) {
560 $wgOut->returnToMain( $auto, $this->mReturnTo
);
562 $wgOut->returnToMain( $auto );
567 function userNotPrivilegedMessage() {
570 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
571 $wgOut->setRobotpolicy( 'noindex,nofollow' );
572 $wgOut->setArticleRelated( false );
574 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
576 $wgOut->returnToMain( false );
580 function userBlockedMessage() {
583 # Let's be nice about this, it's likely that this feature will be used
584 # for blocking large numbers of innocent people, e.g. range blocks on
585 # schools. Don't blame it on the user. There's a small chance that it
586 # really is the user's fault, i.e. the username is blocked and they
587 # haven't bothered to log out before trying to create an account to
588 # evade it, but we'll leave that to their guilty conscience to figure
591 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
592 $wgOut->setRobotpolicy( 'noindex,nofollow' );
593 $wgOut->setArticleRelated( false );
596 $wgOut->addWikiText( wfMsg( 'cantcreateaccounttext', $ip ) );
597 $wgOut->returnToMain( false );
603 function mainLoginForm( $msg, $msgtype = 'error' ) {
604 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
605 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
608 if ( $this->mType
== 'signup' ) {
609 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
610 $this->userNotPrivilegedMessage();
612 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
613 $this->userBlockedMessage();
618 if ( '' == $this->mName
) {
619 if ( $wgUser->isLoggedIn() ) {
620 $this->mName
= $wgUser->getName();
622 $this->mName
= isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ?
$_COOKIE[$wgCookiePrefix.'UserName'] : null;
626 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
628 if ( $this->mType
== 'signup' ) {
629 $template = new UsercreateTemplate();
630 $q = 'action=submitlogin&type=signup';
631 $linkq = 'type=login';
632 $linkmsg = 'gotaccount';
634 $template = new UserloginTemplate();
635 $q = 'action=submitlogin&type=login';
636 $linkq = 'type=signup';
637 $linkmsg = 'nologin';
640 if ( !empty( $this->mReturnTo
) ) {
641 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo
);
646 # Pass any language selection on to the mode switch link
647 if( $wgLoginLanguageSelector && $this->mLanguage
)
648 $linkq .= '&uselang=' . $this->mLanguage
;
650 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
651 $link .= wfMsgHtml( $linkmsg . 'link' );
654 # Don't show a "create account" link if the user can't
655 if( $this->showCreateOrLoginLink( $wgUser ) )
656 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
658 $template->set( 'link', '' );
660 $template->set( 'header', '' );
661 $template->set( 'name', $this->mName
);
662 $template->set( 'password', $this->mPassword
);
663 $template->set( 'retype', $this->mRetype
);
664 $template->set( 'email', $this->mEmail
);
665 $template->set( 'realname', $this->mRealName
);
666 $template->set( 'domain', $this->mDomain
);
668 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
669 $template->set( 'message', $msg );
670 $template->set( 'messagetype', $msgtype );
671 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
672 $template->set( 'userealname', $wgAllowRealName );
673 $template->set( 'useemail', $wgEnableEmail );
674 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
675 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
677 # Prepare language selection links as needed
678 if( $wgLoginLanguageSelector ) {
679 $template->set( 'languages', $this->makeLanguageSelector() );
680 if( $this->mLanguage
)
681 $template->set( 'uselang', $this->mLanguage
);
684 // Give authentication and captcha plugins a chance to modify the form
685 $wgAuth->modifyUITemplate( $template );
686 if ( $this->mType
== 'signup' ) {
687 wfRunHooks( 'UserCreateForm', array( &$template ) );
689 wfRunHooks( 'UserLoginForm', array( &$template ) );
692 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
693 $wgOut->setRobotpolicy( 'noindex,nofollow' );
694 $wgOut->setArticleRelated( false );
695 $wgOut->addTemplate( $template );
701 function showCreateOrLoginLink( &$user ) {
702 if( $this->mType
== 'signup' ) {
704 } elseif( $user->isAllowed( 'createaccount' ) ) {
714 function hasSessionCookie() {
715 global $wgDisableCookieCheck;
716 return ( $wgDisableCookieCheck ) ?
true : ( isset( $_COOKIE[session_name()] ) );
722 function cookieRedirectCheck( $type ) {
725 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
726 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
728 return $wgOut->redirect( $check );
734 function onCookieRedirectCheck( $type ) {
737 if ( !$this->hasSessionCookie() ) {
738 if ( $type == 'new' ) {
739 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
740 } else if ( $type == 'login' ) {
741 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
744 return $this->mainLoginForm( wfMsg( 'error' ) );
747 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
754 function throttleHit( $limit ) {
757 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
761 * Produce a bar of links which allow the user to select another language
762 * during login/registration but retain "returnto"
766 function makeLanguageSelector() {
767 $msg = wfMsgForContent( 'loginlanguagelinks' );
768 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
769 $langs = explode( "\n", $msg );
771 foreach( $langs as $lang ) {
772 $lang = trim( $lang, '* ' );
773 $parts = explode( '|', $lang );
774 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
776 return count( $links ) > 0 ?
wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
783 * Create a language selector link for a particular language
784 * Links back to this page preserving type and returnto
786 * @param $text Link text
787 * @param $lang Language code
789 function makeLanguageSelectorLink( $text, $lang ) {
791 $self = SpecialPage
::getTitleFor( 'Userlogin' );
792 $attr[] = 'uselang=' . $lang;
793 if( $this->mType
== 'signup' )
794 $attr[] = 'type=signup';
795 if( $this->mReturnTo
)
796 $attr[] = 'returnto=' . $this->mReturnTo
;
797 $skin =& $wgUser->getSkin();
798 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );