9 require_once('UserMailer.php');
14 function wfSpecialUserlogin() {
15 global $wgCommandLineMode;
17 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
21 $form = new LoginForm( $wgRequest );
29 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
30 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
31 var $mLoginattempt, $mRemember, $mEmail;
33 function LoginForm( &$request ) {
34 global $wgLang, $wgAllowRealName;
36 $this->mName
= $request->getText( 'wpName' );
37 $this->mPassword
= $request->getText( 'wpPassword' );
38 $this->mRetype
= $request->getText( 'wpRetype' );
39 $this->mReturnto
= $request->getVal( 'returnto' );
40 $this->mCookieCheck
= $request->getVal( "wpCookieCheck" );
41 $this->mPosted
= $request->wasPosted();
42 $this->mCreateaccount
= $request->getCheck( 'wpCreateaccount' );
43 $this->mCreateaccountMail
= $request->getCheck( 'wpCreateaccountMail' );
44 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' );
45 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
46 $this->mAction
= $request->getVal( 'action' );
47 $this->mRemember
= $request->getCheck( 'wpRemember' );
48 $this->mEmail
= $request->getText( 'wpEmail' );
49 if ($wgAllowRealName) {
50 $this->mRealName
= $request->getText( 'wpRealName' );
52 $this->mRealName
= '';
55 # When switching accounts, it sucks to get automatically logged out
56 if( $this->mReturnto
== $wgLang->specialPage( "Userlogout" ) ) {
57 $this->mReturnto
= "";
62 if ( !is_null( $this->mCookieCheck
) ) {
63 $this->onCookieRedirectCheck( $this->mCookieCheck
);
65 } else if( $this->mPosted
) {
66 if( $this->mCreateaccount
) {
67 return $this->addNewAccount();
68 } else if ( $this->mCreateaccountMail
) {
69 return $this->addNewAccountMailPassword();
70 } else if ( $this->mMailmypassword
) {
71 return $this->mailPassword();
72 } else if ( ( "submit" == $this->mAction
) ||
$this->mLoginattempt
) {
73 return $this->processLogin();
76 $this->mainLoginForm( "" );
82 function addNewAccountMailPassword() {
85 if ("" == $this->mEmail
) {
86 $this->mainLoginForm( wfMsg( "noemail", $this->mName
) );
90 $u = $this->addNewaccountInternal();
97 $error = $this->mailPasswordInternal($u);
99 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
100 $wgOut->setRobotpolicy( "noindex,nofollow" );
101 $wgOut->setArticleRelated( false );
103 if ( $error === "" ) {
104 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
105 $wgOut->returnToMain( false );
107 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
117 function addNewAccount() {
118 global $wgUser, $wgOut;
119 global $wgDeferredUpdateList;
121 $u = $this->addNewAccountInternal();
128 $wgUser->setCookies();
130 $up = new UserUpdate();
131 array_push( $wgDeferredUpdateList, $up );
133 if( $this->hasSessionCookie() ) {
134 return $this->successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
136 return $this->cookieRedirectCheck( "new" );
144 function addNewAccountInternal() {
145 global $wgUser, $wgOut;
146 global $wgMaxNameChars;
147 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
149 if (!$wgUser->isAllowedToCreateAccount()) {
150 $this->userNotPrivilegedMessage();
154 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
155 $this->mainLoginForm( wfMsg( "badretype" ) );
159 $name = trim( $this->mName
);
160 $u = User
::newFromName( $name );
161 if ( ( "" == $name ) ||
162 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
163 (strpos( $name, "/" ) !== false) ||
164 (strlen( $name ) > $wgMaxNameChars) ||
165 ucFirst($name) != $u->getName() )
167 $this->mainLoginForm( wfMsg( "noname" ) );
170 if ( wfReadOnly() ) {
171 $wgOut->readOnlyPage();
175 if ( 0 != $u->idForName() ) {
176 $this->mainLoginForm( wfMsg( "userexists" ) );
180 if ( $wgAccountCreationThrottle ) {
181 $key = "$wgDBname:acctcreate:ip:$wgIP";
182 $value = $wgMemc->incr( $key );
184 $wgMemc->set( $key, 1, 86400 );
186 if ( $value > $wgAccountCreationThrottle ) {
187 $this->throttleHit( $wgAccountCreationThrottle );
193 $u->setPassword( $this->mPassword
);
194 $u->setEmail( $this->mEmail
);
195 $u->setRealName( $this->mRealName
);
197 if ( $this->mRemember
) { $r = 1; }
199 $u->setOption( "rememberpassword", $r );
207 function processLogin() {
209 global $wgDeferredUpdateList;
211 if ( "" == $this->mName
) {
212 $this->mainLoginForm( wfMsg( "noname" ) );
215 $u = User
::newFromName( $this->mName
);
216 $id = $u->idForName();
218 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
222 $u->loadFromDatabase();
223 if (!$u->checkPassword( $this->mPassword
)) {
224 $this->mainLoginForm( wfMsg( "wrongpassword" ) );
228 # We've verified now, update the real record
230 if ( $this->mRemember
) {
232 $u->setCookiePassword( $this->mPassword
);
236 $u->setOption( "rememberpassword", $r );
239 $wgUser->setCookies();
241 $up = new UserUpdate();
242 array_push( $wgDeferredUpdateList, $up );
244 if( $this->hasSessionCookie() ) {
245 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
247 return $this->cookieRedirectCheck( "login" );
254 function mailPassword() {
255 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
256 global $wgCookiePath, $wgCookieDomain, $wgDBname;
258 if ( "" == $this->mName
) {
259 $this->mainLoginForm( wfMsg( "noname" ) );
262 $u = User
::newFromName( $this->mName
);
263 $id = $u->idForName();
265 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
269 $u->loadFromDatabase();
271 $error = $this->mailPasswordInternal( $u );
273 $this->mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
275 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
284 function mailPasswordInternal( $u ) {
285 global $wgDeferredUpdateList, $wgOutputEncoding;
286 global $wgPasswordSender, $wgDBname, $wgIP;
287 global $wgCookiePath, $wgCookieDomain;
289 if ( "" == $u->getEmail() ) {
290 $this->mainLoginForm( wfMsg( "noemail", $u->getName() ) );
293 $np = User
::randomPassword();
294 $u->setNewpassword( $np );
296 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
300 if ( "" == $ip ) { $ip = "(Unknown)"; }
302 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
304 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
313 function successfulLogin( $msg ) {
315 global $wgDeferredUpdateList;
318 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
319 $wgOut->setRobotpolicy( "noindex,nofollow" );
320 $wgOut->setArticleRelated( false );
321 $wgOut->addWikiText( $msg );
322 $wgOut->returnToMain();
325 function userNotPrivilegedMessage() {
326 global $wgOut, $wgUser, $wgLang;
328 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
329 $wgOut->setRobotpolicy( "noindex,nofollow" );
330 $wgOut->setArticleRelated( false );
332 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
334 $wgOut->returnToMain( false );
340 function mainLoginForm( $err ) {
341 global $wgUser, $wgOut, $wgLang;
342 global $wgDBname, $wgAllowRealName;
344 $le = wfMsg( "loginerror" );
345 $yn = wfMsg( "yourname" );
346 $yp = wfMsg( "yourpassword" );
347 $ypa = wfMsg( "yourpasswordagain" );
348 $rmp = wfMsg( "remembermypassword" );
349 $nuo = wfMsg( "newusersonly" );
350 $li = wfMsg( "login" );
351 $ca = wfMsg( "createaccount" );
352 $cam = wfMsg( "createaccountmail" );
353 $ye = wfMsg( "youremail" );
354 if ($wgAllowRealName) {
355 $yrn = wfMsg( "yourrealname" );
359 $efl = wfMsg( "emailforlost" );
360 $mmp = wfMsg( "mailmypassword" );
361 $endText = wfMsg( "loginend" );
363 if ( $endText = "<loginend>" ) {
367 if ( "" == $this->mName
) {
368 if ( 0 != $wgUser->getID() ) {
369 $this->mName
= $wgUser->getName();
371 $this->mName
= @$_COOKIE["{$wgDBname}UserName"];
375 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
376 $wgOut->setRobotpolicy( "noindex,nofollow" );
377 $wgOut->setArticleRelated( false );
380 $lp = wfMsg( "loginprompt" );
381 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
383 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
384 color='red'>$err</font>\n" );
386 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
387 $checked = " checked";
392 $q = "action=submit";
393 if ( !empty( $this->mReturnto
) ) {
394 $q .= "&returnto=" . wfUrlencode( $this->mReturnto
);
397 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
398 $action = $titleObj->escapeLocalUrl( $q );
400 $encName = htmlspecialchars( $this->mName
);
401 $encPassword = htmlspecialchars( $this->mPassword
);
402 $encRetype = htmlspecialchars( $this->mRetype
);
403 $encEmail = htmlspecialchars( $this->mEmail
);
404 $encRealName = htmlspecialchars( $this->mRealName
);
406 if ($wgUser->getID() != 0) {
407 $cambutton = "<input tabindex='6' type='submit' name=\"wpCreateaccountMail\" value=\"{$cam}\" />";
413 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
414 <table border='0'><tr>
415 <td align='right'>$yn:</td>
417 <input tabindex='1' type='text' name=\"wpName\" value=\"{$encName}\" size='20' />
420 <input tabindex='3' type='submit' name=\"wpLoginattempt\" value=\"{$li}\" />
424 <td align='right'>$yp:</td>
426 <input tabindex='2' type='password' name=\"wpPassword\" value=\"{$encPassword}\" size='20' />
429 <input tabindex='7' type='checkbox' name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked /><label for=\"wpRemember\">$rmp</label>
433 if ($wgUser->isAllowedToCreateAccount()) {
434 $encRetype = htmlspecialchars( $this->mRetype
);
435 $encEmail = htmlspecialchars( $this->mEmail
);
436 $wgOut->addHTML("<tr><td colspan='3'> </td></tr><tr>
437 <td align='right'>$ypa:</td>
439 <input tabindex='4' type='password' name=\"wpRetype\" value=\"{$encRetype}\"
441 </td><td>$nuo</td></tr>
443 <td align='right'>$ye:</td>
445 <input tabindex='6' type='text' name=\"wpEmail\" value=\"{$encEmail}\" size='20' />
448 if ($wgAllowRealName) {
449 $wgOut->addHTML("<td> </td>
451 <td align='right'>$yrn:</td>
453 <input tabindex='6' type='text' name=\"wpRealName\" value=\"{$encRealName}\" size='20' />
457 $wgOut->addHTML("<td align='left'>
458 <input tabindex='7' type='submit' name=\"wpCreateaccount\" value=\"{$ca}\" />
464 <tr><td colspan='3'> </td></tr><tr>
465 <td colspan='3' align='left'>
467 <input tabindex='8' type='submit' name=\"wpMailmypassword\" value=\"{$mmp}\" /></p>
470 $wgOut->addHTML( $endText );
476 function hasSessionCookie() {
477 global $wgDisableCookieCheck;
478 return ( $wgDisableCookieCheck ) ?
true : ( "" != $_COOKIE[session_name()] );
484 function cookieRedirectCheck( $type ) {
485 global $wgOut, $wgLang;
487 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
488 $check = $titleObj->getFullURL( "wpCookieCheck=$type" );
490 return $wgOut->redirect( $check );
496 function onCookieRedirectCheck( $type ) {
499 if ( !$this->hasSessionCookie() ) {
500 if ( $type == "new" ) {
501 return $this->mainLoginForm( wfMsg( "nocookiesnew" ) );
502 } else if ( $type == "login" ) {
503 return $this->mainLoginForm( wfMsg( "nocookieslogin" ) );
506 return $this->mainLoginForm( wfMsg( "error" ) );
509 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
516 function throttleHit( $limit ) {
519 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );