3 require_once('UserMailer.php');
5 function wfSpecialUserlogin()
7 global $wgCommandLineMode;
9 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
13 $form = new LoginForm( $wgRequest );
18 var $mName, $mPassword, $mRetype, $mReturnto, $mCookieCheck, $mPosted;
19 var $mAction, $mCreateaccount, $mCreateaccountMail, $mMailmypassword;
20 var $mLoginattempt, $mRemember, $mEmail;
22 function LoginForm( &$request ) {
23 global $wgLang, $wgAllowRealName;
25 $this->mName
= $request->getText( 'wpName' );
26 $this->mPassword
= $request->getText( 'wpPassword' );
27 $this->mRetype
= $request->getText( 'wpRetype' );
28 $this->mReturnto
= $request->getVal( 'returnto' );
29 $this->mCookieCheck
= $request->getVal( "wpCookieCheck" );
30 $this->mPosted
= $request->wasPosted();
31 $this->mCreateaccount
= $request->getCheck( 'wpCreateaccount' );
32 $this->mCreateaccountMail
= $request->getCheck( 'wpCreateaccountMail' );
33 $this->mMailmypassword
= $request->getCheck( 'wpMailmypassword' );
34 $this->mLoginattempt
= $request->getCheck( 'wpLoginattempt' );
35 $this->mAction
= $request->getVal( 'action' );
36 $this->mRemember
= $request->getCheck( 'wpRemember' );
37 $this->mEmail
= $request->getText( 'wpEmail' );
38 if ($wgAllowRealName) {
39 $this->mRealName
= $request->getText( 'wpRealName' );
41 $this->mRealName
= '';
44 # When switching accounts, it sucks to get automatically logged out
45 if( $this->mReturnto
== $wgLang->specialPage( "Userlogout" ) ) {
46 $this->mReturnto
= "";
51 if ( !is_null( $this->mCookieCheck
) ) {
52 $this->onCookieRedirectCheck( $this->mCookieCheck
);
53 } else if( $this->mPosted
) {
54 if( $this->mCreateaccount
) {
55 return $this->addNewAccount();
56 } else if ( $this->mCreateaccountMail
) {
57 return $this->addNewAccountMailPassword();
58 } else if ( $this->mMailmypassword
) {
59 return $this->mailPassword();
60 } else if ( ( "submit" == $this->mAction
) ||
$this->mLoginattempt
) {
61 return $this->processLogin();
64 $this->mainLoginForm( "" );
67 /* private */ function addNewAccountMailPassword()
71 if ("" == $this->mEmail
) {
72 $this->mainLoginForm( wfMsg( "noemail", $this->mName
) );
76 $u = $this->addNewaccountInternal();
83 $error = $this->mailPasswordInternal($u);
85 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
86 $wgOut->setRobotpolicy( "noindex,nofollow" );
87 $wgOut->setArticleRelated( false );
89 if ( $error === "" ) {
90 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
91 $wgOut->returnToMain( false );
93 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
100 /* private */ function addNewAccount()
102 global $wgUser, $wgOut;
103 global $wgDeferredUpdateList;
105 $u = $this->addNewAccountInternal();
112 $wgUser->setCookies();
114 $up = new UserUpdate();
115 array_push( $wgDeferredUpdateList, $up );
117 if( $this->hasSessionCookie() ) {
118 return $this->successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
120 return $this->cookieRedirectCheck( "new" );
125 /* private */ function addNewAccountInternal()
127 global $wgUser, $wgOut;
128 global $wgMaxNameChars;
129 global $wgMemc, $wgAccountCreationThrottle, $wgDBname, $wgIP;
131 if ( $wgAccountCreationThrottle ) {
132 $key = "$wgDBname:acctcreate:ip:$wgIP";
133 $value = $wgMemc->incr( $key );
135 $wgMemc->set( $key, 1, 86400 );
137 if ( $value > $wgAccountCreationThrottle ) {
138 $this->throttleHit( $wgAccountCreationThrottle );
143 if (!$wgUser->isAllowedToCreateAccount()) {
144 $this->userNotPrivilegedMessage();
148 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
149 $this->mainLoginForm( wfMsg( "badretype" ) );
153 $name = trim( $this->mName
);
154 if ( ( "" == $name ) ||
155 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
156 (strpos( $name, "/" ) !== false) ||
157 (strlen( $name ) > $wgMaxNameChars) )
159 $this->mainLoginForm( wfMsg( "noname" ) );
162 if ( wfReadOnly() ) {
163 $wgOut->readOnlyPage();
166 $u = User
::newFromName( $name );
168 if ( 0 != $u->idForName() ) {
169 $this->mainLoginForm( wfMsg( "userexists" ) );
173 $u->setPassword( $this->mPassword
);
174 $u->setEmail( $this->mEmail
);
175 $u->setRealName( $this->mRealName
);
177 if ( $this->mRemember
) { $r = 1; }
179 $u->setOption( "rememberpassword", $r );
186 /* private */ function processLogin()
189 global $wgDeferredUpdateList;
191 if ( "" == $this->mName
) {
192 $this->mainLoginForm( wfMsg( "noname" ) );
195 $u = User
::newFromName( $this->mName
);
196 $id = $u->idForName();
198 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
202 $u->loadFromDatabase();
203 $ep = $u->encryptPassword( $this->mPassword
);
204 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
205 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
206 $this->mainLoginForm( wfMsg( "wrongpassword" ) );
211 # We've verified now, update the real record
213 if ( $this->mRemember
) {
215 $u->setCookiePassword( $this->mPassword
);
219 $u->setOption( "rememberpassword", $r );
222 $wgUser->setCookies();
224 $up = new UserUpdate();
225 array_push( $wgDeferredUpdateList, $up );
227 if( $this->hasSessionCookie() ) {
228 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
230 return $this->cookieRedirectCheck( "login" );
234 /* private */ function mailPassword()
236 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
237 global $wgCookiePath, $wgCookieDomain, $wgDBname;
239 if ( "" == $this->mName
) {
240 $this->mainLoginForm( wfMsg( "noname" ) );
243 $u = User
::newFromName( $this->mName
);
244 $id = $u->idForName();
246 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
250 $u->loadFromDatabase();
252 $error = $this->mailPasswordInternal( $u );
254 $this->mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
256 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
262 /* private */ function mailPasswordInternal( $u )
264 global $wgDeferredUpdateList, $wgOutputEncoding;
265 global $wgPasswordSender, $wgDBname, $wgIP;
266 global $wgCookiePath, $wgCookieDomain;
268 if ( "" == $u->getEmail() ) {
269 $this->mainLoginForm( wfMsg( "noemail", $u->getName() ) );
272 $np = User
::randomPassword();
273 $u->setNewpassword( $np );
275 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
279 if ( "" == $ip ) { $ip = "(Unknown)"; }
281 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
283 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
292 /* private */ function successfulLogin( $msg )
295 global $wgDeferredUpdateList;
298 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
299 $wgOut->setRobotpolicy( "noindex,nofollow" );
300 $wgOut->setArticleRelated( false );
301 $wgOut->addHTML( $msg );
302 $wgOut->returnToMain();
305 function userNotPrivilegedMessage()
307 global $wgOut, $wgUser, $wgLang;
309 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
310 $wgOut->setRobotpolicy( "noindex,nofollow" );
311 $wgOut->setArticleRelated( false );
313 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
315 $wgOut->returnToMain( false );
318 /* private */ function mainLoginForm( $err )
320 global $wgUser, $wgOut, $wgLang;
321 global $wgDBname, $wgAllowRealName;
323 $le = wfMsg( "loginerror" );
324 $yn = wfMsg( "yourname" );
325 $yp = wfMsg( "yourpassword" );
326 $ypa = wfMsg( "yourpasswordagain" );
327 $rmp = wfMsg( "remembermypassword" );
328 $nuo = wfMsg( "newusersonly" );
329 $li = wfMsg( "login" );
330 $ca = wfMsg( "createaccount" );
331 $cam = wfMsg( "createaccountmail" );
332 $ye = wfMsg( "youremail" );
333 if ($wgAllowRealName) {
334 $yrn = wfMsg( "yourrealname" );
338 $efl = wfMsg( "emailforlost" );
339 $mmp = wfMsg( "mailmypassword" );
340 $endText = wfMsg( "loginend" );
342 if ( $endText = "<loginend>" ) {
346 if ( "" == $this->mName
) {
347 if ( 0 != $wgUser->getID() ) {
348 $this->mName
= $wgUser->getName();
350 $this->mName
= @$_COOKIE["{$wgDBname}UserName"];
354 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
355 $wgOut->setRobotpolicy( "noindex,nofollow" );
356 $wgOut->setArticleRelated( false );
359 $lp = wfMsg( "loginprompt" );
360 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
362 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
363 color='red'>$err</font>\n" );
365 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
366 $checked = " checked";
371 $q = "action=submit";
372 if ( !empty( $this->mReturnto
) ) {
373 $q .= "&returnto=" . wfUrlencode( $this->mReturnto
);
376 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
377 $action = $titleObj->escapeLocalUrl( $q );
379 $encName = wfEscapeHTML( $this->mName
);
380 $encPassword = wfEscapeHTML( $this->mPassword
);
381 $encRetype = wfEscapeHTML( $this->mRetype
);
382 $encEmail = wfEscapeHTML( $this->mEmail
);
383 $encRealName = wfEscapeHTML( $this->mRealName
);
385 if ($wgUser->getID() != 0) {
386 $cambutton = "<input tabindex='6' type='submit' name=\"wpCreateaccountMail\" value=\"{$cam}\" />";
392 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
393 <table border='0'><tr>
394 <td align='right'>$yn:</td>
396 <input tabindex='1' type='text' name=\"wpName\" value=\"{$encName}\" size='20' />
399 <input tabindex='3' type='submit' name=\"wpLoginattempt\" value=\"{$li}\" />
403 <td align='right'>$yp:</td>
405 <input tabindex='2' type='password' name=\"wpPassword\" value=\"{$encPassword}\" size='20' />
408 <input tabindex='7' type='checkbox' name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked /><label for=\"wpRemember\">$rmp</label>
412 if ($wgUser->isAllowedToCreateAccount()) {
413 $encRetype = htmlspecialchars( $this->mRetype
);
414 $encEmail = htmlspecialchars( $this->mEmail
);
415 $wgOut->addHTML("<tr><td colspan='3'> </td></tr><tr>
416 <td align='right'>$ypa:</td>
418 <input tabindex='4' type='password' name=\"wpRetype\" value=\"{$encRetype}\"
420 </td><td>$nuo</td></tr>
422 <td align='right'>$ye:</td>
424 <input tabindex='6' type='text' name=\"wpEmail\" value=\"{$encEmail}\" size='20' />
427 if ($wgAllowRealName) {
428 $wgOut->addHTML("<td> </td>
430 <td align='right'>$yrn:</td>
432 <input tabindex='6' type='text' name=\"wpRealName\" value=\"{$encRealName}\" size='20' />
436 $wgOut->addHTML("<td align='left'>
437 <input tabindex='7' type='submit' name=\"wpCreateaccount\" value=\"{$ca}\" />
443 <tr><td colspan='3'> </td></tr><tr>
444 <td colspan='3' align='left'>
446 <input tabindex='8' type='submit' name=\"wpMailmypassword\" value=\"{$mmp}\" /></p>
449 $wgOut->addHTML( $endText );
452 /* private */ function hasSessionCookie()
454 global $wgDisableCookieCheck;
455 return ( $wgDisableCookieCheck ) ?
true : ( "" != $_COOKIE[session_name()] );
458 /* private */ function cookieRedirectCheck( $type )
460 global $wgOut, $wgLang;
462 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
463 $check = $titleObj->getFullURL( "wpCookieCheck=$type" );
465 return $wgOut->redirect( $check );
468 /* private */ function onCookieRedirectCheck( $type ) {
471 if ( !$this->hasSessionCookie() ) {
472 if ( $type == "new" ) {
473 return $this->mainLoginForm( wfMsg( "nocookiesnew" ) );
474 } else if ( $type == "login" ) {
475 return $this->mainLoginForm( wfMsg( "nocookieslogin" ) );
478 return $this->mainLoginForm( wfMsg( "error" ) );
481 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
485 /* private */ function throttleHit( $limit ) {
488 $wgOut->addWikiText( wfMsg( 'acct_creation_throttle_hit', $limit ) );