OO OO OO OO OO OO OO OO OO OO OO OO OO style code
[mediawiki.git] / includes / SpecialUserlogin.php
blob84b36ccdb15ad3834d5775821c29bd072adfc8dc
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->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' )
49 && $wgEnableEmail;
50 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
51 && $wgEnableEmail;
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' );
58 } else {
59 $this->mEmail = '';
61 if( $wgAllowRealName ) {
62 $this->mRealName = $request->getText( 'wpRealName' );
63 } else {
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 = '';
78 function execute() {
79 if ( !is_null( $this->mCookieCheck ) ) {
80 $this->onCookieRedirectCheck( $this->mCookieCheck );
81 return;
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( '' );
96 /**
97 * @access private
99 function addNewAccountMailPassword() {
100 global $wgOut;
102 if ('' == $this->mEmail) {
103 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
104 return;
107 $u = $this->addNewaccountInternal();
109 if ($u == NULL) {
110 return;
113 $u->saveSettings();
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() ) );
122 } else {
123 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
124 $wgOut->returnToMain( false );
126 $u = 0;
131 * @access private
133 function addNewAccount() {
134 global $wgUser, $wgOut, $wgEmailAuthentication;
136 $u = $this->addNewAccountInternal();
138 if ($u == NULL) {
139 return;
142 $wgUser = $u;
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() ) );
152 } else {
153 return $this->cookieRedirectCheck( 'new' );
158 * @access private
160 function addNewAccountInternal() {
161 global $wgUser, $wgOut;
162 global $wgUseLatin1, $wgEnableSorbs, $wgProxyWhitelist;
163 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
164 global $wgAuth;
166 // If the user passes an invalid domain, something is fishy
167 if( !$wgAuth->validDomain( $this->mDomain ) ) {
168 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
169 return false;
172 // If we are not allowing users to login locally, we should
173 // be checking to see if the user is actually able to
174 // authenticate to the authentication server before they
175 // create an account (otherwise, they can create a local account
176 // and login as any domain user). We only need to check this for
177 // domains that aren't local.
178 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
179 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
180 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
181 return false;
185 if (!$wgUser->isAllowedToCreateAccount()) {
186 $this->userNotPrivilegedMessage();
187 return false;
190 if ( $wgEnableSorbs && !in_array( $wgIP, $wgProxyWhitelist ) &&
191 $wgUser->inSorbsBlacklist( $wgIP ) )
193 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $wgIP ) . ')' );
194 return;
198 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
199 $this->mainLoginForm( wfMsg( 'badretype' ) );
200 return false;
203 $name = trim( $this->mName );
204 $u = User::newFromName( $name );
205 if ( is_null( $u ) ) {
206 $this->mainLoginForm( wfMsg( 'noname' ) );
207 return false;
210 if ( wfReadOnly() ) {
211 $wgOut->readOnlyPage();
212 return false;
215 if ( 0 != $u->idForName() ) {
216 $this->mainLoginForm( wfMsg( 'userexists' ) );
217 return false;
220 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
221 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
222 return false;
225 if ( $wgAccountCreationThrottle ) {
226 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
227 $value = $wgMemc->incr( $key );
228 if ( !$value ) {
229 $wgMemc->set( $key, 1, 86400 );
231 if ( $value > $wgAccountCreationThrottle ) {
232 $this->throttleHit( $wgAccountCreationThrottle );
233 return false;
237 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
238 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
239 return false;
242 # Update user count
243 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
244 $ssUpdate->doUpdate();
246 return $this->initUser( $u );
250 * Actually add a user to the database.
251 * Give it a User object that has been initialised with a name.
253 * @param User $u
254 * @return User
255 * @access private
257 function &initUser( &$u ) {
258 $u->addToDatabase();
259 $u->setPassword( $this->mPassword );
260 $u->setEmail( $this->mEmail );
261 $u->setRealName( $this->mRealName );
262 $u->setToken();
264 global $wgAuth;
265 $wgAuth->initUser( $u );
267 if ( $this->mRemember ) { $r = 1; }
268 else { $r = 0; }
269 $u->setOption( 'rememberpassword', $r );
271 return $u;
275 * @access private
277 function processLogin() {
278 global $wgUser;
279 global $wgAuth;
281 if ( '' == $this->mName ) {
282 $this->mainLoginForm( wfMsg( 'noname' ) );
283 return;
285 $u = User::newFromName( $this->mName );
286 if( is_null( $u ) ) {
287 $this->mainLoginForm( wfMsg( 'noname' ) );
288 return;
290 if ( 0 == $u->getID() ) {
291 global $wgAuth;
293 * If the external authentication plugin allows it,
294 * automatically create a new account for users that
295 * are externally defined but have not yet logged in.
297 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
298 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
299 $u =& $this->initUser( $u );
300 } else {
301 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
302 return;
304 } else {
305 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
306 return;
308 } else {
309 $u->loadFromDatabase();
312 if (!$u->checkPassword( $this->mPassword )) {
313 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
314 return;
317 # We've verified now, update the real record
319 if ( $this->mRemember ) {
320 $r = 1;
321 } else {
322 $r = 0;
324 $u->setOption( 'rememberpassword', $r );
326 $wgAuth->updateUser( $u );
328 $wgUser = $u;
329 $wgUser->setCookies();
331 $wgUser->saveSettings();
333 if( $this->hasSessionCookie() ) {
334 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
335 } else {
336 return $this->cookieRedirectCheck( 'login' );
341 * @access private
343 function mailPassword() {
344 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
345 global $wgCookiePath, $wgCookieDomain, $wgDBname;
347 if ( '' == $this->mName ) {
348 $this->mainLoginForm( wfMsg( 'noname' ) );
349 return;
351 $u = User::newFromName( $this->mName );
352 if( is_null( $u ) ) {
353 $this->mainLoginForm( wfMsg( 'noname' ) );
354 return;
356 if ( 0 == $u->getID() ) {
357 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
358 return;
361 $u->loadFromDatabase();
363 $result = $this->mailPasswordInternal( $u );
364 if( WikiError::isError( $result ) ) {
365 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
366 } else {
367 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
373 * @return mixed true on success, WikiError on failure
374 * @access private
376 function mailPasswordInternal( $u ) {
377 global $wgPasswordSender, $wgDBname, $wgIP;
378 global $wgCookiePath, $wgCookieDomain;
380 if ( '' == $u->getEmail() ) {
381 return wfMsg( 'noemail', $u->getName() );
384 $np = $u->randomPassword();
385 $u->setNewpassword( $np );
387 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
389 $u->saveSettings();
391 $ip = $wgIP;
392 if ( '' == $ip ) { $ip = '(Unknown)'; }
394 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
396 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
397 return $result;
402 * @param string $msg Message that will be shown on success.
403 * @access private
405 function successfulLogin( $msg ) {
406 global $wgUser;
407 global $wgOut;
409 # Run any hooks; ignore results
411 wfRunHooks('UserLoginComplete', array(&$wgUser));
413 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
414 $wgOut->setRobotpolicy( 'noindex,nofollow' );
415 $wgOut->setArticleRelated( false );
416 $wgOut->addWikiText( $msg );
417 $wgOut->returnToMain();
420 /** */
421 function userNotPrivilegedMessage() {
422 global $wgOut;
424 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
425 $wgOut->setRobotpolicy( 'noindex,nofollow' );
426 $wgOut->setArticleRelated( false );
428 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
430 $wgOut->returnToMain( false );
434 * @access private
436 function mainLoginForm( $err ) {
437 global $wgUser, $wgOut, $wgLang;
438 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
439 global $wgAuth;
441 if ( '' == $this->mName ) {
442 if ( $wgUser->isLoggedIn() ) {
443 $this->mName = $wgUser->getName();
444 } else {
445 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
449 $q = 'action=submitlogin';
450 if ( !empty( $this->mReturnto ) ) {
451 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
453 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
455 require_once( 'templates/Userlogin.php' );
456 $template =& new UserloginTemplate();
458 $template->set( 'name', $this->mName );
459 $template->set( 'password', $this->mPassword );
460 $template->set( 'retype', $this->mRetype );
461 $template->set( 'email', $this->mEmail );
462 $template->set( 'realname', $this->mRealName );
463 $template->set( 'domain', $this->mDomain );
465 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
466 $template->set( 'error', $err );
467 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
468 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
469 $template->set( 'userealname', $wgAllowRealName );
470 $template->set( 'useemail', $wgEnableEmail );
471 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
472 $wgAuth->modifyUITemplate( $template );
474 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
475 $wgOut->setRobotpolicy( 'noindex,nofollow' );
476 $wgOut->setArticleRelated( false );
477 $wgOut->addTemplate( $template );
481 * @access private
483 function hasSessionCookie() {
484 global $wgDisableCookieCheck;
485 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
489 * @access private
491 function cookieRedirectCheck( $type ) {
492 global $wgOut, $wgLang;
494 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
495 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
497 return $wgOut->redirect( $check );
501 * @access private
503 function onCookieRedirectCheck( $type ) {
504 global $wgUser;
506 if ( !$this->hasSessionCookie() ) {
507 if ( $type == 'new' ) {
508 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
509 } else if ( $type == 'login' ) {
510 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
511 } else {
512 # shouldn't happen
513 return $this->mainLoginForm( wfMsg( 'error' ) );
515 } else {
516 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
521 * @access private
523 function throttleHit( $limit ) {
524 global $wgOut;
526 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );