3 require_once('UserMailer.php');
5 function wfSpecialUserlogin()
7 global $wpCreateaccount, $wpCreateaccountMail;
8 global $wpLoginattempt, $wpMailmypassword;
9 global $action, $_REQUEST, $wgCommandLineMode;
10 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
14 $fields = array( "wpName", "wpPassword", "wpName",
15 "wpPassword", "wpRetype", "wpEmail" );
16 wfCleanFormFields( $fields );
18 # When switching accounts, it sucks to get automatically logged out
19 global $returnto, $wgLang;
20 if( $returnto == $wgLang->specialPage( "Userlogout" ) ) $returnto = "";
22 $wpCookieCheck = $_REQUEST[ "wpCookieCheck" ];
24 if ( isset( $wpCookieCheck ) ) {
25 onCookieRedirectCheck( $wpCookieCheck );
26 } else if ( isset( $wpCreateaccount ) ) {
28 } else if ( isset( $wpCreateaccountMail ) ) {
29 addNewAccountMailPassword();
30 } else if ( isset( $wpMailmypassword ) ) {
32 } else if ( "submit" == $action ||
isset( $wpLoginattempt ) ) {
40 /* private */ function addNewAccountMailPassword()
42 global $wgOut, $wpEmail, $wpName;
45 mainLoginForm( wfMsg( "noemail", $wpName ) );
49 $u = addNewaccountInternal();
56 if (mailPasswordInternal($u) == NULL) {
60 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
61 $wgOut->setRobotpolicy( "noindex,nofollow" );
62 $wgOut->setArticleRelated( false );
64 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
65 $wgOut->returnToMain( false );
71 /* private */ function addNewAccount()
73 global $wgUser, $wgOut, $wpPassword, $wpRetype, $wpName, $wpRemember;
74 global $wpEmail, $wgDeferredUpdateList;
76 $u = addNewAccountInternal();
83 $wgUser->setCookies();
85 $up = new UserUpdate();
86 array_push( $wgDeferredUpdateList, $up );
88 if( hasSessionCookie() ) {
89 return successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
91 return cookieRedirectCheck( "new" );
96 /* private */ function addNewAccountInternal()
98 global $wgUser, $wgOut, $wpPassword, $wpRetype, $wpName, $wpRemember;
99 global $wpEmail, $wgMaxNameChars;
101 if (!$wgUser->isAllowedToCreateAccount()) {
102 userNotPrivilegedMessage();
106 if ( 0 != strcmp( $wpPassword, $wpRetype ) ) {
107 mainLoginForm( wfMsg( "badretype" ) );
110 $wpName = trim( $wpName );
111 if ( ( "" == $wpName ) ||
112 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $wpName ) ||
113 (strpos( $wpName, "/" ) !== false) ||
114 (strlen( $wpName ) > $wgMaxNameChars) )
116 mainLoginForm( wfMsg( "noname" ) );
119 if ( wfReadOnly() ) {
120 $wgOut->readOnlyPage();
123 $u = User
::newFromName( $wpName );
125 if ( 0 != $u->idForName() ) {
126 mainLoginForm( wfMsg( "userexists" ) );
130 $u->setPassword( $wpPassword );
131 $u->setEmail( $wpEmail );
132 if ( 1 == $wpRemember ) { $r = 1; }
134 $u->setOption( "rememberpassword", $r );
142 /* private */ function processLogin()
144 global $wgUser, $wpName, $wpPassword, $wpRemember;
145 global $wgDeferredUpdateList;
148 if ( "" == $wpName ) {
149 mainLoginForm( wfMsg( "noname" ) );
152 $u = User
::newFromName( $wpName );
153 $id = $u->idForName();
155 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
159 $u->loadFromDatabase();
160 $ep = $u->encryptPassword( $wpPassword );
161 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
162 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
163 mainLoginForm( wfMsg( "wrongpassword" ) );
168 # We've verified now, update the real record
170 if ( 1 == $wpRemember ) {
172 $u->setCookiePassword( $wpPassword );
176 $u->setOption( "rememberpassword", $r );
179 $wgUser->setCookies();
181 $up = new UserUpdate();
182 array_push( $wgDeferredUpdateList, $up );
184 if( hasSessionCookie() ) {
185 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
187 return cookieRedirectCheck( "login" );
191 /* private */ function mailPassword()
193 global $wgUser, $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
194 global $wgCookiePath, $wgCookieDomain, $wgDBname;
196 if ( "" == $wpName ) {
197 mainLoginForm( wfMsg( "noname" ) );
200 $u = User
::newFromName( $wpName );
201 $id = $u->idForName();
203 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
207 $u->loadFromDatabase();
209 if (mailPasswordInternal($u) == NULL) {
213 mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
217 /* private */ function mailPasswordInternal( $u )
219 global $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
220 global $wgPasswordSender, $wgDBname, $wgIP;
222 if ( "" == $u->getEmail() ) {
223 mainLoginForm( wfMsg( "noemail", $u->getName() ) );
226 $np = User
::randomPassword();
227 $u->setNewpassword( $np );
229 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
233 if ( "" == $ip ) { $ip = "(Unknown)"; }
235 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
237 userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
246 /* private */ function successfulLogin( $msg )
249 global $wgDeferredUpdateList;
252 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
253 $wgOut->setRobotpolicy( "noindex,nofollow" );
254 $wgOut->setArticleRelated( false );
255 $wgOut->addHTML( $msg . "\n<p>" );
256 $wgOut->returnToMain();
259 function userNotPrivilegedMessage()
261 global $wgOut, $wgUser, $wgLang;
263 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
264 $wgOut->setRobotpolicy( "noindex,nofollow" );
265 $wgOut->setArticleRelated( false );
267 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
269 $wgOut->returnToMain( false );
272 /* private */ function mainLoginForm( $err )
274 global $wgUser, $wgOut, $wgLang, $returnto;
275 global $wpName, $wpPassword, $wpRetype, $wpRemember;
276 global $wpEmail, $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" );
295 if ( 0 != $wgUser->getID() ) {
296 $name = $wgUser->getName();
298 $name = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
303 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
304 $wgOut->setRobotpolicy( "noindex,nofollow" );
305 $wgOut->setArticleRelated( false );
308 $lp = wfMsg( "loginprompt" );
309 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
311 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
312 color='red'>$err</font>\n" );
314 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
315 $checked = " checked";
319 $q = "action=submit";
320 if ( "" != $returnto ) { $q .= "&returnto=" . wfUrlencode($returnto); }
321 $action = wfLocalUrlE( $wgLang->specialPage( "Userlogin" ), $q );
323 $wpName = wfEscapeHTML( $wpName );
324 $wpPassword = wfEscapeHTML( $wpPassword );
325 $wpRetype = wfEscapeHTML( $wpRetype );
326 $wpEmail = wfEscapeHTML( $wpEmail );
328 if ($wgUser->getID() != 0) {
329 $cambutton = "<input tabindex=6 type=submit name=\"wpCreateaccountMail\" value=\"{$cam}\">";
333 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
335 <td align=right>$yn:</td>
337 <input tabindex=1 type=text name=\"wpName\" value=\"{$name}\" size=20>
340 <input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
344 <td align=right>$yp:</td>
346 <input tabindex=2 type=password name=\"wpPassword\" value=\"{$pwd}\" size=20>
349 <input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked><label for=\"wpRemember\">$rmp</label>
353 if ($wgUser->isAllowedToCreateAccount()) {
355 $wgOut->addHTML("<tr><td colspan=3> </td></tr><tr>
356 <td align=right>$ypa:</td>
358 <input tabindex=4 type=password name=\"wpRetype\" value=\"{$wpRetype}\"
360 </td><td>$nuo</td></tr>
362 <td align=right>$ye:</td>
364 <input tabindex=5 type=text name=\"wpEmail\" value=\"{$wpEmail}\" size=20>
366 <input tabindex=6 type=submit name=\"wpCreateaccount\" value=\"{$ca}\">
372 <tr><td colspan=3> </td></tr><tr>
373 <td colspan=3 align=left>
375 <input tabindex=8 type=submit name=\"wpMailmypassword\" value=\"{$mmp}\">
378 $wgOut->addHTML( $endText );
381 /* private */ function hasSessionCookie()
383 global $wgDisableCookieCheck;
384 return ( $wgDisableCookieCheck ) ?
true : ( "" != $_COOKIE[session_name()] );
387 /* private */ function cookieRedirectCheck( $type )
389 global $wgOut, $wgLang;
391 $check = wfLocalUrl( wfUrlEncode( $wgLang->specialPage( "Userlogin" ) ),
392 "wpCookieCheck=$type" );
394 return $wgOut->redirect( $check );
397 /* private */ function onCookieRedirectCheck( $type ) {
400 if ( !hasSessionCookie() ) {
401 if ( $type == "new" ) {
402 return mainLoginForm( wfMsg( "nocookiesnew" ) );
403 } else if ( $type == "login" ) {
404 return mainLoginForm( wfMsg( "nocookieslogin" ) );
407 return mainLoginForm( wfMsg( "error" ) );
410 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );