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 ) {
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 $this->mRealName
= $request->getText( 'wpRealName' );
40 # When switching accounts, it sucks to get automatically logged out
41 if( $this->mReturnto
== $wgLang->specialPage( "Userlogout" ) ) {
42 $this->mReturnto
= "";
47 if ( !is_null( $this->mCookieCheck
) ) {
48 $this->onCookieRedirectCheck( $this->mCookieCheck
);
49 } else if( $this->mPosted
) {
50 if( $this->mCreateaccount
) {
51 return $this->addNewAccount();
52 } else if ( $this->mCreateaccountMail
) {
53 return $this->addNewAccountMailPassword();
54 } else if ( $this->mMailmypassword
) {
55 return $this->mailPassword();
56 } else if ( ( "submit" == $this->mAction
) ||
$this->mLoginattempt
) {
57 return $this->processLogin();
60 $this->mainLoginForm( "" );
63 /* private */ function addNewAccountMailPassword()
67 if ("" == $this->mEmail
) {
68 $this->mainLoginForm( wfMsg( "noemail", $this->mName
) );
72 $u = $this->addNewaccountInternal();
79 $error = $this->mailPasswordInternal($u);
81 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
82 $wgOut->setRobotpolicy( "noindex,nofollow" );
83 $wgOut->setArticleRelated( false );
85 if ( $error === "" ) {
86 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
87 $wgOut->returnToMain( false );
89 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
96 /* private */ function addNewAccount()
98 global $wgUser, $wgOut;
99 global $wgDeferredUpdateList;
101 $u = $this->addNewAccountInternal();
108 $wgUser->setCookies();
110 $up = new UserUpdate();
111 array_push( $wgDeferredUpdateList, $up );
113 if( $this->hasSessionCookie() ) {
114 return $this->successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
116 return $this->cookieRedirectCheck( "new" );
121 /* private */ function addNewAccountInternal()
123 global $wgUser, $wgOut;
124 global $wgMaxNameChars;
126 if (!$wgUser->isAllowedToCreateAccount()) {
127 $this->userNotPrivilegedMessage();
131 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
132 $this->mainLoginForm( wfMsg( "badretype" ) );
136 $name = trim( $this->mName
);
137 if ( ( "" == $name ) ||
138 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
139 (strpos( $name, "/" ) !== false) ||
140 (strlen( $name ) > $wgMaxNameChars) )
142 $this->mainLoginForm( wfMsg( "noname" ) );
145 if ( wfReadOnly() ) {
146 $wgOut->readOnlyPage();
149 $u = User
::newFromName( $name );
151 if ( 0 != $u->idForName() ) {
152 $this->mainLoginForm( wfMsg( "userexists" ) );
156 $u->setPassword( $this->mPassword
);
157 $u->setEmail( $this->mEmail
);
158 $u->setRealName( $this->mRealName
);
160 if ( $this->mRemember
) { $r = 1; }
162 $u->setOption( "rememberpassword", $r );
169 /* private */ function processLogin()
172 global $wgDeferredUpdateList;
174 if ( "" == $this->mName
) {
175 $this->mainLoginForm( wfMsg( "noname" ) );
178 $u = User
::newFromName( $this->mName
);
179 $id = $u->idForName();
181 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
185 $u->loadFromDatabase();
186 $ep = $u->encryptPassword( $this->mPassword
);
187 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
188 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
189 $this->mainLoginForm( wfMsg( "wrongpassword" ) );
194 # We've verified now, update the real record
196 if ( $this->mRemember
) {
198 $u->setCookiePassword( $this->mPassword
);
202 $u->setOption( "rememberpassword", $r );
205 $wgUser->setCookies();
207 $up = new UserUpdate();
208 array_push( $wgDeferredUpdateList, $up );
210 if( $this->hasSessionCookie() ) {
211 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
213 return $this->cookieRedirectCheck( "login" );
217 /* private */ function mailPassword()
219 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
220 global $wgCookiePath, $wgCookieDomain, $wgDBname;
222 if ( "" == $this->mName
) {
223 $this->mainLoginForm( wfMsg( "noname" ) );
226 $u = User
::newFromName( $this->mName
);
227 $id = $u->idForName();
229 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
233 $u->loadFromDatabase();
235 $error = $this->mailPasswordInternal( $u );
237 $this->mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
239 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
245 /* private */ function mailPasswordInternal( $u )
247 global $wgDeferredUpdateList, $wgOutputEncoding;
248 global $wgPasswordSender, $wgDBname, $wgIP;
249 global $wgCookiePath, $wgCookieDomain;
251 if ( "" == $u->getEmail() ) {
252 $this->mainLoginForm( wfMsg( "noemail", $u->getName() ) );
255 $np = User
::randomPassword();
256 $u->setNewpassword( $np );
258 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
262 if ( "" == $ip ) { $ip = "(Unknown)"; }
264 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
266 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
275 /* private */ function successfulLogin( $msg )
278 global $wgDeferredUpdateList;
281 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
282 $wgOut->setRobotpolicy( "noindex,nofollow" );
283 $wgOut->setArticleRelated( false );
284 $wgOut->addHTML( $msg );
285 $wgOut->returnToMain();
288 function userNotPrivilegedMessage()
290 global $wgOut, $wgUser, $wgLang;
292 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
293 $wgOut->setRobotpolicy( "noindex,nofollow" );
294 $wgOut->setArticleRelated( false );
296 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
298 $wgOut->returnToMain( false );
301 /* private */ function mainLoginForm( $err )
303 global $wgUser, $wgOut, $wgLang;
306 $le = wfMsg( "loginerror" );
307 $yn = wfMsg( "yourname" );
308 $yp = wfMsg( "yourpassword" );
309 $ypa = wfMsg( "yourpasswordagain" );
310 $rmp = wfMsg( "remembermypassword" );
311 $nuo = wfMsg( "newusersonly" );
312 $li = wfMsg( "login" );
313 $ca = wfMsg( "createaccount" );
314 $cam = wfMsg( "createaccountmail" );
315 $ye = wfMsg( "youremail" );
316 $yrn = wfMsg( "yourrealname" );
317 $efl = wfMsg( "emailforlost" );
318 $mmp = wfMsg( "mailmypassword" );
319 $endText = wfMsg( "loginend" );
321 if ( $endText = "<loginend>" ) {
325 if ( "" == $this->mName
) {
326 if ( 0 != $wgUser->getID() ) {
327 $this->mName
= $wgUser->getName();
329 $this->mName
= @$_COOKIE["{$wgDBname}UserName"];
333 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
334 $wgOut->setRobotpolicy( "noindex,nofollow" );
335 $wgOut->setArticleRelated( false );
338 $lp = wfMsg( "loginprompt" );
339 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
341 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
342 color='red'>$err</font>\n" );
344 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
345 $checked = " checked";
350 $q = "action=submit";
351 if ( !empty( $this->mReturnto
) ) {
352 $q .= "&returnto=" . wfUrlencode( $this->mReturnto
);
355 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
356 $action = $titleObj->escapeLocalUrl( $q );
358 $encName = wfEscapeHTML( $this->mName
);
359 $encPassword = wfEscapeHTML( $this->mPassword
);
360 $encRetype = wfEscapeHTML( $this->mRetype
);
361 $encEmail = wfEscapeHTML( $this->mEmail
);
362 $encRealName = wfEscapeHTML( $this->mRealName
);
364 if ($wgUser->getID() != 0) {
365 $cambutton = "<input tabindex='6' type='submit' name=\"wpCreateaccountMail\" value=\"{$cam}\" />";
371 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
372 <table border='0'><tr>
373 <td align='right'>$yn:</td>
375 <input tabindex='1' type='text' name=\"wpName\" value=\"{$encName}\" size='20' />
378 <input tabindex='3' type='submit' name=\"wpLoginattempt\" value=\"{$li}\" />
382 <td align='right'>$yp:</td>
384 <input tabindex='2' type='password' name=\"wpPassword\" value=\"{$encPassword}\" size='20' />
387 <input tabindex='7' type='checkbox' name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked /><label for=\"wpRemember\">$rmp</label>
391 if ($wgUser->isAllowedToCreateAccount()) {
392 $encRetype = htmlspecialchars( $this->mRetype
);
393 $encEmail = htmlspecialchars( $this->mEmail
);
394 $wgOut->addHTML("<tr><td colspan='3'> </td></tr><tr>
395 <td align='right'>$ypa:</td>
397 <input tabindex='4' type='password' name=\"wpRetype\" value=\"{$encRetype}\"
399 </td><td>$nuo</td></tr>
401 <td align='right'>$ye:</td>
403 <input tabindex='6' type='text' name=\"wpEmail\" value=\"{$encEmail}\" size='20' />
408 <td align='right'>$yrn:</td>
410 <input tabindex='6' type='text' name=\"wpRealName\" value=\"{$encRealName}\" size='20' />
413 <input tabindex='7' type='submit' name=\"wpCreateaccount\" value=\"{$ca}\" />
419 <tr><td colspan='3'> </td></tr><tr>
420 <td colspan='3' align='left'>
422 <input tabindex='8' type='submit' name=\"wpMailmypassword\" value=\"{$mmp}\" /></p>
425 $wgOut->addHTML( $endText );
428 /* private */ function hasSessionCookie()
430 global $wgDisableCookieCheck;
431 return ( $wgDisableCookieCheck ) ?
true : ( "" != $_COOKIE[session_name()] );
434 /* private */ function cookieRedirectCheck( $type )
436 global $wgOut, $wgLang;
438 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
439 $check = $titleObj->getFullURL( "wpCookieCheck=$type" );
441 return $wgOut->redirect( $check );
444 /* private */ function onCookieRedirectCheck( $type ) {
447 if ( !$this->hasSessionCookie() ) {
448 if ( $type == "new" ) {
449 return $this->mainLoginForm( wfMsg( "nocookiesnew" ) );
450 } else if ( $type == "login" ) {
451 return $this->mainLoginForm( wfMsg( "nocookieslogin" ) );
454 return $this->mainLoginForm( wfMsg( "error" ) );
457 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );