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;
131 $u = $this->addNewAccountInternal();
138 $wgUser->setCookies();
140 $wgUser->saveSettings();
142 if( $this->hasSessionCookie() ) {
143 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
145 return $this->cookieRedirectCheck( 'new' );
152 function addNewAccountInternal() {
153 global $wgUser, $wgOut;
154 global $wgMaxNameChars;
155 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
157 if (!$wgUser->isAllowedToCreateAccount()) {
158 $this->userNotPrivilegedMessage();
162 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
163 $this->mainLoginForm( wfMsg( 'badretype' ) );
167 $name = trim( $this->mName
);
168 $u = User
::newFromName( $name );
169 if ( is_null( $u ) ||
171 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
172 (strpos( $name, "/" ) !== false) ||
173 (strlen( $name ) > $wgMaxNameChars) ||
174 ucFirst($name) != $u->getName() )
176 $this->mainLoginForm( wfMsg( 'noname' ) );
179 if ( wfReadOnly() ) {
180 $wgOut->readOnlyPage();
184 if ( 0 != $u->idForName() ) {
185 $this->mainLoginForm( wfMsg( 'userexists' ) );
189 if ( $wgAccountCreationThrottle ) {
190 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
191 $value = $wgMemc->incr( $key );
193 $wgMemc->set( $key, 1, 86400 );
195 if ( $value > $wgAccountCreationThrottle ) {
196 $this->throttleHit( $wgAccountCreationThrottle );
201 return $this->initUser( $u );
205 * Actually add a user to the database.
206 * Give it a User object that has been initialised with a name.
212 function &initUser( &$u ) {
214 $u->setPassword( $this->mPassword
);
215 $u->setEmail( $this->mEmail
);
216 $u->setRealName( $this->mRealName
);
219 $wgAuth->initUser( $u );
221 if ( $this->mRemember
) { $r = 1; }
223 $u->setOption( 'rememberpassword', $r );
231 function processLogin() {
234 if ( '' == $this->mName
) {
235 $this->mainLoginForm( wfMsg( 'noname' ) );
238 $u = User
::newFromName( $this->mName
);
239 if( is_null( $u ) ) {
240 $this->mainLoginForm( wfMsg( 'noname' ) );
243 if ( 0 == $u->getID() ) {
246 * If the external authentication plugin allows it,
247 * automatically create a new account for users that
248 * are externally defined but have not yet logged in.
250 if( $wgAuth->autoCreate() &&
251 $wgAuth->userExists( $u->getName() ) &&
252 $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
253 $u =& $this->initUser( $u );
255 $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 $wgUser->saveSettings();
280 if( $this->hasSessionCookie() ) {
281 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
283 return $this->cookieRedirectCheck( 'login' );
290 function mailPassword() {
291 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
292 global $wgCookiePath, $wgCookieDomain, $wgDBname;
294 if ( '' == $this->mName
) {
295 $this->mainLoginForm( wfMsg( 'noname' ) );
298 $u = User
::newFromName( $this->mName
);
299 if( is_null( $u ) ) {
300 $this->mainLoginForm( wfMsg( 'noname' ) );
303 if ( 0 == $u->getID() ) {
304 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
308 $u->loadFromDatabase();
310 $error = $this->mailPasswordInternal( $u );
312 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
314 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
323 function mailPasswordInternal( $u ) {
324 global $wgDeferredUpdateList, $wgOutputEncoding;
325 global $wgPasswordSender, $wgDBname, $wgIP;
326 global $wgCookiePath, $wgCookieDomain;
328 if ( '' == $u->getEmail() ) {
329 return wfMsg( 'noemail', $u->getName() );
331 $np = User
::randomPassword();
332 $u->setNewpassword( $np );
334 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
338 if ( '' == $ip ) { $ip = '(Unknown)'; }
340 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
342 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
344 return htmlspecialchars( $error );
351 function successfulLogin( $msg ) {
353 global $wgDeferredUpdateList;
356 # Run any hooks; ignore results
358 wfRunHooks('UserLoginComplete', $wgUser);
360 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
361 $wgOut->setRobotpolicy( 'noindex,nofollow' );
362 $wgOut->setArticleRelated( false );
363 $wgOut->addWikiText( $msg );
364 $wgOut->returnToMain();
367 function userNotPrivilegedMessage() {
368 global $wgOut, $wgUser, $wgLang;
370 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
371 $wgOut->setRobotpolicy( 'noindex,nofollow' );
372 $wgOut->setArticleRelated( false );
374 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
376 $wgOut->returnToMain( false );
382 function mainLoginForm( $err ) {
383 global $wgUser, $wgOut, $wgLang;
384 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
386 if ( '' == $this->mName
) {
387 if ( 0 != $wgUser->getID() ) {
388 $this->mName
= $wgUser->getName();
390 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
394 $q = 'action=submit';
395 if ( !empty( $this->mReturnto
) ) {
396 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
398 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
401 require_once( 'templates/Userlogin.php' );
402 $template =& new UserloginTemplate();
404 $template->set( 'name', $this->mName
);
405 $template->set( 'password', $this->mPassword
);
406 $template->set( 'retype', $this->mRetype
);
407 $template->set( 'email', $this->mEmail
);
408 $template->set( 'realname', $this->mRealName
);
410 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
411 $template->set( 'error', $err );
412 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
413 $template->set( 'createemail', $wgEnableEmail && $wgUser->getID() != 0 );
414 $template->set( 'userealname', $wgAllowRealName );
415 $template->set( 'useemail', $wgEnableEmail );
416 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
418 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
419 $wgOut->setRobotpolicy( 'noindex,nofollow' );
420 $wgOut->setArticleRelated( false );
421 $wgOut->addTemplate( $template );
427 function hasSessionCookie() {
428 global $wgDisableCookieCheck;
429 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
435 function cookieRedirectCheck( $type ) {
436 global $wgOut, $wgLang;
438 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
439 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
441 return $wgOut->redirect( $check );
447 function onCookieRedirectCheck( $type ) {
450 if ( !$this->hasSessionCookie() ) {
451 if ( $type == 'new' ) {
452 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
453 } else if ( $type == 'login' ) {
454 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
457 return $this->mainLoginForm( wfMsg( 'error' ) );
460 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
467 function throttleHit( $limit ) {
470 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );