00001 <?php
00010 function wfSpecialUserlogin( $par = '' ) {
00011 global $wgRequest;
00012 if( session_id() == '' ) {
00013 wfSetupSession();
00014 }
00015
00016 $form = new LoginForm( $wgRequest, $par );
00017 $form->execute();
00018 }
00019
00024 class LoginForm {
00025
00026 const SUCCESS = 0;
00027 const NO_NAME = 1;
00028 const ILLEGAL = 2;
00029 const WRONG_PLUGIN_PASS = 3;
00030 const NOT_EXISTS = 4;
00031 const WRONG_PASS = 5;
00032 const EMPTY_PASS = 6;
00033 const RESET_PASS = 7;
00034 const ABORTED = 8;
00035 const CREATE_BLOCKED = 9;
00036 const THROTTLED = 10;
00037 const USER_BLOCKED = 11;
00038 const NEED_TOKEN = 12;
00039 const WRONG_TOKEN = 13;
00040
00041 var $mName, $mPassword, $mRetype, $mReturnTo, $mCookieCheck, $mPosted;
00042 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
00043 var $mLoginattempt, $mRemember, $mEmail, $mDomain, $mLanguage, $mSkipCookieCheck;
00044 var $mToken;
00045
00050 function LoginForm( &$request, $par = '' ) {
00051 global $wgLang, $wgAllowRealName, $wgEnableEmail;
00052 global $wgAuth, $wgRedirectOnLogin;
00053
00054 $this->mType = ( $par == 'signup' ) ? $par : $request->getText( 'type' ); # Check for [[Special:Userlogin/signup]]
00055 $this->mName = $request->getText( 'wpName' );
00056 $this->mPassword = $request->getText( 'wpPassword' );
00057 $this->mRetype = $request->getText( 'wpRetype' );
00058 $this->mDomain = $request->getText( 'wpDomain' );
00059 $this->mReturnTo = $request->getVal( 'returnto' );
00060 $this->mCookieCheck = $request->getVal( 'wpCookieCheck' );
00061 $this->mPosted = $request->wasPosted();
00062 $this->mCreateaccount = $request->getCheck( 'wpCreateaccount' );
00063 $this->mCreateaccountMail = $request->getCheck( 'wpCreateaccountMail' )
00064 && $wgEnableEmail;
00065 $this->mMailmypassword = $request->getCheck( 'wpMailmypassword' )
00066 && $wgEnableEmail;
00067 $this->mLoginattempt = $request->getCheck( 'wpLoginattempt' );
00068 $this->mAction = $request->getVal( 'action' );
00069 $this->mRemember = $request->getCheck( 'wpRemember' );
00070 $this->mLanguage = $request->getText( 'uselang' );
00071 $this->mSkipCookieCheck = $request->getCheck( 'wpSkipCookieCheck' );
00072 $this->mToken = ($this->mType == 'signup' ) ? $request->getVal( 'wpCreateaccountToken' ) : $request->getVal( 'wpLoginToken' );
00073
00074 if ( $wgRedirectOnLogin ) {
00075 $this->mReturnTo = $wgRedirectOnLogin;
00076 }
00077
00078 if( $wgEnableEmail ) {
00079 $this->mEmail = $request->getText( 'wpEmail' );
00080 } else {
00081 $this->mEmail = '';
00082 }
00083 if( $wgAllowRealName ) {
00084 $this->mRealName = $request->getText( 'wpRealName' );
00085 } else {
00086 $this->mRealName = '';
00087 }
00088
00089 if( !$wgAuth->validDomain( $this->mDomain ) ) {
00090 $this->mDomain = 'invaliddomain';
00091 }
00092 $wgAuth->setDomain( $this->mDomain );
00093
00094 # When switching accounts, it sucks to get automatically logged out
00095 if( $this->mReturnTo == $wgLang->specialPage( 'Userlogout' ) ) {
00096 $this->mReturnTo = '';
00097 }
00098 }
00099
00100 function execute() {
00101 if ( !is_null( $this->mCookieCheck ) ) {
00102 $this->onCookieRedirectCheck( $this->mCookieCheck );
00103 return;
00104 } else if( $this->mPosted ) {
00105 if( $this->mCreateaccount ) {
00106 return $this->addNewAccount();
00107 } else if ( $this->mCreateaccountMail ) {
00108 return $this->addNewAccountMailPassword();
00109 } else if ( $this->mMailmypassword ) {
00110 return $this->mailPassword();
00111 } else if ( ( 'submitlogin' == $this->mAction ) || $this->mLoginattempt ) {
00112 return $this->processLogin();
00113 }
00114 }
00115 $this->mainLoginForm( '' );
00116 }
00117
00121 function addNewAccountMailPassword() {
00122 global $wgOut;
00123
00124 if ('' == $this->mEmail) {
00125 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName ) ) );
00126 return;
00127 }
00128
00129 $u = $this->addNewaccountInternal();
00130
00131 if ($u == NULL) {
00132 return;
00133 }
00134
00135
00136 $u->setPassword( null );
00137 $u->saveSettings();
00138 $result = $this->mailPasswordInternal( $u, false, 'createaccount-title', 'createaccount-text' );
00139
00140 wfRunHooks( 'AddNewAccount', array( $u, true ) );
00141 $u->addNewUserLogEntry();
00142
00143 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
00144 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00145 $wgOut->setArticleRelated( false );
00146
00147 if( WikiError::isError( $result ) ) {
00148 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
00149 } else {
00150 $wgOut->addWikiMsg( 'accmailtext', $u->getName(), $u->getEmail() );
00151 $wgOut->returnToMain( false );
00152 }
00153 $u = 0;
00154 }
00155
00156
00160 function addNewAccount() {
00161 global $wgUser, $wgEmailAuthentication;
00162
00163 # Create the account and abort if there's a problem doing so
00164 $u = $this->addNewAccountInternal();
00165 if( $u == NULL )
00166 return;
00167
00168 # If we showed up language selection links, and one was in use, be
00169 # smart (and sensible) and save that language as the user's preference
00170 global $wgLoginLanguageSelector;
00171 if( $wgLoginLanguageSelector && $this->mLanguage )
00172 $u->setOption( 'language', $this->mLanguage );
00173
00174 # Send out an email authentication message if needed
00175 if( $wgEmailAuthentication && User::isValidEmailAddr( $u->getEmail() ) ) {
00176 global $wgOut;
00177 $error = $u->sendConfirmationMail();
00178 if( WikiError::isError( $error ) ) {
00179 $wgOut->addWikiMsg( 'confirmemail_sendfailed', $error->getMessage() );
00180 } else {
00181 $wgOut->addWikiMsg( 'confirmemail_oncreate' );
00182 }
00183 }
00184
00185 # Save settings (including confirmation token)
00186 $u->saveSettings();
00187
00188 # If not logged in, assume the new account as the current one and set
00189 # session cookies then show a "welcome" message or a "need cookies"
00190 # message as needed
00191 if( $wgUser->isAnon() ) {
00192 $wgUser = $u;
00193 $wgUser->setCookies();
00194 wfRunHooks( 'AddNewAccount', array( $wgUser ) );
00195 $wgUser->addNewUserLogEntry();
00196 if( $this->hasSessionCookie() ) {
00197 return $this->successfulCreation();
00198 } else {
00199 return $this->cookieRedirectCheck( 'new' );
00200 }
00201 } else {
00202 # Confirm that the account was created
00203 global $wgOut;
00204 $self = SpecialPage::getTitleFor( 'Userlogin' );
00205 $wgOut->setPageTitle( wfMsgHtml( 'accountcreated' ) );
00206 $wgOut->setArticleRelated( false );
00207 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00208 $wgOut->addHTML( wfMsgWikiHtml( 'accountcreatedtext', $u->getName() ) );
00209 $wgOut->returnToMain( false, $self );
00210 wfRunHooks( 'AddNewAccount', array( $u ) );
00211 $u->addNewUserLogEntry();
00212 return true;
00213 }
00214 }
00215
00219 function addNewAccountInternal() {
00220 global $wgUser, $wgOut;
00221 global $wgEnableSorbs, $wgProxyWhitelist;
00222 global $wgMemc, $wgAccountCreationThrottle;
00223 global $wgAuth, $wgMinimalPasswordLength;
00224 global $wgEmailConfirmToEdit;
00225
00226
00227 if( !$wgAuth->validDomain( $this->mDomain ) ) {
00228 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
00229 return false;
00230 }
00231
00232
00233
00234
00235
00236
00237 if( 'local' != $this->mDomain && '' != $this->mDomain ) {
00238 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName ) || !$wgAuth->authenticate( $this->mName, $this->mPassword ) ) ) {
00239 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
00240 return false;
00241 }
00242 }
00243
00244 if ( wfReadOnly() ) {
00245 $wgOut->readOnlyPage();
00246 return false;
00247 }
00248
00249 # Request forgery checks.
00250 if ( !self::getCreateaccountToken() ) {
00251 self::setCreateaccountToken();
00252 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
00253 return false;
00254 }
00255
00256 # The user didn't pass a createaccount token
00257 if ( !$this->mToken ) {
00258 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
00259 return false;
00260 }
00261
00262 # Validate the createaccount token
00263 if ( $this->mToken !== self::getCreateaccountToken() ) {
00264 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
00265 return false;
00266 }
00267
00268 # Check permissions
00269 if ( !$wgUser->isAllowed( 'createaccount' ) ) {
00270 $this->userNotPrivilegedMessage();
00271 return false;
00272 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
00273 $this->userBlockedMessage();
00274 return false;
00275 }
00276
00277 $ip = wfGetIP();
00278 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
00279 $wgUser->inSorbsBlacklist( $ip ) )
00280 {
00281 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
00282 return false;
00283 }
00284
00285 # Now create a dummy user ($u) and check if it is valid
00286 $name = trim( $this->mName );
00287 $u = User::newFromName( $name, 'creatable' );
00288 if ( is_null( $u ) ) {
00289 $this->mainLoginForm( wfMsg( 'noname' ) );
00290 return false;
00291 }
00292
00293 if ( 0 != $u->idForName() ) {
00294 $this->mainLoginForm( wfMsg( 'userexists' ) );
00295 return false;
00296 }
00297
00298 if ( 0 != strcmp( $this->mPassword, $this->mRetype ) ) {
00299 $this->mainLoginForm( wfMsg( 'badretype' ) );
00300 return false;
00301 }
00302
00303 # check for minimal password length
00304 if ( !$u->isValidPassword( $this->mPassword ) ) {
00305 if ( !$this->mCreateaccountMail ) {
00306 $this->mainLoginForm( wfMsgExt( 'passwordtooshort', array( 'parsemag' ), $wgMinimalPasswordLength ) );
00307 return false;
00308 } else {
00309 # do not force a password for account creation by email
00310 # set invalid password, it will be replaced later by a random generated password
00311 $this->mPassword = null;
00312 }
00313 }
00314
00315 # if you need a confirmed email address to edit, then obviously you
00316 # need an email address.
00317 if ( $wgEmailConfirmToEdit && empty( $this->mEmail ) ) {
00318 $this->mainLoginForm( wfMsg( 'noemailtitle' ) );
00319 return false;
00320 }
00321
00322 if( !empty( $this->mEmail ) && !User::isValidEmailAddr( $this->mEmail ) ) {
00323 $this->mainLoginForm( wfMsg( 'invalidemailaddress' ) );
00324 return false;
00325 }
00326
00327 # Set some additional data so the AbortNewAccount hook can be used for
00328 # more than just username validation
00329 $u->setEmail( $this->mEmail );
00330 $u->setRealName( $this->mRealName );
00331
00332 $abortError = '';
00333 if( !wfRunHooks( 'AbortNewAccount', array( $u, &$abortError ) ) ) {
00334
00335 wfDebug( "LoginForm::addNewAccountInternal: a hook blocked creation\n" );
00336 $this->mainLoginForm( $abortError );
00337 return false;
00338 }
00339
00340 if ( $wgAccountCreationThrottle && $wgUser->isPingLimitable() ) {
00341 $key = wfMemcKey( 'acctcreate', 'ip', $ip );
00342 $value = $wgMemc->get( $key );
00343 if ( !$value ) {
00344 $wgMemc->set( $key, 0, 86400 );
00345 }
00346 if ( $value >= $wgAccountCreationThrottle ) {
00347 $this->throttleHit( $wgAccountCreationThrottle );
00348 return false;
00349 }
00350 $wgMemc->incr( $key );
00351 }
00352
00353 if( !$wgAuth->addUser( $u, $this->mPassword, $this->mEmail, $this->mRealName ) ) {
00354 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
00355 return false;
00356 }
00357
00358 self::clearCreateaccountToken();
00359 return $this->initUser( $u, false );
00360 }
00361
00371 function initUser( $u, $autocreate ) {
00372 global $wgAuth;
00373
00374 $u->addToDatabase();
00375
00376 if ( $wgAuth->allowPasswordChange() ) {
00377 $u->setPassword( $this->mPassword );
00378 }
00379
00380 $u->setEmail( $this->mEmail );
00381 $u->setRealName( $this->mRealName );
00382 $u->setToken();
00383
00384 $wgAuth->initUser( $u, $autocreate );
00385
00386 $u->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
00387 $u->saveSettings();
00388
00389 # Update user count
00390 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
00391 $ssUpdate->doUpdate();
00392
00393 return $u;
00394 }
00395
00405 function authenticateUserData() {
00406 global $wgUser, $wgAuth;
00407 if ( '' == $this->mName ) {
00408 return self::NO_NAME;
00409 }
00410
00411
00412
00413
00414
00415
00416
00417 if ( !self::getLoginToken() ) {
00418 self::setLoginToken();
00419 return self::NEED_TOKEN;
00420 }
00421
00422 if ( !$this->mToken ) {
00423 return self::NEED_TOKEN;
00424 }
00425
00426 global $wgPasswordAttemptThrottle;
00427
00428 $throttleCount=0;
00429 if ( is_array($wgPasswordAttemptThrottle) ) {
00430 $throttleKey = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mName ) );
00431 $count = $wgPasswordAttemptThrottle['count'];
00432 $period = $wgPasswordAttemptThrottle['seconds'];
00433
00434 global $wgMemc;
00435 $throttleCount = $wgMemc->get($throttleKey);
00436 if ( !$throttleCount ) {
00437 $wgMemc->add( $throttleKey, 1, $period );
00438 } else if ( $throttleCount < $count ) {
00439 $wgMemc->incr($throttleKey);
00440 } else if ( $throttleCount >= $count ) {
00441 return self::THROTTLED;
00442 }
00443 }
00444
00445
00446 if ( $this->mToken !== self::getLoginToken() ) {
00447 return self::WRONG_TOKEN;
00448 }
00449
00450
00451
00452
00453
00454
00455
00456 if ( $wgUser->getName() === $this->mName ) {
00457 wfDebug( __METHOD__.": already logged in as {$this->mName}\n" );
00458 return self::SUCCESS;
00459 }
00460 $u = User::newFromName( $this->mName );
00461 if( is_null( $u ) || !User::isUsableName( $u->getName() ) ) {
00462 return self::ILLEGAL;
00463 }
00464
00465 $isAutoCreated = false;
00466 if ( 0 == $u->getID() ) {
00467 $status = $this->attemptAutoCreate( $u );
00468 if ( $status !== self::SUCCESS ) {
00469 return $status;
00470 } else {
00471 $isAutoCreated = true;
00472 }
00473 } else {
00474 $u->load();
00475 }
00476
00477
00478 $abort = self::ABORTED;
00479 if( !wfRunHooks( 'AbortLogin', array( $u, $this->mPassword, &$abort ) ) ) {
00480 return $abort;
00481 }
00482
00483 if (!$u->checkPassword( $this->mPassword )) {
00484 if( $u->checkTemporaryPassword( $this->mPassword ) ) {
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 if( !$u->isEmailConfirmed() ) {
00502 $u->confirmEmail();
00503 $u->saveSettings();
00504 }
00505
00506
00507
00508
00509 $retval = self::RESET_PASS;
00510 } else {
00511 $retval = '' == $this->mPassword ? self::EMPTY_PASS : self::WRONG_PASS;
00512 }
00513 } else {
00514 $wgAuth->updateUser( $u );
00515 $wgUser = $u;
00516
00517
00518 if($throttleCount) {
00519 $wgMemc->delete($throttleKey);
00520 }
00521
00522 if ( $isAutoCreated ) {
00523
00524 wfRunHooks( 'AuthPluginAutoCreate', array( $wgUser ) );
00525 }
00526
00527 $retval = self::SUCCESS;
00528 }
00529 wfRunHooks( 'LoginAuthenticateAudit', array( $u, $this->mPassword, $retval ) );
00530 return $retval;
00531 }
00532
00538 function attemptAutoCreate( $user ) {
00539 global $wgAuth, $wgUser;
00545 if ( !$wgAuth->autoCreate() ) {
00546 return self::NOT_EXISTS;
00547 }
00548 if ( !$wgAuth->userExists( $user->getName() ) ) {
00549 wfDebug( __METHOD__.": user does not exist\n" );
00550 return self::NOT_EXISTS;
00551 }
00552 if ( !$wgAuth->authenticate( $user->getName(), $this->mPassword ) ) {
00553 wfDebug( __METHOD__.": \$wgAuth->authenticate() returned false, aborting\n" );
00554 return self::WRONG_PLUGIN_PASS;
00555 }
00556 if ( $wgUser->isBlockedFromCreateAccount() ) {
00557 wfDebug( __METHOD__.": user is blocked from account creation\n" );
00558 return self::CREATE_BLOCKED;
00559 }
00560
00561 wfDebug( __METHOD__.": creating account\n" );
00562 $user = $this->initUser( $user, true );
00563 return self::SUCCESS;
00564 }
00565
00566 function processLogin() {
00567 global $wgUser, $wgAuth;
00568
00569 switch ($this->authenticateUserData())
00570 {
00571 case self::SUCCESS:
00572 # We've verified now, update the real record
00573 if( (bool)$this->mRemember != (bool)$wgUser->getOption( 'rememberpassword' ) ) {
00574 $wgUser->setOption( 'rememberpassword', $this->mRemember ? 1 : 0 );
00575 $wgUser->saveSettings();
00576 } else {
00577 $wgUser->invalidateCache();
00578 }
00579 $wgUser->setCookies();
00580 self::clearLoginToken();
00581
00582
00583 $key = wfMemcKey( 'password-throttle', wfGetIP(), md5( $this->mName ) );
00584 global $wgMemc;
00585 $wgMemc->delete( $key );
00586
00587 if( $this->hasSessionCookie() || $this->mSkipCookieCheck ) {
00588
00589
00590
00591 global $wgLang, $wgRequest;
00592 $code = $wgRequest->getVal( 'uselang', $wgUser->getOption( 'language' ) );
00593 $wgLang = Language::factory( $code );
00594 return $this->successfulLogin();
00595 } else {
00596 return $this->cookieRedirectCheck( 'login' );
00597 }
00598 break;
00599
00600 case self::NEED_TOKEN:
00601 case self::WRONG_TOKEN:
00602 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
00603 break;
00604 case self::NO_NAME:
00605 case self::ILLEGAL:
00606 $this->mainLoginForm( wfMsg( 'noname' ) );
00607 break;
00608 case self::WRONG_PLUGIN_PASS:
00609 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
00610 break;
00611 case self::NOT_EXISTS:
00612 if( $wgUser->isAllowed( 'createaccount' ) ){
00613 $this->mainLoginForm( wfMsgWikiHtml( 'nosuchuser', htmlspecialchars( $this->mName ) ) );
00614 } else {
00615 $this->mainLoginForm( wfMsg( 'nosuchusershort', htmlspecialchars( $this->mName ) ) );
00616 }
00617 break;
00618 case self::WRONG_PASS:
00619 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
00620 break;
00621 case self::EMPTY_PASS:
00622 $this->mainLoginForm( wfMsg( 'wrongpasswordempty' ) );
00623 break;
00624 case self::RESET_PASS:
00625 $this->resetLoginForm( wfMsg( 'resetpass_announce' ) );
00626 break;
00627 case self::CREATE_BLOCKED:
00628 $this->userBlockedMessage();
00629 break;
00630 case self::THROTTLED:
00631 $this->mainLoginForm( wfMsg( 'login-throttled' ) );
00632 break;
00633 default:
00634 throw new MWException( "Unhandled case value" );
00635 }
00636 }
00637
00638 function resetLoginForm( $error ) {
00639 global $wgOut;
00640 $wgOut->addHTML( Xml::element('p', array( 'class' => 'error' ), $error ) );
00641 $reset = new SpecialResetpass();
00642 $reset->execute( null );
00643 }
00644
00648 function mailPassword() {
00649 global $wgUser, $wgOut, $wgAuth;
00650
00651 if ( wfReadOnly() ) {
00652 $wgOut->readOnlyPage();
00653 return false;
00654 }
00655
00656 if( !$wgAuth->allowPasswordChange() ) {
00657 $this->mainLoginForm( wfMsg( 'resetpass_forbidden' ) );
00658 return;
00659 }
00660
00661 # Check against blocked IPs so blocked users can't flood admins
00662 # with password resets
00663 if( $wgUser->isBlocked() ) {
00664 $this->mainLoginForm( wfMsg( 'blocked-mailpassword' ) );
00665 return;
00666 }
00667
00668 # If the user doesn't have a login token yet, set one.
00669 if ( !self::getLoginToken() ) {
00670 self::setLoginToken();
00671 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
00672 return;
00673 }
00674
00675 # If the user didn't pass a login token, tell them we need one
00676 if ( !$this->mToken ) {
00677 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
00678 return;
00679 }
00680
00681 # Check against the rate limiter
00682 if( $wgUser->pingLimiter( 'mailpassword' ) ) {
00683 $wgOut->rateLimited();
00684 return;
00685 }
00686
00687 if ( '' == $this->mName ) {
00688 $this->mainLoginForm( wfMsg( 'noname' ) );
00689 return;
00690 }
00691 $u = User::newFromName( $this->mName );
00692 if( is_null( $u ) ) {
00693 $this->mainLoginForm( wfMsg( 'noname' ) );
00694 return;
00695 }
00696 if ( 0 == $u->getID() ) {
00697 $this->mainLoginForm( wfMsgWikiHtml( 'nosuchuser', htmlspecialchars( $u->getName() ) ) );
00698 return;
00699 }
00700
00701 # Validate the login token
00702 if ( $this->mToken !== self::getLoginToken() ) {
00703 $this->mainLoginForm( wfMsg( 'sessionfailure' ) );
00704 return;
00705 }
00706
00707 # Check against password throttle
00708 if ( $u->isPasswordReminderThrottled() ) {
00709 global $wgPasswordReminderResendTime;
00710 # Round the time in hours to 3 d.p., in case someone is specifying
00711 # minutes or seconds.
00712 $this->mainLoginForm( wfMsgExt( 'throttled-mailpassword', array( 'parsemag' ),
00713 round( $wgPasswordReminderResendTime, 3 ) ) );
00714 return;
00715 }
00716
00717 $result = $this->mailPasswordInternal( $u, true, 'passwordremindertitle', 'passwordremindertext' );
00718 if( WikiError::isError( $result ) ) {
00719 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
00720 } else {
00721 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
00722 self::clearLoginToken();
00723 }
00724 }
00725
00726
00735 function mailPasswordInternal( $u, $throttle = true, $emailTitle = 'passwordremindertitle', $emailText = 'passwordremindertext' ) {
00736 global $wgServer, $wgScript, $wgUser, $wgNewPasswordExpiry;
00737
00738 if ( '' == $u->getEmail() ) {
00739 return new WikiError( wfMsg( 'noemail', $u->getName() ) );
00740 }
00741 $ip = wfGetIP();
00742 if( !$ip ) {
00743 return new WikiError( wfMsg( 'badipaddress' ) );
00744 }
00745
00746 wfRunHooks( 'User::mailPasswordInternal', array(&$wgUser, &$ip, &$u) );
00747
00748 $np = $u->randomPassword();
00749 $u->setNewpassword( $np, $throttle );
00750 $u->saveSettings();
00751
00752 $m = wfMsgExt( $emailText, array( 'parsemag' ), $ip, $u->getName(), $np,
00753 $wgServer . $wgScript, round( $wgNewPasswordExpiry / 86400 ) );
00754 $result = $u->sendMail( wfMsg( $emailTitle ), $m );
00755
00756 return $result;
00757 }
00758
00759
00770 function successfulLogin() {
00771 global $wgUser, $wgOut;
00772
00773 # Run any hooks; display injected HTML if any, else redirect
00774 $injected_html = '';
00775 wfRunHooks('UserLoginComplete', array(&$wgUser, &$injected_html));
00776
00777 if( $injected_html !== '' ) {
00778 $this->displaySuccessfulLogin( 'loginsuccess', $injected_html );
00779 } else {
00780 $titleObj = Title::newFromText( $this->mReturnTo );
00781 if ( !$titleObj instanceof Title ) {
00782 $titleObj = Title::newMainPage();
00783 }
00784
00785 $wgOut->redirect( $titleObj->getFullURL() );
00786 }
00787 }
00788
00795 function successfulCreation() {
00796 global $wgUser, $wgOut;
00797
00798 # Run any hooks; display injected HTML
00799 $injected_html = '';
00800 wfRunHooks('UserLoginComplete', array(&$wgUser, &$injected_html));
00801
00802 $this->displaySuccessfulLogin( 'welcomecreation', $injected_html );
00803 }
00804
00808 private function displaySuccessfulLogin( $msgname, $injected_html ) {
00809 global $wgOut, $wgUser;
00810
00811 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
00812 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00813 $wgOut->setArticleRelated( false );
00814 $wgOut->addWikiMsg( $msgname, $wgUser->getName() );
00815 $wgOut->addHTML( $injected_html );
00816
00817 if ( !empty( $this->mReturnTo ) ) {
00818 $wgOut->returnToMain( null, $this->mReturnTo );
00819 } else {
00820 $wgOut->returnToMain( null );
00821 }
00822 }
00823
00825 function userNotPrivilegedMessage($errors) {
00826 global $wgOut;
00827
00828 $wgOut->setPageTitle( wfMsg( 'permissionserrors' ) );
00829 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00830 $wgOut->setArticleRelated( false );
00831
00832 $wgOut->addWikitext( $wgOut->formatPermissionsErrorMessage( $errors, 'createaccount' ) );
00833
00834
00835 $wgOut->addWikiMsg( 'cantcreateaccount-nonblock-text' );
00836
00837 $wgOut->returnToMain( false );
00838 }
00839
00841 function userBlockedMessage() {
00842 global $wgOut, $wgUser;
00843
00844 # Let's be nice about this, it's likely that this feature will be used
00845 # for blocking large numbers of innocent people, e.g. range blocks on
00846 # schools. Don't blame it on the user. There's a small chance that it
00847 # really is the user's fault, i.e. the username is blocked and they
00848 # haven't bothered to log out before trying to create an account to
00849 # evade it, but we'll leave that to their guilty conscience to figure
00850 # out.
00851
00852 $wgOut->setPageTitle( wfMsg( 'cantcreateaccounttitle' ) );
00853 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00854 $wgOut->setArticleRelated( false );
00855
00856 $ip = wfGetIP();
00857 $blocker = User::whoIs( $wgUser->mBlock->mBy );
00858 $block_reason = $wgUser->mBlock->mReason;
00859
00860 if ( strval( $block_reason ) === '' ) {
00861 $block_reason = wfMsg( 'blockednoreason' );
00862 }
00863 $wgOut->addWikiMsg( 'cantcreateaccount-text', $ip, $block_reason, $blocker );
00864 $wgOut->returnToMain( false );
00865 }
00866
00870 function mainLoginForm( $msg, $msgtype = 'error' ) {
00871 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
00872 global $wgCookiePrefix, $wgLoginLanguageSelector;
00873 global $wgAuth, $wgEmailConfirmToEdit, $wgCookieExpiration;
00874
00875 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
00876
00877 if ( $this->mType == 'signup' ) {
00878
00879
00880
00881 if ( wfReadOnly() ) {
00882 $wgOut->readOnlyPage();
00883 return;
00884 } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
00885 $this->userBlockedMessage();
00886 return;
00887 } elseif ( count( $permErrors = $titleObj->getUserPermissionsErrors( 'createaccount', $wgUser, true ) )>0 ) {
00888 $wgOut->showPermissionsErrorPage( $permErrors, 'createaccount' );
00889 return;
00890 }
00891 }
00892
00893 if ( '' == $this->mName ) {
00894 if ( $wgUser->isLoggedIn() ) {
00895 $this->mName = $wgUser->getName();
00896 } else {
00897 $this->mName = isset( $_COOKIE[$wgCookiePrefix.'UserName'] ) ? $_COOKIE[$wgCookiePrefix.'UserName'] : null;
00898 }
00899 }
00900
00901 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
00902
00903 if ( $this->mType == 'signup' ) {
00904 $template = new UsercreateTemplate();
00905 $q = 'action=submitlogin&type=signup';
00906 $linkq = 'type=login';
00907 $linkmsg = 'gotaccount';
00908 } else {
00909 $template = new UserloginTemplate();
00910 $q = 'action=submitlogin&type=login';
00911 $linkq = 'type=signup';
00912 $linkmsg = 'nologin';
00913 }
00914
00915 if ( !empty( $this->mReturnTo ) ) {
00916 $returnto = '&returnto=' . wfUrlencode( $this->mReturnTo );
00917 $q .= $returnto;
00918 $linkq .= $returnto;
00919 }
00920
00921 # Pass any language selection on to the mode switch link
00922 if( $wgLoginLanguageSelector && $this->mLanguage )
00923 $linkq .= '&uselang=' . $this->mLanguage;
00924
00925 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
00926 $link .= wfMsgHtml( $linkmsg . 'link' ); # Calling either 'gotaccountlink' or 'nologinlink'
00927 $link .= '</a>';
00928
00929 # Don't show a "create account" link if the user can't
00930 if( $this->showCreateOrLoginLink( $wgUser ) )
00931 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
00932 else
00933 $template->set( 'link', '' );
00934
00935 $template->set( 'header', '' );
00936 $template->set( 'name', $this->mName );
00937 $template->set( 'password', $this->mPassword );
00938 $template->set( 'retype', $this->mRetype );
00939 $template->set( 'email', $this->mEmail );
00940 $template->set( 'realname', $this->mRealName );
00941 $template->set( 'domain', $this->mDomain );
00942
00943 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
00944 $template->set( 'message', $msg );
00945 $template->set( 'messagetype', $msgtype );
00946 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
00947 $template->set( 'userealname', $wgAllowRealName );
00948 $template->set( 'useemail', $wgEnableEmail );
00949 $template->set( 'emailrequired', $wgEmailConfirmToEdit );
00950 $template->set( 'canreset', $wgAuth->allowPasswordChange() );
00951 $template->set( 'canremember', ( $wgCookieExpiration > 0 ) );
00952 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember );
00953
00954 if ( $this->mType == 'signup' ) {
00955 if ( !self::getCreateaccountToken() ) {
00956 self::setCreateaccountToken();
00957 }
00958 $template->set( 'token', self::getCreateaccountToken() );
00959 } else {
00960 if ( !self::getLoginToken() ) {
00961 self::setLoginToken();
00962 }
00963 $template->set( 'token', self::getLoginToken() );
00964 }
00965
00966 # Prepare language selection links as needed
00967 if( $wgLoginLanguageSelector ) {
00968 $template->set( 'languages', $this->makeLanguageSelector() );
00969 if( $this->mLanguage )
00970 $template->set( 'uselang', $this->mLanguage );
00971 }
00972
00973
00974 $wgAuth->modifyUITemplate( $template );
00975 if ( $this->mType == 'signup' ) {
00976 wfRunHooks( 'UserCreateForm', array( &$template ) );
00977 } else {
00978 wfRunHooks( 'UserLoginForm', array( &$template ) );
00979 }
00980
00981 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
00982 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00983 $wgOut->setArticleRelated( false );
00984 $wgOut->disallowUserJs();
00985 $wgOut->addTemplate( $template );
00986 }
00987
00991 function showCreateOrLoginLink( &$user ) {
00992 if( $this->mType == 'signup' ) {
00993 return( true );
00994 } elseif( $user->isAllowed( 'createaccount' ) ) {
00995 return( true );
00996 } else {
00997 return( false );
00998 }
00999 }
01000
01010 function hasSessionCookie() {
01011 global $wgDisableCookieCheck, $wgRequest;
01012 return $wgDisableCookieCheck ? true : $wgRequest->checkSessionCookie();
01013 }
01014
01018 public static function getLoginToken() {
01019 global $wgRequest;
01020 return $wgRequest->getSessionData( 'wsLoginToken' );
01021 }
01022
01026 public static function setLoginToken() {
01027 global $wgRequest;
01028
01029
01030 $wgRequest->setSessionData( 'wsLoginToken', User::generateToken() );
01031 }
01032
01036 public static function clearLoginToken() {
01037 global $wgRequest;
01038 $wgRequest->setSessionData( 'wsLoginToken', null );
01039 }
01040
01044 public static function getCreateaccountToken() {
01045 global $wgRequest;
01046 return $wgRequest->getSessionData( 'wsCreateaccountToken' );
01047 }
01048
01052 public static function setCreateaccountToken() {
01053 global $wgRequest;
01054 $wgRequest->setSessionData( 'wsCreateaccountToken', User::generateToken() );
01055 }
01056
01060 public static function clearCreateaccountToken() {
01061 global $wgRequest;
01062 $wgRequest->setSessionData( 'wsCreateaccountToken', null );
01063 }
01064
01068 function cookieRedirectCheck( $type ) {
01069 global $wgOut;
01070
01071 $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
01072 $query = array( 'wpCookieCheck' => $type );
01073 if ( $this->mReturnTo ) $query['returnto'] = $this->mReturnTo;
01074 $check = $titleObj->getFullURL( $query );
01075
01076 return $wgOut->redirect( $check );
01077 }
01078
01082 function onCookieRedirectCheck( $type ) {
01083 global $wgUser;
01084
01085 if ( !$this->hasSessionCookie() ) {
01086 if ( $type == 'new' ) {
01087 return $this->mainLoginForm( wfMsgExt( 'nocookiesnew', array( 'parseinline' ) ) );
01088 } else if ( $type == 'login' ) {
01089 return $this->mainLoginForm( wfMsgExt( 'nocookieslogin', array( 'parseinline' ) ) );
01090 } else {
01091 # shouldn't happen
01092 return $this->mainLoginForm( wfMsg( 'error' ) );
01093 }
01094 } else {
01095 return $this->successfulLogin();
01096 }
01097 }
01098
01102 function throttleHit( $limit ) {
01103 $this->mainLoginForm( wfMsgExt( 'acct_creation_throttle_hit', array( 'parseinline' ), $limit ) );
01104 }
01105
01112 function makeLanguageSelector() {
01113 global $wgLang;
01114
01115 $msg = wfMsgForContent( 'loginlanguagelinks' );
01116 if( $msg != '' && !wfEmptyMsg( 'loginlanguagelinks', $msg ) ) {
01117 $langs = explode( "\n", $msg );
01118 $links = array();
01119 foreach( $langs as $lang ) {
01120 $lang = trim( $lang, '* ' );
01121 $parts = explode( '|', $lang );
01122 if (count($parts) >= 2) {
01123 $links[] = $this->makeLanguageSelectorLink( $parts[0], $parts[1] );
01124 }
01125 }
01126 return count( $links ) > 0 ? wfMsgHtml( 'loginlanguagelabel', $wgLang->pipeList( $links ) ) : '';
01127 } else {
01128 return '';
01129 }
01130 }
01131
01139 function makeLanguageSelectorLink( $text, $lang ) {
01140 global $wgUser;
01141 $self = SpecialPage::getTitleFor( 'Userlogin' );
01142 $attr[] = 'uselang=' . $lang;
01143 if( $this->mType == 'signup' )
01144 $attr[] = 'type=signup';
01145 if( $this->mReturnTo )
01146 $attr[] = 'returnto=' . $this->mReturnTo;
01147 $skin = $wgUser->getSkin();
01148 return $skin->makeKnownLinkObj( $self, htmlspecialchars( $text ), implode( '&', $attr ) );
01149 }
01150 }