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