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' );
52 $this->mEmail
= $request->getText( 'wpEmail' );
53 if ($wgAllowRealName) {
54 $this->mRealName
= $request->getText( 'wpRealName' );
56 $this->mRealName
= '';
59 # When switching accounts, it sucks to get automatically logged out
60 if( $this->mReturnto
== $wgLang->specialPage( 'Userlogout' ) ) {
61 $this->mReturnto
= '';
66 if ( !is_null( $this->mCookieCheck
) ) {
67 $this->onCookieRedirectCheck( $this->mCookieCheck
);
69 } else if( $this->mPosted
) {
70 if( $this->mCreateaccount
) {
71 return $this->addNewAccount();
72 } else if ( $this->mCreateaccountMail
) {
73 return $this->addNewAccountMailPassword();
74 } else if ( $this->mMailmypassword
) {
75 return $this->mailPassword();
76 } else if ( ( 'submit' == $this->mAction
) ||
$this->mLoginattempt
) {
77 return $this->processLogin();
80 $this->mainLoginForm( '' );
86 function addNewAccountMailPassword() {
89 if ('' == $this->mEmail
) {
90 $this->mainLoginForm( wfMsg( 'noemail', htmlspecialchars( $this->mName
) ) );
94 $u = $this->addNewaccountInternal();
101 $error = $this->mailPasswordInternal($u);
103 $wgOut->setPageTitle( wfMsg( 'accmailtitle' ) );
104 $wgOut->setRobotpolicy( 'noindex,nofollow' );
105 $wgOut->setArticleRelated( false );
107 if ( $error === '' ) {
108 $wgOut->addWikiText( wfMsg( 'accmailtext', $u->getName(), $u->getEmail() ) );
109 $wgOut->returnToMain( false );
111 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
121 function addNewAccount() {
122 global $wgUser, $wgOut;
123 global $wgDeferredUpdateList;
125 $u = $this->addNewAccountInternal();
132 $wgUser->setCookies();
134 $up = new UserUpdate();
135 array_push( $wgDeferredUpdateList, $up );
137 if( $this->hasSessionCookie() ) {
138 return $this->successfulLogin( wfMsg( 'welcomecreation', $wgUser->getName() ) );
140 return $this->cookieRedirectCheck( 'new' );
148 function addNewAccountInternal() {
149 global $wgUser, $wgOut;
150 global $wgMaxNameChars;
151 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
153 if (!$wgUser->isAllowedToCreateAccount()) {
154 $this->userNotPrivilegedMessage();
158 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
159 $this->mainLoginForm( wfMsg( 'badretype' ) );
163 $name = trim( $this->mName
);
164 $u = User
::newFromName( $name );
165 if ( is_null( $u ) ||
167 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
168 (strpos( $name, "/" ) !== false) ||
169 (strlen( $name ) > $wgMaxNameChars) ||
170 ucFirst($name) != $u->getName() )
172 $this->mainLoginForm( wfMsg( 'noname' ) );
175 if ( wfReadOnly() ) {
176 $wgOut->readOnlyPage();
180 if ( 0 != $u->idForName() ) {
181 $this->mainLoginForm( wfMsg( 'userexists' ) );
185 if ( $wgAccountCreationThrottle ) {
186 $key = $wgDBname.':acctcreate:ip:'.$wgIP;
187 $value = $wgMemc->incr( $key );
189 $wgMemc->set( $key, 1, 86400 );
191 if ( $value > $wgAccountCreationThrottle ) {
192 $this->throttleHit( $wgAccountCreationThrottle );
197 return $this->initUser( $u );
201 * Actually add a user to the database.
202 * Give it a User object that has been initialised with a name.
208 function &initUser( &$u ) {
210 $u->setPassword( $this->mPassword
);
211 $u->setEmail( $this->mEmail
);
212 $u->setRealName( $this->mRealName
);
215 $wgAuth->initUser( $u );
217 if ( $this->mRemember
) { $r = 1; }
219 $u->setOption( 'rememberpassword', $r );
227 function processLogin() {
229 global $wgDeferredUpdateList;
231 if ( '' == $this->mName
) {
232 $this->mainLoginForm( wfMsg( 'noname' ) );
235 $u = User
::newFromName( $this->mName
);
236 if( is_null( $u ) ) {
237 $this->mainLoginForm( wfMsg( 'noname' ) );
240 $id = $u->idForName();
244 * If the external authentication plugin allows it,
245 * automatically create a new account for users that
246 * are externally defined but have not yet logged in.
248 if( $wgAuth->autoCreate() &&
249 $wgAuth->userExists( $u->getName() ) &&
250 $wgAuth->authenticate( $u->getName(), $this->mPassword
) ) {
251 $u =& $this->initUser( $u );
253 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
258 $u->loadFromDatabase();
260 if (!$u->checkPassword( $this->mPassword
)) {
261 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
265 # We've verified now, update the real record
267 if ( $this->mRemember
) {
272 $u->setOption( 'rememberpassword', $r );
275 $wgUser->setCookies();
277 $up = new UserUpdate();
278 array_push( $wgDeferredUpdateList, $up );
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 $id = $u->idForName();
305 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
309 $u->loadFromDatabase();
311 $error = $this->mailPasswordInternal( $u );
313 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
315 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
324 function mailPasswordInternal( $u ) {
325 global $wgDeferredUpdateList, $wgOutputEncoding;
326 global $wgPasswordSender, $wgDBname, $wgIP;
327 global $wgCookiePath, $wgCookieDomain;
329 if ( '' == $u->getEmail() ) {
330 return wfMsg( 'noemail', $u->getName() );
332 $np = User
::randomPassword();
333 $u->setNewpassword( $np );
335 setcookie( "{$wgDBname}Token", '', time() - 3600, $wgCookiePath, $wgCookieDomain );
339 if ( '' == $ip ) { $ip = '(Unknown)'; }
341 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
343 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
345 return htmlspecialchars( $error );
352 function successfulLogin( $msg ) {
354 global $wgDeferredUpdateList;
357 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
358 $wgOut->setRobotpolicy( 'noindex,nofollow' );
359 $wgOut->setArticleRelated( false );
360 $wgOut->addWikiText( $msg );
361 $wgOut->returnToMain();
364 function userNotPrivilegedMessage() {
365 global $wgOut, $wgUser, $wgLang;
367 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
368 $wgOut->setRobotpolicy( 'noindex,nofollow' );
369 $wgOut->setArticleRelated( false );
371 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
373 $wgOut->returnToMain( false );
379 function mainLoginForm( $err ) {
380 global $wgUser, $wgOut, $wgLang;
381 global $wgDBname, $wgAllowRealName;
383 $le = wfMsg( 'loginerror' );
384 $yn = wfMsg( 'yourname' );
385 $yp = wfMsg( 'yourpassword' );
386 $ypa = wfMsg( 'yourpasswordagain' );
387 $rmp = wfMsg( 'remembermypassword' );
388 $nuo = wfMsg( 'newusersonly' );
389 $li = wfMsg( 'login' );
390 $ca = wfMsg( 'createaccount' );
391 $cam = wfMsg( 'createaccountmail' );
392 $ye = wfMsg( 'youremail' );
393 if( $wgAllowRealName ) {
394 $yrn = wfMsg( 'yourrealname' );
398 $efl = wfMsg( 'emailforlost' );
399 $mmp = wfMsg( 'mailmypassword' );
400 $endText = wfMsg( 'loginend' );
402 if ( $endText == '<loginend>' ) {
406 if ( '' == $this->mName
) {
407 if ( 0 != $wgUser->getID() ) {
408 $this->mName
= $wgUser->getName();
410 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
414 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
415 $wgOut->setRobotpolicy( 'noindex,nofollow' );
416 $wgOut->setArticleRelated( false );
419 $lp = wfMsg( 'loginprompt' );
420 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
422 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
423 color='red'>$err</font>\n" );
425 if ( 1 == $wgUser->getOption( 'rememberpassword' ) ) {
426 $checked = ' checked';
431 $q = 'action=submit';
432 if ( !empty( $this->mReturnto
) ) {
433 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
436 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
437 $action = $titleObj->escapeLocalUrl( $q );
439 $encName = htmlspecialchars( $this->mName
);
440 $encPassword = htmlspecialchars( $this->mPassword
);
441 $encRetype = htmlspecialchars( $this->mRetype
);
442 $encEmail = htmlspecialchars( $this->mEmail
);
443 $encRealName = htmlspecialchars( $this->mRealName
);
445 if ($wgUser->getID() != 0) {
446 $cambutton = "<input tabindex='6' type='submit' name=\"wpCreateaccountMail\" value=\"{$cam}\" />";
452 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
453 <table border='0'><tr>
454 <td align='right'>$yn:</td>
456 <input tabindex='1' type='text' name=\"wpName\" value=\"{$encName}\" size='20' />
459 <input tabindex='3' type='submit' name=\"wpLoginattempt\" value=\"{$li}\" />
463 <td align='right'>$yp:</td>
465 <input tabindex='2' type='password' name=\"wpPassword\" value=\"{$encPassword}\" size='20' />
468 <input tabindex='4' type='checkbox' name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked /><label for=\"wpRemember\">$rmp</label>
472 if ($wgUser->isAllowedToCreateAccount()) {
473 $encRetype = htmlspecialchars( $this->mRetype
);
474 $encEmail = htmlspecialchars( $this->mEmail
);
475 $wgOut->addHTML("<tr><td colspan='3'> </td></tr><tr>
476 <td align='right'>$ypa:</td>
478 <input tabindex='5' type='password' name=\"wpRetype\" value=\"{$encRetype}\"
480 </td><td>$nuo</td></tr>
482 <td align='right'>$ye:</td>
484 <input tabindex='7' type='text' name=\"wpEmail\" value=\"{$encEmail}\" size='20' />
487 if ($wgAllowRealName) {
488 $wgOut->addHTML("<td> </td>
490 <td align='right'>$yrn:</td>
492 <input tabindex='8' type='text' name=\"wpRealName\" value=\"{$encRealName}\" size='20' />
496 $wgOut->addHTML("<td align='left'>
497 <input tabindex='9' type='submit' name=\"wpCreateaccount\" value=\"{$ca}\" />
503 <tr><td colspan='3'> </td></tr><tr>
504 <td colspan='3' align='left'>
506 <input tabindex='10' type='submit' name=\"wpMailmypassword\" value=\"{$mmp}\" /></p>
509 $wgOut->addHTML( $endText );
515 function hasSessionCookie() {
516 global $wgDisableCookieCheck;
517 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
523 function cookieRedirectCheck( $type ) {
524 global $wgOut, $wgLang;
526 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
527 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
529 return $wgOut->redirect( $check );
535 function onCookieRedirectCheck( $type ) {
538 if ( !$this->hasSessionCookie() ) {
539 if ( $type == 'new' ) {
540 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
541 } else if ( $type == 'login' ) {
542 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
545 return $this->mainLoginForm( wfMsg( 'error' ) );
548 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
555 function throttleHit( $limit ) {
558 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );