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;
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' );
57 if( $wgEnableEmail ) {
58 $this->mEmail
= $request->getText( 'wpEmail' );
62 if( $wgAllowRealName ) {
63 $this->mRealName
= $request->getText( 'wpRealName' );
65 $this->mRealName
= '';
68 if( !$wgAuth->validDomain( $this->mDomain
) ) {
69 $this->mDomain
= 'invaliddomain';
71 $wgAuth->setDomain( $this->mDomain
);
73 # When switching accounts, it sucks to get automatically logged out
74 if( $this->mReturnTo
== $wgLang->specialPage( 'Userlogout' ) ) {
75 $this->mReturnTo
= '';
80 if ( !is_null( $this->mCookieCheck
) ) {
81 $this->onCookieRedirectCheck( $this->mCookieCheck
);
83 } else if( $this->mPosted
) {
84 if( $this->mCreateaccount
) {
85 return $this->addNewAccount();
86 } else if ( $this->mCreateaccountMail
) {
87 return $this->addNewAccountMailPassword();
88 } else if ( $this->mMailmypassword
) {
89 return $this->mailPassword();
90 } else if ( ( 'submitlogin' == $this->mAction
) ||
$this->mLoginattempt
) {
91 return $this->processLogin();
94 $this->mainLoginForm( '' );
100 function addNewAccountMailPassword() {
103 if ('' == $this->mEmail
) {
104 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
108 $u = $this->addNewaccountInternal();
115 $result = $this->mailPasswordInternal($u);
117 wfRunHooks( 'AddNewAccount', array( $u ) );
119 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
120 $wgOut->setRobotpolicy( 'noindex,nofollow' );
121 $wgOut->setArticleRelated( false );
123 if( WikiError
::isError( $result ) ) {
124 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
126 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
127 $wgOut->returnToMain( false );
136 function addNewAccount() {
137 global $wgUser, $wgEmailAuthentication;
139 # Create the account and abort if there's a problem doing so
140 $u = $this->addNewAccountInternal();
144 # Save user settings and send out an email authentication message if needed
146 if( $wgEmailAuthentication && User
::isValidEmailAddr( $u->getEmail() ) )
147 $u->sendConfirmationMail();
149 # If not logged in, assume the new account as the current one and set session cookies
150 # then show a "welcome" message or a "need cookies" message as needed
151 if( $wgUser->isAnon() ) {
153 $wgUser->setCookies();
154 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
155 if( $this->hasSessionCookie() ) {
156 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
158 return $this->cookieRedirectCheck( 'new' );
161 # Confirm that the account was created
163 $skin = $wgUser->getSkin();
164 $self = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
165 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
166 $wgOut->setArticleRelated( false );
167 $wgOut->setRobotPolicy( 'noindex,nofollow' );
168 $wgOut->addHtml( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
169 $wgOut->returnToMain( $self->getPrefixedText() );
170 wfRunHooks( 'AddNewAccount', array( $u ) );
178 function addNewAccountInternal() {
179 global $wgUser, $wgOut;
180 global $wgEnableSorbs, $wgProxyWhitelist;
181 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
182 global $wgAuth, $wgMinimalPasswordLength, $wgReservedUsernames;
184 // If the user passes an invalid domain, something is fishy
185 if( !$wgAuth->validDomain( $this->mDomain
) ) {
186 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
190 // If we are not allowing users to login locally, we should
191 // be checking to see if the user is actually able to
192 // authenticate to the authentication server before they
193 // create an account (otherwise, they can create a local account
194 // and login as any domain user). We only need to check this for
195 // domains that aren't local.
196 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
197 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
198 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
203 if ( wfReadOnly() ) {
204 $wgOut->readOnlyPage();
208 if (!$wgUser->isAllowedToCreateAccount()) {
209 $this->userNotPrivilegedMessage();
214 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
215 $wgUser->inSorbsBlacklist( $ip ) )
217 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
221 $name = trim( $this->mName
);
222 $u = User
::newFromName( $name );
223 if ( is_null( $u ) ||
in_array( $u->getName(), $wgReservedUsernames ) ) {
224 $this->mainLoginForm( wfMsg( 'noname' ) );
228 if ( 0 != $u->idForName() ) {
229 $this->mainLoginForm( wfMsg( 'userexists' ) );
233 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
234 $this->mainLoginForm( wfMsg( 'badretype' ) );
238 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
239 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
243 if ( $wgAccountCreationThrottle ) {
244 $key = $wgDBname.':acctcreate:ip:'.$ip;
245 $value = $wgMemc->incr( $key );
247 $wgMemc->set( $key, 1, 86400 );
249 if ( $value > $wgAccountCreationThrottle ) {
250 $this->throttleHit( $wgAccountCreationThrottle );
256 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
257 // Hook point to add extra creation throttles and blocks
258 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
259 $this->mainLoginForm( $abortError );
263 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
264 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
269 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
270 $ssUpdate->doUpdate();
272 return $this->initUser( $u );
276 * Actually add a user to the database.
277 * Give it a User object that has been initialised with a name.
279 * @param $u User object.
280 * @return User object.
283 function &initUser( &$u ) {
285 $u->setPassword( $this->mPassword
);
286 $u->setEmail( $this->mEmail
);
287 $u->setRealName( $this->mRealName
);
291 $wgAuth->initUser( $u );
293 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
301 function processLogin() {
302 global $wgUser, $wgAuth, $wgReservedUsernames;
304 if ( '' == $this->mName
) {
305 $this->mainLoginForm( wfMsg( 'noname' ) );
308 $u = User
::newFromName( $this->mName
);
309 if( is_null( $u ) ||
in_array( $u->getName(), $wgReservedUsernames ) ) {
310 $this->mainLoginForm( wfMsg( 'noname' ) );
313 if ( 0 == $u->getID() ) {
316 * If the external authentication plugin allows it,
317 * automatically create a new account for users that
318 * are externally defined but have not yet logged in.
320 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
321 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
322 $u =& $this->initUser( $u );
324 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
328 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
332 $u->loadFromDatabase();
335 if (!$u->checkPassword( $this->mPassword
)) {
336 $this->mainLoginForm( wfMsg( $this->mPassword
== '' ?
'wrongpasswordempty' : 'wrongpassword' ) );
340 # We've verified now, update the real record
342 if ( $this->mRemember
) {
347 $u->setOption( 'rememberpassword', $r );
349 $wgAuth->updateUser( $u );
352 $wgUser->setCookies();
354 $wgUser->saveSettings();
356 if( $this->hasSessionCookie() ) {
357 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
359 return $this->cookieRedirectCheck( 'login' );
366 function mailPassword() {
367 global $wgUser, $wgOut;
369 # Check against the rate limiter
370 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
371 $wgOut->rateLimited();
375 if ( '' == $this->mName
) {
376 $this->mainLoginForm( wfMsg( 'noname' ) );
379 $u = User
::newFromName( $this->mName
);
380 if( is_null( $u ) ) {
381 $this->mainLoginForm( wfMsg( 'noname' ) );
384 if ( 0 == $u->getID() ) {
385 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
389 $u->loadFromDatabase();
391 $result = $this->mailPasswordInternal( $u );
392 if( WikiError
::isError( $result ) ) {
393 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
395 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
401 * @return mixed true on success, WikiError on failure
404 function mailPasswordInternal( $u ) {
405 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
406 global $wgServer, $wgScript;
408 if ( '' == $u->getEmail() ) {
409 return wfMsg( 'noemail', $u->getName() );
412 $np = $u->randomPassword();
413 $u->setNewpassword( $np );
415 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
420 if ( '' == $ip ) { $ip = '(Unknown)'; }
422 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np, $wgServer . $wgScript );
424 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
430 * @param string $msg Message that will be shown on success
431 * @param bool $auto Toggle auto-redirect to main page; default true
434 function successfulLogin( $msg, $auto = true ) {
438 # Run any hooks; ignore results
440 wfRunHooks('UserLoginComplete', array(&$wgUser));
442 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
443 $wgOut->setRobotpolicy( 'noindex,nofollow' );
444 $wgOut->setArticleRelated( false );
445 $wgOut->addWikiText( $msg );
446 if ( !empty( $this->mReturnTo
) ) {
447 $wgOut->returnToMain( $auto, $this->mReturnTo
);
449 $wgOut->returnToMain( $auto );
454 function userNotPrivilegedMessage() {
457 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
458 $wgOut->setRobotpolicy( 'noindex,nofollow' );
459 $wgOut->setArticleRelated( false );
461 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
463 $wgOut->returnToMain( false );
469 function mainLoginForm( $msg, $msgtype = 'error' ) {
470 global $wgUser, $wgOut;
471 global $wgAllowRealName, $wgEnableEmail;
472 global $wgCookiePrefix;
475 if ( '' == $this->mName
) {
476 if ( $wgUser->isLoggedIn() ) {
477 $this->mName
= $wgUser->getName();
479 $this->mName
= @$_COOKIE[$wgCookiePrefix.'UserName'];
483 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
485 require_once( 'SkinTemplate.php' );
486 require_once( 'templates/Userlogin.php' );
488 if ( $this->mType
== 'signup' ) {
489 $template =& new UsercreateTemplate();
490 $q = 'action=submitlogin&type=signup';
491 $linkq = 'type=login';
492 $linkmsg = 'gotaccount';
494 $template =& new UserloginTemplate();
495 $q = 'action=submitlogin&type=login';
496 $linkq = 'type=signup';
497 $linkmsg = 'nologin';
500 if ( !empty( $this->mReturnTo
) ) {
501 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo
);
506 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
507 $link .= wfMsgHtml( $linkmsg . 'link' );
510 # Don't show a "create account" link if the user can't
511 if( $this->showCreateOrLoginLink( $wgUser ) )
512 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
514 $template->set( 'link', '' );
516 $template->set( 'header', '' );
517 $template->set( 'name', $this->mName
);
518 $template->set( 'password', $this->mPassword
);
519 $template->set( 'retype', $this->mRetype
);
520 $template->set( 'email', $this->mEmail
);
521 $template->set( 'realname', $this->mRealName
);
522 $template->set( 'domain', $this->mDomain
);
524 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
525 $template->set( 'message', $msg );
526 $template->set( 'messagetype', $msgtype );
527 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
528 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
529 $template->set( 'userealname', $wgAllowRealName );
530 $template->set( 'useemail', $wgEnableEmail );
531 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
533 // Give authentication and captcha plugins a chance to modify the form
534 $wgAuth->modifyUITemplate( $template );
535 if ( $this->mType
== 'signup' ) {
536 wfRunHooks( 'UserCreateForm', array( &$template ) );
538 wfRunHooks( 'UserLoginForm', array( &$template ) );
541 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
542 $wgOut->setRobotpolicy( 'noindex,nofollow' );
543 $wgOut->setArticleRelated( false );
544 $wgOut->addTemplate( $template );
550 function showCreateOrLoginLink( &$user ) {
551 if( $this->mType
== 'signup' ) {
553 } elseif( $user->isAllowedToCreateAccount() ) {
563 function hasSessionCookie() {
564 global $wgDisableCookieCheck;
565 return ( $wgDisableCookieCheck ) ?
true : ( isset( $_COOKIE[session_name()] ) );
571 function cookieRedirectCheck( $type ) {
574 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
575 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
577 return $wgOut->redirect( $check );
583 function onCookieRedirectCheck( $type ) {
586 if ( !$this->hasSessionCookie() ) {
587 if ( $type == 'new' ) {
588 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
589 } else if ( $type == 'login' ) {
590 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
593 return $this->mainLoginForm( wfMsg( 'error' ) );
596 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
603 function throttleHit( $limit ) {
606 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );