3 require_once('UserMailer.php');
5 function wfSpecialUserlogin()
7 global $wgCommandLineMode;
9 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
13 $fields = array( "wpName", "wpPassword", "wpName",
14 "wpPassword", "wpRetype" );
16 foreach( $fields as $x ) {
17 $_REQUEST[$x] = $wgRequest->getText( $x );
20 # When switching accounts, it sucks to get automatically logged out
22 if( $wgRequest->getVal( 'returnto' ) == $wgLang->specialPage( "Userlogout" ) ) {
23 $_REQUEST['returnto'] = "";
26 $wpCookieCheck = $wgRequest->getVal( "wpCookieCheck" );
28 if ( isset( $wpCookieCheck ) ) {
29 onCookieRedirectCheck( $wpCookieCheck );
30 } else if( $wgRequest->wasPosted() ) {
31 if( $wgRequest->getCheck( 'wpCreateaccount' ) ) {
32 return addNewAccount();
33 } else if ( $wgRequest->getCheck( 'wpCreateaccountMail' ) ) {
34 return addNewAccountMailPassword();
35 } else if ( $wgRequest->getCheck( 'wpMailmypassword' ) ) {
36 return mailPassword();
37 } else if ( "submit" == $wgRequest->getVal( 'action' ) ||
$wgRequest->getCheck( 'wpLoginattempt' ) ) {
38 return processLogin();
45 /* private */ function addNewAccountMailPassword()
49 if ("" == $_REQUEST['wpEmail']) {
50 mainLoginForm( wfMsg( "noemail", $_REQUEST['wpName'] ) );
54 $u = addNewaccountInternal();
61 if (mailPasswordInternal($u) == NULL) {
65 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
66 $wgOut->setRobotpolicy( "noindex,nofollow" );
67 $wgOut->setArticleRelated( false );
69 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
70 $wgOut->returnToMain( false );
76 /* private */ function addNewAccount()
78 global $wgUser, $wgOut;
79 global $wgDeferredUpdateList;
81 $u = addNewAccountInternal();
88 $wgUser->setCookies();
90 $up = new UserUpdate();
91 array_push( $wgDeferredUpdateList, $up );
93 if( hasSessionCookie() ) {
94 return successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
96 return cookieRedirectCheck( "new" );
101 /* private */ function addNewAccountInternal()
103 global $wgUser, $wgOut;
104 global $wgMaxNameChars;
107 if (!$wgUser->isAllowedToCreateAccount()) {
108 userNotPrivilegedMessage();
112 if ( 0 != strcmp( $_REQUEST['wpPassword'], $_REQUEST['wpRetype'] ) ) {
113 mainLoginForm( wfMsg( "badretype" ) );
117 $name = trim( $_REQUEST['wpName'] );
118 if ( ( "" == $name ) ||
119 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
120 (strpos( $name, "/" ) !== false) ||
121 (strlen( $name ) > $wgMaxNameChars) )
123 mainLoginForm( wfMsg( "noname" ) );
126 if ( wfReadOnly() ) {
127 $wgOut->readOnlyPage();
130 $u = User
::newFromName( $name );
132 if ( 0 != $u->idForName() ) {
133 mainLoginForm( wfMsg( "userexists" ) );
137 $u->setPassword( $_REQUEST['wpPassword'] );
138 $u->setEmail( $_REQUEST['wpEmail'] );
139 if ( $wgRequest->getCheck( 'wpRemember' ) ) { $r = 1; }
141 $u->setOption( "rememberpassword", $r );
149 /* private */ function processLogin()
152 global $wgDeferredUpdateList;
155 if ( "" == $_REQUEST['wpName'] ) {
156 mainLoginForm( wfMsg( "noname" ) );
159 $u = User
::newFromName( $_REQUEST['wpName'] );
160 $id = $u->idForName();
162 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
166 $u->loadFromDatabase();
167 $ep = $u->encryptPassword( $_REQUEST['wpPassword'] );
168 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
169 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
170 mainLoginForm( wfMsg( "wrongpassword" ) );
175 # We've verified now, update the real record
177 if ( $wgRequest->getCheck( 'wpRemember' ) ) {
179 $u->setCookiePassword( $wgRequest->getText( 'wpPassword' ) );
183 $u->setOption( "rememberpassword", $r );
186 $wgUser->setCookies();
188 $up = new UserUpdate();
189 array_push( $wgDeferredUpdateList, $up );
191 if( hasSessionCookie() ) {
192 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
194 return cookieRedirectCheck( "login" );
198 /* private */ function mailPassword()
200 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
201 global $wgCookiePath, $wgCookieDomain, $wgDBname;
203 if ( "" == $_REQUEST['wpName'] ) {
204 mainLoginForm( wfMsg( "noname" ) );
207 $u = User
::newFromName( $_REQUEST['wpName'] );
208 $id = $u->idForName();
210 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
214 $u->loadFromDatabase();
216 if (mailPasswordInternal($u) == NULL) {
220 mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
224 /* private */ function mailPasswordInternal( $u )
226 global $wgDeferredUpdateList, $wgOutputEncoding;
227 global $wgPasswordSender, $wgDBname, $wgIP;
229 if ( "" == $u->getEmail() ) {
230 mainLoginForm( wfMsg( "noemail", $u->getName() ) );
233 $np = User
::randomPassword();
234 $u->setNewpassword( $np );
236 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
240 if ( "" == $ip ) { $ip = "(Unknown)"; }
242 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
244 userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
253 /* private */ function successfulLogin( $msg )
256 global $wgDeferredUpdateList;
259 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
260 $wgOut->setRobotpolicy( "noindex,nofollow" );
261 $wgOut->setArticleRelated( false );
262 $wgOut->addHTML( $msg . "\n<p>" );
263 $wgOut->returnToMain();
266 function userNotPrivilegedMessage()
268 global $wgOut, $wgUser, $wgLang;
270 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
271 $wgOut->setRobotpolicy( "noindex,nofollow" );
272 $wgOut->setArticleRelated( false );
274 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
276 $wgOut->returnToMain( false );
279 /* private */ function mainLoginForm( $err )
281 global $wgUser, $wgOut, $wgLang;
282 global $wgRequest, $wgDBname;
284 $le = wfMsg( "loginerror" );
285 $yn = wfMsg( "yourname" );
286 $yp = wfMsg( "yourpassword" );
287 $ypa = wfMsg( "yourpasswordagain" );
288 $rmp = wfMsg( "remembermypassword" );
289 $nuo = wfMsg( "newusersonly" );
290 $li = wfMsg( "login" );
291 $ca = wfMsg( "createaccount" );
292 $cam = wfMsg( "createaccountmail" );
293 $ye = wfMsg( "youremail" );
294 $efl = wfMsg( "emailforlost" );
295 $mmp = wfMsg( "mailmypassword" );
296 $endText = wfMsg( "loginend" );
298 if ( $endText = "<loginend>" ) {
302 $name = $wgRequest->getText( 'wpName' );
304 if ( 0 != $wgUser->getID() ) {
305 $name = $wgUser->getName();
307 $name = $_COOKIE["{$wgDBname}UserName"];
310 $pwd = $wgRequest->getText( 'wpPassword' );
312 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
313 $wgOut->setRobotpolicy( "noindex,nofollow" );
314 $wgOut->setArticleRelated( false );
317 $lp = wfMsg( "loginprompt" );
318 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
320 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
321 color='red'>$err</font>\n" );
323 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
324 $checked = " checked";
329 $q = "action=submit";
330 $returnto = $wgRequest->getVal( "returnto" );
331 if ( !empty( $returnto ) ) {
332 $q .= "&returnto=" . wfUrlencode( $returnto );
335 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
336 $action = $titleObj->escapeLocalUrl( $q );
338 $encName = wfEscapeHTML( $name );
339 $encPassword = wfEscapeHTML( $pwd );
340 $encRetype = wfEscapeHTML( $wgRequest->getText( 'wpRetype' ) );
341 $encEmail = wfEscapeHTML( $wgRequest->getVal( 'wpEmail' ) );
343 if ($wgUser->getID() != 0) {
344 $cambutton = "<input tabindex=6 type=submit name=\"wpCreateaccountMail\" value=\"{$cam}\">";
350 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
352 <td align=right>$yn:</td>
354 <input tabindex=1 type=text name=\"wpName\" value=\"{$encName}\" size=20>
357 <input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
361 <td align=right>$yp:</td>
363 <input tabindex=2 type=password name=\"wpPassword\" value=\"{$encPassword}\" size=20>
366 <input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked><label for=\"wpRemember\">$rmp</label>
370 if ($wgUser->isAllowedToCreateAccount()) {
371 $encRetype = htmlspecialchars( $wgRequest->getText( 'wpRetype' ) );
372 $encEmail = htmlspecialchars( $wgRequest->getText( 'wpEmail' ) );
373 $wgOut->addHTML("<tr><td colspan=3> </td></tr><tr>
374 <td align=right>$ypa:</td>
376 <input tabindex=4 type=password name=\"wpRetype\" value=\"{$encRetype}\"
378 </td><td>$nuo</td></tr>
380 <td align=right>$ye:</td>
382 <input tabindex=5 type=text name=\"wpEmail\" value=\"{$encEmail}\" size=20>
384 <input tabindex=6 type=submit name=\"wpCreateaccount\" value=\"{$ca}\">
390 <tr><td colspan=3> </td></tr><tr>
391 <td colspan=3 align=left>
393 <input tabindex=8 type=submit name=\"wpMailmypassword\" value=\"{$mmp}\">
396 $wgOut->addHTML( $endText );
399 /* private */ function hasSessionCookie()
401 global $wgDisableCookieCheck;
402 return ( $wgDisableCookieCheck ) ?
true : ( "" != $_COOKIE[session_name()] );
405 /* private */ function cookieRedirectCheck( $type )
407 global $wgOut, $wgLang;
409 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
410 $check = $titleObj->getFullURL( "wpCookieCheck=$type" );
412 return $wgOut->redirect( $check );
415 /* private */ function onCookieRedirectCheck( $type ) {
418 if ( !hasSessionCookie() ) {
419 if ( $type == "new" ) {
420 return mainLoginForm( wfMsg( "nocookiesnew" ) );
421 } else if ( $type == "login" ) {
422 return mainLoginForm( wfMsg( "nocookieslogin" ) );
425 return mainLoginForm( wfMsg( "error" ) );
428 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );