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->getText( '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' );
39 # When switching accounts, it sucks to get automatically logged out
40 if( $this->mReturnto
== $wgLang->specialPage( "Userlogout" ) ) {
41 $this->mReturnto
= "";
46 if ( !is_null( $this->mCookieCheck
) ) {
47 $this->onCookieRedirectCheck( $this->mCookieCheck
);
48 } else if( $this->mPosted
) {
49 if( $this->mCreateaccount
) {
50 return $this->addNewAccount();
51 } else if ( $this->mCreateaccountMail
) {
52 return $this->addNewAccountMailPassword();
53 } else if ( $this->mMailmypassword
) {
54 return $this->mailPassword();
55 } else if ( ( "submit" == $this->mAction
) ||
$this->mLoginattempt
) {
56 return $this->processLogin();
59 $this->mainLoginForm( "" );
62 /* private */ function addNewAccountMailPassword()
66 if ("" == $this->mEmail
) {
67 $this->mainLoginForm( wfMsg( "noemail", $this->mName
) );
71 $u = $this->addNewaccountInternal();
78 $error = $this->mailPasswordInternal($u);
80 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
81 $wgOut->setRobotpolicy( "noindex,nofollow" );
82 $wgOut->setArticleRelated( false );
84 if ( $error === "" ) {
85 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
86 $wgOut->returnToMain( false );
88 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
95 /* private */ function addNewAccount()
97 global $wgUser, $wgOut;
98 global $wgDeferredUpdateList;
100 $u = $this->addNewAccountInternal();
107 $wgUser->setCookies();
109 $up = new UserUpdate();
110 array_push( $wgDeferredUpdateList, $up );
112 if( $this->hasSessionCookie() ) {
113 return $this->successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
115 return $this->cookieRedirectCheck( "new" );
120 /* private */ function addNewAccountInternal()
122 global $wgUser, $wgOut;
123 global $wgMaxNameChars;
125 if (!$wgUser->isAllowedToCreateAccount()) {
126 $this->userNotPrivilegedMessage();
130 if ( 0 != strcmp( $this->mPassword
, $this->mRetype
) ) {
131 $this->mainLoginForm( wfMsg( "badretype" ) );
135 $name = trim( $this->mName
);
136 if ( ( "" == $name ) ||
137 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
138 (strpos( $name, "/" ) !== false) ||
139 (strlen( $name ) > $wgMaxNameChars) )
141 $this->mainLoginForm( wfMsg( "noname" ) );
144 if ( wfReadOnly() ) {
145 $wgOut->readOnlyPage();
148 $u = User
::newFromName( $name );
150 if ( 0 != $u->idForName() ) {
151 $this->mainLoginForm( wfMsg( "userexists" ) );
155 $u->setPassword( $this->mPassword
);
156 $u->setEmail( $this->mEmail
);
157 if ( $this->mRemember
) { $r = 1; }
159 $u->setOption( "rememberpassword", $r );
166 /* private */ function processLogin()
169 global $wgDeferredUpdateList;
171 if ( "" == $this->mName
) {
172 $this->mainLoginForm( wfMsg( "noname" ) );
175 $u = User
::newFromName( $this->mName
);
176 $id = $u->idForName();
178 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
182 $u->loadFromDatabase();
183 $ep = $u->encryptPassword( $this->mPassword
);
184 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
185 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
186 $this->mainLoginForm( wfMsg( "wrongpassword" ) );
191 # We've verified now, update the real record
193 if ( $this->mRemember
) {
195 $u->setCookiePassword( $this->mPassword
);
199 $u->setOption( "rememberpassword", $r );
202 $wgUser->setCookies();
204 $up = new UserUpdate();
205 array_push( $wgDeferredUpdateList, $up );
207 if( $this->hasSessionCookie() ) {
208 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
210 return $this->cookieRedirectCheck( "login" );
214 /* private */ function mailPassword()
216 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
217 global $wgCookiePath, $wgCookieDomain, $wgDBname;
219 if ( "" == $this->mName
) {
220 $this->mainLoginForm( wfMsg( "noname" ) );
223 $u = User
::newFromName( $this->mName
);
224 $id = $u->idForName();
226 $this->mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
230 $u->loadFromDatabase();
232 $error = $this->mailPasswordInternal( $u );
234 $this->mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
236 $this->mainLoginForm( wfMsg( "mailerror", $error ) );
242 /* private */ function mailPasswordInternal( $u )
244 global $wgDeferredUpdateList, $wgOutputEncoding;
245 global $wgPasswordSender, $wgDBname, $wgIP;
246 global $wgCookiePath, $wgCookieDomain;
248 if ( "" == $u->getEmail() ) {
249 $this->mainLoginForm( wfMsg( "noemail", $u->getName() ) );
252 $np = User
::randomPassword();
253 $u->setNewpassword( $np );
255 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
259 if ( "" == $ip ) { $ip = "(Unknown)"; }
261 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
263 $error = userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
272 /* private */ function successfulLogin( $msg )
275 global $wgDeferredUpdateList;
278 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
279 $wgOut->setRobotpolicy( "noindex,nofollow" );
280 $wgOut->setArticleRelated( false );
281 $wgOut->addHTML( $msg );
282 $wgOut->returnToMain();
285 function userNotPrivilegedMessage()
287 global $wgOut, $wgUser, $wgLang;
289 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
290 $wgOut->setRobotpolicy( "noindex,nofollow" );
291 $wgOut->setArticleRelated( false );
293 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
295 $wgOut->returnToMain( false );
298 /* private */ function mainLoginForm( $err )
300 global $wgUser, $wgOut, $wgLang;
303 $le = wfMsg( "loginerror" );
304 $yn = wfMsg( "yourname" );
305 $yp = wfMsg( "yourpassword" );
306 $ypa = wfMsg( "yourpasswordagain" );
307 $rmp = wfMsg( "remembermypassword" );
308 $nuo = wfMsg( "newusersonly" );
309 $li = wfMsg( "login" );
310 $ca = wfMsg( "createaccount" );
311 $cam = wfMsg( "createaccountmail" );
312 $ye = wfMsg( "youremail" );
313 $efl = wfMsg( "emailforlost" );
314 $mmp = wfMsg( "mailmypassword" );
315 $endText = wfMsg( "loginend" );
317 if ( $endText = "<loginend>" ) {
321 if ( "" == $this->mName
) {
322 if ( 0 != $wgUser->getID() ) {
323 $this->mName
= $wgUser->getName();
325 $this->mName
= @$_COOKIE["{$wgDBname}UserName"];
329 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
330 $wgOut->setRobotpolicy( "noindex,nofollow" );
331 $wgOut->setArticleRelated( false );
334 $lp = wfMsg( "loginprompt" );
335 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
337 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
338 color='red'>$err</font>\n" );
340 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
341 $checked = " checked";
346 $q = "action=submit";
347 if ( !empty( $this->mReturnto
) ) {
348 $q .= "&returnto=" . wfUrlencode( $this->mReturnto
);
351 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
352 $action = $titleObj->escapeLocalUrl( $q );
354 $encName = wfEscapeHTML( $this->mName
);
355 $encPassword = wfEscapeHTML( $this->mPassword
);
356 $encRetype = wfEscapeHTML( $this->mRetype
);
357 $encEmail = wfEscapeHTML( $this->mEmail
);
359 if ($wgUser->getID() != 0) {
360 $cambutton = "<input tabindex='6' type='submit' name=\"wpCreateaccountMail\" value=\"{$cam}\" />";
366 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
367 <table border='0'><tr>
368 <td align='right'>$yn:</td>
370 <input tabindex='1' type='text' name=\"wpName\" value=\"{$encName}\" size='20' />
373 <input tabindex='3' type='submit' name=\"wpLoginattempt\" value=\"{$li}\" />
377 <td align='right'>$yp:</td>
379 <input tabindex='2' type='password' name=\"wpPassword\" value=\"{$encPassword}\" size='20' />
382 <input tabindex='7' type='checkbox' name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked /><label for=\"wpRemember\">$rmp</label>
386 if ($wgUser->isAllowedToCreateAccount()) {
387 $encRetype = htmlspecialchars( $this->mRetype
);
388 $encEmail = htmlspecialchars( $this->mEmail
);
389 $wgOut->addHTML("<tr><td colspan='3'> </td></tr><tr>
390 <td align='right'>$ypa:</td>
392 <input tabindex='4' type='password' name=\"wpRetype\" value=\"{$encRetype}\"
394 </td><td>$nuo</td></tr>
396 <td align='right'>$ye:</td>
398 <input tabindex='5' type='text' name=\"wpEmail\" value=\"{$encEmail}\" size='20' />
399 </td><td align='left'>
400 <input tabindex='6' type='submit' name=\"wpCreateaccount\" value=\"{$ca}\" />
406 <tr><td colspan='3'> </td></tr><tr>
407 <td colspan='3' align='left'>
409 <input tabindex='8' type='submit' name=\"wpMailmypassword\" value=\"{$mmp}\" /></p>
412 $wgOut->addHTML( $endText );
415 /* private */ function hasSessionCookie()
417 global $wgDisableCookieCheck;
418 return ( $wgDisableCookieCheck ) ?
true : ( "" != $_COOKIE[session_name()] );
421 /* private */ function cookieRedirectCheck( $type )
423 global $wgOut, $wgLang;
425 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
426 $check = $titleObj->getFullURL( "wpCookieCheck=$type" );
428 return $wgOut->redirect( $check );
431 /* private */ function onCookieRedirectCheck( $type ) {
434 if ( !$this->hasSessionCookie() ) {
435 if ( $type == "new" ) {
436 return $this->mainLoginForm( wfMsg( "nocookiesnew" ) );
437 } else if ( $type == "login" ) {
438 return $this->mainLoginForm( wfMsg( "nocookieslogin" ) );
441 return $this->mainLoginForm( wfMsg( "error" ) );
444 return $this->successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );