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 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
118 $wgOut->setRobotpolicy( 'noindex,nofollow' );
119 $wgOut->setArticleRelated( false );
121 if( WikiError
::isError( $result ) ) {
122 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
124 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
125 $wgOut->returnToMain( false );
134 function addNewAccount() {
135 global $wgUser, $wgEmailAuthentication;
137 $u = $this->addNewAccountInternal();
144 $wgUser->setCookies();
146 $wgUser->saveSettings();
147 if( $wgEmailAuthentication && $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
148 $wgUser->sendConfirmationMail();
151 wfRunHooks( 'AddNewAccount' );
153 if( $this->hasSessionCookie() ) {
154 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
156 return $this->cookieRedirectCheck( 'new' );
163 function addNewAccountInternal() {
164 global $wgUser, $wgOut;
165 global $wgEnableSorbs, $wgProxyWhitelist;
166 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
167 global $wgAuth, $wgMinimalPasswordLength;
169 // If the user passes an invalid domain, something is fishy
170 if( !$wgAuth->validDomain( $this->mDomain
) ) {
171 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
175 // If we are not allowing users to login locally, we should
176 // be checking to see if the user is actually able to
177 // authenticate to the authentication server before they
178 // create an account (otherwise, they can create a local account
179 // and login as any domain user). We only need to check this for
180 // domains that aren't local.
181 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
182 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
183 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
188 if ( wfReadOnly() ) {
189 $wgOut->readOnlyPage();
193 if (!$wgUser->isAllowedToCreateAccount()) {
194 $this->userNotPrivilegedMessage();
199 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
200 $wgUser->inSorbsBlacklist( $ip ) )
202 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
206 $name = trim( $this->mName
);
207 $u = User
::newFromName( $name );
208 if ( is_null( $u ) ||
$u->getName() = 'MediaWiki default' ) {
209 $this->mainLoginForm( wfMsg( 'noname' ) );
213 if ( 0 != $u->idForName() ) {
214 $this->mainLoginForm( wfMsg( 'userexists' ) );
218 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
219 $this->mainLoginForm( wfMsg( 'badretype' ) );
223 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
224 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
228 if ( $wgAccountCreationThrottle ) {
229 $key = $wgDBname.':acctcreate:ip:'.$ip;
230 $value = $wgMemc->incr( $key );
232 $wgMemc->set( $key, 1, 86400 );
234 if ( $value > $wgAccountCreationThrottle ) {
235 $this->throttleHit( $wgAccountCreationThrottle );
241 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
242 // Hook point to add extra creation throttles and blocks
243 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
244 $this->mainLoginForm( $abortError );
248 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
249 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
254 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
255 $ssUpdate->doUpdate();
257 return $this->initUser( $u );
261 * Actually add a user to the database.
262 * Give it a User object that has been initialised with a name.
268 function &initUser( &$u ) {
270 $u->setPassword( $this->mPassword
);
271 $u->setEmail( $this->mEmail
);
272 $u->setRealName( $this->mRealName
);
276 $wgAuth->initUser( $u );
278 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
286 function processLogin() {
290 if ( '' == $this->mName
) {
291 $this->mainLoginForm( wfMsg( 'noname' ) );
294 $u = User
::newFromName( $this->mName
);
295 if( is_null( $u ) ||
$u->getName() == 'MediaWiki default' ) {
296 $this->mainLoginForm( wfMsg( 'noname' ) );
299 if ( 0 == $u->getID() ) {
302 * If the external authentication plugin allows it,
303 * automatically create a new account for users that
304 * are externally defined but have not yet logged in.
306 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
307 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
308 $u =& $this->initUser( $u );
310 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
314 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
318 $u->loadFromDatabase();
321 if (!$u->checkPassword( $this->mPassword
)) {
322 $this->mainLoginForm( wfMsg( $this->mPassword
== '' ?
'wrongpasswordempty' : 'wrongpassword' ) );
326 # We've verified now, update the real record
328 if ( $this->mRemember
) {
333 $u->setOption( 'rememberpassword', $r );
335 $wgAuth->updateUser( $u );
338 $wgUser->setCookies();
340 $wgUser->saveSettings();
342 if( $this->hasSessionCookie() ) {
343 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
345 return $this->cookieRedirectCheck( 'login' );
352 function mailPassword() {
353 if ( '' == $this->mName
) {
354 $this->mainLoginForm( wfMsg( 'noname' ) );
357 $u = User
::newFromName( $this->mName
);
358 if( is_null( $u ) ) {
359 $this->mainLoginForm( wfMsg( 'noname' ) );
362 if ( 0 == $u->getID() ) {
363 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
367 $u->loadFromDatabase();
369 $result = $this->mailPasswordInternal( $u );
370 if( WikiError
::isError( $result ) ) {
371 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
373 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
379 * @return mixed true on success, WikiError on failure
382 function mailPasswordInternal( $u ) {
383 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
385 if ( '' == $u->getEmail() ) {
386 return wfMsg( 'noemail', $u->getName() );
389 $np = $u->randomPassword();
390 $u->setNewpassword( $np );
392 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
397 if ( '' == $ip ) { $ip = '(Unknown)'; }
399 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
401 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
407 * @param string $msg Message that will be shown on success
408 * @param bool $auto Toggle auto-redirect to main page; default true
411 function successfulLogin( $msg, $auto = true ) {
415 # Run any hooks; ignore results
417 wfRunHooks('UserLoginComplete', array(&$wgUser));
419 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
420 $wgOut->setRobotpolicy( 'noindex,nofollow' );
421 $wgOut->setArticleRelated( false );
422 $wgOut->addWikiText( $msg );
423 $wgOut->returnToMain( $auto );
427 function userNotPrivilegedMessage() {
430 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
431 $wgOut->setRobotpolicy( 'noindex,nofollow' );
432 $wgOut->setArticleRelated( false );
434 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
436 $wgOut->returnToMain( false );
442 function mainLoginForm( $msg, $msgtype = 'error' ) {
443 global $wgUser, $wgOut;
444 global $wgAllowRealName, $wgEnableEmail;
445 global $wgCookiePrefix;
448 if ( '' == $this->mName
) {
449 if ( $wgUser->isLoggedIn() ) {
450 $this->mName
= $wgUser->getName();
452 $this->mName
= @$_COOKIE[$wgCookiePrefix.'UserName'];
456 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
458 require_once( 'SkinTemplate.php' );
459 require_once( 'templates/Userlogin.php' );
461 if ( $this->mType
== 'signup' ) {
462 $template =& new UsercreateTemplate();
463 $q = 'action=submitlogin&type=signup';
464 $linkq = 'type=login';
465 $linkmsg = 'gotaccount';
467 $template =& new UserloginTemplate();
468 $q = 'action=submitlogin&type=login';
469 $linkq = 'type=signup';
470 $linkmsg = 'nologin';
473 if ( !empty( $this->mReturnto
) ) {
474 $returnto = '&returnto=' . wfUrlencode( $this->mReturnto
);
479 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
480 $link .= wfMsgHtml( $linkmsg . 'link' );
483 # Don't show a "create account" link if the user can't
484 if( $this->showCreateOrLoginLink( $wgUser ) )
485 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
487 $template->set( 'link', '' );
489 $template->set( 'header', '' );
490 $template->set( 'name', $this->mName
);
491 $template->set( 'password', $this->mPassword
);
492 $template->set( 'retype', $this->mRetype
);
493 $template->set( 'email', $this->mEmail
);
494 $template->set( 'realname', $this->mRealName
);
495 $template->set( 'domain', $this->mDomain
);
497 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
498 $template->set( 'message', $msg );
499 $template->set( 'messagetype', $msgtype );
500 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
501 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
502 $template->set( 'userealname', $wgAllowRealName );
503 $template->set( 'useemail', $wgEnableEmail );
504 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
506 // Give authentication and captcha plugins a chance to modify the form
507 $wgAuth->modifyUITemplate( $template );
508 if ( $this->mType
== 'signup' ) {
509 wfRunHooks( 'UserCreateForm', array( &$template ) );
511 wfRunHooks( 'UserLoginForm', array( &$template ) );
514 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
515 $wgOut->setRobotpolicy( 'noindex,nofollow' );
516 $wgOut->setArticleRelated( false );
517 $wgOut->addTemplate( $template );
523 function showCreateOrLoginLink( &$user ) {
524 if( $this->mType
== 'signup' ) {
526 } elseif( $user->isAllowedToCreateAccount() ) {
536 function hasSessionCookie() {
537 global $wgDisableCookieCheck;
538 return ( $wgDisableCookieCheck ) ?
true : ( isset( $_COOKIE[session_name()] ) );
544 function cookieRedirectCheck( $type ) {
547 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
548 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
550 return $wgOut->redirect( $check );
556 function onCookieRedirectCheck( $type ) {
559 if ( !$this->hasSessionCookie() ) {
560 if ( $type == 'new' ) {
561 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
562 } else if ( $type == 'login' ) {
563 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
566 return $this->mainLoginForm( wfMsg( 'error' ) );
569 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
576 function throttleHit( $limit ) {
579 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );