3 require_once('UserMailer.php');
5 function wfSpecialUserlogin()
7 global $wgCommandLineMode;
8 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
12 $fields = array( "wpName", "wpPassword", "wpName",
13 "wpPassword", "wpRetype" );
15 foreach( $fields as $x ) {
16 $_REQUEST[$x] = $wgRequest->getText( $x );
19 # When switching accounts, it sucks to get automatically logged out
21 if( $_REQUEST['returnto'] == $wgLang->specialPage( "Userlogout" ) ) $_REQUEST['returnto'] = "";
23 $wpCookieCheck = $_REQUEST[ "wpCookieCheck" ];
25 if ( isset( $wpCookieCheck ) ) {
26 onCookieRedirectCheck( $wpCookieCheck );
27 } else if ( isset( $_REQUEST['wpCreateaccount'] ) ) {
29 } else if ( isset( $_REQUEST['wpCreateaccountMail'] ) ) {
30 addNewAccountMailPassword();
31 } else if ( isset( $_REQUEST['wpMailmypassword'] ) ) {
33 } else if ( "submit" == $_REQUEST['action'] ||
array_key_exists('wpLoginattempt', $_REQUEST) ) {
41 /* private */ function addNewAccountMailPassword()
45 if ("" == $_REQUEST['wpEmail']) {
46 mainLoginForm( wfMsg( "noemail", $_REQUEST['wpName'] ) );
50 $u = addNewaccountInternal();
57 if (mailPasswordInternal($u) == NULL) {
61 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
62 $wgOut->setRobotpolicy( "noindex,nofollow" );
63 $wgOut->setArticleRelated( false );
65 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
66 $wgOut->returnToMain( false );
72 /* private */ function addNewAccount()
74 global $wgUser, $wgOut;
75 global $wgDeferredUpdateList;
77 $u = addNewAccountInternal();
84 $wgUser->setCookies();
86 $up = new UserUpdate();
87 array_push( $wgDeferredUpdateList, $up );
89 if( hasSessionCookie() ) {
90 return successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
92 return cookieRedirectCheck( "new" );
97 /* private */ function addNewAccountInternal()
99 global $wgUser, $wgOut;
100 global $wgMaxNameChars;
102 if (!$wgUser->isAllowedToCreateAccount()) {
103 userNotPrivilegedMessage();
107 if ( 0 != strcmp( $_REQUEST['wpPassword'], $_REQUEST['wpRetype'] ) ) {
108 mainLoginForm( wfMsg( "badretype" ) );
112 $name = trim( $_REQUEST['wpName'] );
113 if ( ( "" == $name ) ||
114 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
115 (strpos( $name, "/" ) !== false) ||
116 (strlen( $name ) > $wgMaxNameChars) )
118 mainLoginForm( wfMsg( "noname" ) );
121 if ( wfReadOnly() ) {
122 $wgOut->readOnlyPage();
125 $u = User
::newFromName( $name );
127 if ( 0 != $u->idForName() ) {
128 mainLoginForm( wfMsg( "userexists" ) );
132 $u->setPassword( $_REQUEST['wpPassword'] );
133 $u->setEmail( $_REQUEST['wpEmail'] );
134 if ( 1 == $_REQUEST['wpRemember'] ) { $r = 1; }
136 $u->setOption( "rememberpassword", $r );
144 /* private */ function processLogin()
147 global $wgDeferredUpdateList;
149 if ( "" == $_REQUEST['wpName'] ) {
150 mainLoginForm( wfMsg( "noname" ) );
153 $u = User
::newFromName( $_REQUEST['wpName'] );
154 $id = $u->idForName();
156 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
160 $u->loadFromDatabase();
161 $ep = $u->encryptPassword( $_REQUEST['wpPassword'] );
162 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
163 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
164 mainLoginForm( wfMsg( "wrongpassword" ) );
169 # We've verified now, update the real record
171 if ( 1 == $_REQUEST['wpRemember'] ) {
173 $u->setCookiePassword( $_REQUEST['wpPassword'] );
177 $u->setOption( "rememberpassword", $r );
180 $wgUser->setCookies();
182 $up = new UserUpdate();
183 array_push( $wgDeferredUpdateList, $up );
185 if( hasSessionCookie() ) {
186 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
188 return cookieRedirectCheck( "login" );
192 /* private */ function mailPassword()
194 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
195 global $wgCookiePath, $wgCookieDomain, $wgDBname;
197 if ( "" == $_REQUEST['wpName'] ) {
198 mainLoginForm( wfMsg( "noname" ) );
201 $u = User
::newFromName( $_REQUEST['wpName'] );
202 $id = $u->idForName();
204 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
208 $u->loadFromDatabase();
210 if (mailPasswordInternal($u) == NULL) {
214 mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
218 /* private */ function mailPasswordInternal( $u )
220 global $wgDeferredUpdateList, $wgOutputEncoding;
221 global $wgPasswordSender, $wgDBname, $wgIP;
223 if ( "" == $u->getEmail() ) {
224 mainLoginForm( wfMsg( "noemail", $u->getName() ) );
227 $np = User
::randomPassword();
228 $u->setNewpassword( $np );
230 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
234 if ( "" == $ip ) { $ip = "(Unknown)"; }
236 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
238 userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
247 /* private */ function successfulLogin( $msg )
250 global $wgDeferredUpdateList;
253 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
254 $wgOut->setRobotpolicy( "noindex,nofollow" );
255 $wgOut->setArticleRelated( false );
256 $wgOut->addHTML( $msg . "\n<p>" );
257 $wgOut->returnToMain();
260 function userNotPrivilegedMessage()
262 global $wgOut, $wgUser, $wgLang;
264 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
265 $wgOut->setRobotpolicy( "noindex,nofollow" );
266 $wgOut->setArticleRelated( false );
268 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
270 $wgOut->returnToMain( false );
273 /* private */ function mainLoginForm( $err )
275 global $wgUser, $wgOut, $wgLang;
276 global $HTTP_COOKIE_VARS, $wgDBname;
278 $le = wfMsg( "loginerror" );
279 $yn = wfMsg( "yourname" );
280 $yp = wfMsg( "yourpassword" );
281 $ypa = wfMsg( "yourpasswordagain" );
282 $rmp = wfMsg( "remembermypassword" );
283 $nuo = wfMsg( "newusersonly" );
284 $li = wfMsg( "login" );
285 $ca = wfMsg( "createaccount" );
286 $cam = wfMsg( "createaccountmail" );
287 $ye = wfMsg( "youremail" );
288 $efl = wfMsg( "emailforlost" );
289 $mmp = wfMsg( "mailmypassword" );
290 $endText = wfMsg( "loginend" );
292 if ( $endText = "<loginend>" ) {
296 $name = $_REQUEST['wpName'];
298 if ( 0 != $wgUser->getID() ) {
299 $name = $wgUser->getName();
301 $name = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
304 $pwd = $_REQUEST['wpPassword'];
306 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
307 $wgOut->setRobotpolicy( "noindex,nofollow" );
308 $wgOut->setArticleRelated( false );
311 $lp = wfMsg( "loginprompt" );
312 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
314 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
315 color='red'>$err</font>\n" );
317 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
318 $checked = " checked";
322 $q = "action=submit";
323 if ( "" != $_REQUEST['returnto'] ) { $q .= "&returnto=" . wfUrlencode($_REQUEST['returnto']); }
324 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
325 $action = $titleObj->escapeLocalUrl( $q );
327 $encName = wfEscapeHTML( $name );
328 $encPassword = wfEscapeHTML( $pwd );
329 $encRetype = wfEscapeHTML( $_REQUEST['wpRetype'] );
330 $encEmail = wfEscapeHTML( $_REQUEST['wpEmail'] );
332 if ($wgUser->getID() != 0) {
333 $cambutton = "<input tabindex=6 type=submit name=\"wpCreateaccountMail\" value=\"{$cam}\">";
337 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
339 <td align=right>$yn:</td>
341 <input tabindex=1 type=text name=\"wpName\" value=\"{$encName}\" size=20>
344 <input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
348 <td align=right>$yp:</td>
350 <input tabindex=2 type=password name=\"wpPassword\" value=\"{$encPassword}\" size=20>
353 <input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked><label for=\"wpRemember\">$rmp</label>
357 if ($wgUser->isAllowedToCreateAccount()) {
358 $encRetype = htmlspecialchars( $_REQUEST['wpRetype'] );
359 $encEmail = htmlspecialchars( $_REQUEST['wpCreateAccount'] );
360 $wgOut->addHTML("<tr><td colspan=3> </td></tr><tr>
361 <td align=right>$ypa:</td>
363 <input tabindex=4 type=password name=\"wpRetype\" value=\"{$encRetype}\"
365 </td><td>$nuo</td></tr>
367 <td align=right>$ye:</td>
369 <input tabindex=5 type=text name=\"wpEmail\" value=\"{$encEmail}\" size=20>
371 <input tabindex=6 type=submit name=\"wpCreateaccount\" value=\"{$ca}\">
377 <tr><td colspan=3> </td></tr><tr>
378 <td colspan=3 align=left>
380 <input tabindex=8 type=submit name=\"wpMailmypassword\" value=\"{$mmp}\">
383 $wgOut->addHTML( $endText );
386 /* private */ function hasSessionCookie()
388 global $wgDisableCookieCheck;
389 return ( $wgDisableCookieCheck ) ?
true : ( "" != $_COOKIE[session_name()] );
392 /* private */ function cookieRedirectCheck( $type )
394 global $wgOut, $wgLang;
396 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Userlogin" );
397 $check = $titleObj->getFullURL( "wpCookieCheck=$type" );
399 return $wgOut->redirect( $check );
402 /* private */ function onCookieRedirectCheck( $type ) {
405 if ( !hasSessionCookie() ) {
406 if ( $type == "new" ) {
407 return mainLoginForm( wfMsg( "nocookiesnew" ) );
408 } else if ( $type == "login" ) {
409 return mainLoginForm( wfMsg( "nocookieslogin" ) );
412 return mainLoginForm( wfMsg( "error" ) );
415 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );