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;
38 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
39 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
40 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
44 * @param webrequest $request A webrequest object passed by reference
46 function LoginForm( &$request ) {
47 global $wgLang, $wgAllowRealName, $wgEnableEmail;
50 $this->mType
= $request->getText( 'type' );
51 $this->mName
= $request->getText( 'wpName' );
52 $this->mPassword
= $request->getText( 'wpPassword' );
53 $this->mRetype
= $request->getText( 'wpRetype' );
54 $this->mDomain
= $request->getText( 'wpDomain' );
55 $this->mReturnTo
= $request->getVal( 'returnto' );
56 $this->mCookieCheck
= $request->getVal( 'wpCookieCheck' );
57 $this->mPosted
= $request->wasPosted();
58 $this->mCreateaccount
= $request->getCheck( 'wpCreateaccount' );
59 $this->mCreateaccountMail
= $request->getCheck( 'wpCreateaccountMail' )
61 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
63 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
64 $this->mAction
= $request->getVal( 'action' );
65 $this->mRemember
= $request->getCheck( 'wpRemember' );
66 $this->mLanguage
= $request->getText( 'uselang' );
68 if( $wgEnableEmail ) {
69 $this->mEmail
= $request->getText( 'wpEmail' );
73 if( $wgAllowRealName ) {
74 $this->mRealName
= $request->getText( 'wpRealName' );
76 $this->mRealName
= '';
79 if( !$wgAuth->validDomain( $this->mDomain
) ) {
80 $this->mDomain
= 'invaliddomain';
82 $wgAuth->setDomain( $this->mDomain
);
84 # When switching accounts, it sucks to get automatically logged out
85 if( $this->mReturnTo
== $wgLang->specialPage( 'Userlogout' ) ) {
86 $this->mReturnTo
= '';
91 if ( !is_null( $this->mCookieCheck
) ) {
92 $this->onCookieRedirectCheck( $this->mCookieCheck
);
94 } else if( $this->mPosted
) {
95 if( $this->mCreateaccount
) {
96 return $this->addNewAccount();
97 } else if ( $this->mCreateaccountMail
) {
98 return $this->addNewAccountMailPassword();
99 } else if ( $this->mMailmypassword
) {
100 return $this->mailPassword();
101 } else if ( ( 'submitlogin' == $this->mAction
) ||
$this->mLoginattempt
) {
102 return $this->processLogin();
105 $this->mainLoginForm( '' );
111 function addNewAccountMailPassword() {
114 if ('' == $this->mEmail
) {
115 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
119 $u = $this->addNewaccountInternal();
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() ) )
164 $u->sendConfirmationMail();
166 # If not logged in, assume the new account as the current one and set session cookies
167 # then show a "welcome" message or a "need cookies" message as needed
168 if( $wgUser->isAnon() ) {
170 $wgUser->setCookies();
171 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
172 if( $this->hasSessionCookie() ) {
173 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
175 return $this->cookieRedirectCheck( 'new' );
178 # Confirm that the account was created
180 $self = SpecialPage
::getTitleFor( 'Userlogin' );
181 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
182 $wgOut->setArticleRelated( false );
183 $wgOut->setRobotPolicy( 'noindex,nofollow' );
184 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
185 $wgOut->returnToMain( $self->getPrefixedText() );
186 wfRunHooks( 'AddNewAccount', array( $u ) );
194 function addNewAccountInternal() {
195 global $wgUser, $wgOut;
196 global $wgEnableSorbs, $wgProxyWhitelist;
197 global $wgMemc, $wgAccountCreationThrottle;
198 global $wgAuth, $wgMinimalPasswordLength;
200 // If the user passes an invalid domain, something is fishy
201 if( !$wgAuth->validDomain( $this->mDomain
) ) {
202 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
206 // If we are not allowing users to login locally, we should
207 // be checking to see if the user is actually able to
208 // authenticate to the authentication server before they
209 // create an account (otherwise, they can create a local account
210 // and login as any domain user). We only need to check this for
211 // domains that aren't local.
212 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
213 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
214 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
219 if ( wfReadOnly() ) {
220 $wgOut->readOnlyPage();
224 if (!$wgUser->isAllowedToCreateAccount()) {
225 $this->userNotPrivilegedMessage();
230 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
231 $wgUser->inSorbsBlacklist( $ip ) )
233 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
237 $name = trim( $this->mName
);
238 $u = User
::newFromName( $name, 'creatable' );
239 if ( is_null( $u ) ) {
240 $this->mainLoginForm( wfMsg( 'noname' ) );
244 if ( 0 != $u->idForName() ) {
245 $this->mainLoginForm( wfMsg( 'userexists' ) );
249 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
250 $this->mainLoginForm( wfMsg( 'badretype' ) );
254 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
255 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
260 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
261 // Hook point to add extra creation throttles and blocks
262 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
263 $this->mainLoginForm( $abortError );
267 if ( $wgAccountCreationThrottle ) {
268 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
269 $value = $wgMemc->incr( $key );
271 $wgMemc->set( $key, 1, 86400 );
273 if ( $value > $wgAccountCreationThrottle ) {
274 $this->throttleHit( $wgAccountCreationThrottle );
279 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
280 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
285 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
286 $ssUpdate->doUpdate();
288 return $this->initUser( $u );
292 * Actually add a user to the database.
293 * Give it a User object that has been initialised with a name.
295 * @param $u User object.
296 * @return User object.
299 function &initUser( &$u ) {
301 $u->setPassword( $this->mPassword
);
302 $u->setEmail( $this->mEmail
);
303 $u->setRealName( $this->mRealName
);
307 $wgAuth->initUser( $u );
309 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
318 function authenticateUserData()
320 global $wgUser, $wgAuth;
321 if ( '' == $this->mName
) {
322 return self
::NO_NAME
;
324 $u = User
::newFromName( $this->mName
);
325 if( is_null( $u ) ||
!User
::isUsableName( $u->getName() ) ) {
326 return self
::ILLEGAL
;
328 if ( 0 == $u->getID() ) {
331 * If the external authentication plugin allows it,
332 * automatically create a new account for users that
333 * are externally defined but have not yet logged in.
335 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
336 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
337 $u =& $this->initUser( $u );
339 return self
::WRONG_PLUGIN_PASS
;
342 return self
::NOT_EXISTS
;
348 if (!$u->checkPassword( $this->mPassword
)) {
349 return '' == $this->mPassword ? self
::EMPTY_PASS
: self
::WRONG_PASS
;
353 $wgAuth->updateUser( $u );
356 return self
::SUCCESS
;
360 function processLogin() {
361 global $wgUser, $wgAuth;
363 switch ($this->authenticateUserData())
366 # We've verified now, update the real record
367 if( (bool)$this->mRemember
!= (bool)$wgUser->getOption( 'rememberpassword' ) ) {
368 $wgUser->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
369 $wgUser->saveSettings();
371 $wgUser->invalidateCache();
373 $wgUser->setCookies();
375 if( $this->hasSessionCookie() ) {
376 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
378 return $this->cookieRedirectCheck( 'login' );
384 $this->mainLoginForm( wfMsg( 'noname' ) );
386 case self
::WRONG_PLUGIN_PASS
:
387 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
389 case self
::NOT_EXISTS
:
390 $this->mainLoginForm( wfMsg( 'nosuchuser', htmlspecialchars( $this->mName
) ) );
392 case self
::WRONG_PASS
:
393 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
395 case self
::EMPTY_PASS
:
396 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
399 wfDebugDieBacktrace( "Unhandled case value" );
406 function mailPassword() {
407 global $wgUser, $wgOut;
409 # Check against blocked IPs
410 # fixme -- should we not?
411 if( $wgUser->isBlocked() ) {
412 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
416 # Check against the rate limiter
417 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
418 $wgOut->rateLimited();
422 if ( '' == $this->mName
) {
423 $this->mainLoginForm( wfMsg( 'noname' ) );
426 $u = User
::newFromName( $this->mName
);
427 if( is_null( $u ) ) {
428 $this->mainLoginForm( wfMsg( 'noname' ) );
431 if ( 0 == $u->getID() ) {
432 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
436 # Check against password throttle
437 if ( $u->isPasswordReminderThrottled() ) {
438 global $wgPasswordReminderResendTime;
439 # Round the time in hours to 3 d.p., in case someone is specifying minutes or seconds.
440 $this->mainLoginForm( wfMsg( 'throttled-mailpassword',
441 round( $wgPasswordReminderResendTime, 3 ) ) );
445 $result = $this->mailPasswordInternal( $u, true );
446 if( WikiError
::isError( $result ) ) {
447 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
449 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
455 * @return mixed true on success, WikiError on failure
458 function mailPasswordInternal( $u, $throttle = true ) {
459 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
460 global $wgServer, $wgScript;
462 if ( '' == $u->getEmail() ) {
463 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
466 $np = $u->randomPassword();
467 $u->setNewpassword( $np, $throttle );
469 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
474 if ( '' == $ip ) { $ip = '(Unknown)'; }
476 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
478 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
484 * @param string $msg Message that will be shown on success
485 * @param bool $auto Toggle auto-redirect to main page; default true
488 function successfulLogin( $msg, $auto = true ) {
492 # Run any hooks; ignore results
494 wfRunHooks('UserLoginComplete', array(&$wgUser));
496 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
497 $wgOut->setRobotpolicy( 'noindex,nofollow' );
498 $wgOut->setArticleRelated( false );
499 $wgOut->addWikiText( $msg );
500 if ( !empty( $this->mReturnTo
) ) {
501 $wgOut->returnToMain( $auto, $this->mReturnTo
);
503 $wgOut->returnToMain( $auto );
508 function userNotPrivilegedMessage() {
511 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
512 $wgOut->setRobotpolicy( 'noindex,nofollow' );
513 $wgOut->setArticleRelated( false );
515 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
517 $wgOut->returnToMain( false );
521 function userBlockedMessage() {
524 # Let's be nice about this, it's likely that this feature will be used
525 # for blocking large numbers of innocent people, e.g. range blocks on
526 # schools. Don't blame it on the user. There's a small chance that it
527 # really is the user's fault, i.e. the username is blocked and they
528 # haven't bothered to log out before trying to create an account to
529 # evade it, but we'll leave that to their guilty conscience to figure
532 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
533 $wgOut->setRobotpolicy( 'noindex,nofollow' );
534 $wgOut->setArticleRelated( false );
537 $wgOut->addWikiText( wfMsg( 'cantcreateaccounttext', $ip ) );
538 $wgOut->returnToMain( false );
544 function mainLoginForm( $msg, $msgtype = 'error' ) {
545 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
546 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
548 if ( $this->mType
== 'signup' ) {
549 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
550 $this->userNotPrivilegedMessage();
552 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
553 $this->userBlockedMessage();
558 if ( '' == $this->mName
) {
559 if ( $wgUser->isLoggedIn() ) {
560 $this->mName
= $wgUser->getName();
562 $this->mName
= @$_COOKIE[$wgCookiePrefix.'UserName'];
566 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
568 if ( $this->mType
== 'signup' ) {
569 $template = new UsercreateTemplate();
570 $q = 'action=submitlogin&type=signup';
571 $linkq = 'type=login';
572 $linkmsg = 'gotaccount';
574 $template = new UserloginTemplate();
575 $q = 'action=submitlogin&type=login';
576 $linkq = 'type=signup';
577 $linkmsg = 'nologin';
580 if ( !empty( $this->mReturnTo
) ) {
581 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo
);
586 # Pass any language selection on to the mode switch link
587 if( $wgLoginLanguageSelector && $this->mLanguage
)
588 $linkq .= '&uselang=' . $this->mLanguage
;
590 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
591 $link .= wfMsgHtml( $linkmsg . 'link' );
594 # Don't show a "create account" link if the user can't
595 if( $this->showCreateOrLoginLink( $wgUser ) )
596 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
598 $template->set( 'link', '' );
600 $template->set( 'header', '' );
601 $template->set( 'name', $this->mName
);
602 $template->set( 'password', $this->mPassword
);
603 $template->set( 'retype', $this->mRetype
);
604 $template->set( 'email', $this->mEmail
);
605 $template->set( 'realname', $this->mRealName
);
606 $template->set( 'domain', $this->mDomain
);
608 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
609 $template->set( 'message', $msg );
610 $template->set( 'messagetype', $msgtype );
611 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
612 $template->set( 'userealname', $wgAllowRealName );
613 $template->set( 'useemail', $wgEnableEmail );
614 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
616 # Prepare language selection links as needed
617 if( $wgLoginLanguageSelector ) {
618 $template->set( 'languages', $this->makeLanguageSelector() );
619 if( $this->mLanguage
)
620 $template->set( 'uselang', $this->mLanguage
);
623 // Give authentication and captcha plugins a chance to modify the form
624 $wgAuth->modifyUITemplate( $template );
625 if ( $this->mType
== 'signup' ) {
626 wfRunHooks( 'UserCreateForm', array( &$template ) );
628 wfRunHooks( 'UserLoginForm', array( &$template ) );
631 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
632 $wgOut->setRobotpolicy( 'noindex,nofollow' );
633 $wgOut->setArticleRelated( false );
634 $wgOut->addTemplate( $template );
640 function showCreateOrLoginLink( &$user ) {
641 if( $this->mType
== 'signup' ) {
643 } elseif( $user->isAllowed( 'createaccount' ) ) {
653 function hasSessionCookie() {
654 global $wgDisableCookieCheck;
655 return ( $wgDisableCookieCheck ) ?
true : ( isset( $_COOKIE[session_name()] ) );
661 function cookieRedirectCheck( $type ) {
664 $titleObj = SpecialPage
::getTitleFor( 'Userlogin' );
665 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
667 return $wgOut->redirect( $check );
673 function onCookieRedirectCheck( $type ) {
676 if ( !$this->hasSessionCookie() ) {
677 if ( $type == 'new' ) {
678 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
679 } else if ( $type == 'login' ) {
680 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
683 return $this->mainLoginForm( wfMsg( 'error' ) );
686 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
693 function throttleHit( $limit ) {
696 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
700 * Produce a bar of links which allow the user to select another language
701 * during login/registration but retain "returnto"
705 function makeLanguageSelector() {
706 $msg = wfMsgForContent( 'loginlanguagelinks' );
707 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
708 $langs = explode( "\n", $msg );
710 foreach( $langs as $lang ) {
711 $lang = trim( $lang, '* ' );
712 $parts = explode( '|', $lang );
713 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
715 return count( $links ) > 0 ?
wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
722 * Create a language selector link for a particular language
723 * Links back to this page preserving type and returnto
725 * @param $text Link text
726 * @param $lang Language code
728 function makeLanguageSelectorLink( $text, $lang ) {
729 $self = SpecialPage
::getTitleFor( 'Userlogin' );
730 $attr[] = 'uselang=' . $lang;
731 if( $this->mType
== 'signup' )
732 $attr[] = 'type=signup';
733 if( $this->mReturnTo
)
734 $attr[] = 'returnto=' . $this->mReturnTo
;
735 return Linker
::makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );