5 * @subpackage SpecialPage
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[session_name()] ) ) {
18 $form = new LoginForm( $wgRequest );
25 * @subpackage SpecialPage
28 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
29 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
30 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage;
34 * @param webrequest $request A webrequest object passed by reference
36 function LoginForm( &$request ) {
37 global $wgLang, $wgAllowRealName, $wgEnableEmail;
40 $this->mType
= $request->getText( 'type' );
41 $this->mName
= $request->getText( 'wpName' );
42 $this->mPassword
= $request->getText( 'wpPassword' );
43 $this->mRetype
= $request->getText( 'wpRetype' );
44 $this->mDomain
= $request->getText( 'wpDomain' );
45 $this->mReturnTo
= $request->getVal( 'returnto' );
46 $this->mCookieCheck
= $request->getVal( 'wpCookieCheck' );
47 $this->mPosted
= $request->wasPosted();
48 $this->mCreateaccount
= $request->getCheck( 'wpCreateaccount' );
49 $this->mCreateaccountMail
= $request->getCheck( 'wpCreateaccountMail' )
51 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
53 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
54 $this->mAction
= $request->getVal( 'action' );
55 $this->mRemember
= $request->getCheck( 'wpRemember' );
56 $this->mLanguage
= $request->getText( 'uselang' );
58 if( $wgEnableEmail ) {
59 $this->mEmail
= $request->getText( 'wpEmail' );
63 if( $wgAllowRealName ) {
64 $this->mRealName
= $request->getText( 'wpRealName' );
66 $this->mRealName
= '';
69 if( !$wgAuth->validDomain( $this->mDomain
) ) {
70 $this->mDomain
= 'invaliddomain';
72 $wgAuth->setDomain( $this->mDomain
);
74 # When switching accounts, it sucks to get automatically logged out
75 if( $this->mReturnTo
== $wgLang->specialPage( 'Userlogout' ) ) {
76 $this->mReturnTo
= '';
81 if ( !is_null( $this->mCookieCheck
) ) {
82 $this->onCookieRedirectCheck( $this->mCookieCheck
);
84 } else if( $this->mPosted
) {
85 if( $this->mCreateaccount
) {
86 return $this->addNewAccount();
87 } else if ( $this->mCreateaccountMail
) {
88 return $this->addNewAccountMailPassword();
89 } else if ( $this->mMailmypassword
) {
90 return $this->mailPassword();
91 } else if ( ( 'submitlogin' == $this->mAction
) ||
$this->mLoginattempt
) {
92 return $this->processLogin();
95 $this->mainLoginForm( '' );
101 function addNewAccountMailPassword() {
104 if ('' == $this->mEmail
) {
105 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
109 $u = $this->addNewaccountInternal();
116 $result = $this->mailPasswordInternal($u);
118 wfRunHooks( 'AddNewAccount', array( $u ) );
120 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
121 $wgOut->setRobotpolicy( 'noindex,nofollow' );
122 $wgOut->setArticleRelated( false );
124 if( WikiError
::isError( $result ) ) {
125 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
127 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
128 $wgOut->returnToMain( false );
137 function addNewAccount() {
138 global $wgUser, $wgEmailAuthentication;
140 # Create the account and abort if there's a problem doing so
141 $u = $this->addNewAccountInternal();
145 # If we showed up language selection links, and one was in use, be
146 # smart (and sensible) and save that language as the user's preference
147 global $wgLoginLanguageSelector;
148 if( $wgLoginLanguageSelector && $this->mLanguage
)
149 $u->setOption( 'language', $this->mLanguage
);
151 # Save user settings and send out an email authentication message if needed
153 if( $wgEmailAuthentication && User
::isValidEmailAddr( $u->getEmail() ) )
154 $u->sendConfirmationMail();
156 # If not logged in, assume the new account as the current one and set session cookies
157 # then show a "welcome" message or a "need cookies" message as needed
158 if( $wgUser->isAnon() ) {
160 $wgUser->setCookies();
161 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
162 if( $this->hasSessionCookie() ) {
163 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
165 return $this->cookieRedirectCheck( 'new' );
168 # Confirm that the account was created
170 $skin = $wgUser->getSkin();
171 $self = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
172 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
173 $wgOut->setArticleRelated( false );
174 $wgOut->setRobotPolicy( 'noindex,nofollow' );
175 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
176 $wgOut->returnToMain( $self->getPrefixedText() );
177 wfRunHooks( 'AddNewAccount', array( $u ) );
185 function addNewAccountInternal() {
186 global $wgUser, $wgOut;
187 global $wgEnableSorbs, $wgProxyWhitelist;
188 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
189 global $wgAuth, $wgMinimalPasswordLength, $wgReservedUsernames;
191 // If the user passes an invalid domain, something is fishy
192 if( !$wgAuth->validDomain( $this->mDomain
) ) {
193 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
197 // If we are not allowing users to login locally, we should
198 // be checking to see if the user is actually able to
199 // authenticate to the authentication server before they
200 // create an account (otherwise, they can create a local account
201 // and login as any domain user). We only need to check this for
202 // domains that aren't local.
203 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
204 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
205 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
210 if ( wfReadOnly() ) {
211 $wgOut->readOnlyPage();
215 if (!$wgUser->isAllowedToCreateAccount()) {
216 $this->userNotPrivilegedMessage();
221 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
222 $wgUser->inSorbsBlacklist( $ip ) )
224 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
228 $name = trim( $this->mName
);
229 $u = User
::newFromName( $name );
230 if ( is_null( $u ) ||
in_array( $u->getName(), $wgReservedUsernames ) ) {
231 $this->mainLoginForm( wfMsg( 'noname' ) );
235 if ( 0 != $u->idForName() ) {
236 $this->mainLoginForm( wfMsg( 'userexists' ) );
240 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
241 $this->mainLoginForm( wfMsg( 'badretype' ) );
245 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
246 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
250 if ( $wgAccountCreationThrottle ) {
251 $key = $wgDBname.':acctcreate:ip:'.$ip;
252 $value = $wgMemc->incr( $key );
254 $wgMemc->set( $key, 1, 86400 );
256 if ( $value > $wgAccountCreationThrottle ) {
257 $this->throttleHit( $wgAccountCreationThrottle );
263 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
264 // Hook point to add extra creation throttles and blocks
265 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
266 $this->mainLoginForm( $abortError );
270 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
271 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
276 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
277 $ssUpdate->doUpdate();
279 return $this->initUser( $u );
283 * Actually add a user to the database.
284 * Give it a User object that has been initialised with a name.
286 * @param $u User object.
287 * @return User object.
290 function &initUser( &$u ) {
292 $u->setPassword( $this->mPassword
);
293 $u->setEmail( $this->mEmail
);
294 $u->setRealName( $this->mRealName
);
298 $wgAuth->initUser( $u );
300 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
308 function processLogin() {
309 global $wgUser, $wgAuth, $wgReservedUsernames;
311 if ( '' == $this->mName
) {
312 $this->mainLoginForm( wfMsg( 'noname' ) );
315 $u = User
::newFromName( $this->mName
);
316 if( is_null( $u ) ||
in_array( $u->getName(), $wgReservedUsernames ) ) {
317 $this->mainLoginForm( wfMsg( 'noname' ) );
320 if ( 0 == $u->getID() ) {
323 * If the external authentication plugin allows it,
324 * automatically create a new account for users that
325 * are externally defined but have not yet logged in.
327 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
328 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
329 $u =& $this->initUser( $u );
331 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
335 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
339 $u->loadFromDatabase();
342 if (!$u->checkPassword( $this->mPassword
)) {
343 $this->mainLoginForm( wfMsg( $this->mPassword
== '' ?
'wrongpasswordempty' : 'wrongpassword' ) );
347 # We've verified now, update the real record
349 if ( $this->mRemember
) {
354 $u->setOption( 'rememberpassword', $r );
356 $wgAuth->updateUser( $u );
359 $wgUser->setCookies();
361 $wgUser->saveSettings();
363 if( $this->hasSessionCookie() ) {
364 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
366 return $this->cookieRedirectCheck( 'login' );
373 function mailPassword() {
374 global $wgUser, $wgOut;
376 # Check against the rate limiter
377 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
378 $wgOut->rateLimited();
382 if ( '' == $this->mName
) {
383 $this->mainLoginForm( wfMsg( 'noname' ) );
386 $u = User
::newFromName( $this->mName
);
387 if( is_null( $u ) ) {
388 $this->mainLoginForm( wfMsg( 'noname' ) );
391 if ( 0 == $u->getID() ) {
392 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
396 $u->loadFromDatabase();
398 $result = $this->mailPasswordInternal( $u );
399 if( WikiError
::isError( $result ) ) {
400 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
402 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
408 * @return mixed true on success, WikiError on failure
411 function mailPasswordInternal( $u ) {
412 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
413 global $wgServer, $wgScript;
415 if ( '' == $u->getEmail() ) {
416 return wfMsg( 'noemail', $u->getName() );
419 $np = $u->randomPassword();
420 $u->setNewpassword( $np );
422 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
427 if ( '' == $ip ) { $ip = '(Unknown)'; }
429 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
431 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
437 * @param string $msg Message that will be shown on success
438 * @param bool $auto Toggle auto-redirect to main page; default true
441 function successfulLogin( $msg, $auto = true ) {
445 # Run any hooks; ignore results
447 wfRunHooks('UserLoginComplete', array(&$wgUser));
449 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
450 $wgOut->setRobotpolicy( 'noindex,nofollow' );
451 $wgOut->setArticleRelated( false );
452 $wgOut->addWikiText( $msg );
453 if ( !empty( $this->mReturnTo
) ) {
454 $wgOut->returnToMain( $auto, $this->mReturnTo
);
456 $wgOut->returnToMain( $auto );
461 function userNotPrivilegedMessage() {
464 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
465 $wgOut->setRobotpolicy( 'noindex,nofollow' );
466 $wgOut->setArticleRelated( false );
468 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
470 $wgOut->returnToMain( false );
474 function userBlockedMessage() {
477 # Let's be nice about this, it's likely that this feature will be used
478 # for blocking large numbers of innocent people, e.g. range blocks on
479 # schools. Don't blame it on the user. There's a small chance that it
480 # really is the user's fault, i.e. the username is blocked and they
481 # haven't bothered to log out before trying to create an account to
482 # evade it, but we'll leave that to their guilty conscience to figure
485 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
486 $wgOut->setRobotpolicy( 'noindex,nofollow' );
487 $wgOut->setArticleRelated( false );
490 $wgOut->addWikiText( wfMsg( 'cantcreateaccounttext', $ip ) );
491 $wgOut->returnToMain( false );
497 function mainLoginForm( $msg, $msgtype = 'error' ) {
498 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
499 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
501 if ( $this->mType
== 'signup' ) {
502 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
503 $this->userNotPrivilegedMessage();
505 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
506 $this->userBlockedMessage();
511 if ( '' == $this->mName
) {
512 if ( $wgUser->isLoggedIn() ) {
513 $this->mName
= $wgUser->getName();
515 $this->mName
= @$_COOKIE[$wgCookiePrefix.'UserName'];
519 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
521 if ( $this->mType
== 'signup' ) {
522 $template = new UsercreateTemplate();
523 $q = 'action=submitlogin&type=signup';
524 $linkq = 'type=login';
525 $linkmsg = 'gotaccount';
527 $template = new UserloginTemplate();
528 $q = 'action=submitlogin&type=login';
529 $linkq = 'type=signup';
530 $linkmsg = 'nologin';
533 if ( !empty( $this->mReturnTo
) ) {
534 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo
);
539 # Pass any language selection on to the mode switch link
540 if( $wgLoginLanguageSelector && $this->mLanguage
)
541 $linkq .= '&uselang=' . $this->mLanguage
;
543 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
544 $link .= wfMsgHtml( $linkmsg . 'link' );
547 # Don't show a "create account" link if the user can't
548 if( $this->showCreateOrLoginLink( $wgUser ) )
549 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
551 $template->set( 'link', '' );
553 $template->set( 'header', '' );
554 $template->set( 'name', $this->mName
);
555 $template->set( 'password', $this->mPassword
);
556 $template->set( 'retype', $this->mRetype
);
557 $template->set( 'email', $this->mEmail
);
558 $template->set( 'realname', $this->mRealName
);
559 $template->set( 'domain', $this->mDomain
);
561 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
562 $template->set( 'message', $msg );
563 $template->set( 'messagetype', $msgtype );
564 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
565 $template->set( 'userealname', $wgAllowRealName );
566 $template->set( 'useemail', $wgEnableEmail );
567 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
569 # Prepare language selection links as needed
570 if( $wgLoginLanguageSelector ) {
571 $template->set( 'languages', $this->makeLanguageSelector() );
572 if( $this->mLanguage
)
573 $template->set( 'uselang', $this->mLanguage
);
576 // Give authentication and captcha plugins a chance to modify the form
577 $wgAuth->modifyUITemplate( $template );
578 if ( $this->mType
== 'signup' ) {
579 wfRunHooks( 'UserCreateForm', array( &$template ) );
581 wfRunHooks( 'UserLoginForm', array( &$template ) );
584 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
585 $wgOut->setRobotpolicy( 'noindex,nofollow' );
586 $wgOut->setArticleRelated( false );
587 $wgOut->addTemplate( $template );
593 function showCreateOrLoginLink( &$user ) {
594 if( $this->mType
== 'signup' ) {
596 } elseif( $user->isAllowed( 'createaccount' ) ) {
606 function hasSessionCookie() {
607 global $wgDisableCookieCheck;
608 return ( $wgDisableCookieCheck ) ?
true : ( isset( $_COOKIE[session_name()] ) );
614 function cookieRedirectCheck( $type ) {
617 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
618 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
620 return $wgOut->redirect( $check );
626 function onCookieRedirectCheck( $type ) {
629 if ( !$this->hasSessionCookie() ) {
630 if ( $type == 'new' ) {
631 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
632 } else if ( $type == 'login' ) {
633 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
636 return $this->mainLoginForm( wfMsg( 'error' ) );
639 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
646 function throttleHit( $limit ) {
649 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );
653 * Produce a bar of links which allow the user to select another language
654 * during login/registration but retain "returnto"
658 function makeLanguageSelector() {
659 $msg = wfMsgForContent( 'loginlanguagelinks' );
660 if( $msg != '' && $msg != '<loginlanguagelinks>' ) {
661 $langs = explode( "\n", $msg );
663 foreach( $langs as $lang ) {
664 $lang = trim( $lang, '* ' );
665 $parts = explode( '|', $lang );
666 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
668 return count( $links ) > 0 ?
wfMsgHtml( 'loginlanguagelabel', implode( ' | ', $links ) ) : '';
675 * Create a language selector link for a particular language
676 * Links back to this page preserving type and returnto
678 * @param $text Link text
679 * @param $lang Language code
681 function makeLanguageSelectorLink( $text, $lang ) {
683 $self = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
684 $attr[] = 'uselang=' . $lang;
685 if( $this->mType
== 'signup' )
686 $attr[] = 'type=signup';
687 if( $this->mReturnTo
)
688 $attr[] = 'returnto=' . $this->mReturnTo
;
689 $skin =& $wgUser->getSkin();
690 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );