Squid integration changes
[lhc/web/wiklou.git] / includes / User.php
1 <?
2 # See user.doc
3
4 include_once( "WatchedItem.php" );
5
6 class User {
7 /* private */ var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
8 /* private */ var $mRights, $mOptions;
9 /* private */ var $mDataLoaded, $mNewpassword;
10 /* private */ var $mSkin;
11 /* private */ var $mBlockedby, $mBlockreason;
12 /* private */ var $mTouched;
13 /* private */ var $mCookiePassword;
14
15 function User()
16 {
17 $this->loadDefaults();
18 }
19
20 # Static factory method
21 #
22 function newFromName( $name )
23 {
24 $u = new User();
25
26 # Clean up name according to title rules
27
28 $t = Title::newFromText( $name );
29 $u->setName( $t->getText() );
30 return $u;
31 }
32
33 /* static */ function whoIs( $id )
34 {
35 return wfGetSQL( "user", "user_name", "user_id=$id" );
36 }
37
38 /* static */ function idFromName( $name )
39 {
40 $nt = Title::newFromText( $name );
41 $sql = "SELECT user_id FROM user WHERE user_name='" .
42 wfStrencode( $nt->getText() ) . "'";
43 $res = wfQuery( $sql, DB_READ, "User::idFromName" );
44
45 if ( 0 == wfNumRows( $res ) ) { return 0; }
46 else {
47 $s = wfFetchObject( $res );
48 return $s->user_id;
49 }
50 }
51
52 # does the string match an anonymous user IP address?
53 /* static */ function isIP( $name ) {
54 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
55
56 }
57
58 /* static */ function randomPassword()
59 {
60 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
61 $l = strlen( $pwchars ) - 1;
62
63 wfSeedRandom();
64 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
65 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
66 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
67 $pwchars{mt_rand( 0, $l )};
68 return $np;
69 }
70
71 function loadDefaults()
72 {
73 global $wgLang, $wgIP;
74 global $wgNamespacesToBeSearchedDefault;
75
76 $this->mId = $this->mNewtalk = 0;
77 $this->mName = $wgIP;
78 $this->mEmail = "";
79 $this->mPassword = $this->mNewpassword = "";
80 $this->mRights = array();
81 $defOpt = $wgLang->getDefaultUserOptions() ;
82 foreach ( $defOpt as $oname => $val ) {
83 $this->mOptions[$oname] = $val;
84 }
85 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
86 $this->mOptions["searchNs".$nsnum] = $val;
87 }
88 unset( $this->mSkin );
89 $this->mDataLoaded = false;
90 $this->mBlockedby = -1; # Unset
91 $this->mTouched = '0'; # Allow any pages to be cached
92 $this->cookiePassword = "";
93 }
94
95 /* private */ function getBlockedStatus()
96 {
97 global $wgBadRanges, $wgBadUserAgents, $wgRangeBlockUser, $wgRangeBlockReason, $wgIP;
98
99 if ( -1 != $this->mBlockedby ) { return; }
100
101 # Range/user-agent blocking
102
103 $fBlock = false; # Mmmm, Hungarian
104 if ( ( !is_array( $wgBadUserAgents ) ||
105 array_key_exists( getenv( "HTTP_USER_AGENT" ), $wgBadUserAgents ) ) &&
106 is_array( $wgBadRanges ) )
107 {
108 $iIp = ip2long( $wgIP );
109 foreach ( $wgBadRanges as $range ) {
110 $start = ip2long( $range[0] );
111 $end = ip2long( $range[1] );
112 if ( $iIp >= $start && $iIp <= $end ) {
113 $fBlock = true;
114 break;
115 }
116 }
117 }
118
119 if ( $fBlock ) {
120 $this->mBlockedby = $wgRangeBlockUser;
121 $this->mBlockreason = $wgRangeBlockReason;
122 return;
123 }
124
125 # User/IP blocking
126
127 $block = new Block();
128 if ( !$block->load( $wgIP , $this->mId ) ) {
129 wfDebug( $wgIP ." is not blocked\n" );
130 $this->mBlockedby = 0;
131 return;
132 }
133
134 $this->mBlockedby = $block->mBy;
135 $this->mBlockreason = $block->mReason;
136 }
137
138 function isBlocked()
139 {
140 $this->getBlockedStatus();
141 if ( 0 == $this->mBlockedby ) { return false; }
142 return true;
143 }
144
145 function blockedBy() {
146 $this->getBlockedStatus();
147 return $this->mBlockedby;
148 }
149
150 function blockedFor() {
151 $this->getBlockedStatus();
152 return $this->mBlockreason;
153 }
154
155 function SetupSession() {
156 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
157 global $wsUserID, $wsUserName, $wsUserPassword, $wsUploadFiles;
158 if( $wgSessionsInMemcached ) {
159 include_once( "MemcachedSessions.php" );
160 }
161 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
162 session_cache_limiter( "private, must-revalidate" );
163 session_start();
164 session_register( "wsUserID" );
165 session_register( "wsUserName" );
166 session_register( "wsUserPassword" );
167 session_register( "wsUploadFiles" );
168 }
169
170 /* static */ function loadFromSession()
171 {
172 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
173 global $wgMemc, $wgDBname;
174
175 if ( isset( $wsUserID ) ) {
176 if ( 0 != $wsUserID ) {
177 $sId = $wsUserID;
178 } else {
179 return new User();
180 }
181 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}UserID"] ) ) {
182 $sId = $HTTP_COOKIE_VARS["{$wgDBname}UserID"];
183 $wsUserID = $sId;
184 } else {
185 return new User();
186 }
187 if ( isset( $wsUserName ) ) {
188 $sName = $wsUserName;
189 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}UserName"] ) ) {
190 $sName = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
191 $wsUserName = $sName;
192 } else {
193 return new User();
194 }
195
196 $passwordCorrect = FALSE;
197 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
198 if($makenew = !$user) {
199 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
200 $user = new User();
201 $user->mId = $sId;
202 $user->loadFromDatabase();
203 } else {
204 wfDebug( "User::loadFromSession() got from cache!\n" );
205 }
206
207 if ( isset( $wsUserPassword ) ) {
208 $passwordCorrect = $wsUserPassword == $user->mPassword;
209 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}Password"] ) ) {
210 $user->mCookiePassword = $HTTP_COOKIE_VARS["{$wgDBname}Password"];
211 $wsUserPassword = $user->addSalt( $user->mCookiePassword );
212 $passwordCorrect = $wsUserPassword == $user->mPassword;
213 } else {
214 return new User(); # Can't log in from session
215 }
216
217 if ( ( $sName == $user->mName ) && $passwordCorrect ) {
218 if($makenew) {
219 if($wgMemc->set( $key, $user ))
220 wfDebug( "User::loadFromSession() successfully saved user\n" );
221 else
222 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
223 }
224 $user->spreadBlock();
225 return $user;
226 }
227 return new User(); # Can't log in from session
228 }
229
230 function loadFromDatabase()
231 {
232 if ( $this->mDataLoaded ) { return; }
233 # check in separate table if there are changes to the talk page
234 $this->mNewtalk=0; # reset talk page status
235 if($this->mId) {
236 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
237 $res = wfQuery ($sql, DB_READ, "User::loadFromDatabase" );
238
239 if (wfNumRows($res)>0) {
240 $this->mNewtalk= 1;
241 }
242 wfFreeResult( $res );
243 } else {
244 # TEST THIS @@@
245 global $wgDBname, $wgMemc;
246 $key = "$wgDBname:newtalk:ip:{$this->mName}";
247 $newtalk = $wgMemc->get( $key );
248 if( ! is_scalar( $newtalk ) ){
249 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
250 $res = wfQuery ($sql, DB_READ, "User::loadFromDatabase" );
251
252 $this->mNewtalk = (wfNumRows($res)>0) ? 1 : 0;
253 wfFreeResult( $res );
254
255 $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
256 } else {
257 $this->mNewtalk = $newtalk ? 1 : 0;
258 }
259 }
260 if(!$this->mId) {
261 $this->mDataLoaded = true;
262 return;
263 } # the following stuff is for non-anonymous users only
264
265 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
266 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
267 "{$this->mId}";
268 $res = wfQuery( $sql, DB_READ, "User::loadFromDatabase" );
269
270 if ( wfNumRows( $res ) > 0 ) {
271 $s = wfFetchObject( $res );
272 $this->mName = $s->user_name;
273 $this->mEmail = $s->user_email;
274 $this->mPassword = $s->user_password;
275 $this->mNewpassword = $s->user_newpassword;
276 $this->decodeOptions( $s->user_options );
277 $this->mRights = explode( ",", strtolower( $s->user_rights ) );
278 $this->mTouched = $s->user_touched;
279 }
280
281 wfFreeResult( $res );
282 $this->mDataLoaded = true;
283 }
284
285 function getID() { return $this->mId; }
286 function setID( $v ) {
287 $this->mId = $v;
288 $this->mDataLoaded = false;
289 }
290
291 function getName() {
292 $this->loadFromDatabase();
293 return $this->mName;
294 }
295
296 function setName( $str )
297 {
298 $this->loadFromDatabase();
299 $this->mName = $str;
300 }
301
302 function getNewtalk()
303 {
304 $this->loadFromDatabase();
305 return ( 0 != $this->mNewtalk );
306 }
307
308 function setNewtalk( $val )
309 {
310 $this->loadFromDatabase();
311 $this->mNewtalk = $val;
312 $this->invalidateCache();
313 }
314
315 function invalidateCache() {
316 $this->loadFromDatabase();
317 $this->mTouched = wfTimestampNow();
318 # Don't forget to save the options after this or
319 # it won't take effect!
320 }
321
322 function validateCache( $timestamp ) {
323 $this->loadFromDatabase();
324 return ($timestamp >= $this->mTouched);
325 }
326
327 function getPassword()
328 {
329 $this->loadFromDatabase();
330 return $this->mPassword;
331 }
332
333 function getNewpassword()
334 {
335 $this->loadFromDatabase();
336 return $this->mNewpassword;
337 }
338
339 function addSalt( $p )
340 {
341 global $wgPasswordSalt;
342 if($wgPasswordSalt)
343 return md5( "{$this->mId}-{$p}" );
344 else
345 return $p;
346 }
347
348 function encryptPassword( $p )
349 {
350 return $this->addSalt( md5( $p ) );
351 }
352
353 function setPassword( $str )
354 {
355 $this->loadFromDatabase();
356 $this->setCookiePassword( $str );
357 $this->mPassword = $this->encryptPassword( $str );
358 $this->mNewpassword = "";
359 }
360
361 function setCookiePassword( $str )
362 {
363 $this->loadFromDatabase();
364 $this->mCookiePassword = md5( $str );
365 }
366
367 function setNewpassword( $str )
368 {
369 $this->loadFromDatabase();
370 $this->mNewpassword = $this->encryptPassword( $str );
371 }
372
373 function getEmail()
374 {
375 $this->loadFromDatabase();
376 return $this->mEmail;
377 }
378
379 function setEmail( $str )
380 {
381 $this->loadFromDatabase();
382 $this->mEmail = $str;
383 }
384
385 function getOption( $oname )
386 {
387 $this->loadFromDatabase();
388 if ( array_key_exists( $oname, $this->mOptions ) ) {
389 return $this->mOptions[$oname];
390 } else {
391 return "";
392 }
393 }
394
395 function setOption( $oname, $val )
396 {
397 $this->loadFromDatabase();
398 $this->mOptions[$oname] = $val;
399 $this->invalidateCache();
400 }
401
402 function getRights()
403 {
404 $this->loadFromDatabase();
405 return $this->mRights;
406 }
407
408 function addRight( $rname )
409 {
410 $this->loadFromDatabase();
411 array_push( $this->mRights, $rname );
412 $this->invalidateCache();
413 }
414
415 function isSysop()
416 {
417 $this->loadFromDatabase();
418 if ( 0 == $this->mId ) { return false; }
419
420 return in_array( "sysop", $this->mRights );
421 }
422
423 function isDeveloper()
424 {
425 $this->loadFromDatabase();
426 if ( 0 == $this->mId ) { return false; }
427
428 return in_array( "developer", $this->mRights );
429 }
430
431 function isBot()
432 {
433 $this->loadFromDatabase();
434 if ( 0 == $this->mId ) { return false; }
435
436 return in_array( "bot", $this->mRights );
437 }
438
439 function &getSkin()
440 {
441 if ( ! isset( $this->mSkin ) ) {
442 $skinNames = Skin::getSkinNames();
443 $s = $this->getOption( "skin" );
444 if ( "" == $s ) { $s = 0; }
445
446 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
447 else $sn = "Skin" . $skinNames[$s];
448 $this->mSkin = new $sn;
449 }
450 return $this->mSkin;
451 }
452
453 function isWatched( $title ) {
454 $wl = WatchedItem::fromUserTitle( $this, $title );
455 return $wl->isWatched();
456 }
457
458 function addWatch( $title ) {
459 $wl = WatchedItem::fromUserTitle( $this, $title );
460 $wl->addWatch();
461 $this->invalidateCache();
462 }
463
464 function removeWatch( $title ) {
465 $wl = WatchedItem::fromUserTitle( $this, $title );
466 $wl->removeWatch();
467 $this->invalidateCache();
468 }
469
470
471 /* private */ function encodeOptions()
472 {
473 $a = array();
474 foreach ( $this->mOptions as $oname => $oval ) {
475 array_push( $a, "{$oname}={$oval}" );
476 }
477 $s = implode( "\n", $a );
478 return wfStrencode( $s );
479 }
480
481 /* private */ function decodeOptions( $str )
482 {
483 $a = explode( "\n", $str );
484 foreach ( $a as $s ) {
485 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
486 $this->mOptions[$m[1]] = $m[2];
487 }
488 }
489 }
490
491 function setCookies()
492 {
493 global $wsUserID, $wsUserName, $wsUserPassword;
494 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
495 if ( 0 == $this->mId ) return;
496 $this->loadFromDatabase();
497 $exp = time() + $wgCookieExpiration;
498
499 $wsUserID = $this->mId;
500 setcookie( "{$wgDBname}UserID", $this->mId, $exp, $wgCookiePath, $wgCookieDomain );
501
502 $wsUserName = $this->mName;
503 setcookie( "{$wgDBname}UserName", $this->mName, $exp, $wgCookiePath, $wgCookieDomain );
504
505 $wsUserPassword = $this->mPassword;
506 if ( 1 == $this->getOption( "rememberpassword" ) ) {
507 setcookie( "{$wgDBname}Password", $this->mCookiePassword, $exp, $wgCookiePath, $wgCookieDomain );
508 } else {
509 setcookie( "{$wgDBname}Password", "", time() - 3600 );
510 }
511 }
512
513 function logout()
514 {
515 global $wsUserID, $wgCookiePath, $wgCookieDomain, $wgDBname;
516 $this->mId = 0;
517
518 $wsUserID = 0;
519
520 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
521 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
522 }
523
524 function saveSettings()
525 {
526 global $wgMemc, $wgDBname;
527
528 if ( ! $this->mNewtalk ) {
529 if( $this->mId ) {
530 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
531 wfQuery ($sql, DB_WRITE, "User::saveSettings");
532 } else {
533 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
534 wfQuery ($sql, DB_WRITE, "User::saveSettings");
535 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
536 }
537 }
538 if ( 0 == $this->mId ) { return; }
539
540 $sql = "UPDATE user SET " .
541 "user_name= '" . wfStrencode( $this->mName ) . "', " .
542 "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
543 "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
544 "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
545 "user_options= '" . $this->encodeOptions() . "', " .
546 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " .
547 "user_touched= '" . wfStrencode( $this->mTouched ) .
548 "' WHERE user_id={$this->mId}";
549 wfQuery( $sql, DB_WRITE, "User::saveSettings" );
550 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
551 }
552
553 # Checks if a user with the given name exists
554 #
555 function idForName()
556 {
557 $gotid = 0;
558 $s = trim( $this->mName );
559 if ( 0 == strcmp( "", $s ) ) return 0;
560
561 $sql = "SELECT user_id FROM user WHERE user_name='" .
562 wfStrencode( $s ) . "'";
563 $res = wfQuery( $sql, DB_READ, "User::idForName" );
564 if ( 0 == wfNumRows( $res ) ) { return 0; }
565
566 $s = wfFetchObject( $res );
567 if ( "" == $s ) return 0;
568
569 $gotid = $s->user_id;
570 wfFreeResult( $res );
571 return $gotid;
572 }
573
574 function addToDatabase()
575 {
576 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
577 "user_email, user_rights, user_options) " .
578 " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
579 wfStrencode( $this->mPassword ) . "', '" .
580 wfStrencode( $this->mNewpassword ) . "', '" .
581 wfStrencode( $this->mEmail ) . "', '" .
582 wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
583 $this->encodeOptions() . "')";
584 wfQuery( $sql, DB_WRITE, "User::addToDatabase" );
585 $this->mId = $this->idForName();
586 }
587
588 function spreadBlock()
589 {
590 global $wgIP;
591 # If the (non-anonymous) user is blocked, this function will block any IP address
592 # that they successfully log on from.
593 $fname = "User::spreadBlock";
594
595 wfDebug( "User:spreadBlock()\n" );
596 if ( $this->mId == 0 ) {
597 return;
598 }
599
600 $userblock = Block::newFromDB( "", $this->mId );
601 if ( !$userblock->isValid() ) {
602 return;
603 }
604
605 # Check if this IP address is already blocked
606 $ipblock = Block::newFromDB( $wgIP );
607 if ( $ipblock->isValid() ) {
608 # Just update the timestamp
609 $ipblock->updateTimestamp();
610 return;
611 }
612
613 # Make a new block object with the desired properties
614 wfDebug( "Autoblocking {$this->mUserName}@{$wgIP}\n" );
615 $ipblock->mAddress = $wgIP;
616 $ipblock->mUser = 0;
617 $ipblock->mBy = $userblock->mBy;
618 $ipblock->mReason = wfMsg( "autoblocker", $this->getName(), $userblock->mReason );
619 $ipblock->mTimestamp = wfTimestampNow();
620 $ipblock->mAuto = 1;
621
622 # Insert it
623 $ipblock->insert();
624
625 }
626
627 function getPageRenderingHash(){
628 static $hash = false;
629 if( $hash ){
630 return $hash;
631 }
632
633 // stubthreshold is only included below for completeness,
634 // it will always be 0 when this function is called by parsercache.
635
636 $confstr = $this->getOption( "quickbar" );
637 $confstr .= "!" . $this->getOption( "underline" );
638 $confstr .= "!" . $this->getOption( "hover" );
639 $confstr .= "!" . $this->getOption( "skin" );
640 $confstr .= "!" . $this->getOption( "math" );
641 $confstr .= "!" . $this->getOption( "highlightbroken" );
642 $confstr .= "!" . $this->getOption( "stubthreshold" );
643 $confstr .= "!" . $this->getOption( "editsection" );
644 $confstr .= "!" . $this->getOption( "editsectiononrightclick" );
645 $confstr .= "!" . $this->getOption( "showtoc" );
646 $confstr .= "!" . $this->getOption( "date" );
647
648 if(strlen($confstr) > 32)
649 $hash = md5($confstr);
650 else
651 $hash = $confstr;
652 return $hash;
653 }
654
655 function isAllowedToCreateAccount()
656 {
657 global $wgWhitelistAccount;
658 $allowed = false;
659
660 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
661 foreach ($wgWhitelistAccount as $right => $ok) {
662 $userHasRight = (!strcmp($right, "user") || in_array($right, $this->getRights()));
663 $allowed |= ($ok && $userHasRight);
664 }
665 return $allowed;
666 }
667
668
669
670 }
671
672 ?>