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