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;
34 * @param webrequest $request A webrequest object passed by reference
36 function LoginForm( &$request ) {
37 global $wgLang, $wgAllowRealName, $wgEnableEmail;
39 $this->mName
= $request->getText( 'wpName' );
40 $this->mPassword
= $request->getText( 'wpPassword' );
41 $this->mRetype
= $request->getText( 'wpRetype' );
42 $this->mReturnto
= $request->getVal( 'returnto' );
43 $this->mCookieCheck
= $request->getVal( 'wpCookieCheck' );
44 $this->mPosted
= $request->wasPosted();
45 $this->mCreateaccount
= $request->getCheck( 'wpCreateaccount' );
46 $this->mCreateaccountMail
= $request->getCheck( 'wpCreateaccountMail' )
48 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
50 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
51 $this->mAction
= $request->getVal( 'action' );
52 $this->mRemember
= $request->getCheck( 'wpRemember' );
54 if( $wgEnableEmail ) {
55 $this->mEmail
= $request->getText( 'wpEmail' );
59 if( $wgAllowRealName ) {
60 $this->mRealName
= $request->getText( 'wpRealName' );
62 $this->mRealName
= '';
65 # When switching accounts, it sucks to get automatically logged out
66 if( $this->mReturnto
== $wgLang->specialPage( 'Userlogout' ) ) {
67 $this->mReturnto
= '';
72 if ( !is_null( $this->mCookieCheck
) ) {
73 $this->onCookieRedirectCheck( $this->mCookieCheck
);
75 } else if( $this->mPosted
) {
76 if( $this->mCreateaccount
) {
77 return $this->addNewAccount();
78 } else if ( $this->mCreateaccountMail
) {
79 return $this->addNewAccountMailPassword();
80 } else if ( $this->mMailmypassword
) {
81 return $this->mailPassword();
82 } else if ( ( 'submitlogin' == $this->mAction
) ||
$this->mLoginattempt
) {
83 return $this->processLogin();
86 $this->mainLoginForm( '' );
92 function addNewAccountMailPassword() {
95 if ('' == $this->mEmail
) {
96 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
100 $u = $this->addNewaccountInternal();
107 $result = $this->mailPasswordInternal($u);
109 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
110 $wgOut->setRobotpolicy( 'noindex,nofollow' );
111 $wgOut->setArticleRelated( false );
113 if( WikiError
::isError( $result ) ) {
114 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
116 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
117 $wgOut->returnToMain( false );
126 function addNewAccount() {
127 global $wgUser, $wgOut;
129 $u = $this->addNewAccountInternal();
136 $wgUser->setCookies();
138 $wgUser->saveSettings();
139 if( $wgUser->isValidEmailAddr( $wgUser->getEmail() ) ) {
140 $wgUser->sendConfirmationMail();
143 if( $this->hasSessionCookie() ) {
144 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
146 return $this->cookieRedirectCheck( 'new' );
153 function addNewAccountInternal() {
154 global $wgUser, $wgOut;
155 global $wgMaxNameChars;
156 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
157 global $wgMinimalPasswordLength;
159 if (!$wgUser->isAllowedToCreateAccount()) {
160 $this->userNotPrivilegedMessage();
164 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
165 $this->mainLoginForm( wfMsg( 'badretype' ) );
169 $name = trim( $this->mName
);
170 $u = User
::newFromName( $name );
171 if ( is_null( $u ) ||
173 $wgUser->isIP( $name ) ||
174 (strpos( $name, '/' ) !== false) ||
175 (strlen( $name ) > $wgMaxNameChars) ||
176 ucFirst($name) != $u->getName() )
178 $this->mainLoginForm( wfMsg( 'noname' ) );
181 if ( wfReadOnly() ) {
182 $wgOut->readOnlyPage();
186 if ( 0 != $u->idForName() ) {
187 $this->mainLoginForm( wfMsg( 'userexists' ) );
191 if ( strlen( $this->mPassword
) < $wgMinimalPasswordLength ) {
192 $this->mainLoginForm( wfMsg( 'passwordtooshort', $wgMinimalPasswordLength ) );
196 if ( $wgAccountCreationThrottle ) {
197 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
198 $value = $wgMemc->incr( $key );
200 $wgMemc->set( $key, 1, 86400 );
202 if ( $value > $wgAccountCreationThrottle ) {
203 $this->throttleHit( $wgAccountCreationThrottle );
208 return $this->initUser( $u );
212 * Actually add a user to the database.
213 * Give it a User object that has been initialised with a name.
219 function &initUser( &$u ) {
221 $u->setPassword( $this->mPassword
);
222 $u->setEmail( $this->mEmail
);
223 $u->setRealName( $this->mRealName
);
227 $wgAuth->initUser( $u );
229 if ( $this->mRemember
) { $r = 1; }
231 $u->setOption( 'rememberpassword', $r );
239 function processLogin() {
242 if ( '' == $this->mName
) {
243 $this->mainLoginForm( wfMsg( 'noname' ) );
246 $u = User
::newFromName( $this->mName
);
247 if( is_null( $u ) ) {
248 $this->mainLoginForm( wfMsg( 'noname' ) );
251 if ( 0 == $u->getID() ) {
254 * If the external authentication plugin allows it,
255 * automatically create a new account for users that
256 * are externally defined but have not yet logged in.
258 if ( $wgAuth->autoCreate() && $wgAuth->userExists( $u->getName() ) ) {
259 if ( $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
260 $u =& $this->initUser( $u );
262 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
266 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
270 $u->loadFromDatabase();
273 if (!$u->checkPassword( $this->mPassword
)) {
274 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
278 # We've verified now, update the real record
280 if ( $this->mRemember
) {
285 $u->setOption( 'rememberpassword', $r );
288 $wgUser->setCookies();
290 $wgUser->saveSettings();
292 if( $this->hasSessionCookie() ) {
293 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
295 return $this->cookieRedirectCheck( 'login' );
302 function mailPassword() {
303 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
304 global $wgCookiePath, $wgCookieDomain, $wgDBname;
306 if ( '' == $this->mName
) {
307 $this->mainLoginForm( wfMsg( 'noname' ) );
310 $u = User
::newFromName( $this->mName
);
311 if( is_null( $u ) ) {
312 $this->mainLoginForm( wfMsg( 'noname' ) );
315 if ( 0 == $u->getID() ) {
316 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
320 $u->loadFromDatabase();
322 $result = $this->mailPasswordInternal( $u );
323 if( WikiError
::isError( $result ) ) {
324 $this->mainLoginForm( wfMsg( 'mailerror', $result->getMessage() ) );
326 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
332 * @return mixed true on success, WikiError on failure
335 function mailPasswordInternal( $u ) {
336 global $wgPasswordSender, $wgDBname, $wgIP;
337 global $wgCookiePath, $wgCookieDomain;
339 if ( '' == $u->getEmail() ) {
340 return wfMsg( 'noemail', $u->getName() );
343 $np = $u->randomPassword();
344 $u->setNewpassword( $np );
346 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
351 if ( '' == $ip ) { $ip = '(Unknown)'; }
353 $m = wfMsg( 'passwordremindermailbody', $ip, $u->getName(), wfUrlencode($u->getName()), $np );
354 $result = $u->sendMail( wfMsg( 'passwordremindermailsubject' ), $m );
361 * @param string $msg Message that will be shown on success.
364 function successfulLogin( $msg ) {
368 # Run any hooks; ignore results
370 wfRunHooks('UserLoginComplete', array(&$wgUser));
372 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
373 $wgOut->setRobotpolicy( 'noindex,nofollow' );
374 $wgOut->setArticleRelated( false );
375 $wgOut->addWikiText( $msg );
376 $wgOut->returnToMain();
380 function userNotPrivilegedMessage() {
383 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
384 $wgOut->setRobotpolicy( 'noindex,nofollow' );
385 $wgOut->setArticleRelated( false );
387 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
389 $wgOut->returnToMain( false );
395 function mainLoginForm( $err ) {
396 global $wgUser, $wgOut, $wgLang;
397 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
399 if ( '' == $this->mName
) {
400 if ( $wgUser->isLoggedIn() ) {
401 $this->mName
= $wgUser->getName();
403 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
407 $q = 'action=submitlogin';
408 if ( !empty( $this->mReturnto
) ) {
409 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
411 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
413 require_once( 'templates/Userlogin.php' );
414 $template =& new UserloginTemplate();
416 $template->set( 'name', $this->mName
);
417 $template->set( 'password', $this->mPassword
);
418 $template->set( 'retype', $this->mRetype
);
419 $template->set( 'email', $this->mEmail
);
420 $template->set( 'realname', $this->mRealName
);
422 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
423 $template->set( 'error', $err );
424 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
425 $template->set( 'createemail', $wgEnableEmail && $wgUser->isLoggedIn() );
426 $template->set( 'userealname', $wgAllowRealName );
427 $template->set( 'useemail', $wgEnableEmail );
428 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) or $this->mRemember
);
430 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
431 $wgOut->setRobotpolicy( 'noindex,nofollow' );
432 $wgOut->setArticleRelated( false );
433 $wgOut->addTemplate( $template );
439 function hasSessionCookie() {
440 global $wgDisableCookieCheck;
441 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
447 function cookieRedirectCheck( $type ) {
448 global $wgOut, $wgLang;
450 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
451 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
453 return $wgOut->redirect( $check );
459 function onCookieRedirectCheck( $type ) {
462 if ( !$this->hasSessionCookie() ) {
463 if ( $type == 'new' ) {
464 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
465 } else if ( $type == 'login' ) {
466 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
469 return $this->mainLoginForm( wfMsg( 'error' ) );
472 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
479 function throttleHit( $limit ) {
482 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );