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', $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 );
198 $u->setPassword( $this->mPassword
);
199 $u->setEmail( $this->mEmail
);
200 $u->setRealName( $this->mRealName
);
202 if ( $this->mRemember
) { $r = 1; }
204 $u->setOption( 'rememberpassword', $r );
212 function processLogin() {
214 global $wgDeferredUpdateList;
216 if ( '' == $this->mName
) {
217 $this->mainLoginForm( wfMsg( 'noname' ) );
220 $u = User
::newFromName( $this->mName
);
221 if( is_null( $u ) ) {
222 $this->mainLoginForm( wfMsg( 'noname' ) );
225 $id = $u->idForName();
227 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
231 $u->loadFromDatabase();
232 if (!$u->checkPassword( $this->mPassword
)) {
233 $this->mainLoginForm( wfMsg( 'wrongpassword' ) );
237 # We've verified now, update the real record
239 if ( $this->mRemember
) {
241 $u->setCookiePassword( $this->mPassword
);
245 $u->setOption( 'rememberpassword', $r );
248 $wgUser->setCookies();
250 $up = new UserUpdate();
251 array_push( $wgDeferredUpdateList, $up );
253 if( $this->hasSessionCookie() ) {
254 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
256 return $this->cookieRedirectCheck( 'login' );
263 function mailPassword() {
264 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
265 global $wgCookiePath, $wgCookieDomain, $wgDBname;
267 if ( '' == $this->mName
) {
268 $this->mainLoginForm( wfMsg( 'noname' ) );
271 $u = User
::newFromName( $this->mName
);
272 if( is_null( $u ) ) {
273 $this->mainLoginForm( wfMsg( 'noname' ) );
276 $id = $u->idForName();
278 $this->mainLoginForm( wfMsg( 'nosuchuser', $u->getName() ) );
282 $u->loadFromDatabase();
284 $error = $this->mailPasswordInternal( $u );
286 $this->mainLoginForm( wfMsg( 'passwordsent', $u->getName() ) );
288 $this->mainLoginForm( wfMsg( 'mailerror', $error ) );
297 function mailPasswordInternal( $u ) {
298 global $wgDeferredUpdateList, $wgOutputEncoding;
299 global $wgPasswordSender, $wgDBname, $wgIP;
300 global $wgCookiePath, $wgCookieDomain;
302 if ( '' == $u->getEmail() ) {
303 return wfMsg( 'noemail', $u->getName() );
305 $np = User
::randomPassword();
306 $u->setNewpassword( $np );
308 setcookie( $wgDBname.'Password', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
312 if ( '' == $ip ) { $ip = '(Unknown)'; }
314 $m = wfMsg( 'passwordremindertext', $ip, $u->getName(), $np );
316 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( 'passwordremindertitle' ), $m );
325 function successfulLogin( $msg ) {
327 global $wgDeferredUpdateList;
330 $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) );
331 $wgOut->setRobotpolicy( 'noindex,nofollow' );
332 $wgOut->setArticleRelated( false );
333 $wgOut->addWikiText( $msg );
334 $wgOut->returnToMain();
337 function userNotPrivilegedMessage() {
338 global $wgOut, $wgUser, $wgLang;
340 $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
341 $wgOut->setRobotpolicy( 'noindex,nofollow' );
342 $wgOut->setArticleRelated( false );
344 $wgOut->addWikiText( wfMsg( 'whitelistacctext' ) );
346 $wgOut->returnToMain( false );
352 function mainLoginForm( $err ) {
353 global $wgUser, $wgOut, $wgLang;
354 global $wgDBname, $wgAllowRealName;
356 $le = wfMsg( 'loginerror' );
357 $yn = wfMsg( 'yourname' );
358 $yp = wfMsg( 'yourpassword' );
359 $ypa = wfMsg( 'yourpasswordagain' );
360 $rmp = wfMsg( 'remembermypassword' );
361 $nuo = wfMsg( 'newusersonly' );
362 $li = wfMsg( 'login' );
363 $ca = wfMsg( 'createaccount' );
364 $cam = wfMsg( 'createaccountmail' );
365 $ye = wfMsg( 'youremail' );
366 if( $wgAllowRealName ) {
367 $yrn = wfMsg( 'yourrealname' );
371 $efl = wfMsg( 'emailforlost' );
372 $mmp = wfMsg( 'mailmypassword' );
373 $endText = wfMsg( 'loginend' );
375 if ( $endText = '<loginend>' ) {
379 if ( '' == $this->mName
) {
380 if ( 0 != $wgUser->getID() ) {
381 $this->mName
= $wgUser->getName();
383 $this->mName
= @$_COOKIE[$wgDBname.'UserName'];
387 $wgOut->setPageTitle( wfMsg( 'userlogin' ) );
388 $wgOut->setRobotpolicy( 'noindex,nofollow' );
389 $wgOut->setArticleRelated( false );
392 $lp = wfMsg( 'loginprompt' );
393 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
395 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
396 color='red'>$err</font>\n" );
398 if ( 1 == $wgUser->getOption( 'rememberpassword' ) ) {
399 $checked = ' checked';
404 $q = 'action=submit';
405 if ( !empty( $this->mReturnto
) ) {
406 $q .= '&returnto=' . wfUrlencode( $this->mReturnto
);
409 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
410 $action = $titleObj->escapeLocalUrl( $q );
412 $encName = htmlspecialchars( $this->mName
);
413 $encPassword = htmlspecialchars( $this->mPassword
);
414 $encRetype = htmlspecialchars( $this->mRetype
);
415 $encEmail = htmlspecialchars( $this->mEmail
);
416 $encRealName = htmlspecialchars( $this->mRealName
);
418 if ($wgUser->getID() != 0) {
419 $cambutton = "<input tabindex='6' type='submit' name=\"wpCreateaccountMail\" value=\"{$cam}\" />";
425 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
426 <table border='0'><tr>
427 <td align='right'>$yn:</td>
429 <input tabindex='1' type='text' name=\"wpName\" value=\"{$encName}\" size='20' />
432 <input tabindex='3' type='submit' name=\"wpLoginattempt\" value=\"{$li}\" />
436 <td align='right'>$yp:</td>
438 <input tabindex='2' type='password' name=\"wpPassword\" value=\"{$encPassword}\" size='20' />
441 <input tabindex='4' type='checkbox' name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked /><label for=\"wpRemember\">$rmp</label>
445 if ($wgUser->isAllowedToCreateAccount()) {
446 $encRetype = htmlspecialchars( $this->mRetype
);
447 $encEmail = htmlspecialchars( $this->mEmail
);
448 $wgOut->addHTML("<tr><td colspan='3'> </td></tr><tr>
449 <td align='right'>$ypa:</td>
451 <input tabindex='5' type='password' name=\"wpRetype\" value=\"{$encRetype}\"
453 </td><td>$nuo</td></tr>
455 <td align='right'>$ye:</td>
457 <input tabindex='7' type='text' name=\"wpEmail\" value=\"{$encEmail}\" size='20' />
460 if ($wgAllowRealName) {
461 $wgOut->addHTML("<td> </td>
463 <td align='right'>$yrn:</td>
465 <input tabindex='8' type='text' name=\"wpRealName\" value=\"{$encRealName}\" size='20' />
469 $wgOut->addHTML("<td align='left'>
470 <input tabindex='9' type='submit' name=\"wpCreateaccount\" value=\"{$ca}\" />
476 <tr><td colspan='3'> </td></tr><tr>
477 <td colspan='3' align='left'>
479 <input tabindex='10' type='submit' name=\"wpMailmypassword\" value=\"{$mmp}\" /></p>
482 $wgOut->addHTML( $endText );
488 function hasSessionCookie() {
489 global $wgDisableCookieCheck;
490 return ( $wgDisableCookieCheck ) ?
true : ( '' != $_COOKIE[session_name()] );
496 function cookieRedirectCheck( $type ) {
497 global $wgOut, $wgLang;
499 $titleObj = Title
::makeTitle( NS_SPECIAL
, 'Userlogin' );
500 $check = $titleObj->getFullURL( 'wpCookieCheck='.$type );
502 return $wgOut->redirect( $check );
508 function onCookieRedirectCheck( $type ) {
511 if ( !$this->hasSessionCookie() ) {
512 if ( $type == 'new' ) {
513 return $this->mainLoginForm( wfMsg( 'nocookiesnew' ) );
514 } else if ( $type == 'login' ) {
515 return $this->mainLoginForm( wfMsg( 'nocookieslogin' ) );
518 return $this->mainLoginForm( wfMsg( 'error' ) );
521 return $this->successfulLogin( wfMsg( 'loginsuccess', $wgUser->getName() ) );
528 function throttleHit( $limit ) {
531 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );