719be812775ea6a25232b9b0713a54dfda7c6681
5 * @subpackage SpecialPage
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[session_name()] ) ) {
18 $form = new LoginForm( $wgRequest );
25 * @subpackage SpecialPage
28 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
29 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
30 var $mLoginattempt, $mRemember, $mEmail, $mDomain;
34 * @param webrequest $request A webrequest object passed by reference
36 function LoginForm( &$request ) {
37 global $wgLang, $wgAllowRealName, $wgEnableEmail;
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' )
51 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
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' );
62 if( $wgAllowRealName ) {
63 $this->mRealName
= $request->getText( 'wpRealName' );
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
= '';
80 if ( !is_null( $this->mCookieCheck
) ) {
81 $this->onCookieRedirectCheck( $this->mCookieCheck
);
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( '' );
100 function addNewAccountMailPassword() {
103 if ('' == $this->mEmail
) {
104 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
108 $u = $this->addNewaccountInternal();
115 $result = $this->mailPasswordInternal($u);
117 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
118 $wgOut->setRobotpolicy( 'noindex,nofollow' );
119 $wgOut->setArticleRelated( false );
121 if( WikiError
::isError( $result ) ) {
122 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
124 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
125 $wgOut->returnToMain( false );
134 function addNewAccount() {
135 global $wgUser, $wgOut, $wgEmailAuthentication;
137 $u = $this->addNewAccountInternal();
144 $wgUser->setCookies();
146 $wgUser->saveSettings();
147 if( $wgEmailAuthentication && $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
148 $wgUser->sendConfirmationMail();
151 wfRunHooks( 'AddNewAccount' );
153 if( $this->hasSessionCookie() ) {
154 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
156 return $this->cookieRedirectCheck( 'new' );
163 function addNewAccountInternal() {
164 global $wgUser, $wgOut;
165 global $wgEnableSorbs, $wgProxyWhitelist;
166 global $wgMemc, $wgAccountCreationThrottle, $wgDBname;
167 global $wgAuth, $wgMinimalPasswordLength;
169 // If the user passes an invalid domain, something is fishy
170 if( !$wgAuth->validDomain( $this->mDomain
) ) {
171 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
175 // If we are not allowing users to login locally, we should
176 // be checking to see if the user is actually able to
177 // authenticate to the authentication server before they
178 // create an account (otherwise, they can create a local account
179 // and login as any domain user). We only need to check this for
180 // domains that aren't local.
181 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
182 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
183 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
188 if (!$wgUser->isAllowedToCreateAccount()) {
189 $this->userNotPrivilegedMessage();
194 if ( $wgEnableSorbs && !in_array( $ip, $wgProxyWhitelist ) &&
195 $wgUser->inSorbsBlacklist( $ip ) )
197 $this->mainLoginForm( wfMsg( 'sorbs_create_account_reason' ) . ' (' . htmlspecialchars( $ip ) . ')' );
202 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
203 $this->mainLoginForm( wfMsg( 'badretype' ) );
207 $name = trim( $this->mName
);
208 $u = User
::newFromName( $name );
209 if ( is_null( $u ) ) {
210 $this->mainLoginForm( wfMsg( 'noname' ) );
214 if ( wfReadOnly() ) {
215 $wgOut->readOnlyPage();
219 if ( 0 != $u->idForName() ) {
220 $this->mainLoginForm( wfMsg( 'userexists' ) );
224 if ( !$wgUser->isValidPassword( $this->mPassword
) ) {
225 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
229 if ( $wgAccountCreationThrottle ) {
230 $key = $wgDBname.':acctcreate:ip:'.$ip;
231 $value = $wgMemc->incr( $key );
233 $wgMemc->set( $key, 1, 86400 );
235 if ( $value > $wgAccountCreationThrottle ) {
236 $this->throttleHit( $wgAccountCreationThrottle );
241 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
242 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
247 $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
248 $ssUpdate->doUpdate();
250 return $this->initUser( $u );
254 * Actually add a user to the database.
255 * Give it a User object that has been initialised with a name.
261 function &initUser( &$u ) {
263 $u->setPassword( $this->mPassword
);
264 $u->setEmail( $this->mEmail
);
265 $u->setRealName( $this->mRealName
);
269 $wgAuth->initUser( $u );
271 $u->setOption( 'rememberpassword', $this->mRemember ?
1 : 0 );
279 function processLogin() {
283 if ( '' == $this->mName
) {
284 $this->mainLoginForm( wfMsg( 'noname' ) );
287 $u = User
::newFromName( $this->mName
);
288 if( is_null( $u ) ) {
289 $this->mainLoginForm( wfMsg( 'noname' ) );
292 if ( 0 == $u->getID() ) {
295 * If the external authentication plugin allows it,
296 * automatically create a new account for users that
297 * are externally defined but have not yet logged in.
299 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
300 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
301 $u =& $this->initUser( $u );
303 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
307 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
311 $u->loadFromDatabase();
314 if (!$u->checkPassword( $this->mPassword
)) {
315 $this->mainLoginForm( wfMsg( $this->mPassword
== '' ?
'wrongpasswordempty' : 'wrongpassword' ) );
319 # We've verified now, update the real record
321 if ( $this->mRemember
) {
326 $u->setOption( 'rememberpassword', $r );
328 $wgAuth->updateUser( $u );
331 $wgUser->setCookies();
333 $wgUser->saveSettings();
335 if( $this->hasSessionCookie() ) {
336 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
338 return $this->cookieRedirectCheck( 'login' );
345 function mailPassword() {
346 global $wgUser, $wgDBname;
348 if ( '' == $this->mName
) {
349 $this->mainLoginForm( wfMsg( 'noname' ) );
352 $u = User
::newFromName( $this->mName
);
353 if( is_null( $u ) ) {
354 $this->mainLoginForm( wfMsg( 'noname' ) );
357 if ( 0 == $u->getID() ) {
358 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
362 $u->loadFromDatabase();
364 $result = $this->mailPasswordInternal( $u );
365 if( WikiError
::isError( $result ) ) {
366 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
368 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ), 'success' );
374 * @return mixed true on success, WikiError on failure
377 function mailPasswordInternal( $u ) {
378 global $wgDBname, $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 );
392 if ( '' == $ip ) { $ip = '(Unknown)'; }
394 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
396 $result = $u->sendMail( wfMsg( 'passwordremindertitle' ), $m );
402 * @param string $msg Message that will be shown on success.
405 function successfulLogin( $msg ) {
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();
421 function userNotPrivilegedMessage() {
424 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
425 $wgOut->setRobotpolicy( 'noindex,nofollow' );
426 $wgOut->setArticleRelated( false );
428 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
430 $wgOut->returnToMain( false );
436 function mainLoginForm( $msg, $msgtype = 'error' ) {
437 global $wgUser, $wgOut, $wgLang;
438 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
441 if ( '' == $this->mName
) {
442 if ( $wgUser->isLoggedIn() ) {
443 $this->mName
= $wgUser->getName();
445 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
449 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
451 require_once( 'templates/Userlogin.php' );
453 if ( $this->mType
== 'signup' ) {
454 $template =& new UsercreateTemplate();
455 $q = 'action=submitlogin&type=signup';
456 $linkq = 'type=login';
457 $linkmsg = 'gotaccount';
459 $template =& new UserloginTemplate();
460 $q = 'action=submitlogin&type=login';
461 $linkq = 'type=signup';
462 $linkmsg = 'nologin';
465 if ( !empty( $this->mReturnto
) ) {
466 $returnto = '&returnto=' . wfUrlencode( $this->mReturnto
);
471 $link = '<a href="' . htmlspecialchars ( $titleObj->getLocalUrl( $linkq ) ) . '">';
472 $link .= wfMsgHtml( $linkmsg . 'link' );
475 $template->set( 'link', wfMsgHtml( $linkmsg, $link ) );
477 $template->set( 'name', $this->mName
);
478 $template->set( 'password', $this->mPassword
);
479 $template->set( 'retype', $this->mRetype
);
480 $template->set( 'email', $this->mEmail
);
481 $template->set( 'realname', $this->mRealName
);
482 $template->set( 'domain', $this->mDomain
);
484 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
485 $template->set( 'message', $msg );
486 $template->set( 'messagetype', $msgtype );
487 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
488 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
489 $template->set( 'userealname', $wgAllowRealName );
490 $template->set( 'useemail', $wgEnableEmail );
491 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
492 $wgAuth->modifyUITemplate( $template );
494 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
495 $wgOut->setRobotpolicy( 'noindex,nofollow' );
496 $wgOut->setArticleRelated( false );
497 $wgOut->addTemplate( $template );
503 function hasSessionCookie() {
504 global $wgDisableCookieCheck;
505 return ( $wgDisableCookieCheck ) ?
true : ( isset( $_COOKIE[session_name()] ) );
511 function cookieRedirectCheck( $type ) {
512 global $wgOut, $wgLang;
514 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
515 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
517 return $wgOut->redirect( $check );
523 function onCookieRedirectCheck( $type ) {
526 if ( !$this->hasSessionCookie() ) {
527 if ( $type == 'new' ) {
528 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
529 } else if ( $type == 'login' ) {
530 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
533 return $this->mainLoginForm( wfMsg( 'error' ) );
536 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
543 function throttleHit( $limit ) {
546 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );