#3364: pt_br name typo
[mediawiki.git] / includes / SpecialUserlogin.php
blob179a9b08c74f62babcebd8c44cfdcabf72a95a69
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 wfRunHooks( 'AddNewAccount' );
152 if( $this->hasSessionCookie() ) {
153 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
154 } else {
155 return $this->cookieRedirectCheck( 'new' );
160 * @access private
162 function addNewAccountInternal() {
163 global $wgUser, $wgOut;
164 global $wgUseLatin1, $wgEnableSorbs, $wgProxyWhitelist;
165 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
166 global $wgAuth;
168 // If the user passes an invalid domain, something is fishy
169 if( !$wgAuth->validDomain( $this->mDomain ) ) {
170 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
171 return false;
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' ) );
183 return false;
187 if (!$wgUser->isAllowedToCreateAccount()) {
188 $this->userNotPrivilegedMessage();
189 return false;
192 $ip = wfGetIP();
193 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
194 $wgUser->inSorbsBlacklist( $ip ) )
196 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
197 return;
201 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
202 $this->mainLoginForm( wfMsg( 'badretype' ) );
203 return false;
206 $name = trim( $this->mName );
207 $u = User::newFromName( $name );
208 if ( is_null( $u ) ) {
209 $this->mainLoginForm( wfMsg( 'noname' ) );
210 return false;
213 if ( wfReadOnly() ) {
214 $wgOut->readOnlyPage();
215 return false;
218 if ( 0 != $u->idForName() ) {
219 $this->mainLoginForm( wfMsg( 'userexists' ) );
220 return false;
223 if ( !$wgUser->isValidPassword( $this->mPassword ) ) {
224 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
225 return false;
228 if ( $wgAccountCreationThrottle ) {
229 $key = $wgDBname.':acctcreate:ip:'.$ip;
230 $value = $wgMemc->incr( $key );
231 if ( !$value ) {
232 $wgMemc->set( $key, 1, 86400 );
234 if ( $value > $wgAccountCreationThrottle ) {
235 $this->throttleHit( $wgAccountCreationThrottle );
236 return false;
240 if( !$wgAuth->addUser( $u, $this->mPassword ) ) {
241 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
242 return false;
245 # Update user count
246 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
247 $ssUpdate->doUpdate();
249 return $this->initUser( $u );
253 * Actually add a user to the database.
254 * Give it a User object that has been initialised with a name.
256 * @param User $u
257 * @return User
258 * @access private
260 function &initUser( &$u ) {
261 $u->addToDatabase();
262 $u->setPassword( $this->mPassword );
263 $u->setEmail( $this->mEmail );
264 $u->setRealName( $this->mRealName );
265 $u->setToken();
267 global $wgAuth;
268 $wgAuth->initUser( $u );
270 if ( $this->mRemember ) { $r = 1; }
271 else { $r = 0; }
272 $u->setOption( 'rememberpassword', $r );
274 return $u;
278 * @access private
280 function processLogin() {
281 global $wgUser;
282 global $wgAuth;
284 if ( '' == $this->mName ) {
285 $this->mainLoginForm( wfMsg( 'noname' ) );
286 return;
288 $u = User::newFromName( $this->mName );
289 if( is_null( $u ) ) {
290 $this->mainLoginForm( wfMsg( 'noname' ) );
291 return;
293 if ( 0 == $u->getID() ) {
294 global $wgAuth;
296 * If the external authentication plugin allows it,
297 * automatically create a new account for users that
298 * are externally defined but have not yet logged in.
300 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
301 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword ) ) {
302 $u =& $this->initUser( $u );
303 } else {
304 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
305 return;
307 } else {
308 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
309 return;
311 } else {
312 $u->loadFromDatabase();
315 if (!$u->checkPassword( $this->mPassword )) {
316 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
317 return;
320 # We've verified now, update the real record
322 if ( $this->mRemember ) {
323 $r = 1;
324 } else {
325 $r = 0;
327 $u->setOption( 'rememberpassword', $r );
329 $wgAuth->updateUser( $u );
331 $wgUser = $u;
332 $wgUser->setCookies();
334 $wgUser->saveSettings();
336 if( $this->hasSessionCookie() ) {
337 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
338 } else {
339 return $this->cookieRedirectCheck( 'login' );
344 * @access private
346 function mailPassword() {
347 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
348 global $wgCookiePath, $wgCookieDomain, $wgDBname;
350 if ( '' == $this->mName ) {
351 $this->mainLoginForm( wfMsg( 'noname' ) );
352 return;
354 $u = User::newFromName( $this->mName );
355 if( is_null( $u ) ) {
356 $this->mainLoginForm( wfMsg( 'noname' ) );
357 return;
359 if ( 0 == $u->getID() ) {
360 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
361 return;
364 $u->loadFromDatabase();
366 $result = $this->mailPasswordInternal( $u );
367 if( WikiError::isError( $result ) ) {
368 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
369 } else {
370 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
376 * @return mixed true on success, WikiError on failure
377 * @access private
379 function mailPasswordInternal( $u ) {
380 global $wgPasswordSender, $wgDBname;
381 global $wgCookiePath, $wgCookieDomain;
383 if ( '' == $u->getEmail() ) {
384 return wfMsg( 'noemail', $u->getName() );
387 $np = $u->randomPassword();
388 $u->setNewpassword( $np );
390 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
392 $u->saveSettings();
394 $ip = wfGetIP();
395 if ( '' == $ip ) { $ip = '(Unknown)'; }
397 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
399 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
400 return $result;
405 * @param string $msg Message that will be shown on success.
406 * @access private
408 function successfulLogin( $msg ) {
409 global $wgUser;
410 global $wgOut;
412 # Run any hooks; ignore results
414 wfRunHooks('UserLoginComplete', array(&$wgUser));
416 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
417 $wgOut->setRobotpolicy( 'noindex,nofollow' );
418 $wgOut->setArticleRelated( false );
419 $wgOut->addWikiText( $msg );
420 $wgOut->returnToMain();
423 /** */
424 function userNotPrivilegedMessage() {
425 global $wgOut;
427 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
428 $wgOut->setRobotpolicy( 'noindex,nofollow' );
429 $wgOut->setArticleRelated( false );
431 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
433 $wgOut->returnToMain( false );
437 * @access private
439 function mainLoginForm( $err ) {
440 global $wgUser, $wgOut, $wgLang;
441 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
442 global $wgAuth;
444 if ( '' == $this->mName ) {
445 if ( $wgUser->isLoggedIn() ) {
446 $this->mName = $wgUser->getName();
447 } else {
448 $this->mName = @$_COOKIE[$wgDBname.'UserName'];
452 $q = 'action=submitlogin';
453 if ( !empty( $this->mReturnto ) ) {
454 $q .= '&returnto=' . wfUrlencode( $this->mReturnto );
456 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
458 require_once( 'templates/Userlogin.php' );
459 $template =& new UserloginTemplate();
461 $template->set( 'name', $this->mName );
462 $template->set( 'password', $this->mPassword );
463 $template->set( 'retype', $this->mRetype );
464 $template->set( 'email', $this->mEmail );
465 $template->set( 'realname', $this->mRealName );
466 $template->set( 'domain', $this->mDomain );
468 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
469 $template->set( 'error', $err );
470 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
471 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
472 $template->set( 'userealname', $wgAllowRealName );
473 $template->set( 'useemail', $wgEnableEmail );
474 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
475 $wgAuth->modifyUITemplate( $template );
477 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
478 $wgOut->setRobotpolicy( 'noindex,nofollow' );
479 $wgOut->setArticleRelated( false );
480 $wgOut->addTemplate( $template );
484 * @access private
486 function hasSessionCookie() {
487 global $wgDisableCookieCheck;
488 return ( $wgDisableCookieCheck ) ? true : ( isset( $_COOKIE[session_name()] ) );
492 * @access private
494 function cookieRedirectCheck( $type ) {
495 global $wgOut, $wgLang;
497 $titleObj = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
498 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
500 return $wgOut->redirect( $check );
504 * @access private
506 function onCookieRedirectCheck( $type ) {
507 global $wgUser;
509 if ( !$this->hasSessionCookie() ) {
510 if ( $type == 'new' ) {
511 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
512 } else if ( $type == 'login' ) {
513 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
514 } else {
515 # shouldn't happen
516 return $this->mainLoginForm( wfMsg( 'error' ) );
518 } else {
519 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
524 * @access private
526 function throttleHit( $limit ) {
527 global $wgOut;
529 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );