5 * @subpackage SpecialPage
11 require_once('UserMailer.php');
16 function wfSpecialUserlogin() {
17 global $wgCommandLineMode;
19 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get('session.name')] ) ) {
23 $form = new LoginForm( $wgRequest );
30 * @subpackage SpecialPage
33 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
34 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
35 var $mLoginattempt, $mRemember, $mEmail;
37 function LoginForm( &$request ) {
38 global $wgLang, $wgAllowRealName;
40 $this->mName
= $request->getText( 'wpName' );
41 $this->mPassword
= $request->getText( 'wpPassword' );
42 $this->mRetype
= $request->getText( 'wpRetype' );
43 $this->mReturnto
= $request->getVal( 'returnto' );
44 $this->mCookieCheck
= $request->getVal( 'wpCookieCheck' );
45 $this->mPosted
= $request->wasPosted();
46 $this->mCreateaccount
= $request->getCheck( 'wpCreateaccount' );
47 $this->mCreateaccountMail
= $request->getCheck( 'wpCreateaccountMail' );
48 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' );
49 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
50 $this->mAction
= $request->getVal( 'action' );
51 $this->mRemember
= $request->getCheck( 'wpRemember' );
53 $this->mEmail
= $request->getText( 'wpEmail' );
54 if( $wgAllowRealName ) {
55 $this->mRealName
= $request->getText( 'wpRealName' );
57 $this->mRealName
= '';
60 # When switching accounts, it sucks to get automatically logged out
61 if( $this->mReturnto
== $wgLang->specialPage( 'Userlogout' ) ) {
62 $this->mReturnto
= '';
67 if ( !is_null( $this->mCookieCheck
) ) {
68 $this->onCookieRedirectCheck( $this->mCookieCheck
);
70 } else if( $this->mPosted
) {
71 if( $this->mCreateaccount
) {
72 return $this->addNewAccount();
73 } else if ( $this->mCreateaccountMail
) {
74 return $this->addNewAccountMailPassword();
75 } else if ( $this->mMailmypassword
) {
76 return $this->mailPassword();
77 } else if ( ( 'submit' == $this->mAction
) ||
$this->mLoginattempt
) {
78 return $this->processLogin();
81 $this->mainLoginForm( '' );
87 function addNewAccountMailPassword() {
90 if ('' == $this->mEmail
) {
91 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
95 $u = $this->addNewaccountInternal();
102 $error = $this->mailPasswordInternal($u);
104 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
105 $wgOut->setRobotpolicy( 'noindex,nofollow' );
106 $wgOut->setArticleRelated( false );
108 if ( $error === '' ) {
109 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
110 $wgOut->returnToMain( false );
112 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
122 function addNewAccount() {
123 global $wgUser, $wgOut;
124 global $wgDeferredUpdateList;
126 $u = $this->addNewAccountInternal();
133 $wgUser->setCookies();
135 $up = new UserUpdate();
136 array_push( $wgDeferredUpdateList, $up );
138 if( $this->hasSessionCookie() ) {
139 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
141 return $this->cookieRedirectCheck( 'new' );
149 function addNewAccountInternal() {
150 global $wgUser, $wgOut;
151 global $wgMaxNameChars;
152 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
154 if (!$wgUser->isAllowedToCreateAccount()) {
155 $this->userNotPrivilegedMessage();
159 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
160 $this->mainLoginForm( wfMsg( 'badretype' ) );
164 $name = trim( $this->mName
);
165 $u = User
::newFromName( $name );
166 if ( is_null( $u ) ||
168 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
169 (strpos( $name, "/" ) !== false) ||
170 (strlen( $name ) > $wgMaxNameChars) ||
171 ucFirst($name) != $u->getName() )
173 $this->mainLoginForm( wfMsg( 'noname' ) );
176 if ( wfReadOnly() ) {
177 $wgOut->readOnlyPage();
181 if ( 0 != $u->idForName() ) {
182 $this->mainLoginForm( wfMsg( 'userexists' ) );
186 if ( $wgAccountCreationThrottle ) {
187 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
188 $value = $wgMemc->incr( $key );
190 $wgMemc->set( $key, 1, 86400 );
192 if ( $value > $wgAccountCreationThrottle ) {
193 $this->throttleHit( $wgAccountCreationThrottle );
198 return $this->initUser( $u );
202 * Actually add a user to the database.
203 * Give it a User object that has been initialised with a name.
209 function &initUser( &$u ) {
211 $u->setPassword( $this->mPassword
);
212 $u->setEmail( $this->mEmail
);
213 $u->setRealName( $this->mRealName
);
216 $wgAuth->initUser( $u );
218 if ( $this->mRemember
) { $r = 1; }
220 $u->setOption( 'rememberpassword', $r );
228 function processLogin() {
230 global $wgDeferredUpdateList;
232 if ( '' == $this->mName
) {
233 $this->mainLoginForm( wfMsg( 'noname' ) );
236 $u = User
::newFromName( $this->mName
);
237 if( is_null( $u ) ) {
238 $this->mainLoginForm( wfMsg( 'noname' ) );
241 $id = $u->idForName();
245 * If the external authentication plugin allows it,
246 * automatically create a new account for users that
247 * are externally defined but have not yet logged in.
249 if( $wgAuth->autoCreate() &&
250 $wgAuth->userExists( $u->getName() ) &&
251 $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
252 $u =& $this->initUser( $u );
254 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
259 $u->loadFromDatabase();
261 if (!$u->checkPassword( $this->mPassword
)) {
262 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
266 # We've verified now, update the real record
268 if ( $this->mRemember
) {
273 $u->setOption( 'rememberpassword', $r );
276 $wgUser->setCookies();
278 $up = new UserUpdate();
279 array_push( $wgDeferredUpdateList, $up );
281 if( $this->hasSessionCookie() ) {
282 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
284 return $this->cookieRedirectCheck( 'login' );
291 function mailPassword() {
292 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
293 global $wgCookiePath, $wgCookieDomain, $wgDBname;
295 if ( '' == $this->mName
) {
296 $this->mainLoginForm( wfMsg( 'noname' ) );
299 $u = User
::newFromName( $this->mName
);
300 if( is_null( $u ) ) {
301 $this->mainLoginForm( wfMsg( 'noname' ) );
304 $id = $u->idForName();
306 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
310 $u->loadFromDatabase();
312 $error = $this->mailPasswordInternal( $u );
314 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
316 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
325 function mailPasswordInternal( $u ) {
326 global $wgDeferredUpdateList, $wgOutputEncoding;
327 global $wgPasswordSender, $wgDBname, $wgIP;
328 global $wgCookiePath, $wgCookieDomain;
330 if ( '' == $u->getEmail() ) {
331 return wfMsg( 'noemail', $u->getName() );
333 $np = User
::randomPassword();
334 $u->setNewpassword( $np );
336 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
340 if ( '' == $ip ) { $ip = '(Unknown)'; }
342 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
344 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
346 return htmlspecialchars( $error );
353 function successfulLogin( $msg ) {
355 global $wgDeferredUpdateList;
358 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
359 $wgOut->setRobotpolicy( 'noindex,nofollow' );
360 $wgOut->setArticleRelated( false );
361 $wgOut->addWikiText( $msg );
362 $wgOut->returnToMain();
365 function userNotPrivilegedMessage() {
366 global $wgOut, $wgUser, $wgLang;
368 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
369 $wgOut->setRobotpolicy( 'noindex,nofollow' );
370 $wgOut->setArticleRelated( false );
372 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
374 $wgOut->returnToMain( false );
380 function mainLoginForm( $err ) {
381 global $wgUser, $wgOut, $wgLang;
382 global $wgDBname, $wgAllowRealName;
384 if ( '' == $this->mName
) {
385 if ( 0 != $wgUser->getID() ) {
386 $this->mName
= $wgUser->getName();
388 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
392 $q = 'action=submit';
393 if ( !empty( $this->mReturnto
) ) {
394 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
396 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
399 require_once( 'templates/Userlogin.php' );
400 $template =& new UserloginTemplate();
402 $template->set( 'name', $this->mName
);
403 $template->set( 'password', $this->mPassword
);
404 $template->set( 'retype', $this->mRetype
);
405 $template->set( 'email', $this->mEmail
);
406 $template->set( 'realname', $this->mRealName
);
408 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
409 $template->set( 'error', $err );
410 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
411 $template->set( 'createemail', $wgUser->getID() != 0 );
412 $template->set( 'userealname', $wgAllowRealName );
413 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
415 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
416 $wgOut->setRobotpolicy( 'noindex,nofollow' );
417 $wgOut->setArticleRelated( false );
418 $wgOut->addTemplate( $template );
424 function hasSessionCookie() {
425 global $wgDisableCookieCheck;
426 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
432 function cookieRedirectCheck( $type ) {
433 global $wgOut, $wgLang;
435 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
436 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
438 return $wgOut->redirect( $check );
444 function onCookieRedirectCheck( $type ) {
447 if ( !$this->hasSessionCookie() ) {
448 if ( $type == 'new' ) {
449 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
450 } else if ( $type == 'login' ) {
451 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
454 return $this->mainLoginForm( wfMsg( 'error' ) );
457 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
464 function throttleHit( $limit ) {
467 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );