4 require_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;
15 /* private */ var $mUserTable;
19 $this->loadDefaults();
22 # Static factory method
24 function newFromName( $name )
28 # Clean up name according to title rules
30 $t = Title
::newFromText( $name );
31 $u->setName( $t->getText() );
35 /* static */ function whoIs( $id )
37 return wfGetSQL( "user", "user_name", "user_id=$id" );
40 /* static */ function whoIsReal( $id )
42 return wfGetSQL( "user", "user_real_name", "user_id=$id" );
45 /* static */ function idFromName( $name )
48 $nt = Title
::newFromText( $name );
49 if( is_null( $nt ) ) {
53 $usertable=$wgIsPg?
'"user"':'user';
54 $sql = "SELECT user_id FROM $usertable WHERE user_name='" .
55 wfStrencode( $nt->getText() ) . "'";
56 $res = wfQuery( $sql, DB_READ
, "User::idFromName" );
58 if ( 0 == wfNumRows( $res ) ) {
61 $s = wfFetchObject( $res );
67 # does the string match an anonymous user IP address?
68 /* static */ function isIP( $name ) {
69 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
73 /* static */ function randomPassword()
75 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
76 $l = strlen( $pwchars ) - 1;
79 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
80 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
81 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
82 $pwchars{mt_rand( 0, $l )};
86 function loadDefaults()
88 global $wgLang, $wgIP, $wgIsPg;
89 global $wgNamespacesToBeSearchedDefault;
91 $this->mId
= $this->mNewtalk
= 0;
94 $this->mPassword
= $this->mNewpassword
= "";
95 $this->mRights
= array();
96 $defOpt = $wgLang->getDefaultUserOptions() ;
97 foreach ( $defOpt as $oname => $val ) {
98 $this->mOptions
[$oname] = $val;
100 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
101 $this->mOptions
["searchNs".$nsnum] = $val;
103 unset( $this->mSkin
);
104 $this->mDataLoaded
= false;
105 $this->mBlockedby
= -1; # Unset
106 $this->mTouched
= '0'; # Allow any pages to be cached
107 $this->cookiePassword
= "";
109 /* avoid reserved keyword usage for PostgreSQL */
110 $this->mUserTable
=$wgIsPg?
'"user"':'user';
113 /* private */ function getBlockedStatus()
115 global $wgIP, $wgBlockCache;
117 if ( -1 != $this->mBlockedby
) { return; }
119 $this->mBlockedby
= 0;
123 $block = new Block();
124 if ( $block->load( $wgIP , $this->mId
) ) {
125 $this->mBlockedby
= $block->mBy
;
126 $this->mBlockreason
= $block->mReason
;
131 if ( !$this->mBlockedby
) {
132 $block = $wgBlockCache->get( $wgIP );
133 if ( $block !== false ) {
134 $this->mBlockedby
= $block->mBy
;
135 $this->mBlockreason
= $block->mReason
;
142 $this->getBlockedStatus();
143 if ( 0 == $this->mBlockedby
) { return false; }
147 function blockedBy() {
148 $this->getBlockedStatus();
149 return $this->mBlockedby
;
152 function blockedFor() {
153 $this->getBlockedStatus();
154 return $this->mBlockreason
;
157 function SetupSession() {
158 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
159 if( $wgSessionsInMemcached ) {
160 require_once( "MemcachedSessions.php" );
162 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
163 session_cache_limiter( "private, must-revalidate" );
167 /* static */ function loadFromSession()
169 global $wgMemc, $wgDBname;
171 if ( isset( $_SESSION['wsUserID'] ) ) {
172 if ( 0 != $_SESSION['wsUserID'] ) {
173 $sId = $_SESSION['wsUserID'];
177 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
178 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
179 $_SESSION['wsUserID'] = $sId;
183 if ( isset( $_SESSION['wsUserName'] ) ) {
184 $sName = $_SESSION['wsUserName'];
185 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
186 $sName = $_COOKIE["{$wgDBname}UserName"];
187 $_SESSION['wsUserName'] = $sName;
192 $passwordCorrect = FALSE;
193 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
194 if($makenew = !$user) {
195 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
198 $user->loadFromDatabase();
200 wfDebug( "User::loadFromSession() got from cache!\n" );
203 if ( isset( $_SESSION['wsUserPassword'] ) ) {
204 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
205 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
206 $user->mCookiePassword
= $_COOKIE["{$wgDBname}Password"];
207 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword
);
208 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
210 return new User(); # Can't log in from session
213 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
215 if($wgMemc->set( $key, $user ))
216 wfDebug( "User::loadFromSession() successfully saved user\n" );
218 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
220 $user->spreadBlock();
223 return new User(); # Can't log in from session
226 function loadFromDatabase()
228 global $wgCommandLineMode;
229 if ( $this->mDataLoaded ||
$wgCommandLineMode ) {
234 $this->mId
= IntVal( $this->mId
);
236 # check in separate table if there are changes to the talk page
237 $this->mNewtalk
=0; # reset talk page status
239 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
240 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
242 if (wfNumRows($res)>0) {
245 wfFreeResult( $res );
247 global $wgDBname, $wgMemc;
248 $key = "$wgDBname:newtalk:ip:{$this->mName}";
249 $newtalk = $wgMemc->get( $key );
250 if( ! is_integer( $newtalk ) ){
251 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
252 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
254 $this->mNewtalk
= (wfNumRows($res)>0) ?
1 : 0;
255 wfFreeResult( $res );
257 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
259 $this->mNewtalk
= $newtalk ?
1 : 0;
263 $this->mDataLoaded
= true;
265 } # the following stuff is for non-anonymous users only
267 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
268 "user_real_name,user_options,user_rights,user_touched " .
269 " FROM {$this->mUserTable} WHERE user_id=" . $this->mId
;
270 $res = wfQuery( $sql, DB_READ
, "User::loadFromDatabase" );
272 if ( wfNumRows( $res ) > 0 ) {
273 $s = wfFetchObject( $res );
274 $this->mName
= $s->user_name
;
275 $this->mEmail
= $s->user_email
;
276 $this->mRealName
= $s->user_real_name
;
277 $this->mPassword
= $s->user_password
;
278 $this->mNewpassword
= $s->user_newpassword
;
279 $this->decodeOptions( $s->user_options
);
280 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
281 $this->mTouched
= $s->user_touched
;
284 wfFreeResult( $res );
285 $this->mDataLoaded
= true;
288 function getID() { return $this->mId
; }
289 function setID( $v ) {
291 $this->mDataLoaded
= false;
295 $this->loadFromDatabase();
299 function setName( $str )
301 $this->loadFromDatabase();
305 function getNewtalk()
307 $this->loadFromDatabase();
308 return ( 0 != $this->mNewtalk
);
311 function setNewtalk( $val )
313 $this->loadFromDatabase();
314 $this->mNewtalk
= $val;
315 $this->invalidateCache();
318 function invalidateCache() {
319 $this->loadFromDatabase();
320 $this->mTouched
= wfTimestampNow();
321 # Don't forget to save the options after this or
322 # it won't take effect!
325 function validateCache( $timestamp ) {
326 $this->loadFromDatabase();
327 return ($timestamp >= $this->mTouched
);
330 function getPassword()
332 $this->loadFromDatabase();
333 return $this->mPassword
;
336 function getNewpassword()
338 $this->loadFromDatabase();
339 return $this->mNewpassword
;
342 function addSalt( $p )
344 global $wgPasswordSalt;
346 return md5( "{$this->mId}-{$p}" );
351 function encryptPassword( $p )
353 return $this->addSalt( md5( $p ) );
356 function setPassword( $str )
358 $this->loadFromDatabase();
359 $this->setCookiePassword( $str );
360 $this->mPassword
= $this->encryptPassword( $str );
361 $this->mNewpassword
= "";
364 function setCookiePassword( $str )
366 $this->loadFromDatabase();
367 $this->mCookiePassword
= md5( $str );
370 function setNewpassword( $str )
372 $this->loadFromDatabase();
373 $this->mNewpassword
= $this->encryptPassword( $str );
378 $this->loadFromDatabase();
379 return $this->mEmail
;
382 function setEmail( $str )
384 $this->loadFromDatabase();
385 $this->mEmail
= $str;
388 function getRealName()
390 $this->loadFromDatabase();
391 return $this->mRealName
;
394 function setRealName( $str )
396 $this->loadFromDatabase();
397 $this->mRealName
= $str;
400 function getOption( $oname )
402 $this->loadFromDatabase();
403 if ( array_key_exists( $oname, $this->mOptions
) ) {
404 return $this->mOptions
[$oname];
410 function setOption( $oname, $val )
412 $this->loadFromDatabase();
413 if ( $oname == 'skin' ) {
414 # Clear cached skin, so the new one displays immediately in Special:Preferences
415 unset( $this->mSkin
);
417 $this->mOptions
[$oname] = $val;
418 $this->invalidateCache();
423 $this->loadFromDatabase();
424 return $this->mRights
;
427 function addRight( $rname )
429 $this->loadFromDatabase();
430 array_push( $this->mRights
, $rname );
431 $this->invalidateCache();
436 $this->loadFromDatabase();
437 if ( 0 == $this->mId
) { return false; }
439 return in_array( "sysop", $this->mRights
);
442 function isDeveloper()
444 $this->loadFromDatabase();
445 if ( 0 == $this->mId
) { return false; }
447 return in_array( "developer", $this->mRights
);
450 function isBureaucrat()
452 $this->loadFromDatabase();
453 if ( 0 == $this->mId
) { return false; }
455 return in_array( "bureaucrat", $this->mRights
);
460 $this->loadFromDatabase();
462 # Why was this here? I need a UID=0 conversion script [TS]
463 # if ( 0 == $this->mId ) { return false; }
465 return in_array( "bot", $this->mRights
);
470 if ( ! isset( $this->mSkin
) ) {
471 $skinNames = Skin
::getSkinNames();
472 $s = $this->getOption( "skin" );
473 if ( "" == $s ) { $s = 'standard'; }
475 if ( !isset( $skinNames[$s] ) ) {
478 1 => "SkinNostalgia",
479 2 => "SkinCologneBlue");
480 if ( isset( $skinNames['monobook'] ) ) {
481 $fallback[0] = "SkinMonoBook";
484 if(is_numeric($s) && isset( $fallback[$s]) ){
487 $sn = "SkinStandard";
490 $sn = "Skin" . $skinNames[$s];
492 $this->mSkin
= new $sn;
497 function isWatched( $title ) {
498 $wl = WatchedItem
::fromUserTitle( $this, $title );
499 return $wl->isWatched();
502 function addWatch( $title ) {
503 $wl = WatchedItem
::fromUserTitle( $this, $title );
505 $this->invalidateCache();
508 function removeWatch( $title ) {
509 $wl = WatchedItem
::fromUserTitle( $this, $title );
511 $this->invalidateCache();
515 /* private */ function encodeOptions()
518 foreach ( $this->mOptions
as $oname => $oval ) {
519 array_push( $a, "{$oname}={$oval}" );
521 $s = implode( "\n", $a );
522 return wfStrencode( $s );
525 /* private */ function decodeOptions( $str )
527 $a = explode( "\n", $str );
528 foreach ( $a as $s ) {
529 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
530 $this->mOptions
[$m[1]] = $m[2];
535 function setCookies()
537 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
538 if ( 0 == $this->mId
) return;
539 $this->loadFromDatabase();
540 $exp = time() +
$wgCookieExpiration;
542 $_SESSION['wsUserID'] = $this->mId
;
543 setcookie( "{$wgDBname}UserID", $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
545 $_SESSION['wsUserName'] = $this->mName
;
546 setcookie( "{$wgDBname}UserName", $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
548 $_SESSION['wsUserPassword'] = $this->mPassword
;
549 if ( 1 == $this->getOption( "rememberpassword" ) ) {
550 setcookie( "{$wgDBname}Password", $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
552 setcookie( "{$wgDBname}Password", "", time() - 3600 );
558 global $wgCookiePath, $wgCookieDomain, $wgDBname;
561 $_SESSION['wsUserID'] = 0;
563 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
564 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
567 function saveSettings()
569 global $wgMemc, $wgDBname;
570 if ( ! $this->mNewtalk
) {
572 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
573 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
575 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
576 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
577 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
580 if ( 0 == $this->mId
) { return; }
582 $sql = "UPDATE {$this->mUserTable} SET " .
583 "user_name= '" . wfStrencode( $this->mName
) . "', " .
584 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
585 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
586 "user_real_name= '" . wfStrencode( $this->mRealName
) . "', " .
587 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
588 "user_options= '" . $this->encodeOptions() . "', " .
589 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
590 "user_touched= '" . wfStrencode( $this->mTouched
) .
591 "' WHERE user_id={$this->mId}";
592 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
593 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
596 # Checks if a user with the given name exists
601 $s = trim( $this->mName
);
602 if ( 0 == strcmp( "", $s ) ) return 0;
604 $sql = "SELECT user_id FROM {$this->mUserTable} WHERE user_name='" .
605 wfStrencode( $s ) . "'";
606 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
607 if ( 0 == wfNumRows( $res ) ) { return 0; }
609 $s = wfFetchObject( $res );
610 if ( "" == $s ) return 0;
612 $gotid = $s->user_id
;
613 wfFreeResult( $res );
617 function addToDatabase()
619 $sql = "INSERT INTO {$this->mUserTable} (user_name,user_password,user_newpassword," .
620 "user_email, user_real_name, user_rights, user_options) " .
621 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
622 wfStrencode( $this->mPassword
) . "', '" .
623 wfStrencode( $this->mNewpassword
) . "', '" .
624 wfStrencode( $this->mEmail
) . "', '" .
625 wfStrencode( $this->mRealName
) . "', '" .
626 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
627 $this->encodeOptions() . "')";
628 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
629 $this->mId
= $this->idForName();
632 function spreadBlock()
635 # If the (non-anonymous) user is blocked, this function will block any IP address
636 # that they successfully log on from.
637 $fname = "User::spreadBlock";
639 wfDebug( "User:spreadBlock()\n" );
640 if ( $this->mId
== 0 ) {
644 $userblock = Block
::newFromDB( "", $this->mId
);
645 if ( !$userblock->isValid() ) {
649 # Check if this IP address is already blocked
650 $ipblock = Block
::newFromDB( $wgIP );
651 if ( $ipblock->isValid() ) {
652 # Just update the timestamp
653 $ipblock->updateTimestamp();
657 # Make a new block object with the desired properties
658 wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
659 $ipblock->mAddress
= $wgIP;
661 $ipblock->mBy
= $userblock->mBy
;
662 $ipblock->mReason
= wfMsg( "autoblocker", $this->getName(), $userblock->mReason
);
663 $ipblock->mTimestamp
= wfTimestampNow();
665 # If the user is already blocked with an expiry date, we don't
666 # want to pile on top of that!
667 if($userblock->mExpiry
) {
668 $ipblock->mExpiry
= min ( $userblock->mExpiry
, Block
::getAutoblockExpiry( $ipblock->mTimestamp
));
670 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $ipblock->mTimestamp
);
678 function getPageRenderingHash(){
679 static $hash = false;
684 // stubthreshold is only included below for completeness,
685 // it will always be 0 when this function is called by parsercache.
687 $confstr = $this->getOption( "quickbar" );
688 $confstr .= "!" . $this->getOption( "underline" );
689 $confstr .= "!" . $this->getOption( "hover" );
690 $confstr .= "!" . $this->getOption( "skin" );
691 $confstr .= "!" . $this->getOption( "math" );
692 $confstr .= "!" . $this->getOption( "highlightbroken" );
693 $confstr .= "!" . $this->getOption( "stubthreshold" );
694 $confstr .= "!" . $this->getOption( "editsection" );
695 $confstr .= "!" . $this->getOption( "editsectiononrightclick" );
696 $confstr .= "!" . $this->getOption( "showtoc" );
697 $confstr .= "!" . $this->getOption( "date" );
699 if(strlen($confstr) > 32)
700 $hash = md5($confstr);
706 function isAllowedToCreateAccount()
708 global $wgWhitelistAccount;
711 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
712 foreach ($wgWhitelistAccount as $right => $ok) {
713 $userHasRight = (!strcmp($right, "user") ||
in_array($right, $this->getRights()));
714 $allowed |
= ($ok && $userHasRight);
719 # Set mDataLoaded, return previous value
720 # Use this to prevent DB access in command-line scripts or similar situations
721 function setLoaded( $loaded )
723 wfSetVar( $this->mDataLoaded
, $loaded );
726 function getUserPage() {
727 return Title
::makeTitle( NS_USER
, $this->mName
);