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 $id = $u->idForName();
247 * If the external authentication plugin allows it,
248 * automatically create a new account for users that
249 * are externally defined but have not yet logged in.
251 if( $wgAuth->autoCreate() &&
252 $wgAuth->userExists( $u->getName() ) &&
253 $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
254 $u =& $this->initUser( $u );
256 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
261 $u->loadFromDatabase();
263 if (!$u->checkPassword( $this->mPassword
)) {
264 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
268 # We've verified now, update the real record
270 if ( $this->mRemember
) {
275 $u->setOption( 'rememberpassword', $r );
278 $wgUser->setCookies();
280 $wgUser->saveSettings();
282 if( $this->hasSessionCookie() ) {
283 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
285 return $this->cookieRedirectCheck( 'login' );
292 function mailPassword() {
293 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
294 global $wgCookiePath, $wgCookieDomain, $wgDBname;
296 if ( '' == $this->mName
) {
297 $this->mainLoginForm( wfMsg( 'noname' ) );
300 $u = User
::newFromName( $this->mName
);
301 if( is_null( $u ) ) {
302 $this->mainLoginForm( wfMsg( 'noname' ) );
305 $id = $u->idForName();
307 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
311 $u->loadFromDatabase();
313 $error = $this->mailPasswordInternal( $u );
315 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
317 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
326 function mailPasswordInternal( $u ) {
327 global $wgDeferredUpdateList, $wgOutputEncoding;
328 global $wgPasswordSender, $wgDBname, $wgIP;
329 global $wgCookiePath, $wgCookieDomain;
331 if ( '' == $u->getEmail() ) {
332 return wfMsg( 'noemail', $u->getName() );
334 $np = User
::randomPassword();
335 $u->setNewpassword( $np );
337 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
341 if ( '' == $ip ) { $ip = '(Unknown)'; }
343 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
345 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
347 return htmlspecialchars( $error );
354 function successfulLogin( $msg ) {
356 global $wgDeferredUpdateList;
359 # Run any hooks; ignore results
361 wfRunHooks('UserLoginComplete', $wgUser);
363 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
364 $wgOut->setRobotpolicy( 'noindex,nofollow' );
365 $wgOut->setArticleRelated( false );
366 $wgOut->addWikiText( $msg );
367 $wgOut->returnToMain();
370 function userNotPrivilegedMessage() {
371 global $wgOut, $wgUser, $wgLang;
373 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
374 $wgOut->setRobotpolicy( 'noindex,nofollow' );
375 $wgOut->setArticleRelated( false );
377 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
379 $wgOut->returnToMain( false );
385 function mainLoginForm( $err ) {
386 global $wgUser, $wgOut, $wgLang;
387 global $wgDBname, $wgAllowRealName, $wgEnableEmail;
389 if ( '' == $this->mName
) {
390 if ( 0 != $wgUser->getID() ) {
391 $this->mName
= $wgUser->getName();
393 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
397 $q = 'action=submit';
398 if ( !empty( $this->mReturnto
) ) {
399 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
401 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
404 require_once( 'templates/Userlogin.php' );
405 $template =& new UserloginTemplate();
407 $template->set( 'name', $this->mName
);
408 $template->set( 'password', $this->mPassword
);
409 $template->set( 'retype', $this->mRetype
);
410 $template->set( 'email', $this->mEmail
);
411 $template->set( 'realname', $this->mRealName
);
413 $template->set( 'action', $titleObj->getLocalUrl( $q ) );
414 $template->set( 'error', $err );
415 $template->set( 'create', $wgUser->isAllowedToCreateAccount() );
416 $template->set( 'createemail', $wgEnableEmail && $wgUser->getID() != 0 );
417 $template->set( 'userealname', $wgAllowRealName );
418 $template->set( 'useemail', $wgEnableEmail );
419 $template->set( 'remember', $wgUser->getOption( 'rememberpassword' ) );
421 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
422 $wgOut->setRobotpolicy( 'noindex,nofollow' );
423 $wgOut->setArticleRelated( false );
424 $wgOut->addTemplate( $template );
430 function hasSessionCookie() {
431 global $wgDisableCookieCheck;
432 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
438 function cookieRedirectCheck( $type ) {
439 global $wgOut, $wgLang;
441 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
442 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
444 return $wgOut->redirect( $check );
450 function onCookieRedirectCheck( $type ) {
453 if ( !$this->hasSessionCookie() ) {
454 if ( $type == 'new' ) {
455 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
456 } else if ( $type == 'login' ) {
457 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
460 return $this->mainLoginForm( wfMsg( 'error' ) );
463 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
470 function throttleHit( $limit ) {
473 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );