Revert to r14165 . Did too many changes, didnt even run parserTests (i am bad)
[mediawiki.git] / includes / SpecialUserlogin.php
blob65ff6e2d35d0fc2a41d12430c861385b5ff0bb9d
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
8 /**
9 * constructor
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
13 global $wgRequest;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[session_name()] ) ) {
15 User::SetupSession();
18 $form = new LoginForm( $wgRequest );
19 $form->execute();
22 /**
24 * @package MediaWiki
25 * @subpackage SpecialPage
27 class LoginForm {
28 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
29 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
30 var $mLoginattempt, $mRemember, $mEmail, $mDomain;
32 /**
33 * Constructor
34 * @param webrequest $request A webrequest object passed by reference
36 function LoginForm( &$request ) {
37 global $wgLang, $wgAllowRealName, $wgEnableEmail;
38 global $wgAuth;
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' )
50 && $wgEnableEmail;
51 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
52 && $wgEnableEmail;
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' );
59 } else {
60 $this->mEmail = '';
62 if( $wgAllowRealName ) {
63 $this->mRealName = $request->getText( 'wpRealName' );
64 } else {
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 = '';
79 function execute() {
80 if ( !is_null( $this->mCookieCheck ) ) {
81 $this->onCookieRedirectCheck( $this->mCookieCheck );
82 return;
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( '' );
97 /**
98 * @private
100 function addNewAccountMailPassword() {
101 global $wgOut;
103 if ('' == $this->mEmail) {
104 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
105 return;
108 $u = $this->addNewaccountInternal();
110 if ($u == NULL) {
111 return;
114 $u->saveSettings();
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() ) );
125 } else {
126 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
127 $wgOut->returnToMain( false );
129 $u = 0;
134 * @private
136 function addNewAccount() {
137 global $wgUser, $wgEmailAuthentication;
139 # Create the account and abort if there's a problem doing so
140 $u = $this->addNewAccountInternal();
141 if( $u == NULL )
142 return;
144 # Save user settings and send out an email authentication message if needed
145 $u->saveSettings();
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() ) {
152 $wgUser = $u;
153 $wgUser->setCookies();
154 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
155 if( $this->hasSessionCookie() ) {
156 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
157 } else {
158 return $this->cookieRedirectCheck( 'new' );
160 } else {
161 # Confirm that the account was created
162 global $wgOut;
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 ) );
171 return true;
176 * @private
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' ) );
187 return false;
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' ) );
199 return false;
203 if ( wfReadOnly() ) {
204 $wgOut->readOnlyPage();
205 return false;
208 if (!$wgUser->isAllowedToCreateAccount()) {
209 $this->userNotPrivilegedMessage();
210 return false;
213 $ip = wfGetIP();
214 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
215 $wgUser->inSorbsBlacklist( $ip ) )
217 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
218 return;
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' ) );
225 return false;
228 if ( 0 != $u->idForName() ) {
229 $this->mainLoginForm( wfMsg( 'userexists' ) );
230 return false;
233 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
234 $this->mainLoginForm( wfMsg( 'badretype' ) );
235 return false;
238 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
239 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
240 return false;
243 if ( $wgAccountCreationThrottle ) {
244 $key = $wgDBname.':acctcreate:ip:'.$ip;
245 $value = $wgMemc->incr( $key );
246 if ( !$value ) {
247 $wgMemc->set( $key, 1, 86400 );
249 if ( $value > $wgAccountCreationThrottle ) {
250 $this->throttleHit( $wgAccountCreationThrottle );
251 return false;
255 $abortError = '';
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 );
260 return false;
263 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
264 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
265 return false;
268 # Update user count
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.
281 * @private
283 function &initUser( &$u ) {
284 $u->addToDatabase();
285 $u->setPassword( $this->mPassword );
286 $u->setEmail( $this->mEmail );
287 $u->setRealName( $this->mRealName );
288 $u->setToken();
290 global $wgAuth;
291 $wgAuth->initUser( $u );
293 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
295 return $u;
299 * @private
301 function processLogin() {
302 global $wgUser, $wgAuth, $wgReservedUsernames;
304 if ( '' == $this->mName ) {
305 $this->mainLoginForm( wfMsg( 'noname' ) );
306 return;
308 $u = User::newFromName( $this->mName );
309 if( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
310 $this->mainLoginForm( wfMsg( 'noname' ) );
311 return;
313 if ( 0 == $u->getID() ) {
314 global $wgAuth;
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 );
323 } else {
324 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
325 return;
327 } else {
328 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
329 return;
331 } else {
332 $u->loadFromDatabase();
335 if (!$u->checkPassword( $this->mPassword )) {
336 $this->mainLoginForm( wfMsg( $this->mPassword == '' ? 'wrongpasswordempty' : 'wrongpassword' ) );
337 return;
340 # We've verified now, update the real record
342 if ( $this->mRemember ) {
343 $r = 1;
344 } else {
345 $r = 0;
347 $u->setOption( 'rememberpassword', $r );
349 $wgAuth->updateUser( $u );
351 $wgUser = $u;
352 $wgUser->setCookies();
354 $wgUser->saveSettings();
356 if( $this->hasSessionCookie() ) {
357 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
358 } else {
359 return $this->cookieRedirectCheck( 'login' );
364 * @private
366 function mailPassword() {
367 if ( '' == $this->mName ) {
368 $this->mainLoginForm( wfMsg( 'noname' ) );
369 return;
371 $u = User::newFromName( $this->mName );
372 if( is_null( $u ) ) {
373 $this->mainLoginForm( wfMsg( 'noname' ) );
374 return;
376 if ( 0 == $u->getID() ) {
377 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
378 return;
381 $u->loadFromDatabase();
383 $result = $this->mailPasswordInternal( $u );
384 if( WikiError::isError( $result ) ) {
385 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
386 } else {
387 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
393 * @return mixed true on success, WikiError on failure
394 * @private
396 function mailPasswordInternal( $u ) {
397 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
399 if ( '' == $u->getEmail() ) {
400 return wfMsg( 'noemail', $u->getName() );
403 $np = $u->randomPassword();
404 $u->setNewpassword( $np );
406 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
408 $u->saveSettings();
410 $ip = wfGetIP();
411 if ( '' == $ip ) { $ip = '(Unknown)'; }
413 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
415 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
416 return $result;
421 * @param string $msg Message that will be shown on success
422 * @param bool $auto Toggle auto-redirect to main page; default true
423 * @private
425 function successfulLogin( $msg, $auto = true ) {
426 global $wgUser;
427 global $wgOut;
429 # Run any hooks; ignore results
431 wfRunHooks('UserLoginComplete', array(&$wgUser));
433 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
434 $wgOut->setRobotpolicy( 'noindex,nofollow' );
435 $wgOut->setArticleRelated( false );
436 $wgOut->addWikiText( $msg );
437 if ( !empty( $this->mReturnTo ) ) {
438 $wgOut->returnToMain( $auto, $this->mReturnTo );
439 } else {
440 $wgOut->returnToMain( $auto );
444 /** */
445 function userNotPrivilegedMessage() {
446 global $wgOut;
448 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
449 $wgOut->setRobotpolicy( 'noindex,nofollow' );
450 $wgOut->setArticleRelated( false );
452 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
454 $wgOut->returnToMain( false );
458 * @private
460 function mainLoginForm( $msg, $msgtype = 'error' ) {
461 global $wgUser, $wgOut;
462 global $wgAllowRealName, $wgEnableEmail;
463 global $wgCookiePrefix;
464 global $wgAuth;
466 if ( '' == $this->mName ) {
467 if ( $wgUser->isLoggedIn() ) {
468 $this->mName = $wgUser->getName();
469 } else {
470 $this->mName = @$_COOKIE[$wgCookiePrefix.'UserName'];
474 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
476 require_once( 'SkinTemplate.php' );
477 require_once( 'templates/Userlogin.php' );
479 if ( $this->mType == 'signup' ) {
480 $template =& new UsercreateTemplate();
481 $q = 'action=submitlogin&type=signup';
482 $linkq = 'type=login';
483 $linkmsg = 'gotaccount';
484 } else {
485 $template =& new UserloginTemplate();
486 $q = 'action=submitlogin&type=login';
487 $linkq = 'type=signup';
488 $linkmsg = 'nologin';
491 if ( !empty( $this->mReturnTo ) ) {
492 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
493 $q .= $returnto;
494 $linkq .= $returnto;
497 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
498 $link .= wfMsgHtml( $linkmsg . 'link' );
499 $link .= '</a>';
501 # Don't show a "create account" link if the user can't
502 if( $this->showCreateOrLoginLink( $wgUser ) )
503 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
504 else
505 $template->set( 'link', '' );
507 $template->set( 'header', '' );
508 $template->set( 'name', $this->mName );
509 $template->set( 'password', $this->mPassword );
510 $template->set( 'retype', $this->mRetype );
511 $template->set( 'email', $this->mEmail );
512 $template->set( 'realname', $this->mRealName );
513 $template->set( 'domain', $this->mDomain );
515 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
516 $template->set( 'message', $msg );
517 $template->set( 'messagetype', $msgtype );
518 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
519 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
520 $template->set( 'userealname', $wgAllowRealName );
521 $template->set( 'useemail', $wgEnableEmail );
522 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
524 // Give authentication and captcha plugins a chance to modify the form
525 $wgAuth->modifyUITemplate( $template );
526 if ( $this->mType == 'signup' ) {
527 wfRunHooks( 'UserCreateForm', array( &$template ) );
528 } else {
529 wfRunHooks( 'UserLoginForm', array( &$template ) );
532 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
533 $wgOut->setRobotpolicy( 'noindex,nofollow' );
534 $wgOut->setArticleRelated( false );
535 $wgOut->addTemplate( $template );
539 * @private
541 function showCreateOrLoginLink( &$user ) {
542 if( $this->mType == 'signup' ) {
543 return( true );
544 } elseif( $user->isAllowedToCreateAccount() ) {
545 return( true );
546 } else {
547 return( false );
552 * @private
554 function hasSessionCookie() {
555 global $wgDisableCookieCheck;
556 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
560 * @private
562 function cookieRedirectCheck( $type ) {
563 global $wgOut;
565 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
566 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
568 return $wgOut->redirect( $check );
572 * @private
574 function onCookieRedirectCheck( $type ) {
575 global $wgUser;
577 if ( !$this->hasSessionCookie() ) {
578 if ( $type == 'new' ) {
579 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
580 } else if ( $type == 'login' ) {
581 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
582 } else {
583 # shouldn't happen
584 return $this->mainLoginForm( wfMsg( 'error' ) );
586 } else {
587 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
592 * @private
594 function throttleHit( $limit ) {
595 global $wgOut;
597 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );