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->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 wfRunHooks( 'AddNewAccount', null );
152 if( $this->hasSessionCookie() ) {
153 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
155 return $this->cookieRedirectCheck( 'new' );
162 function addNewAccountInternal() {
163 global $wgUser, $wgOut;
164 global $wgUseLatin1, $wgEnableSorbs, $wgProxyWhitelist;
165 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
168 // If the user passes an invalid domain, something is fishy
169 if( !$wgAuth->validDomain( $this->mDomain
) ) {
170 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
174 // If we are not allowing users to login locally, we should
175 // be checking to see if the user is actually able to
176 // authenticate to the authentication server before they
177 // create an account (otherwise, they can create a local account
178 // and login as any domain user). We only need to check this for
179 // domains that aren't local.
180 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
181 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
182 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
187 if (!$wgUser->isAllowedToCreateAccount()) {
188 $this->userNotPrivilegedMessage();
192 if ( $wgEnableSorbs && !in_array( $wgIP, $wgProxyWhitelist ) &&
193 $wgUser->inSorbsBlacklist( $wgIP ) )
195 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $wgIP ) . ')' );
200 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
201 $this->mainLoginForm( wfMsg( 'badretype' ) );
205 $name = trim( $this->mName
);
206 $u = User
::newFromName( $name );
207 if ( is_null( $u ) ) {
208 $this->mainLoginForm( wfMsg( 'noname' ) );
212 if ( wfReadOnly() ) {
213 $wgOut->readOnlyPage();
217 if ( 0 != $u->idForName() ) {
218 $this->mainLoginForm( wfMsg( 'userexists' ) );
222 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
223 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
227 if ( $wgAccountCreationThrottle ) {
228 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
229 $value = $wgMemc->incr( $key );
231 $wgMemc->set( $key, 1, 86400 );
233 if ( $value > $wgAccountCreationThrottle ) {
234 $this->throttleHit( $wgAccountCreationThrottle );
239 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
240 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
245 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
246 $ssUpdate->doUpdate();
248 return $this->initUser( $u );
252 * Actually add a user to the database.
253 * Give it a User object that has been initialised with a name.
259 function &initUser( &$u ) {
261 $u->setPassword( $this->mPassword
);
262 $u->setEmail( $this->mEmail
);
263 $u->setRealName( $this->mRealName
);
267 $wgAuth->initUser( $u );
269 if ( $this->mRemember
) { $r = 1; }
271 $u->setOption( 'rememberpassword', $r );
279 function processLogin() {
283 if ( '' == $this->mName
) {
284 $this->mainLoginForm( wfMsg( 'noname' ) );
287 $u = User
::newFromName( $this->mName
);
288 if( is_null( $u ) ) {
289 $this->mainLoginForm( wfMsg( 'noname' ) );
292 if ( 0 == $u->getID() ) {
295 * If the external authentication plugin allows it,
296 * automatically create a new account for users that
297 * are externally defined but have not yet logged in.
299 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
300 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
301 $u =& $this->initUser( $u );
303 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
307 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
311 $u->loadFromDatabase();
314 if (!$u->checkPassword( $this->mPassword
)) {
315 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
319 # We've verified now, update the real record
321 if ( $this->mRemember
) {
326 $u->setOption( 'rememberpassword', $r );
328 $wgAuth->updateUser( $u );
331 $wgUser->setCookies();
333 $wgUser->saveSettings();
335 if( $this->hasSessionCookie() ) {
336 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
338 return $this->cookieRedirectCheck( 'login' );
345 function mailPassword() {
346 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
347 global $wgCookiePath, $wgCookieDomain, $wgDBname;
349 if ( '' == $this->mName
) {
350 $this->mainLoginForm( wfMsg( 'noname' ) );
353 $u = User
::newFromName( $this->mName
);
354 if( is_null( $u ) ) {
355 $this->mainLoginForm( wfMsg( 'noname' ) );
358 if ( 0 == $u->getID() ) {
359 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
363 $u->loadFromDatabase();
365 $result = $this->mailPasswordInternal( $u );
366 if( WikiError
::isError( $result ) ) {
367 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
369 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
375 * @return mixed true on success, WikiError on failure
378 function mailPasswordInternal( $u ) {
379 global $wgPasswordSender, $wgDBname, $wgIP;
380 global $wgCookiePath, $wgCookieDomain;
382 if ( '' == $u->getEmail() ) {
383 return wfMsg( 'noemail', $u->getName() );
386 $np = $u->randomPassword();
387 $u->setNewpassword( $np );
389 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
394 if ( '' == $ip ) { $ip = '(Unknown)'; }
396 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
398 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
404 * @param string $msg Message that will be shown on success.
407 function successfulLogin( $msg ) {
411 # Run any hooks; ignore results
413 wfRunHooks('UserLoginComplete', array(&$wgUser));
415 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
416 $wgOut->setRobotpolicy( 'noindex,nofollow' );
417 $wgOut->setArticleRelated( false );
418 $wgOut->addWikiText( $msg );
419 $wgOut->returnToMain();
423 function userNotPrivilegedMessage() {
426 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
427 $wgOut->setRobotpolicy( 'noindex,nofollow' );
428 $wgOut->setArticleRelated( false );
430 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
432 $wgOut->returnToMain( false );
438 function mainLoginForm( $err ) {
439 global $wgUser, $wgOut, $wgLang;
440 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
443 if ( '' == $this->mName
) {
444 if ( $wgUser->isLoggedIn() ) {
445 $this->mName
= $wgUser->getName();
447 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
451 $q = 'action=submitlogin';
452 if ( !empty( $this->mReturnto
) ) {
453 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
455 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
457 require_once( 'templates/Userlogin.php' );
458 $template =& new UserloginTemplate();
460 $template->set( 'name', $this->mName
);
461 $template->set( 'password', $this->mPassword
);
462 $template->set( 'retype', $this->mRetype
);
463 $template->set( 'email', $this->mEmail
);
464 $template->set( 'realname', $this->mRealName
);
465 $template->set( 'domain', $this->mDomain
);
467 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
468 $template->set( 'error', $err );
469 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
470 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
471 $template->set( 'userealname', $wgAllowRealName );
472 $template->set( 'useemail', $wgEnableEmail );
473 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
474 $wgAuth->modifyUITemplate( $template );
476 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
477 $wgOut->setRobotpolicy( 'noindex,nofollow' );
478 $wgOut->setArticleRelated( false );
479 $wgOut->addTemplate( $template );
485 function hasSessionCookie() {
486 global $wgDisableCookieCheck;
487 return ( $wgDisableCookieCheck ) ?
true : ( isset( $_COOKIE[session_name()] ) );
493 function cookieRedirectCheck( $type ) {
494 global $wgOut, $wgLang;
496 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
497 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
499 return $wgOut->redirect( $check );
505 function onCookieRedirectCheck( $type ) {
508 if ( !$this->hasSessionCookie() ) {
509 if ( $type == 'new' ) {
510 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
511 } else if ( $type == 'login' ) {
512 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
515 return $this->mainLoginForm( wfMsg( 'error' ) );
518 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
525 function throttleHit( $limit ) {
528 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );