4 include_once( "WatchedItem.php" );
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 /* private */ var $mRealName;
18 $this->loadDefaults();
21 # Static factory method
23 function newFromName( $name )
27 # Clean up name according to title rules
29 $t = Title
::newFromText( $name );
30 $u->setName( $t->getText() );
34 /* static */ function whoIs( $id )
36 return wfGetSQL( "user", "user_name", "user_id=$id" );
39 /* static */ function whoIsReal( $id )
41 return wfGetSQL( "user", "user_real_name", "user_id=$id" );
44 /* static */ function idFromName( $name )
46 $nt = Title
::newFromText( $name );
47 $sql = "SELECT user_id FROM user WHERE user_name='" .
48 wfStrencode( $nt->getText() ) . "'";
49 $res = wfQuery( $sql, DB_READ
, "User::idFromName" );
51 if ( 0 == wfNumRows( $res ) ) { return 0; }
53 $s = wfFetchObject( $res );
58 # does the string match an anonymous user IP address?
59 /* static */ function isIP( $name ) {
60 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
64 /* static */ function randomPassword()
66 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
67 $l = strlen( $pwchars ) - 1;
70 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
71 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
72 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
73 $pwchars{mt_rand( 0, $l )};
77 function loadDefaults()
79 global $wgLang, $wgIP;
80 global $wgNamespacesToBeSearchedDefault;
82 $this->mId
= $this->mNewtalk
= 0;
85 $this->mPassword
= $this->mNewpassword
= "";
86 $this->mRights
= array();
87 $defOpt = $wgLang->getDefaultUserOptions() ;
88 foreach ( $defOpt as $oname => $val ) {
89 $this->mOptions
[$oname] = $val;
91 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
92 $this->mOptions
["searchNs".$nsnum] = $val;
94 unset( $this->mSkin
);
95 $this->mDataLoaded
= false;
96 $this->mBlockedby
= -1; # Unset
97 $this->mTouched
= '0'; # Allow any pages to be cached
98 $this->cookiePassword
= "";
101 /* private */ function getBlockedStatus()
103 global $wgIP, $wgBlockCache;
105 if ( -1 != $this->mBlockedby
) { return; }
107 $this->mBlockedby
= 0;
111 $block = new Block();
112 if ( $block->load( $wgIP , $this->mId
) ) {
113 $this->mBlockedby
= $block->mBy
;
114 $this->mBlockreason
= $block->mReason
;
119 if ( !$this->mBlockedby
) {
120 $block = $wgBlockCache->get( $wgIP );
121 if ( $block !== false ) {
122 $this->mBlockedby
= $block->mBy
;
123 $this->mBlockreason
= $block->mReason
;
130 $this->getBlockedStatus();
131 if ( 0 == $this->mBlockedby
) { return false; }
135 function blockedBy() {
136 $this->getBlockedStatus();
137 return $this->mBlockedby
;
140 function blockedFor() {
141 $this->getBlockedStatus();
142 return $this->mBlockreason
;
145 function SetupSession() {
146 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
147 if( $wgSessionsInMemcached ) {
148 include_once( "MemcachedSessions.php" );
150 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
151 session_cache_limiter( "private, must-revalidate" );
155 /* static */ function loadFromSession()
157 global $wgMemc, $wgDBname;
159 if ( isset( $_SESSION['wsUserID'] ) ) {
160 if ( 0 != $_SESSION['wsUserID'] ) {
161 $sId = $_SESSION['wsUserID'];
165 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
166 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
167 $_SESSION['wsUserID'] = $sId;
171 if ( isset( $_SESSION['wsUserName'] ) ) {
172 $sName = $_SESSION['wsUserName'];
173 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
174 $sName = $_COOKIE["{$wgDBname}UserName"];
175 $_SESSION['wsUserName'] = $sName;
180 $passwordCorrect = FALSE;
181 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
182 if($makenew = !$user) {
183 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
186 $user->loadFromDatabase();
188 wfDebug( "User::loadFromSession() got from cache!\n" );
191 if ( isset( $_SESSION['wsUserPassword'] ) ) {
192 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
193 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
194 $user->mCookiePassword
= $_COOKIE["{$wgDBname}Password"];
195 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword
);
196 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
198 return new User(); # Can't log in from session
201 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
203 if($wgMemc->set( $key, $user ))
204 wfDebug( "User::loadFromSession() successfully saved user\n" );
206 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
208 $user->spreadBlock();
211 return new User(); # Can't log in from session
214 function loadFromDatabase()
216 global $wgCommandLineMode;
217 if ( $this->mDataLoaded ||
$wgCommandLineMode ) {
222 $this->mId
= IntVal( $this->mId
);
224 # check in separate table if there are changes to the talk page
225 $this->mNewtalk
=0; # reset talk page status
227 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
228 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
230 if (wfNumRows($res)>0) {
233 wfFreeResult( $res );
235 global $wgDBname, $wgMemc;
236 $key = "$wgDBname:newtalk:ip:{$this->mName}";
237 $newtalk = $wgMemc->get( $key );
238 if( ! is_integer( $newtalk ) ){
239 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
240 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
242 $this->mNewtalk
= (wfNumRows($res)>0) ?
1 : 0;
243 wfFreeResult( $res );
245 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
247 $this->mNewtalk
= $newtalk ?
1 : 0;
251 $this->mDataLoaded
= true;
253 } # the following stuff is for non-anonymous users only
255 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
256 "user_real_name,user_options,user_rights,user_touched " .
257 " FROM user WHERE user_id=" . $this->mId
;
258 $res = wfQuery( $sql, DB_READ
, "User::loadFromDatabase" );
260 if ( wfNumRows( $res ) > 0 ) {
261 $s = wfFetchObject( $res );
262 $this->mName
= $s->user_name
;
263 $this->mEmail
= $s->user_email
;
264 $this->mRealName
= $s->user_real_name
;
265 $this->mPassword
= $s->user_password
;
266 $this->mNewpassword
= $s->user_newpassword
;
267 $this->decodeOptions( $s->user_options
);
268 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
269 $this->mTouched
= $s->user_touched
;
272 wfFreeResult( $res );
273 $this->mDataLoaded
= true;
276 function getID() { return $this->mId
; }
277 function setID( $v ) {
279 $this->mDataLoaded
= false;
283 $this->loadFromDatabase();
287 function setName( $str )
289 $this->loadFromDatabase();
293 function getNewtalk()
295 $this->loadFromDatabase();
296 return ( 0 != $this->mNewtalk
);
299 function setNewtalk( $val )
301 $this->loadFromDatabase();
302 $this->mNewtalk
= $val;
303 $this->invalidateCache();
306 function invalidateCache() {
307 $this->loadFromDatabase();
308 $this->mTouched
= wfTimestampNow();
309 # Don't forget to save the options after this or
310 # it won't take effect!
313 function validateCache( $timestamp ) {
314 $this->loadFromDatabase();
315 return ($timestamp >= $this->mTouched
);
318 function getPassword()
320 $this->loadFromDatabase();
321 return $this->mPassword
;
324 function getNewpassword()
326 $this->loadFromDatabase();
327 return $this->mNewpassword
;
330 function addSalt( $p )
332 global $wgPasswordSalt;
334 return md5( "{$this->mId}-{$p}" );
339 function encryptPassword( $p )
341 return $this->addSalt( md5( $p ) );
344 function setPassword( $str )
346 $this->loadFromDatabase();
347 $this->setCookiePassword( $str );
348 $this->mPassword
= $this->encryptPassword( $str );
349 $this->mNewpassword
= "";
352 function setCookiePassword( $str )
354 $this->loadFromDatabase();
355 $this->mCookiePassword
= md5( $str );
358 function setNewpassword( $str )
360 $this->loadFromDatabase();
361 $this->mNewpassword
= $this->encryptPassword( $str );
366 $this->loadFromDatabase();
367 return $this->mEmail
;
370 function setEmail( $str )
372 $this->loadFromDatabase();
373 $this->mEmail
= $str;
376 function getRealName()
378 $this->loadFromDatabase();
379 return $this->mRealName
;
382 function setRealName( $str )
384 $this->loadFromDatabase();
385 $this->mRealName
= $str;
388 function getOption( $oname )
390 $this->loadFromDatabase();
391 if ( array_key_exists( $oname, $this->mOptions
) ) {
392 return $this->mOptions
[$oname];
398 function setOption( $oname, $val )
400 $this->loadFromDatabase();
401 if ( $oname == 'skin' ) {
402 # Clear cached skin, so the new one displays immediately in Special:Preferences
403 unset( $this->mSkin
);
405 $this->mOptions
[$oname] = $val;
406 $this->invalidateCache();
411 $this->loadFromDatabase();
412 return $this->mRights
;
415 function addRight( $rname )
417 $this->loadFromDatabase();
418 array_push( $this->mRights
, $rname );
419 $this->invalidateCache();
424 $this->loadFromDatabase();
425 if ( 0 == $this->mId
) { return false; }
427 return in_array( "sysop", $this->mRights
);
430 function isDeveloper()
432 $this->loadFromDatabase();
433 if ( 0 == $this->mId
) { return false; }
435 return in_array( "developer", $this->mRights
);
438 function isBureaucrat()
440 $this->loadFromDatabase();
441 if ( 0 == $this->mId
) { return false; }
443 return in_array( "bureaucrat", $this->mRights
);
448 $this->loadFromDatabase();
450 # Why was this here? I need a UID=0 conversion script [TS]
451 # if ( 0 == $this->mId ) { return false; }
453 return in_array( "bot", $this->mRights
);
458 if ( ! isset( $this->mSkin
) ) {
459 $skinNames = Skin
::getSkinNames();
460 $s = $this->getOption( "skin" );
461 if ( "" == $s ) { $s = 'standard'; }
463 if ( !isset( $skinNames[$s] ) ) {
465 'standard' => "Standard",
466 'nostalgia' => "Nostalgia",
467 'cologneblue' => "Cologne Blue");
468 if(is_int($s) && isset( $fallback[$s]) ){
471 $sn = "SkinStandard";
474 $sn = "Skin" . $skinNames[$s];
476 $this->mSkin
= new $sn;
481 function isWatched( $title ) {
482 $wl = WatchedItem
::fromUserTitle( $this, $title );
483 return $wl->isWatched();
486 function addWatch( $title ) {
487 $wl = WatchedItem
::fromUserTitle( $this, $title );
489 $this->invalidateCache();
492 function removeWatch( $title ) {
493 $wl = WatchedItem
::fromUserTitle( $this, $title );
495 $this->invalidateCache();
499 /* private */ function encodeOptions()
502 foreach ( $this->mOptions
as $oname => $oval ) {
503 array_push( $a, "{$oname}={$oval}" );
505 $s = implode( "\n", $a );
506 return wfStrencode( $s );
509 /* private */ function decodeOptions( $str )
511 $a = explode( "\n", $str );
512 foreach ( $a as $s ) {
513 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
514 $this->mOptions
[$m[1]] = $m[2];
519 function setCookies()
521 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
522 if ( 0 == $this->mId
) return;
523 $this->loadFromDatabase();
524 $exp = time() +
$wgCookieExpiration;
526 $_SESSION['wsUserID'] = $this->mId
;
527 setcookie( "{$wgDBname}UserID", $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
529 $_SESSION['wsUserName'] = $this->mName
;
530 setcookie( "{$wgDBname}UserName", $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
532 $_SESSION['wsUserPassword'] = $this->mPassword
;
533 if ( 1 == $this->getOption( "rememberpassword" ) ) {
534 setcookie( "{$wgDBname}Password", $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
536 setcookie( "{$wgDBname}Password", "", time() - 3600 );
542 global $wgCookiePath, $wgCookieDomain, $wgDBname;
545 $_SESSION['wsUserID'] = 0;
547 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
548 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
551 function saveSettings()
553 global $wgMemc, $wgDBname;
555 if ( ! $this->mNewtalk
) {
557 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
558 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
560 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
561 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
562 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
565 if ( 0 == $this->mId
) { return; }
567 $sql = "UPDATE user SET " .
568 "user_name= '" . wfStrencode( $this->mName
) . "', " .
569 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
570 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
571 "user_real_name= '" . wfStrencode( $this->mRealName
) . "', " .
572 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
573 "user_options= '" . $this->encodeOptions() . "', " .
574 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
575 "user_touched= '" . wfStrencode( $this->mTouched
) .
576 "' WHERE user_id={$this->mId}";
577 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
578 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
581 # Checks if a user with the given name exists
586 $s = trim( $this->mName
);
587 if ( 0 == strcmp( "", $s ) ) return 0;
589 $sql = "SELECT user_id FROM user WHERE user_name='" .
590 wfStrencode( $s ) . "'";
591 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
592 if ( 0 == wfNumRows( $res ) ) { return 0; }
594 $s = wfFetchObject( $res );
595 if ( "" == $s ) return 0;
597 $gotid = $s->user_id
;
598 wfFreeResult( $res );
602 function addToDatabase()
604 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
605 "user_email, user_real_name, user_rights, user_options) " .
606 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
607 wfStrencode( $this->mPassword
) . "', '" .
608 wfStrencode( $this->mNewpassword
) . "', '" .
609 wfStrencode( $this->mEmail
) . "', '" .
610 wfStrencode( $this->mRealName
) . "', '" .
611 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
612 $this->encodeOptions() . "')";
613 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
614 $this->mId
= $this->idForName();
617 function spreadBlock()
620 # If the (non-anonymous) user is blocked, this function will block any IP address
621 # that they successfully log on from.
622 $fname = "User::spreadBlock";
624 wfDebug( "User:spreadBlock()\n" );
625 if ( $this->mId
== 0 ) {
629 $userblock = Block
::newFromDB( "", $this->mId
);
630 if ( !$userblock->isValid() ) {
634 # Check if this IP address is already blocked
635 $ipblock = Block
::newFromDB( $wgIP );
636 if ( $ipblock->isValid() ) {
637 # Just update the timestamp
638 $ipblock->updateTimestamp();
642 # Make a new block object with the desired properties
643 wfDebug( "Autoblocking {$this->mUserName}@{$wgIP}\n" );
644 $ipblock->mAddress
= $wgIP;
646 $ipblock->mBy
= $userblock->mBy
;
647 $ipblock->mReason
= wfMsg( "autoblocker", $this->getName(), $userblock->mReason
);
648 $ipblock->mTimestamp
= wfTimestampNow();
650 # If the user is already blocked with an expiry date, we don't
651 # want to pile on top of that!
652 if($userblock->mExpiry
) {
653 $ipblock->mExpiry
= min ( $userblock->mExpiry
, Block
::getAutoblockExpiry( $ipblock->mTimestamp
));
655 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $ipblock->mTimestamp
);
663 function getPageRenderingHash(){
664 static $hash = false;
669 // stubthreshold is only included below for completeness,
670 // it will always be 0 when this function is called by parsercache.
672 $confstr = $this->getOption( "quickbar" );
673 $confstr .= "!" . $this->getOption( "underline" );
674 $confstr .= "!" . $this->getOption( "hover" );
675 $confstr .= "!" . $this->getOption( "skin" );
676 $confstr .= "!" . $this->getOption( "math" );
677 $confstr .= "!" . $this->getOption( "highlightbroken" );
678 $confstr .= "!" . $this->getOption( "stubthreshold" );
679 $confstr .= "!" . $this->getOption( "editsection" );
680 $confstr .= "!" . $this->getOption( "editsectiononrightclick" );
681 $confstr .= "!" . $this->getOption( "showtoc" );
682 $confstr .= "!" . $this->getOption( "date" );
684 if(strlen($confstr) > 32)
685 $hash = md5($confstr);
691 function isAllowedToCreateAccount()
693 global $wgWhitelistAccount;
696 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
697 foreach ($wgWhitelistAccount as $right => $ok) {
698 $userHasRight = (!strcmp($right, "user") ||
in_array($right, $this->getRights()));
699 $allowed |
= ($ok && $userHasRight);
704 # Set mDataLoaded, return previous value
705 # Use this to prevent DB access in command-line scripts or similar situations
706 function setLoaded( $loaded )
708 wfSetVar( $this->mDataLoaded
, $loaded );
711 function getUserPage() {
712 return Title
::makeTitle( NS_USER
, $this->mName
);