Break backwards compatibility: Make image links parse for extended syntax even if...
[mediawiki.git] / includes / SpecialUserlogin.php
blob186521b9da54fb1fc93719f2e8c3d090f09d1232
1 <?
3 function wfSpecialUserlogin()
5 global $wpCreateaccount, $wpCreateaccountMail;
6 global $wpLoginattempt, $wpMailmypassword;
7 global $action, $_REQUEST;
9 $fields = array( "wpName", "wpPassword", "wpName",
10 "wpPassword", "wpRetype", "wpEmail" );
11 wfCleanFormFields( $fields );
13 # When switching accounts, it sucks to get automatically logged out
14 global $returnto, $wgLang;
15 if( $returnto == $wgLang->specialPage( "Userlogout" ) ) $returnto = "";
17 $wpCookieCheck = $_REQUEST[ "wpCookieCheck" ];
19 if ( isset( $wpCookieCheck ) ) {
20 onCookieRedirectCheck( $wpCookieCheck );
21 } else if ( isset( $wpCreateaccount ) ) {
22 addNewAccount();
23 } else if ( isset( $wpCreateaccountMail ) ) {
24 addNewAccountMailPassword();
25 } else if ( isset( $wpMailmypassword ) ) {
26 mailPassword();
27 } else if ( "submit" == $action || isset( $wpLoginattempt ) ) {
28 processLogin();
29 } else {
30 mainLoginForm( "" );
35 /* private */ function addNewAccountMailPassword()
37 global $wgOut, $wpEmail, $wpName;
39 if ("" == $wpEmail) {
40 mainLoginForm( wfMsg( "noemail", $wpName ) );
41 return;
44 $u = addNewaccountInternal();
46 if ($u == NULL) {
47 return;
50 $u->saveSettings();
51 if (mailPasswordInternal($u) == NULL) {
52 return;
55 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
56 $wgOut->setRobotpolicy( "noindex,nofollow" );
57 $wgOut->setArticleRelated( false );
59 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
60 $wgOut->returnToMain( false );
62 $u = 0;
66 /* private */ function addNewAccount()
68 global $wgUser, $wgOut, $wpPassword, $wpRetype, $wpName, $wpRemember;
69 global $wpEmail, $wgDeferredUpdateList;
71 $u = addNewAccountInternal();
73 if ($u == NULL) {
74 return;
77 $wgUser = $u;
78 $wgUser->setCookies();
80 $up = new UserUpdate();
81 array_push( $wgDeferredUpdateList, $up );
83 if( hasSessionCookie() ) {
84 return successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
85 } else {
86 return cookieRedirectCheck( "new" );
91 /* private */ function addNewAccountInternal()
93 global $wgUser, $wgOut, $wpPassword, $wpRetype, $wpName, $wpRemember;
94 global $wpEmail, $wgMaxNameChars;
96 if (!$wgUser->isAllowedToCreateAccount()) {
97 userNotPrivilegedMessage();
98 return;
101 if ( 0 != strcmp( $wpPassword, $wpRetype ) ) {
102 mainLoginForm( wfMsg( "badretype" ) );
103 return;
105 $wpName = trim( $wpName );
106 if ( ( "" == $wpName ) ||
107 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $wpName ) ||
108 (strpos( $wpName, "/" ) !== false) ||
109 (strlen( $wpName ) > $wgMaxNameChars) )
111 mainLoginForm( wfMsg( "noname" ) );
112 return;
114 if ( wfReadOnly() ) {
115 $wgOut->readOnlyPage();
116 return;
118 $u = User::newFromName( $wpName );
120 if ( 0 != $u->idForName() ) {
121 mainLoginForm( wfMsg( "userexists" ) );
122 return;
124 $u->addToDatabase();
125 $u->setPassword( $wpPassword );
126 $u->setEmail( $wpEmail );
127 if ( 1 == $wpRemember ) { $r = 1; }
128 else { $r = 0; }
129 $u->setOption( "rememberpassword", $r );
131 return $u;
137 /* private */ function processLogin()
139 global $wgUser, $wpName, $wpPassword, $wpRemember;
140 global $wgDeferredUpdateList;
141 global $returnto;
143 if ( "" == $wpName ) {
144 mainLoginForm( wfMsg( "noname" ) );
145 return;
147 $u = User::newFromName( $wpName );
148 $id = $u->idForName();
149 if ( 0 == $id ) {
150 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
151 return;
153 $u->setId( $id );
154 $u->loadFromDatabase();
155 $ep = $u->encryptPassword( $wpPassword );
156 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
157 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
158 mainLoginForm( wfMsg( "wrongpassword" ) );
159 return;
163 # We've verified now, update the real record
165 if ( 1 == $wpRemember ) {
166 $r = 1;
167 $u->setCookiePassword( $wpPassword );
168 } else {
169 $r = 0;
171 $u->setOption( "rememberpassword", $r );
173 $wgUser = $u;
174 $wgUser->setCookies();
176 $up = new UserUpdate();
177 array_push( $wgDeferredUpdateList, $up );
179 if( hasSessionCookie() ) {
180 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
181 } else {
182 return cookieRedirectCheck( "login" );
186 /* private */ function mailPassword()
188 global $wgUser, $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
189 global $wgCookiePath, $wgCookieDomain, $wgDBname;
191 if ( "" == $wpName ) {
192 mainLoginForm( wfMsg( "noname" ) );
193 return;
195 $u = User::newFromName( $wpName );
196 $id = $u->idForName();
197 if ( 0 == $id ) {
198 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
199 return;
201 $u->setId( $id );
202 $u->loadFromDatabase();
204 if (mailPasswordInternal($u) == NULL) {
205 return;
208 mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
212 /* private */ function mailPasswordInternal( $u )
214 global $wpName, $wgDeferredUpdateList, $wgOutputEncoding;
215 global $wgPasswordSender, $wgDBname;
217 if ( "" == $u->getEmail() ) {
218 mainLoginForm( wfMsg( "noemail", $u->getName() ) );
219 return;
221 $np = User::randomPassword();
222 $u->setNewpassword( $np );
224 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
225 $u->saveSettings();
227 $ip = getenv( "REMOTE_ADDR" );
228 if ( "" == $ip ) { $ip = "(Unknown)"; }
230 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
232 mail( $u->getEmail(), wfMsg( "passwordremindertitle" ), $m,
233 "MIME-Version: 1.0\r\n" .
234 "Content-type: text/plain; charset={$wgOutputEncoding}\r\n" .
235 "Content-transfer-encoding: 8bit\r\n" .
236 "From: $wgPasswordSender" );
238 return $u;
245 /* private */ function successfulLogin( $msg )
247 global $wgUser;
248 global $wgDeferredUpdateList;
249 global $wgOut;
251 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
252 $wgOut->setRobotpolicy( "noindex,nofollow" );
253 $wgOut->setArticleRelated( false );
254 $wgOut->addHTML( $msg . "\n<p>" );
255 $wgOut->returnToMain();
258 function userNotPrivilegedMessage()
260 global $wgOut, $wgUser, $wgLang;
262 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
263 $wgOut->setRobotpolicy( "noindex,nofollow" );
264 $wgOut->setArticleRelated( false );
266 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
268 $wgOut->returnToMain( false );
271 /* private */ function mainLoginForm( $err )
273 global $wgUser, $wgOut, $wgLang, $returnto;
274 global $wpName, $wpPassword, $wpRetype, $wpRemember;
275 global $wpEmail, $HTTP_COOKIE_VARS, $wgDBname;
277 $le = wfMsg( "loginerror" );
278 $yn = wfMsg( "yourname" );
279 $yp = wfMsg( "yourpassword" );
280 $ypa = wfMsg( "yourpasswordagain" );
281 $rmp = wfMsg( "remembermypassword" );
282 $nuo = wfMsg( "newusersonly" );
283 $li = wfMsg( "login" );
284 $ca = wfMsg( "createaccount" );
285 $cam = wfMsg( "createaccountmail" );
286 $ye = wfMsg( "youremail" );
287 $efl = wfMsg( "emailforlost" );
288 $mmp = wfMsg( "mailmypassword" );
289 $endText = wfMsg( "loginend" );
292 $name = $wpName;
293 if ( "" == $name ) {
294 if ( 0 != $wgUser->getID() ) {
295 $name = $wgUser->getName();
296 } else {
297 $name = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
300 $pwd = $wpPassword;
302 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
303 $wgOut->setRobotpolicy( "noindex,nofollow" );
304 $wgOut->setArticleRelated( false );
306 if ( "" == $err ) {
307 $lp = wfMsg( "loginprompt" );
308 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
309 } else {
310 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
311 color='red'>$err</font>\n" );
313 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
314 $checked = " checked";
315 } else {
316 $checked = "";
318 $q = "action=submit";
319 if ( "" != $returnto ) { $q .= "&returnto=" . wfUrlencode($returnto); }
320 $action = wfLocalUrlE( $wgLang->specialPage( "Userlogin" ), $q );
322 $wpName = wfEscapeHTML( $wpName );
323 $wpPassword = wfEscapeHTML( $wpPassword );
324 $wpRetype = wfEscapeHTML( $wpRetype );
325 $wpEmail = wfEscapeHTML( $wpEmail );
327 if ($wgUser->getID() != 0) {
328 $cambutton = "<input tabindex=6 type=submit name=\"wpCreateaccountMail\" value=\"{$cam}\">";
331 $wgOut->addHTML( "
332 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
333 <table border=0><tr>
334 <td align=right>$yn:</td>
335 <td align=left>
336 <input tabindex=1 type=text name=\"wpName\" value=\"{$name}\" size=20>
337 </td>
338 <td align=left>
339 <input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
340 </td>
341 </tr>
342 <tr>
343 <td align=right>$yp:</td>
344 <td align=left>
345 <input tabindex=2 type=password name=\"wpPassword\" value=\"{$pwd}\" size=20>
346 </td>
347 <td align=left>
348 <input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked><label for=\"wpRemember\">$rmp</label>
349 </td>
350 </tr>");
352 if ($wgUser->isAllowedToCreateAccount()) {
354 $wgOut->addHTML("<tr><td colspan=3>&nbsp;</td></tr><tr>
355 <td align=right>$ypa:</td>
356 <td align=left>
357 <input tabindex=4 type=password name=\"wpRetype\" value=\"{$wpRetype}\"
358 size=20>
359 </td><td>$nuo</td></tr>
360 <tr>
361 <td align=right>$ye:</td>
362 <td align=left>
363 <input tabindex=5 type=text name=\"wpEmail\" value=\"{$wpEmail}\" size=20>
364 </td><td align=left>
365 <input tabindex=6 type=submit name=\"wpCreateaccount\" value=\"{$ca}\">
366 $cambutton
367 </td></tr>");
370 $wgOut->addHTML("
371 <tr><td colspan=3>&nbsp;</td></tr><tr>
372 <td colspan=3 align=left>
373 <p>$efl<br>
374 <input tabindex=8 type=submit name=\"wpMailmypassword\" value=\"{$mmp}\">
375 </td></tr></table>
376 </form>\n" );
377 $wgOut->addHTML( $endText );
380 /* private */ function hasSessionCookie()
382 global $wgDisableCookieCheck;
383 return ( $wgDisableCookieCheck ) ? true : ( "" != $_COOKIE[session_name()] );
386 /* private */ function cookieRedirectCheck( $type )
388 global $wgOut, $wgLang;
390 $check = wfLocalUrl( wfUrlEncode( $wgLang->specialPage( "Userlogin" ) ),
391 "wpCookieCheck=$type" );
393 return $wgOut->redirect( $check );
396 /* private */ function onCookieRedirectCheck( $type ) {
397 global $wgUser;
399 if ( !hasSessionCookie() ) {
400 if ( $type == "new" ) {
401 return mainLoginForm( wfMsg( "nocookiesnew" ) );
402 } else if ( $type == "login" ) {
403 return mainLoginForm( wfMsg( "nocookieslogin" ) );
404 } else {
405 # shouldn't happen
406 return mainLoginForm( wfMsg( "error" ) );
408 } else {
409 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );