5 * @subpackage SpecialPage
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('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->mName
= $request->getText( 'wpName' );
41 $this->mPassword
= $request->getText( 'wpPassword' );
42 $this->mRetype
= $request->getText( 'wpRetype' );
43 $this->mDomain
= $request->getText( 'wpDomain' );
44 $this->mReturnto
= $request->getVal( 'returnto' );
45 $this->mCookieCheck
= $request->getVal( 'wpCookieCheck' );
46 $this->mPosted
= $request->wasPosted();
47 $this->mCreateaccount
= $request->getCheck( 'wpCreateaccount' );
48 $this->mCreateaccountMail
= $request->getCheck( 'wpCreateaccountMail' )
50 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
52 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
53 $this->mAction
= $request->getVal( 'action' );
54 $this->mRemember
= $request->getCheck( 'wpRemember' );
56 if( $wgEnableEmail ) {
57 $this->mEmail
= $request->getText( 'wpEmail' );
61 if( $wgAllowRealName ) {
62 $this->mRealName
= $request->getText( 'wpRealName' );
64 $this->mRealName
= '';
67 if( !$wgAuth->validDomain( $this->mDomain
) ) {
68 $this->mDomain
= 'invaliddomain';
70 $wgAuth->setDomain( $this->mDomain
);
72 # When switching accounts, it sucks to get automatically logged out
73 if( $this->mReturnto
== $wgLang->specialPage( 'Userlogout' ) ) {
74 $this->mReturnto
= '';
79 if ( !is_null( $this->mCookieCheck
) ) {
80 $this->onCookieRedirectCheck( $this->mCookieCheck
);
82 } else if( $this->mPosted
) {
83 if( $this->mCreateaccount
) {
84 return $this->addNewAccount();
85 } else if ( $this->mCreateaccountMail
) {
86 return $this->addNewAccountMailPassword();
87 } else if ( $this->mMailmypassword
) {
88 return $this->mailPassword();
89 } else if ( ( 'submitlogin' == $this->mAction
) ||
$this->mLoginattempt
) {
90 return $this->processLogin();
93 $this->mainLoginForm( '' );
99 function addNewAccountMailPassword() {
102 if ('' == $this->mEmail
) {
103 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
107 $u = $this->addNewaccountInternal();
114 $result = $this->mailPasswordInternal($u);
116 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
117 $wgOut->setRobotpolicy( 'noindex,nofollow' );
118 $wgOut->setArticleRelated( false );
120 if( WikiError
::isError( $result ) ) {
121 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
123 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
124 $wgOut->returnToMain( false );
133 function addNewAccount() {
134 global $wgUser, $wgOut, $wgEmailAuthentication;
136 $u = $this->addNewAccountInternal();
143 $wgUser->setCookies();
145 $wgUser->saveSettings();
146 if( $wgEmailAuthentication && $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
147 $wgUser->sendConfirmationMail();
150 if( $this->hasSessionCookie() ) {
151 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
153 return $this->cookieRedirectCheck( 'new' );
160 function addNewAccountInternal() {
161 global $wgUser, $wgOut;
162 global $wgMaxNameChars, $wgUseLatin1, $wgEnableSorbs, $wgProxyWhitelist;
163 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
164 global $wgMinimalPasswordLength;
167 // If the user passes an invalid domain, something is fishy
168 if( !$wgAuth->validDomain( $this->mDomain
) ) {
169 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
173 // If we are not allowing users to login locally, we should
174 // be checking to see if the user is actually able to
175 // authenticate to the authentication server before they
176 // create an account (otherwise, they can create a local account
177 // and login as any domain user). We only need to check this for
178 // domains that aren't local.
179 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
180 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
181 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
188 if (!$wgUser->isAllowedToCreateAccount()) {
189 $this->userNotPrivilegedMessage();
193 if ( $wgEnableSorbs && !in_array( $wgIP, $wgProxyWhitelist ) &&
194 $wgUser->inSorbsBlacklist( $wgIP ) )
196 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) );
201 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
202 $this->mainLoginForm( wfMsg( 'badretype' ) );
206 $name = trim( $this->mName
);
207 $u = User
::newFromName( $name );
208 if ( is_null( $u ) ||
210 $wgUser->isIP( $name ) ||
211 (strpos( $name, '/' ) !== false) ||
212 (strlen( $name ) > $wgMaxNameChars) ||
213 ucFirst($name) != $u->getName() )
215 $this->mainLoginForm( wfMsg( 'noname' ) );
218 if ( wfReadOnly() ) {
219 $wgOut->readOnlyPage();
223 if ( 0 != $u->idForName() ) {
224 $this->mainLoginForm( wfMsg( 'userexists' ) );
228 if ( strlen( $this->mPassword
) < $wgMinimalPasswordLength ) {
229 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
233 if ( $wgAccountCreationThrottle ) {
234 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
235 $value = $wgMemc->incr( $key );
237 $wgMemc->set( $key, 1, 86400 );
239 if ( $value > $wgAccountCreationThrottle ) {
240 $this->throttleHit( $wgAccountCreationThrottle );
245 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
246 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
251 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
252 $ssUpdate->doUpdate();
254 return $this->initUser( $u );
258 * Actually add a user to the database.
259 * Give it a User object that has been initialised with a name.
265 function &initUser( &$u ) {
267 $u->setPassword( $this->mPassword
);
268 $u->setEmail( $this->mEmail
);
269 $u->setRealName( $this->mRealName
);
273 $wgAuth->initUser( $u );
275 if ( $this->mRemember
) { $r = 1; }
277 $u->setOption( 'rememberpassword', $r );
285 function processLogin() {
289 if ( '' == $this->mName
) {
290 $this->mainLoginForm( wfMsg( 'noname' ) );
293 $u = User
::newFromName( $this->mName
);
294 if( is_null( $u ) ) {
295 $this->mainLoginForm( wfMsg( 'noname' ) );
298 if ( 0 == $u->getID() ) {
301 * If the external authentication plugin allows it,
302 * automatically create a new account for users that
303 * are externally defined but have not yet logged in.
305 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
306 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
307 $u =& $this->initUser( $u );
309 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
313 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
317 $u->loadFromDatabase();
320 if (!$u->checkPassword( $this->mPassword
)) {
321 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
325 # We've verified now, update the real record
327 if ( $this->mRemember
) {
332 $u->setOption( 'rememberpassword', $r );
334 $wgAuth->updateUser( $u );
337 $wgUser->setCookies();
339 $wgUser->saveSettings();
341 if( $this->hasSessionCookie() ) {
342 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
344 return $this->cookieRedirectCheck( 'login' );
351 function mailPassword() {
352 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
353 global $wgCookiePath, $wgCookieDomain, $wgDBname;
355 if ( '' == $this->mName
) {
356 $this->mainLoginForm( wfMsg( 'noname' ) );
359 $u = User
::newFromName( $this->mName
);
360 if( is_null( $u ) ) {
361 $this->mainLoginForm( wfMsg( 'noname' ) );
364 if ( 0 == $u->getID() ) {
365 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
369 $u->loadFromDatabase();
371 $result = $this->mailPasswordInternal( $u );
372 if( WikiError
::isError( $result ) ) {
373 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
375 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
381 * @return mixed true on success, WikiError on failure
384 function mailPasswordInternal( $u ) {
385 global $wgPasswordSender, $wgDBname, $wgIP;
386 global $wgCookiePath, $wgCookieDomain;
388 if ( '' == $u->getEmail() ) {
389 return wfMsg( 'noemail', $u->getName() );
392 $np = $u->randomPassword();
393 $u->setNewpassword( $np );
395 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
400 if ( '' == $ip ) { $ip = '(Unknown)'; }
402 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
403 $result = $u->sendMail( wfMsg( 'passwordremindermailsubject' ), $m );
410 * @param string $msg Message that will be shown on success.
413 function successfulLogin( $msg ) {
417 # Run any hooks; ignore results
419 wfRunHooks('UserLoginComplete', array(&$wgUser));
421 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
422 $wgOut->setRobotpolicy( 'noindex,nofollow' );
423 $wgOut->setArticleRelated( false );
424 $wgOut->addWikiText( $msg );
425 $wgOut->returnToMain();
429 function userNotPrivilegedMessage() {
432 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
433 $wgOut->setRobotpolicy( 'noindex,nofollow' );
434 $wgOut->setArticleRelated( false );
436 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
438 $wgOut->returnToMain( false );
444 function mainLoginForm( $err ) {
445 global $wgUser, $wgOut, $wgLang;
446 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
449 if ( '' == $this->mName
) {
450 if ( $wgUser->isLoggedIn() ) {
451 $this->mName
= $wgUser->getName();
453 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
457 $q = 'action=submitlogin';
458 if ( !empty( $this->mReturnto
) ) {
459 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
461 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
463 require_once( 'templates/Userlogin.php' );
464 $template =& new UserloginTemplate();
466 $template->set( 'name', $this->mName
);
467 $template->set( 'password', $this->mPassword
);
468 $template->set( 'retype', $this->mRetype
);
469 $template->set( 'email', $this->mEmail
);
470 $template->set( 'realname', $this->mRealName
);
471 $template->set( 'domain', $this->mDomain
);
473 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
474 $template->set( 'error', $err );
475 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
476 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
477 $template->set( 'userealname', $wgAllowRealName );
478 $template->set( 'useemail', $wgEnableEmail );
479 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
480 $wgAuth->modifyUITemplate( $template );
482 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
483 $wgOut->setRobotpolicy( 'noindex,nofollow' );
484 $wgOut->setArticleRelated( false );
485 $wgOut->addTemplate( $template );
491 function hasSessionCookie() {
492 global $wgDisableCookieCheck;
493 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
499 function cookieRedirectCheck( $type ) {
500 global $wgOut, $wgLang;
502 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
503 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
505 return $wgOut->redirect( $check );
511 function onCookieRedirectCheck( $type ) {
514 if ( !$this->hasSessionCookie() ) {
515 if ( $type == 'new' ) {
516 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
517 } else if ( $type == 'login' ) {
518 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
521 return $this->mainLoginForm( wfMsg( 'error' ) );
524 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
531 function throttleHit( $limit ) {
534 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );