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 #Â Check anonymous user ($wgUser) limitations :
232 if (!$wgUser->isAllowedToCreateAccount()) {
233 $this->userNotPrivilegedMessage();
238 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
239 $wgUser->inSorbsBlacklist( $ip ) )
241 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
245 # Now create a dummy user ($u) and check if it is valid
246 $name = trim( $this->mName
);
247 $u = User
::newFromName( $name, 'creatable' );
248 if ( is_null( $u ) ) {
249 $this->mainLoginForm( wfMsg( 'noname' ) );
253 if ( 0 != $u->idForName() ) {
254 $this->mainLoginForm( wfMsg( 'userexists' ) );
258 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
259 $this->mainLoginForm( wfMsg( 'badretype' ) );
263 if ( !$u->isValidPassword( $this->mPassword
) ) {
264 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
269 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
270 // Hook point to add extra creation throttles and blocks
271 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
272 $this->mainLoginForm( $abortError );
276 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
277 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
278 $value = $wgMemc->incr( $key );
280 $wgMemc->set( $key, 1, 86400 );
282 if ( $value > $wgAccountCreationThrottle ) {
283 $this->throttleHit( $wgAccountCreationThrottle );
288 if( !$wgAuth->addUser( $u, $this->mPassword
, $this->mEmail
, $this->mRealName
) ) {
289 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
293 return $this->initUser( $u );
297 * Actually add a user to the database.
298 * Give it a User object that has been initialised with a name.
300 * @param $u User object.
301 * @return User object.
304 function initUser( $u ) {
309 if ( $wgAuth->allowPasswordChange() ) {
310 $u->setPassword( $this->mPassword
);
313 $u->setEmail( $this->mEmail
);
314 $u->setRealName( $this->mRealName
);
317 $wgAuth->initUser( $u );
319 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
323 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
324 $ssUpdate->doUpdate();
330 * Internally authenticate the login request.
332 * This may create a local account as a side effect if the
333 * authentication plugin allows transparent local account
338 function authenticateUserData() {
339 global $wgUser, $wgAuth;
340 if ( '' == $this->mName
) {
341 return self
::NO_NAME
;
343 $u = User
::newFromName( $this->mName
);
344 if( is_null( $u ) ||
!User
::isUsableName( $u->getName() ) ) {
345 return self
::ILLEGAL
;
347 if ( 0 == $u->getID() ) {
350 * If the external authentication plugin allows it,
351 * automatically create a new account for users that
352 * are externally defined but have not yet logged in.
354 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
355 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
356 $u = $this->initUser( $u );
358 return self
::WRONG_PLUGIN_PASS
;
361 return self
::NOT_EXISTS
;
367 if (!$u->checkPassword( $this->mPassword
)) {
368 if( $u->checkTemporaryPassword( $this->mPassword
) ) {
369 // The e-mailed temporary password should not be used
370 // for actual logins; that's a very sloppy habit,
371 // and insecure if an attacker has a few seconds to
372 // click "search" on someone's open mail reader.
374 // Allow it to be used only to reset the password
375 // a single time to a new value, which won't be in
376 // the user's e-mail archives.
378 // For backwards compatibility, we'll still recognize
379 // it at the login form to minimize surprises for
380 // people who have been logging in with a temporary
381 // password for some time.
383 // As a side-effect, we can authenticate the user's
384 // e-mail address if it's not already done, since
385 // the temporary password was sent via e-mail.
387 if( !$u->isEmailConfirmed() ) {
391 // At this point we just return an appropriate code
392 // indicating that the UI should show a password
393 // reset form; bot interfaces etc will probably just
394 // fail cleanly here.
396 return self
::RESET_PASS
;
398 return '' == $this->mPassword ? self
::EMPTY_PASS
: self
::WRONG_PASS
;
401 $wgAuth->updateUser( $u );
404 return self
::SUCCESS
;
408 function processLogin() {
409 global $wgUser, $wgAuth;
411 switch ($this->authenticateUserData())
414 # We've verified now, update the real record
415 if( (bool)$this->mRemember
!= (bool)$wgUser->getOption( 'rememberpassword' ) ) {
416 $wgUser->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
417 $wgUser->saveSettings();
419 $wgUser->invalidateCache();
421 $wgUser->setCookies();
423 if( $this->hasSessionCookie() ) {
424 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
426 return $this->cookieRedirectCheck( 'login' );
432 $this->mainLoginForm( wfMsg( 'noname' ) );
434 case self
::WRONG_PLUGIN_PASS
:
435 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
437 case self
::NOT_EXISTS
:
438 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName
) ) );
440 case self
::WRONG_PASS
:
441 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
443 case self
::EMPTY_PASS
:
444 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
446 case self
::RESET_PASS
:
447 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
450 wfDebugDieBacktrace( "Unhandled case value" );
454 function resetLoginForm( $error ) {
456 $wgOut->addWikiText( "<div class=\"errorbox\">$error</div>" );
457 $reset = new PasswordResetForm( $this->mName
, $this->mPassword
);
464 function mailPassword() {
465 global $wgUser, $wgOut, $wgAuth;
467 if( !$wgAuth->allowPasswordChange() ) {
468 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
472 # Check against blocked IPs
473 # fixme -- should we not?
474 if( $wgUser->isBlocked() ) {
475 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
479 # Check against the rate limiter
480 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
481 $wgOut->rateLimited();
485 if ( '' == $this->mName
) {
486 $this->mainLoginForm( wfMsg( 'noname' ) );
489 $u = User
::newFromName( $this->mName
);
490 if( is_null( $u ) ) {
491 $this->mainLoginForm( wfMsg( 'noname' ) );
494 if ( 0 == $u->getID() ) {
495 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
499 # Check against password throttle
500 if ( $u->isPasswordReminderThrottled() ) {
501 global $wgPasswordReminderResendTime;
502 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
503 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
504 round( $wgPasswordReminderResendTime, 3 ) ) );
508 $result = $this->mailPasswordInternal( $u, true );
509 if( WikiError
::isError( $result ) ) {
510 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
512 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
518 * @return mixed true on success, WikiError on failure
521 function mailPasswordInternal( $u, $throttle = true ) {
522 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
523 global $wgServer, $wgScript;
525 if ( '' == $u->getEmail() ) {
526 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
529 $np = $u->randomPassword();
530 $u->setNewpassword( $np, $throttle );
532 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
537 if ( '' == $ip ) { $ip = '(Unknown)'; }
539 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
541 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
547 * @param string $msg Message that will be shown on success
548 * @param bool $auto Toggle auto-redirect to main page; default true
551 function successfulLogin( $msg, $auto = true ) {
555 # Run any hooks; ignore results
557 wfRunHooks('UserLoginComplete', array(&$wgUser));
559 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
560 $wgOut->setRobotpolicy( 'noindex,nofollow' );
561 $wgOut->setArticleRelated( false );
562 $wgOut->addWikiText( $msg );
563 if ( !empty( $this->mReturnTo
) ) {
564 $wgOut->returnToMain( $auto, $this->mReturnTo
);
566 $wgOut->returnToMain( $auto );
571 function userNotPrivilegedMessage() {
574 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
575 $wgOut->setRobotpolicy( 'noindex,nofollow' );
576 $wgOut->setArticleRelated( false );
578 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
580 $wgOut->returnToMain( false );
584 function userBlockedMessage() {
587 # Let's be nice about this, it's likely that this feature will be used
588 # for blocking large numbers of innocent people, e.g. range blocks on
589 # schools. Don't blame it on the user. There's a small chance that it
590 # really is the user's fault, i.e. the username is blocked and they
591 # haven't bothered to log out before trying to create an account to
592 # evade it, but we'll leave that to their guilty conscience to figure
595 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
596 $wgOut->setRobotpolicy( 'noindex,nofollow' );
597 $wgOut->setArticleRelated( false );
600 $wgOut->addWikiText( wfMsg( 'cantcreateaccounttext', $ip ) );
601 $wgOut->returnToMain( false );
607 function mainLoginForm( $msg, $msgtype = 'error' ) {
608 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
609 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
612 if ( $this->mType
== 'signup' ) {
613 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
614 $this->userNotPrivilegedMessage();
616 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
617 $this->userBlockedMessage();
622 if ( '' == $this->mName
) {
623 if ( $wgUser->isLoggedIn() ) {
624 $this->mName
= $wgUser->getName();
626 $this->mName
= isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ?
$_COOKIE[$wgCookiePrefix.'UserName'] : null;
630 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
632 if ( $this->mType
== 'signup' ) {
633 $template = new UsercreateTemplate();
634 $q = 'action=submitlogin&type=signup';
635 $linkq = 'type=login';
636 $linkmsg = 'gotaccount';
638 $template = new UserloginTemplate();
639 $q = 'action=submitlogin&type=login';
640 $linkq = 'type=signup';
641 $linkmsg = 'nologin';
644 if ( !empty( $this->mReturnTo
) ) {
645 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo
);
650 # Pass any language selection on to the mode switch link
651 if( $wgLoginLanguageSelector && $this->mLanguage
)
652 $linkq .= '&uselang=' . $this->mLanguage
;
654 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
655 $link .= wfMsgHtml( $linkmsg . 'link' );
658 # Don't show a "create account" link if the user can't
659 if( $this->showCreateOrLoginLink( $wgUser ) )
660 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
662 $template->set( 'link', '' );
664 $template->set( 'header', '' );
665 $template->set( 'name', $this->mName
);
666 $template->set( 'password', $this->mPassword
);
667 $template->set( 'retype', $this->mRetype
);
668 $template->set( 'email', $this->mEmail
);
669 $template->set( 'realname', $this->mRealName
);
670 $template->set( 'domain', $this->mDomain
);
672 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
673 $template->set( 'message', $msg );
674 $template->set( 'messagetype', $msgtype );
675 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
676 $template->set( 'userealname', $wgAllowRealName );
677 $template->set( 'useemail', $wgEnableEmail );
678 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
679 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
681 # Prepare language selection links as needed
682 if( $wgLoginLanguageSelector ) {
683 $template->set( 'languages', $this->makeLanguageSelector() );
684 if( $this->mLanguage
)
685 $template->set( 'uselang', $this->mLanguage
);
688 // Give authentication and captcha plugins a chance to modify the form
689 $wgAuth->modifyUITemplate( $template );
690 if ( $this->mType
== 'signup' ) {
691 wfRunHooks( 'UserCreateForm', array( &$template ) );
693 wfRunHooks( 'UserLoginForm', array( &$template ) );
696 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
697 $wgOut->setRobotpolicy( 'noindex,nofollow' );
698 $wgOut->setArticleRelated( false );
699 $wgOut->addTemplate( $template );
705 function showCreateOrLoginLink( &$user ) {
706 if( $this->mType
== 'signup' ) {
708 } elseif( $user->isAllowed( 'createaccount' ) ) {
716 * Check if a session cookie is present.
718 * This will not pick up a cookie set during _this_ request, but is
719 * meant to ensure that the client is returning the cookie which was
720 * set on a previous pass through the system.
724 function hasSessionCookie() {
725 global $wgDisableCookieCheck, $wgRequest;
726 return $wgDisableCookieCheck ?
true : $wgRequest->checkSessionCookie();
732 function cookieRedirectCheck( $type ) {
735 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
736 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
738 return $wgOut->redirect( $check );
744 function onCookieRedirectCheck( $type ) {
747 if ( !$this->hasSessionCookie() ) {
748 if ( $type == 'new' ) {
749 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
750 } else if ( $type == 'login' ) {
751 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
754 return $this->mainLoginForm( wfMsg( 'error' ) );
757 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
764 function throttleHit( $limit ) {
767 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
771 * Produce a bar of links which allow the user to select another language
772 * during login/registration but retain "returnto"
776 function makeLanguageSelector() {
777 $msg = wfMsgForContent( 'loginlanguagelinks' );
778 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
779 $langs = explode( "\n", $msg );
781 foreach( $langs as $lang ) {
782 $lang = trim( $lang, '* ' );
783 $parts = explode( '|', $lang );
784 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
786 return count( $links ) > 0 ?
wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
793 * Create a language selector link for a particular language
794 * Links back to this page preserving type and returnto
796 * @param $text Link text
797 * @param $lang Language code
799 function makeLanguageSelectorLink( $text, $lang ) {
801 $self = SpecialPage
::getTitleFor( 'Userlogin' );
802 $attr[] = 'uselang=' . $lang;
803 if( $this->mType
== 'signup' )
804 $attr[] = 'type=signup';
805 if( $this->mReturnTo
)
806 $attr[] = 'returnto=' . $this->mReturnTo
;
807 $skin = $wgUser->getSkin();
808 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );