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