Squid branch merge. Calls to purge functions in Article.php and special pages.
[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_integer( $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 isBureaucrat()
432 {
433 $this->loadFromDatabase();
434 if ( 0 == $this->mId ) { return false; }
435
436 return in_array( "bureaucrat", $this->mRights );
437 }
438
439 function isBot()
440 {
441 $this->loadFromDatabase();
442 if ( 0 == $this->mId ) { return false; }
443
444 return in_array( "bot", $this->mRights );
445 }
446
447 function &getSkin()
448 {
449 if ( ! isset( $this->mSkin ) ) {
450 $skinNames = Skin::getSkinNames();
451 $s = $this->getOption( "skin" );
452 if ( "" == $s ) { $s = 0; }
453
454 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
455 else $sn = "Skin" . $skinNames[$s];
456 $this->mSkin = new $sn;
457 }
458 return $this->mSkin;
459 }
460
461 function isWatched( $title ) {
462 $wl = WatchedItem::fromUserTitle( $this, $title );
463 return $wl->isWatched();
464 }
465
466 function addWatch( $title ) {
467 $wl = WatchedItem::fromUserTitle( $this, $title );
468 $wl->addWatch();
469 $this->invalidateCache();
470 }
471
472 function removeWatch( $title ) {
473 $wl = WatchedItem::fromUserTitle( $this, $title );
474 $wl->removeWatch();
475 $this->invalidateCache();
476 }
477
478
479 /* private */ function encodeOptions()
480 {
481 $a = array();
482 foreach ( $this->mOptions as $oname => $oval ) {
483 array_push( $a, "{$oname}={$oval}" );
484 }
485 $s = implode( "\n", $a );
486 return wfStrencode( $s );
487 }
488
489 /* private */ function decodeOptions( $str )
490 {
491 $a = explode( "\n", $str );
492 foreach ( $a as $s ) {
493 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
494 $this->mOptions[$m[1]] = $m[2];
495 }
496 }
497 }
498
499 function setCookies()
500 {
501 global $wsUserID, $wsUserName, $wsUserPassword;
502 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
503 if ( 0 == $this->mId ) return;
504 $this->loadFromDatabase();
505 $exp = time() + $wgCookieExpiration;
506
507 $wsUserID = $this->mId;
508 setcookie( "{$wgDBname}UserID", $this->mId, $exp, $wgCookiePath, $wgCookieDomain );
509
510 $wsUserName = $this->mName;
511 setcookie( "{$wgDBname}UserName", $this->mName, $exp, $wgCookiePath, $wgCookieDomain );
512
513 $wsUserPassword = $this->mPassword;
514 if ( 1 == $this->getOption( "rememberpassword" ) ) {
515 setcookie( "{$wgDBname}Password", $this->mCookiePassword, $exp, $wgCookiePath, $wgCookieDomain );
516 } else {
517 setcookie( "{$wgDBname}Password", "", time() - 3600 );
518 }
519 }
520
521 function logout()
522 {
523 global $wsUserID, $wgCookiePath, $wgCookieDomain, $wgDBname;
524 $this->mId = 0;
525
526 $wsUserID = 0;
527
528 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
529 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
530 }
531
532 function saveSettings()
533 {
534 global $wgMemc, $wgDBname;
535
536 if ( ! $this->mNewtalk ) {
537 if( $this->mId ) {
538 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
539 wfQuery ($sql, DB_WRITE, "User::saveSettings");
540 } else {
541 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
542 wfQuery ($sql, DB_WRITE, "User::saveSettings");
543 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
544 }
545 }
546 if ( 0 == $this->mId ) { return; }
547
548 $sql = "UPDATE user SET " .
549 "user_name= '" . wfStrencode( $this->mName ) . "', " .
550 "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
551 "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
552 "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
553 "user_options= '" . $this->encodeOptions() . "', " .
554 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " .
555 "user_touched= '" . wfStrencode( $this->mTouched ) .
556 "' WHERE user_id={$this->mId}";
557 wfQuery( $sql, DB_WRITE, "User::saveSettings" );
558 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
559 }
560
561 # Checks if a user with the given name exists
562 #
563 function idForName()
564 {
565 $gotid = 0;
566 $s = trim( $this->mName );
567 if ( 0 == strcmp( "", $s ) ) return 0;
568
569 $sql = "SELECT user_id FROM user WHERE user_name='" .
570 wfStrencode( $s ) . "'";
571 $res = wfQuery( $sql, DB_READ, "User::idForName" );
572 if ( 0 == wfNumRows( $res ) ) { return 0; }
573
574 $s = wfFetchObject( $res );
575 if ( "" == $s ) return 0;
576
577 $gotid = $s->user_id;
578 wfFreeResult( $res );
579 return $gotid;
580 }
581
582 function addToDatabase()
583 {
584 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
585 "user_email, user_rights, user_options) " .
586 " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
587 wfStrencode( $this->mPassword ) . "', '" .
588 wfStrencode( $this->mNewpassword ) . "', '" .
589 wfStrencode( $this->mEmail ) . "', '" .
590 wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
591 $this->encodeOptions() . "')";
592 wfQuery( $sql, DB_WRITE, "User::addToDatabase" );
593 $this->mId = $this->idForName();
594 }
595
596 function spreadBlock()
597 {
598 global $wgIP;
599 # If the (non-anonymous) user is blocked, this function will block any IP address
600 # that they successfully log on from.
601 $fname = "User::spreadBlock";
602
603 wfDebug( "User:spreadBlock()\n" );
604 if ( $this->mId == 0 ) {
605 return;
606 }
607
608 $userblock = Block::newFromDB( "", $this->mId );
609 if ( !$userblock->isValid() ) {
610 return;
611 }
612
613 # Check if this IP address is already blocked
614 $ipblock = Block::newFromDB( $wgIP );
615 if ( $ipblock->isValid() ) {
616 # Just update the timestamp
617 $ipblock->updateTimestamp();
618 return;
619 }
620
621 # Make a new block object with the desired properties
622 wfDebug( "Autoblocking {$this->mUserName}@{$wgIP}\n" );
623 $ipblock->mAddress = $wgIP;
624 $ipblock->mUser = 0;
625 $ipblock->mBy = $userblock->mBy;
626 $ipblock->mReason = wfMsg( "autoblocker", $this->getName(), $userblock->mReason );
627 $ipblock->mTimestamp = wfTimestampNow();
628 $ipblock->mAuto = 1;
629
630 # Insert it
631 $ipblock->insert();
632
633 }
634
635 function getPageRenderingHash(){
636 static $hash = false;
637 if( $hash ){
638 return $hash;
639 }
640
641 // stubthreshold is only included below for completeness,
642 // it will always be 0 when this function is called by parsercache.
643
644 $confstr = $this->getOption( "quickbar" );
645 $confstr .= "!" . $this->getOption( "underline" );
646 $confstr .= "!" . $this->getOption( "hover" );
647 $confstr .= "!" . $this->getOption( "skin" );
648 $confstr .= "!" . $this->getOption( "math" );
649 $confstr .= "!" . $this->getOption( "highlightbroken" );
650 $confstr .= "!" . $this->getOption( "stubthreshold" );
651 $confstr .= "!" . $this->getOption( "editsection" );
652 $confstr .= "!" . $this->getOption( "editsectiononrightclick" );
653 $confstr .= "!" . $this->getOption( "showtoc" );
654 $confstr .= "!" . $this->getOption( "date" );
655
656 if(strlen($confstr) > 32)
657 $hash = md5($confstr);
658 else
659 $hash = $confstr;
660 return $hash;
661 }
662
663 function isAllowedToCreateAccount()
664 {
665 global $wgWhitelistAccount;
666 $allowed = false;
667
668 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
669 foreach ($wgWhitelistAccount as $right => $ok) {
670 $userHasRight = (!strcmp($right, "user") || in_array($right, $this->getRights()));
671 $allowed |= ($ok && $userHasRight);
672 }
673 return $allowed;
674 }
675
676
677
678 }
679
680 ?>