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, $wgEnableEmail;
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' )
49 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' )
51 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
52 $this->mAction
= $request->getVal( 'action' );
53 $this->mRemember
= $request->getCheck( 'wpRemember' );
55 if( $wgEnableEmail ) {
56 $this->mEmail
= $request->getText( 'wpEmail' );
60 if( $wgAllowRealName ) {
61 $this->mRealName
= $request->getText( 'wpRealName' );
63 $this->mRealName
= '';
66 # When switching accounts, it sucks to get automatically logged out
67 if( $this->mReturnto
== $wgLang->specialPage( 'Userlogout' ) ) {
68 $this->mReturnto
= '';
73 if ( !is_null( $this->mCookieCheck
) ) {
74 $this->onCookieRedirectCheck( $this->mCookieCheck
);
76 } else if( $this->mPosted
) {
77 if( $this->mCreateaccount
) {
78 return $this->addNewAccount();
79 } else if ( $this->mCreateaccountMail
) {
80 return $this->addNewAccountMailPassword();
81 } else if ( $this->mMailmypassword
) {
82 return $this->mailPassword();
83 } else if ( ( 'submit' == $this->mAction
) ||
$this->mLoginattempt
) {
84 return $this->processLogin();
87 $this->mainLoginForm( '' );
93 function addNewAccountMailPassword() {
96 if ('' == $this->mEmail
) {
97 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
101 $u = $this->addNewaccountInternal();
108 $error = $this->mailPasswordInternal($u);
110 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
111 $wgOut->setRobotpolicy( 'noindex,nofollow' );
112 $wgOut->setArticleRelated( false );
114 if ( $error === '' ) {
115 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
116 $wgOut->returnToMain( false );
118 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
128 function addNewAccount() {
129 global $wgUser, $wgOut;
130 global $wgDeferredUpdateList;
132 $u = $this->addNewAccountInternal();
139 $wgUser->setCookies();
141 $up = new UserUpdate();
142 array_push( $wgDeferredUpdateList, $up );
144 if( $this->hasSessionCookie() ) {
145 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
147 return $this->cookieRedirectCheck( 'new' );
155 function addNewAccountInternal() {
156 global $wgUser, $wgOut;
157 global $wgMaxNameChars;
158 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
160 if (!$wgUser->isAllowedToCreateAccount()) {
161 $this->userNotPrivilegedMessage();
165 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
166 $this->mainLoginForm( wfMsg( 'badretype' ) );
170 $name = trim( $this->mName
);
171 $u = User
::newFromName( $name );
172 if ( is_null( $u ) ||
174 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
175 (strpos( $name, "/" ) !== false) ||
176 (strlen( $name ) > $wgMaxNameChars) ||
177 ucFirst($name) != $u->getName() )
179 $this->mainLoginForm( wfMsg( 'noname' ) );
182 if ( wfReadOnly() ) {
183 $wgOut->readOnlyPage();
187 if ( 0 != $u->idForName() ) {
188 $this->mainLoginForm( wfMsg( 'userexists' ) );
192 if ( $wgAccountCreationThrottle ) {
193 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
194 $value = $wgMemc->incr( $key );
196 $wgMemc->set( $key, 1, 86400 );
198 if ( $value > $wgAccountCreationThrottle ) {
199 $this->throttleHit( $wgAccountCreationThrottle );
204 return $this->initUser( $u );
208 * Actually add a user to the database.
209 * Give it a User object that has been initialised with a name.
215 function &initUser( &$u ) {
217 $u->setPassword( $this->mPassword
);
218 $u->setEmail( $this->mEmail
);
219 $u->setRealName( $this->mRealName
);
222 $wgAuth->initUser( $u );
224 if ( $this->mRemember
) { $r = 1; }
226 $u->setOption( 'rememberpassword', $r );
234 function processLogin() {
236 global $wgDeferredUpdateList;
238 if ( '' == $this->mName
) {
239 $this->mainLoginForm( wfMsg( 'noname' ) );
242 $u = User
::newFromName( $this->mName
);
243 if( is_null( $u ) ) {
244 $this->mainLoginForm( wfMsg( 'noname' ) );
247 $id = $u->idForName();
251 * If the external authentication plugin allows it,
252 * automatically create a new account for users that
253 * are externally defined but have not yet logged in.
255 if( $wgAuth->autoCreate() &&
256 $wgAuth->userExists( $u->getName() ) &&
257 $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
258 $u =& $this->initUser( $u );
260 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
265 $u->loadFromDatabase();
267 if (!$u->checkPassword( $this->mPassword
)) {
268 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
272 # We've verified now, update the real record
274 if ( $this->mRemember
) {
279 $u->setOption( 'rememberpassword', $r );
282 $wgUser->setCookies();
284 $up = new UserUpdate();
285 array_push( $wgDeferredUpdateList, $up );
287 if( $this->hasSessionCookie() ) {
288 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
290 return $this->cookieRedirectCheck( 'login' );
297 function mailPassword() {
298 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
299 global $wgCookiePath, $wgCookieDomain, $wgDBname;
301 if ( '' == $this->mName
) {
302 $this->mainLoginForm( wfMsg( 'noname' ) );
305 $u = User
::newFromName( $this->mName
);
306 if( is_null( $u ) ) {
307 $this->mainLoginForm( wfMsg( 'noname' ) );
310 $id = $u->idForName();
312 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
316 $u->loadFromDatabase();
318 $error = $this->mailPasswordInternal( $u );
320 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
322 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
331 function mailPasswordInternal( $u ) {
332 global $wgDeferredUpdateList, $wgOutputEncoding;
333 global $wgPasswordSender, $wgDBname, $wgIP;
334 global $wgCookiePath, $wgCookieDomain;
336 if ( '' == $u->getEmail() ) {
337 return wfMsg( 'noemail', $u->getName() );
339 $np = User
::randomPassword();
340 $u->setNewpassword( $np );
342 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
346 if ( '' == $ip ) { $ip = '(Unknown)'; }
348 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
350 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
352 return htmlspecialchars( $error );
359 function successfulLogin( $msg ) {
361 global $wgDeferredUpdateList;
364 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
365 $wgOut->setRobotpolicy( 'noindex,nofollow' );
366 $wgOut->setArticleRelated( false );
367 $wgOut->addWikiText( $msg );
368 $wgOut->returnToMain();
371 function userNotPrivilegedMessage() {
372 global $wgOut, $wgUser, $wgLang;
374 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
375 $wgOut->setRobotpolicy( 'noindex,nofollow' );
376 $wgOut->setArticleRelated( false );
378 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
380 $wgOut->returnToMain( false );
386 function mainLoginForm( $err ) {
387 global $wgUser, $wgOut, $wgLang;
388 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
390 if ( '' == $this->mName
) {
391 if ( 0 != $wgUser->getID() ) {
392 $this->mName
= $wgUser->getName();
394 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
398 $q = 'action=submit';
399 if ( !empty( $this->mReturnto
) ) {
400 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
402 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
405 require_once( 'templates/Userlogin.php' );
406 $template =& new UserloginTemplate();
408 $template->set( 'name', $this->mName
);
409 $template->set( 'password', $this->mPassword
);
410 $template->set( 'retype', $this->mRetype
);
411 $template->set( 'email', $this->mEmail
);
412 $template->set( 'realname', $this->mRealName
);
414 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
415 $template->set( 'error', $err );
416 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
417 $template->set( 'createemail', $wgEnableEmail && $wgUser->getID() != 0 );
418 $template->set( 'userealname', $wgAllowRealName );
419 $template->set( 'useemail', $wgEnableEmail );
420 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
422 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
423 $wgOut->setRobotpolicy( 'noindex,nofollow' );
424 $wgOut->setArticleRelated( false );
425 $wgOut->addTemplate( $template );
431 function hasSessionCookie() {
432 global $wgDisableCookieCheck;
433 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
439 function cookieRedirectCheck( $type ) {
440 global $wgOut, $wgLang;
442 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
443 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
445 return $wgOut->redirect( $check );
451 function onCookieRedirectCheck( $type ) {
454 if ( !$this->hasSessionCookie() ) {
455 if ( $type == 'new' ) {
456 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
457 } else if ( $type == 'login' ) {
458 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
461 return $this->mainLoginForm( wfMsg( 'error' ) );
464 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
471 function throttleHit( $limit ) {
474 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );