5bfa70601fef4d6e0819875ddc337a977ab68ada
5 * @subpackage SpecialPage
11 function wfSpecialUserlogin() {
12 global $wgCommandLineMode;
14 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('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->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' )
50 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
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' );
61 if( $wgAllowRealName ) {
62 $this->mRealName
= $request->getText( 'wpRealName' );
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
= '';
79 if ( !is_null( $this->mCookieCheck
) ) {
80 $this->onCookieRedirectCheck( $this->mCookieCheck
);
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( '' );
99 function addNewAccountMailPassword() {
102 if ('' == $this->mEmail
) {
103 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
107 $u = $this->addNewaccountInternal();
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() ) );
123 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
124 $wgOut->returnToMain( false );
133 function addNewAccount() {
134 global $wgUser, $wgOut;
136 $u = $this->addNewAccountInternal();
143 $wgUser->setCookies();
145 $wgUser->saveSettings();
146 if( $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
147 $wgUser->sendConfirmationMail();
150 if( $this->hasSessionCookie() ) {
151 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
153 return $this->cookieRedirectCheck( 'new' );
160 function addNewAccountInternal() {
161 global $wgUser, $wgOut;
162 global $wgMaxNameChars;
163 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
164 global $wgMinimalPasswordLength;
167 // If the user passes an invalid domain, something is fishy
168 if( !$wgAuth->validDomain( $this->mDomain
) ) {
169 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
173 // If we are not allowing users to login locally, we should
174 // be checking to see if the user is actually able to
175 // authenticate to the authentication server before they
176 // create an account (otherwise, they can create a local account
177 // and login as any domain user). We only need to check this for
178 // domains that aren't local.
179 if( 'local' != $this->mDomain
&& '' != $this->mDomain
) {
180 if( !$wgAuth->canCreateAccounts() && ( !$wgAuth->userExists( $this->mName
) ||
!$wgAuth->authenticate( $this->mName
, $this->mPassword
) ) ) {
181 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
188 if (!$wgUser->isAllowedToCreateAccount()) {
189 $this->userNotPrivilegedMessage();
193 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
194 $this->mainLoginForm( wfMsg( 'badretype' ) );
198 $name = trim( $this->mName
);
199 $u = User
::newFromName( $name );
200 if ( is_null( $u ) ||
202 $wgUser->isIP( $name ) ||
203 (strpos( $name, '/' ) !== false) ||
204 (strlen( $name ) > $wgMaxNameChars) ||
205 ucFirst($name) != $u->getName() )
207 $this->mainLoginForm( wfMsg( 'noname' ) );
210 if ( wfReadOnly() ) {
211 $wgOut->readOnlyPage();
215 if ( 0 != $u->idForName() ) {
216 $this->mainLoginForm( wfMsg( 'userexists' ) );
220 if ( strlen( $this->mPassword
) < $wgMinimalPasswordLength ) {
221 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
225 if ( $wgAccountCreationThrottle ) {
226 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
227 $value = $wgMemc->incr( $key );
229 $wgMemc->set( $key, 1, 86400 );
231 if ( $value > $wgAccountCreationThrottle ) {
232 $this->throttleHit( $wgAccountCreationThrottle );
237 if( !$wgAuth->addUser( $u, $this->mPassword
) ) {
238 $this->mainLoginForm( wfMsg( 'externaldberror' ) );
242 return $this->initUser( $u );
246 * Actually add a user to the database.
247 * Give it a User object that has been initialised with a name.
253 function &initUser( &$u ) {
255 $u->setPassword( $this->mPassword
);
256 $u->setEmail( $this->mEmail
);
257 $u->setRealName( $this->mRealName
);
261 $wgAuth->initUser( $u );
263 if ( $this->mRemember
) { $r = 1; }
265 $u->setOption( 'rememberpassword', $r );
273 function processLogin() {
277 if ( '' == $this->mName
) {
278 $this->mainLoginForm( wfMsg( 'noname' ) );
281 $u = User
::newFromName( $this->mName
);
282 if( is_null( $u ) ) {
283 $this->mainLoginForm( wfMsg( 'noname' ) );
286 if ( 0 == $u->getID() ) {
289 * If the external authentication plugin allows it,
290 * automatically create a new account for users that
291 * are externally defined but have not yet logged in.
293 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
294 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
295 $u =& $this->initUser( $u );
297 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
301 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
305 $u->loadFromDatabase();
308 if (!$u->checkPassword( $this->mPassword
)) {
309 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
313 # We've verified now, update the real record
315 if ( $this->mRemember
) {
320 $u->setOption( 'rememberpassword', $r );
322 $wgAuth->updateUser( $u );
325 $wgUser->setCookies();
327 $wgUser->saveSettings();
329 if( $this->hasSessionCookie() ) {
330 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
332 return $this->cookieRedirectCheck( 'login' );
339 function mailPassword() {
340 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
341 global $wgCookiePath, $wgCookieDomain, $wgDBname;
343 if ( '' == $this->mName
) {
344 $this->mainLoginForm( wfMsg( 'noname' ) );
347 $u = User
::newFromName( $this->mName
);
348 if( is_null( $u ) ) {
349 $this->mainLoginForm( wfMsg( 'noname' ) );
352 if ( 0 == $u->getID() ) {
353 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
357 $u->loadFromDatabase();
359 $result = $this->mailPasswordInternal( $u );
360 if( WikiError
::isError( $result ) ) {
361 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
363 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
369 * @return mixed true on success, WikiError on failure
372 function mailPasswordInternal( $u ) {
373 global $wgPasswordSender, $wgDBname, $wgIP;
374 global $wgCookiePath, $wgCookieDomain;
376 if ( '' == $u->getEmail() ) {
377 return wfMsg( 'noemail', $u->getName() );
380 $np = $u->randomPassword();
381 $u->setNewpassword( $np );
383 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
388 if ( '' == $ip ) { $ip = '(Unknown)'; }
390 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
391 $result = $u->sendMail( wfMsg( 'passwordremindermailsubject' ), $m );
398 * @param string $msg Message that will be shown on success.
401 function successfulLogin( $msg ) {
405 # Run any hooks; ignore results
407 wfRunHooks('UserLoginComplete', array(&$wgUser));
409 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
410 $wgOut->setRobotpolicy( 'noindex,nofollow' );
411 $wgOut->setArticleRelated( false );
412 $wgOut->addWikiText( $msg );
413 $wgOut->returnToMain();
417 function userNotPrivilegedMessage() {
420 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
421 $wgOut->setRobotpolicy( 'noindex,nofollow' );
422 $wgOut->setArticleRelated( false );
424 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
426 $wgOut->returnToMain( false );
432 function mainLoginForm( $err ) {
433 global $wgUser, $wgOut, $wgLang;
434 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
437 if ( '' == $this->mName
) {
438 if ( $wgUser->isLoggedIn() ) {
439 $this->mName
= $wgUser->getName();
441 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
445 $q = 'action=submitlogin';
446 if ( !empty( $this->mReturnto
) ) {
447 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
449 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
451 require_once( 'templates/Userlogin.php' );
452 $template =& new UserloginTemplate();
454 $template->set( 'name', $this->mName
);
455 $template->set( 'password', $this->mPassword
);
456 $template->set( 'retype', $this->mRetype
);
457 $template->set( 'email', $this->mEmail
);
458 $template->set( 'realname', $this->mRealName
);
459 $template->set( 'domain', $this->mDomain
);
461 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
462 $template->set( 'error', $err );
463 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
464 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
465 $template->set( 'userealname', $wgAllowRealName );
466 $template->set( 'useemail', $wgEnableEmail );
467 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
468 $wgAuth->modifyUITemplate( $template );
470 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
471 $wgOut->setRobotpolicy( 'noindex,nofollow' );
472 $wgOut->setArticleRelated( false );
473 $wgOut->addTemplate( $template );
479 function hasSessionCookie() {
480 global $wgDisableCookieCheck;
481 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
487 function cookieRedirectCheck( $type ) {
488 global $wgOut, $wgLang;
490 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
491 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
493 return $wgOut->redirect( $check );
499 function onCookieRedirectCheck( $type ) {
502 if ( !$this->hasSessionCookie() ) {
503 if ( $type == 'new' ) {
504 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
505 } else if ( $type == 'login' ) {
506 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
509 return $this->mainLoginForm( wfMsg( 'error' ) );
512 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
519 function throttleHit( $limit ) {
522 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );