Emergency revert of r 13955, serious privacy violation
[mediawiki.git] / includes / SpecialUserlogin.php
blobcaedffbcadc5eaa07d60f17e59ce8af57b85a50e
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 $u = $this->addNewAccountInternal();
141 if ($u == NULL) {
142 return;
145 $wgUser = $u;
146 $wgUser->setCookies();
148 $wgUser->saveSettings();
149 if( $wgEmailAuthentication && $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
150 $wgUser->sendConfirmationMail();
153 wfRunHooks( 'AddNewAccount', array( $u ) );
155 if( $this->hasSessionCookie() ) {
156 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ), false );
157 } else {
158 return $this->cookieRedirectCheck( 'new' );
163 * @private
165 function addNewAccountInternal() {
166 global $wgUser, $wgOut;
167 global $wgEnableSorbs, $wgProxyWhitelist;
168 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
169 global $wgAuth, $wgMinimalPasswordLength, $wgReservedUsernames;
171 // If the user passes an invalid domain, something is fishy
172 if( !$wgAuth->validDomain( $this->mDomain ) ) {
173 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
174 return false;
177 // If we are not allowing users to login locally, we should
178 // be checking to see if the user is actually able to
179 // authenticate to the authentication server before they
180 // create an account (otherwise, they can create a local account
181 // and login as any domain user). We only need to check this for
182 // domains that aren't local.
183 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
184 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
185 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
186 return false;
190 if ( wfReadOnly() ) {
191 $wgOut->readOnlyPage();
192 return false;
195 if (!$wgUser->isAllowedToCreateAccount()) {
196 $this->userNotPrivilegedMessage();
197 return false;
200 $ip = wfGetIP();
201 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
202 $wgUser->inSorbsBlacklist( $ip ) )
204 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
205 return;
208 $name = trim( $this->mName );
209 $u = User::newFromName( $name );
210 if ( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
211 $this->mainLoginForm( wfMsg( 'noname' ) );
212 return false;
215 if ( 0 != $u->idForName() ) {
216 $this->mainLoginForm( wfMsg( 'userexists' ) );
217 return false;
220 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
221 $this->mainLoginForm( wfMsg( 'badretype' ) );
222 return false;
225 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
226 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
227 return false;
230 if ( $wgAccountCreationThrottle ) {
231 $key = $wgDBname.':acctcreate:ip:'.$ip;
232 $value = $wgMemc->incr( $key );
233 if ( !$value ) {
234 $wgMemc->set( $key, 1, 86400 );
236 if ( $value > $wgAccountCreationThrottle ) {
237 $this->throttleHit( $wgAccountCreationThrottle );
238 return false;
242 $abortError = '';
243 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
244 // Hook point to add extra creation throttles and blocks
245 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
246 $this->mainLoginForm( $abortError );
247 return false;
250 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
251 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
252 return false;
255 # Update user count
256 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
257 $ssUpdate->doUpdate();
259 return $this->initUser( $u );
263 * Actually add a user to the database.
264 * Give it a User object that has been initialised with a name.
266 * @param $u User object.
267 * @return User object.
268 * @private
270 function &initUser( &$u ) {
271 $u->addToDatabase();
272 $u->setPassword( $this->mPassword );
273 $u->setEmail( $this->mEmail );
274 $u->setRealName( $this->mRealName );
275 $u->setToken();
277 global $wgAuth;
278 $wgAuth->initUser( $u );
280 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
282 return $u;
286 * @private
288 function processLogin() {
289 global $wgUser, $wgAuth, $wgReservedUsernames;
291 if ( '' == $this->mName ) {
292 $this->mainLoginForm( wfMsg( 'noname' ) );
293 return;
295 $u = User::newFromName( $this->mName );
296 if( is_null( $u ) || in_array( $u->getName(), $wgReservedUsernames ) ) {
297 $this->mainLoginForm( wfMsg( 'noname' ) );
298 return;
300 if ( 0 == $u->getID() ) {
301 global $wgAuth;
303 * If the external authentication plugin allows it,
304 * automatically create a new account for users that
305 * are externally defined but have not yet logged in.
307 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
308 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
309 $u =& $this->initUser( $u );
310 } else {
311 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
312 return;
314 } else {
315 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
316 return;
318 } else {
319 $u->loadFromDatabase();
322 if (!$u->checkPassword( $this->mPassword )) {
323 $this->mainLoginForm( wfMsg( $this->mPassword == '' ? 'wrongpasswordempty' : 'wrongpassword' ) );
324 return;
327 # We've verified now, update the real record
329 if ( $this->mRemember ) {
330 $r = 1;
331 } else {
332 $r = 0;
334 $u->setOption( 'rememberpassword', $r );
336 $wgAuth->updateUser( $u );
338 $wgUser = $u;
339 $wgUser->setCookies();
341 $wgUser->saveSettings();
343 if( $this->hasSessionCookie() ) {
344 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
345 } else {
346 return $this->cookieRedirectCheck( 'login' );
351 * @private
353 function mailPassword() {
354 if ( '' == $this->mName ) {
355 $this->mainLoginForm( wfMsg( 'noname' ) );
356 return;
358 $u = User::newFromName( $this->mName );
359 if( is_null( $u ) ) {
360 $this->mainLoginForm( wfMsg( 'noname' ) );
361 return;
363 if ( 0 == $u->getID() ) {
364 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
365 return;
368 $u->loadFromDatabase();
370 $result = $this->mailPasswordInternal( $u );
371 if( WikiError::isError( $result ) ) {
372 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
373 } else {
374 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
380 * @return mixed true on success, WikiError on failure
381 * @private
383 function mailPasswordInternal( $u ) {
384 global $wgCookiePath, $wgCookieDomain, $wgCookiePrefix, $wgCookieSecure;
386 if ( '' == $u->getEmail() ) {
387 return wfMsg( 'noemail', $u->getName() );
390 $np = $u->randomPassword();
391 $u->setNewpassword( $np );
393 setcookie( "{$wgCookiePrefix}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
395 $u->saveSettings();
397 $ip = wfGetIP();
398 if ( '' == $ip ) { $ip = '(Unknown)'; }
400 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
402 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
403 return $result;
408 * @param string $msg Message that will be shown on success
409 * @param bool $auto Toggle auto-redirect to main page; default true
410 * @private
412 function successfulLogin( $msg, $auto = true ) {
413 global $wgUser;
414 global $wgOut;
416 # Run any hooks; ignore results
418 wfRunHooks('UserLoginComplete', array(&$wgUser));
420 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
421 $wgOut->setRobotpolicy( 'noindex,nofollow' );
422 $wgOut->setArticleRelated( false );
423 $wgOut->addWikiText( $msg );
424 if ( !empty( $this->mReturnTo ) ) {
425 $wgOut->returnToMain( $auto, $this->mReturnTo );
426 } else {
427 $wgOut->returnToMain( $auto );
431 /** */
432 function userNotPrivilegedMessage() {
433 global $wgOut;
435 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
436 $wgOut->setRobotpolicy( 'noindex,nofollow' );
437 $wgOut->setArticleRelated( false );
439 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
441 $wgOut->returnToMain( false );
445 * @private
447 function mainLoginForm( $msg, $msgtype = 'error' ) {
448 global $wgUser, $wgOut;
449 global $wgAllowRealName, $wgEnableEmail;
450 global $wgCookiePrefix;
451 global $wgAuth;
453 if ( '' == $this->mName ) {
454 if ( $wgUser->isLoggedIn() ) {
455 $this->mName = $wgUser->getName();
456 } else {
457 $this->mName = @$_COOKIE[$wgCookiePrefix.'UserName'];
461 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
463 require_once( 'SkinTemplate.php' );
464 require_once( 'templates/Userlogin.php' );
466 if ( $this->mType == 'signup' ) {
467 $template =& new UsercreateTemplate();
468 $q = 'action=submitlogin&type=signup';
469 $linkq = 'type=login';
470 $linkmsg = 'gotaccount';
471 } else {
472 $template =& new UserloginTemplate();
473 $q = 'action=submitlogin&type=login';
474 $linkq = 'type=signup';
475 $linkmsg = 'nologin';
478 if ( !empty( $this->mReturnTo ) ) {
479 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
480 $q .= $returnto;
481 $linkq .= $returnto;
484 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
485 $link .= wfMsgHtml( $linkmsg . 'link' );
486 $link .= '</a>';
488 # Don't show a "create account" link if the user can't
489 if( $this->showCreateOrLoginLink( $wgUser ) )
490 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
491 else
492 $template->set( 'link', '' );
494 $template->set( 'header', '' );
495 $template->set( 'name', $this->mName );
496 $template->set( 'password', $this->mPassword );
497 $template->set( 'retype', $this->mRetype );
498 $template->set( 'email', $this->mEmail );
499 $template->set( 'realname', $this->mRealName );
500 $template->set( 'domain', $this->mDomain );
502 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
503 $template->set( 'message', $msg );
504 $template->set( 'messagetype', $msgtype );
505 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
506 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
507 $template->set( 'userealname', $wgAllowRealName );
508 $template->set( 'useemail', $wgEnableEmail );
509 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
511 // Give authentication and captcha plugins a chance to modify the form
512 $wgAuth->modifyUITemplate( $template );
513 if ( $this->mType == 'signup' ) {
514 wfRunHooks( 'UserCreateForm', array( &$template ) );
515 } else {
516 wfRunHooks( 'UserLoginForm', array( &$template ) );
519 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
520 $wgOut->setRobotpolicy( 'noindex,nofollow' );
521 $wgOut->setArticleRelated( false );
522 $wgOut->addTemplate( $template );
526 * @private
528 function showCreateOrLoginLink( &$user ) {
529 if( $this->mType == 'signup' ) {
530 return( true );
531 } elseif( $user->isAllowedToCreateAccount() ) {
532 return( true );
533 } else {
534 return( false );
539 * @private
541 function hasSessionCookie() {
542 global $wgDisableCookieCheck;
543 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
547 * @private
549 function cookieRedirectCheck( $type ) {
550 global $wgOut;
552 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
553 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
555 return $wgOut->redirect( $check );
559 * @private
561 function onCookieRedirectCheck( $type ) {
562 global $wgUser;
564 if ( !$this->hasSessionCookie() ) {
565 if ( $type == 'new' ) {
566 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
567 } else if ( $type == 'login' ) {
568 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
569 } else {
570 # shouldn't happen
571 return $this->mainLoginForm( wfMsg( 'error' ) );
573 } else {
574 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
579 * @private
581 function throttleHit( $limit ) {
582 global $wgOut;
584 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );