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();
127 $result = $this->mailPasswordInternal( $u, false );
129 wfRunHooks( 'AddNewAccount', array( $u ) );
131 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
132 $wgOut->setRobotpolicy( 'noindex,nofollow' );
133 $wgOut->setArticleRelated( false );
135 if( WikiError
::isError( $result ) ) {
136 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
138 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
139 $wgOut->returnToMain( false );
148 function addNewAccount() {
149 global $wgUser, $wgEmailAuthentication;
151 # Create the account and abort if there's a problem doing so
152 $u = $this->addNewAccountInternal();
156 # If we showed up language selection links, and one was in use, be
157 # smart (and sensible) and save that language as the user's preference
158 global $wgLoginLanguageSelector;
159 if( $wgLoginLanguageSelector && $this->mLanguage
)
160 $u->setOption( 'language', $this->mLanguage
);
162 # Save user settings and send out an email authentication message if needed
164 if( $wgEmailAuthentication && User
::isValidEmailAddr( $u->getEmail() ) )
165 $u->sendConfirmationMail();
167 # If not logged in, assume the new account as the current one and set session cookies
168 # then show a "welcome" message or a "need cookies" message as needed
169 if( $wgUser->isAnon() ) {
171 $wgUser->setCookies();
172 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
173 if( $this->hasSessionCookie() ) {
174 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
176 return $this->cookieRedirectCheck( 'new' );
179 # Confirm that the account was created
181 $self = SpecialPage
::getTitleFor( 'Userlogin' );
182 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
183 $wgOut->setArticleRelated( false );
184 $wgOut->setRobotPolicy( 'noindex,nofollow' );
185 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
186 $wgOut->returnToMain( $self->getPrefixedText() );
187 wfRunHooks( 'AddNewAccount', array( $u ) );
195 function addNewAccountInternal() {
196 global $wgUser, $wgOut;
197 global $wgEnableSorbs, $wgProxyWhitelist;
198 global $wgMemc, $wgAccountCreationThrottle;
199 global $wgAuth, $wgMinimalPasswordLength;
201 // If the user passes an invalid domain, something is fishy
202 if( !$wgAuth->validDomain( $this->mDomain
) ) {
203 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
207 // If we are not allowing users to login locally, we should
208 // be checking to see if the user is actually able to
209 // authenticate to the authentication server before they
210 // create an account (otherwise, they can create a local account
211 // and login as any domain user). We only need to check this for
212 // domains that aren't local.
213 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
214 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
215 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
220 if ( wfReadOnly() ) {
221 $wgOut->readOnlyPage();
225 if (!$wgUser->isAllowedToCreateAccount()) {
226 $this->userNotPrivilegedMessage();
231 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
232 $wgUser->inSorbsBlacklist( $ip ) )
234 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
238 $name = trim( $this->mName
);
239 $u = User
::newFromName( $name, 'creatable' );
240 if ( is_null( $u ) ) {
241 $this->mainLoginForm( wfMsg( 'noname' ) );
245 if ( 0 != $u->idForName() ) {
246 $this->mainLoginForm( wfMsg( 'userexists' ) );
250 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
251 $this->mainLoginForm( wfMsg( 'badretype' ) );
255 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
256 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
261 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
262 // Hook point to add extra creation throttles and blocks
263 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
264 $this->mainLoginForm( $abortError );
268 if ( $wgAccountCreationThrottle ) {
269 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
270 $value = $wgMemc->incr( $key );
272 $wgMemc->set( $key, 1, 86400 );
274 if ( $value > $wgAccountCreationThrottle ) {
275 $this->throttleHit( $wgAccountCreationThrottle );
280 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
281 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
286 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
287 $ssUpdate->doUpdate();
289 return $this->initUser( $u );
293 * Actually add a user to the database.
294 * Give it a User object that has been initialised with a name.
296 * @param $u User object.
297 * @return User object.
300 function initUser( $u ) {
302 $u->setPassword( $this->mPassword
);
303 $u->setEmail( $this->mEmail
);
304 $u->setRealName( $this->mRealName
);
308 $wgAuth->initUser( $u );
310 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
317 * Internally authenticate the login request.
319 * This may create a local account as a side effect if the
320 * authentication plugin allows transparent local account
325 function authenticateUserData() {
326 global $wgUser, $wgAuth;
327 if ( '' == $this->mName
) {
328 return self
::NO_NAME
;
330 $u = User
::newFromName( $this->mName
);
331 if( is_null( $u ) ||
!User
::isUsableName( $u->getName() ) ) {
332 return self
::ILLEGAL
;
334 if ( 0 == $u->getID() ) {
337 * If the external authentication plugin allows it,
338 * automatically create a new account for users that
339 * are externally defined but have not yet logged in.
341 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
342 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
343 $u = $this->initUser( $u );
345 return self
::WRONG_PLUGIN_PASS
;
348 return self
::NOT_EXISTS
;
354 if (!$u->checkPassword( $this->mPassword
)) {
355 if( $u->checkTemporaryPassword( $this->mPassword
) ) {
356 // The e-mailed temporary password should not be used
357 // for actual logins; that's a very sloppy habit,
358 // and insecure if an attacker has a few seconds to
359 // click "search" on someone's open mail reader.
361 // Allow it to be used only to reset the password
362 // a single time to a new value, which won't be in
363 // the user's e-mail archives.
365 // For backwards compatibility, we'll still recognize
366 // it at the login form to minimize surprises for
367 // people who have been logging in with a temporary
368 // password for some time.
370 // At this point we just return an appropriate code
371 // indicating that the UI should show a password
372 // reset form; bot interfaces etc will probably just
373 // fail cleanly here.
374 return self
::RESET_PASS
;
376 return '' == $this->mPassword ? self
::EMPTY_PASS
: self
::WRONG_PASS
;
379 $wgAuth->updateUser( $u );
382 return self
::SUCCESS
;
386 function processLogin() {
387 global $wgUser, $wgAuth;
389 switch ($this->authenticateUserData())
392 # We've verified now, update the real record
393 if( (bool)$this->mRemember
!= (bool)$wgUser->getOption( 'rememberpassword' ) ) {
394 $wgUser->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
395 $wgUser->saveSettings();
397 $wgUser->invalidateCache();
399 $wgUser->setCookies();
401 if( $this->hasSessionCookie() ) {
402 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
404 return $this->cookieRedirectCheck( 'login' );
410 $this->mainLoginForm( wfMsg( 'noname' ) );
412 case self
::WRONG_PLUGIN_PASS
:
413 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
415 case self
::NOT_EXISTS
:
416 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName
) ) );
418 case self
::WRONG_PASS
:
419 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
421 case self
::EMPTY_PASS
:
422 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
424 case self
::RESET_PASS
:
425 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
428 wfDebugDieBacktrace( "Unhandled case value" );
432 function resetLoginForm( $error ) {
434 $wgOut->addWikiText( "<div class=\"errorbox\">$error</div>" );
435 $reset = new PasswordResetForm( $this->mName
, $this->mPassword
);
442 function mailPassword() {
443 global $wgUser, $wgOut, $wgAuth;
445 if( !$wgAuth->allowPasswordChange() ) {
446 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
450 # Check against blocked IPs
451 # fixme -- should we not?
452 if( $wgUser->isBlocked() ) {
453 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
457 # Check against the rate limiter
458 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
459 $wgOut->rateLimited();
463 if ( '' == $this->mName
) {
464 $this->mainLoginForm( wfMsg( 'noname' ) );
467 $u = User
::newFromName( $this->mName
);
468 if( is_null( $u ) ) {
469 $this->mainLoginForm( wfMsg( 'noname' ) );
472 if ( 0 == $u->getID() ) {
473 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
477 # Check against password throttle
478 if ( $u->isPasswordReminderThrottled() ) {
479 global $wgPasswordReminderResendTime;
480 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
481 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
482 round( $wgPasswordReminderResendTime, 3 ) ) );
486 $result = $this->mailPasswordInternal( $u, true );
487 if( WikiError
::isError( $result ) ) {
488 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
490 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
496 * @return mixed true on success, WikiError on failure
499 function mailPasswordInternal( $u, $throttle = true ) {
500 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
501 global $wgServer, $wgScript;
503 if ( '' == $u->getEmail() ) {
504 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
507 $np = $u->randomPassword();
508 $u->setNewpassword( $np, $throttle );
510 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
515 if ( '' == $ip ) { $ip = '(Unknown)'; }
517 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
519 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
525 * @param string $msg Message that will be shown on success
526 * @param bool $auto Toggle auto-redirect to main page; default true
529 function successfulLogin( $msg, $auto = true ) {
533 # Run any hooks; ignore results
535 wfRunHooks('UserLoginComplete', array(&$wgUser));
537 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
538 $wgOut->setRobotpolicy( 'noindex,nofollow' );
539 $wgOut->setArticleRelated( false );
540 $wgOut->addWikiText( $msg );
541 if ( !empty( $this->mReturnTo
) ) {
542 $wgOut->returnToMain( $auto, $this->mReturnTo
);
544 $wgOut->returnToMain( $auto );
549 function userNotPrivilegedMessage() {
552 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
553 $wgOut->setRobotpolicy( 'noindex,nofollow' );
554 $wgOut->setArticleRelated( false );
556 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
558 $wgOut->returnToMain( false );
562 function userBlockedMessage() {
565 # Let's be nice about this, it's likely that this feature will be used
566 # for blocking large numbers of innocent people, e.g. range blocks on
567 # schools. Don't blame it on the user. There's a small chance that it
568 # really is the user's fault, i.e. the username is blocked and they
569 # haven't bothered to log out before trying to create an account to
570 # evade it, but we'll leave that to their guilty conscience to figure
573 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
574 $wgOut->setRobotpolicy( 'noindex,nofollow' );
575 $wgOut->setArticleRelated( false );
578 $wgOut->addWikiText( wfMsg( 'cantcreateaccounttext', $ip ) );
579 $wgOut->returnToMain( false );
585 function mainLoginForm( $msg, $msgtype = 'error' ) {
586 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
587 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
590 if ( $this->mType
== 'signup' ) {
591 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
592 $this->userNotPrivilegedMessage();
594 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
595 $this->userBlockedMessage();
600 if ( '' == $this->mName
) {
601 if ( $wgUser->isLoggedIn() ) {
602 $this->mName
= $wgUser->getName();
604 $this->mName
= isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ?
$_COOKIE[$wgCookiePrefix.'UserName'] : null;
608 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
610 if ( $this->mType
== 'signup' ) {
611 $template = new UsercreateTemplate();
612 $q = 'action=submitlogin&type=signup';
613 $linkq = 'type=login';
614 $linkmsg = 'gotaccount';
616 $template = new UserloginTemplate();
617 $q = 'action=submitlogin&type=login';
618 $linkq = 'type=signup';
619 $linkmsg = 'nologin';
622 if ( !empty( $this->mReturnTo
) ) {
623 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo
);
628 # Pass any language selection on to the mode switch link
629 if( $wgLoginLanguageSelector && $this->mLanguage
)
630 $linkq .= '&uselang=' . $this->mLanguage
;
632 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
633 $link .= wfMsgHtml( $linkmsg . 'link' );
636 # Don't show a "create account" link if the user can't
637 if( $this->showCreateOrLoginLink( $wgUser ) )
638 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
640 $template->set( 'link', '' );
642 $template->set( 'header', '' );
643 $template->set( 'name', $this->mName
);
644 $template->set( 'password', $this->mPassword
);
645 $template->set( 'retype', $this->mRetype
);
646 $template->set( 'email', $this->mEmail
);
647 $template->set( 'realname', $this->mRealName
);
648 $template->set( 'domain', $this->mDomain
);
650 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
651 $template->set( 'message', $msg );
652 $template->set( 'messagetype', $msgtype );
653 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
654 $template->set( 'userealname', $wgAllowRealName );
655 $template->set( 'useemail', $wgEnableEmail );
656 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
657 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
659 # Prepare language selection links as needed
660 if( $wgLoginLanguageSelector ) {
661 $template->set( 'languages', $this->makeLanguageSelector() );
662 if( $this->mLanguage
)
663 $template->set( 'uselang', $this->mLanguage
);
666 // Give authentication and captcha plugins a chance to modify the form
667 $wgAuth->modifyUITemplate( $template );
668 if ( $this->mType
== 'signup' ) {
669 wfRunHooks( 'UserCreateForm', array( &$template ) );
671 wfRunHooks( 'UserLoginForm', array( &$template ) );
674 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
675 $wgOut->setRobotpolicy( 'noindex,nofollow' );
676 $wgOut->setArticleRelated( false );
677 $wgOut->addTemplate( $template );
683 function showCreateOrLoginLink( &$user ) {
684 if( $this->mType
== 'signup' ) {
686 } elseif( $user->isAllowed( 'createaccount' ) ) {
696 function hasSessionCookie() {
697 global $wgDisableCookieCheck;
698 return ( $wgDisableCookieCheck ) ?
true : ( isset( $_COOKIE[session_name()] ) );
704 function cookieRedirectCheck( $type ) {
707 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
708 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
710 return $wgOut->redirect( $check );
716 function onCookieRedirectCheck( $type ) {
719 if ( !$this->hasSessionCookie() ) {
720 if ( $type == 'new' ) {
721 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
722 } else if ( $type == 'login' ) {
723 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
726 return $this->mainLoginForm( wfMsg( 'error' ) );
729 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
736 function throttleHit( $limit ) {
739 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
743 * Produce a bar of links which allow the user to select another language
744 * during login/registration but retain "returnto"
748 function makeLanguageSelector() {
749 $msg = wfMsgForContent( 'loginlanguagelinks' );
750 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
751 $langs = explode( "\n", $msg );
753 foreach( $langs as $lang ) {
754 $lang = trim( $lang, '* ' );
755 $parts = explode( '|', $lang );
756 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
758 return count( $links ) > 0 ?
wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
765 * Create a language selector link for a particular language
766 * Links back to this page preserving type and returnto
768 * @param $text Link text
769 * @param $lang Language code
771 function makeLanguageSelectorLink( $text, $lang ) {
773 $self = SpecialPage
::getTitleFor( 'Userlogin' );
774 $attr[] = 'uselang=' . $lang;
775 if( $this->mType
== 'signup' )
776 $attr[] = 'type=signup';
777 if( $this->mReturnTo
)
778 $attr[] = 'returnto=' . $this->mReturnTo
;
779 $skin =& $wgUser->getSkin();
780 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );