* Fixed magic quotes in $_REQUEST, in Setup.php
[lhc/web/wiklou.git] / includes / SpecialUserlogin.php
1 <?php
2
3 require_once('UserMailer.php');
4
5 function wfSpecialUserlogin()
6 {
7 global $wgCommandLineMode;
8 if( !$wgCommandLineMode && !isset( $_COOKIE[ini_get("session.name")] ) ) {
9 User::SetupSession();
10 }
11
12 $fields = array( "wpName", "wpPassword", "wpName",
13 "wpPassword", "wpRetype", "wpEmail" );
14 wfCleanFormFields( $fields );
15
16 # When switching accounts, it sucks to get automatically logged out
17 global $wgLang;
18 if( $_REQUEST['returnto'] == $wgLang->specialPage( "Userlogout" ) ) $_REQUEST['returnto'] = "";
19
20 $wpCookieCheck = $_REQUEST[ "wpCookieCheck" ];
21
22 if ( isset( $wpCookieCheck ) ) {
23 onCookieRedirectCheck( $wpCookieCheck );
24 } else if ( isset( $_REQUEST['wpCreateaccount'] ) ) {
25 addNewAccount();
26 } else if ( isset( $_REQUEST['wpCreateaccountMail'] ) ) {
27 addNewAccountMailPassword();
28 } else if ( isset( $_REQUEST['wpMailmypassword'] ) ) {
29 mailPassword();
30 } else if ( "submit" == $_REQUEST['action'] || array_key_exists('wpLoginattempt', $_REQUEST) ) {
31 processLogin();
32 } else {
33 mainLoginForm( "" );
34 }
35 }
36
37
38 /* private */ function addNewAccountMailPassword()
39 {
40 global $wgOut;
41
42 if ("" == $_REQUEST['wpEmail']) {
43 mainLoginForm( wfMsg( "noemail", $_REQUEST['wpName'] ) );
44 return;
45 }
46
47 $u = addNewaccountInternal();
48
49 if ($u == NULL) {
50 return;
51 }
52
53 $u->saveSettings();
54 if (mailPasswordInternal($u) == NULL) {
55 return;
56 }
57
58 $wgOut->setPageTitle( wfMsg( "accmailtitle" ) );
59 $wgOut->setRobotpolicy( "noindex,nofollow" );
60 $wgOut->setArticleRelated( false );
61
62 $wgOut->addWikiText( wfMsg( "accmailtext", $u->getName(), $u->getEmail() ) );
63 $wgOut->returnToMain( false );
64
65 $u = 0;
66 }
67
68
69 /* private */ function addNewAccount()
70 {
71 global $wgUser, $wgOut;
72 global $wgDeferredUpdateList;
73
74 $u = addNewAccountInternal();
75
76 if ($u == NULL) {
77 return;
78 }
79
80 $wgUser = $u;
81 $wgUser->setCookies();
82
83 $up = new UserUpdate();
84 array_push( $wgDeferredUpdateList, $up );
85
86 if( hasSessionCookie() ) {
87 return successfulLogin( wfMsg( "welcomecreation", $wgUser->getName() ) );
88 } else {
89 return cookieRedirectCheck( "new" );
90 }
91 }
92
93
94 /* private */ function addNewAccountInternal()
95 {
96 global $wgUser, $wgOut;
97 global $wgMaxNameChars;
98
99 if (!$wgUser->isAllowedToCreateAccount()) {
100 userNotPrivilegedMessage();
101 return;
102 }
103
104 if ( 0 != strcmp( $_REQUEST['wpPassword'], $_REQUEST['wpRetype'] ) ) {
105 mainLoginForm( wfMsg( "badretype" ) );
106 return;
107 }
108
109 $name = trim( $_REQUEST['wpName'] );
110 if ( ( "" == $name ) ||
111 preg_match( "/\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/", $name ) ||
112 (strpos( $name, "/" ) !== false) ||
113 (strlen( $name ) > $wgMaxNameChars) )
114 {
115 mainLoginForm( wfMsg( "noname" ) );
116 return;
117 }
118 if ( wfReadOnly() ) {
119 $wgOut->readOnlyPage();
120 return;
121 }
122 $u = User::newFromName( $name );
123
124 if ( 0 != $u->idForName() ) {
125 mainLoginForm( wfMsg( "userexists" ) );
126 return;
127 }
128 $u->addToDatabase();
129 $u->setPassword( $_REQUEST['wpPassword'] );
130 $u->setEmail( $_REQUEST['wpEmail'] );
131 if ( 1 == $_REQUEST['wpRemember'] ) { $r = 1; }
132 else { $r = 0; }
133 $u->setOption( "rememberpassword", $r );
134
135 return $u;
136 }
137
138
139
140
141 /* private */ function processLogin()
142 {
143 global $wgUser;
144 global $wgDeferredUpdateList;
145
146 if ( "" == $_REQUEST['wpName'] ) {
147 mainLoginForm( wfMsg( "noname" ) );
148 return;
149 }
150 $u = User::newFromName( $_REQUEST['wpName'] );
151 $id = $u->idForName();
152 if ( 0 == $id ) {
153 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
154 return;
155 }
156 $u->setId( $id );
157 $u->loadFromDatabase();
158 $ep = $u->encryptPassword( $_REQUEST['wpPassword'] );
159 if ( 0 != strcmp( $ep, $u->getPassword() ) ) {
160 if ( 0 != strcmp( $ep, $u->getNewpassword() ) ) {
161 mainLoginForm( wfMsg( "wrongpassword" ) );
162 return;
163 }
164 }
165
166 # We've verified now, update the real record
167 #
168 if ( 1 == $_REQUEST['wpRemember'] ) {
169 $r = 1;
170 $u->setCookiePassword( $_REQUEST['wpPassword'] );
171 } else {
172 $r = 0;
173 }
174 $u->setOption( "rememberpassword", $r );
175
176 $wgUser = $u;
177 $wgUser->setCookies();
178
179 $up = new UserUpdate();
180 array_push( $wgDeferredUpdateList, $up );
181
182 if( hasSessionCookie() ) {
183 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
184 } else {
185 return cookieRedirectCheck( "login" );
186 }
187 }
188
189 /* private */ function mailPassword()
190 {
191 global $wgUser, $wgDeferredUpdateList, $wgOutputEncoding;
192 global $wgCookiePath, $wgCookieDomain, $wgDBname;
193
194 if ( "" == $_REQUEST['wpName'] ) {
195 mainLoginForm( wfMsg( "noname" ) );
196 return;
197 }
198 $u = User::newFromName( $_REQUEST['wpName'] );
199 $id = $u->idForName();
200 if ( 0 == $id ) {
201 mainLoginForm( wfMsg( "nosuchuser", $u->getName() ) );
202 return;
203 }
204 $u->setId( $id );
205 $u->loadFromDatabase();
206
207 if (mailPasswordInternal($u) == NULL) {
208 return;
209 }
210
211 mainLoginForm( wfMsg( "passwordsent", $u->getName() ) );
212 }
213
214
215 /* private */ function mailPasswordInternal( $u )
216 {
217 global $wgDeferredUpdateList, $wgOutputEncoding;
218 global $wgPasswordSender, $wgDBname, $wgIP;
219
220 if ( "" == $u->getEmail() ) {
221 mainLoginForm( wfMsg( "noemail", $u->getName() ) );
222 return;
223 }
224 $np = User::randomPassword();
225 $u->setNewpassword( $np );
226
227 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
228 $u->saveSettings();
229
230 $ip = $wgIP;
231 if ( "" == $ip ) { $ip = "(Unknown)"; }
232
233 $m = wfMsg( "passwordremindertext", $ip, $u->getName(), $np );
234
235 userMailer( $u->getEmail(), $wgPasswordSender, wfMsg( "passwordremindertitle" ), $m );
236
237 return $u;
238 }
239
240
241
242
243
244 /* private */ function successfulLogin( $msg )
245 {
246 global $wgUser;
247 global $wgDeferredUpdateList;
248 global $wgOut;
249
250 $wgOut->setPageTitle( wfMsg( "loginsuccesstitle" ) );
251 $wgOut->setRobotpolicy( "noindex,nofollow" );
252 $wgOut->setArticleRelated( false );
253 $wgOut->addHTML( $msg . "\n<p>" );
254 $wgOut->returnToMain();
255 }
256
257 function userNotPrivilegedMessage()
258 {
259 global $wgOut, $wgUser, $wgLang;
260
261 $wgOut->setPageTitle( wfMsg( "whitelistacctitle" ) );
262 $wgOut->setRobotpolicy( "noindex,nofollow" );
263 $wgOut->setArticleRelated( false );
264
265 $wgOut->addWikiText( wfMsg( "whitelistacctext" ) );
266
267 $wgOut->returnToMain( false );
268 }
269
270 /* private */ function mainLoginForm( $err )
271 {
272 global $wgUser, $wgOut, $wgLang;
273 global $HTTP_COOKIE_VARS, $wgDBname;
274
275 $le = wfMsg( "loginerror" );
276 $yn = wfMsg( "yourname" );
277 $yp = wfMsg( "yourpassword" );
278 $ypa = wfMsg( "yourpasswordagain" );
279 $rmp = wfMsg( "remembermypassword" );
280 $nuo = wfMsg( "newusersonly" );
281 $li = wfMsg( "login" );
282 $ca = wfMsg( "createaccount" );
283 $cam = wfMsg( "createaccountmail" );
284 $ye = wfMsg( "youremail" );
285 $efl = wfMsg( "emailforlost" );
286 $mmp = wfMsg( "mailmypassword" );
287 $endText = wfMsg( "loginend" );
288
289 if ( $endText = "&lt;loginend&gt;" ) {
290 $endText = "";
291 }
292
293 $name = $_REQUEST['wpName'];
294 if ( "" == $name ) {
295 if ( 0 != $wgUser->getID() ) {
296 $name = $wgUser->getName();
297 } else {
298 $name = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
299 }
300 }
301 $pwd = $_REQUEST['wpPassword'];
302
303 $wgOut->setPageTitle( wfMsg( "userlogin" ) );
304 $wgOut->setRobotpolicy( "noindex,nofollow" );
305 $wgOut->setArticleRelated( false );
306
307 if ( "" == $err ) {
308 $lp = wfMsg( "loginprompt" );
309 $wgOut->addHTML( "<h2>$li:</h2>\n<p>$lp</p>" );
310 } else {
311 $wgOut->addHTML( "<h2>$le:</h2>\n<font size='+1'
312 color='red'>$err</font>\n" );
313 }
314 if ( 1 == $wgUser->getOption( "rememberpassword" ) ) {
315 $checked = " checked";
316 } else {
317 $checked = "";
318 }
319 $q = "action=submit";
320 if ( "" != $_REQUEST['returnto'] ) { $q .= "&returnto=" . wfUrlencode($_REQUEST['returnto']); }
321 $titleObj = Title::makeTitle( NS_SPECIAL, "Userlogin" );
322 $action = $titleObj->getURL( $q, true );
323
324 $encName = wfEscapeHTML( $name );
325 $encPassword = wfEscapeHTML( $pwd );
326 $encRetype = wfEscapeHTML( $_REQUEST['wpRetype'] );
327 $encEmail = wfEscapeHTML( $_REQUEST['wpEmail'] );
328
329 if ($wgUser->getID() != 0) {
330 $cambutton = "<input tabindex=6 type=submit name=\"wpCreateaccountMail\" value=\"{$cam}\">";
331 }
332
333 $wgOut->addHTML( "
334 <form name=\"userlogin\" id=\"userlogin\" method=\"post\" action=\"{$action}\">
335 <table border=0><tr>
336 <td align=right>$yn:</td>
337 <td align=left>
338 <input tabindex=1 type=text name=\"wpName\" value=\"{$encName}\" size=20>
339 </td>
340 <td align=left>
341 <input tabindex=3 type=submit name=\"wpLoginattempt\" value=\"{$li}\">
342 </td>
343 </tr>
344 <tr>
345 <td align=right>$yp:</td>
346 <td align=left>
347 <input tabindex=2 type=password name=\"wpPassword\" value=\"{$encPassword}\" size=20>
348 </td>
349 <td align=left>
350 <input tabindex=7 type=checkbox name=\"wpRemember\" value=\"1\" id=\"wpRemember\"$checked><label for=\"wpRemember\">$rmp</label>
351 </td>
352 </tr>");
353
354 if ($wgUser->isAllowedToCreateAccount()) {
355 $encRetype = htmlspecialchars( $_REQUEST['wpRetype'] );
356 $encEmail = htmlspecialchars( $_REQUEST['wpCreateAccount'] );
357 $wgOut->addHTML("<tr><td colspan=3>&nbsp;</td></tr><tr>
358 <td align=right>$ypa:</td>
359 <td align=left>
360 <input tabindex=4 type=password name=\"wpRetype\" value=\"{$encRetype}\"
361 size=20>
362 </td><td>$nuo</td></tr>
363 <tr>
364 <td align=right>$ye:</td>
365 <td align=left>
366 <input tabindex=5 type=text name=\"wpEmail\" value=\"{$encEmail}\" size=20>
367 </td><td align=left>
368 <input tabindex=6 type=submit name=\"wpCreateaccount\" value=\"{$ca}\">
369 $cambutton
370 </td></tr>");
371 }
372
373 $wgOut->addHTML("
374 <tr><td colspan=3>&nbsp;</td></tr><tr>
375 <td colspan=3 align=left>
376 <p>$efl<br>
377 <input tabindex=8 type=submit name=\"wpMailmypassword\" value=\"{$mmp}\">
378 </td></tr></table>
379 </form>\n" );
380 $wgOut->addHTML( $endText );
381 }
382
383 /* private */ function hasSessionCookie()
384 {
385 global $wgDisableCookieCheck;
386 return ( $wgDisableCookieCheck ) ? true : ( "" != $_COOKIE[session_name()] );
387 }
388
389 /* private */ function cookieRedirectCheck( $type )
390 {
391 global $wgOut, $wgLang;
392
393 $titleObj = Title::makeTitle( NS_SPECIAL, "Userlogin" );
394 $check = $titleObj->getURL( "wpCookieCheck=$type" );
395
396 return $wgOut->redirect( $check );
397 }
398
399 /* private */ function onCookieRedirectCheck( $type ) {
400 global $wgUser;
401
402 if ( !hasSessionCookie() ) {
403 if ( $type == "new" ) {
404 return mainLoginForm( wfMsg( "nocookiesnew" ) );
405 } else if ( $type == "login" ) {
406 return mainLoginForm( wfMsg( "nocookieslogin" ) );
407 } else {
408 # shouldn't happen
409 return mainLoginForm( wfMsg( "error" ) );
410 }
411 } else {
412 return successfulLogin( wfMsg( "loginsuccess", $wgUser->getName() ) );
413 }
414 }
415
416 ?>