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;
17 $this->loadDefaults();
20 # Static factory method
22 function newFromName( $name )
26 # Clean up name according to title rules
28 $t = Title
::newFromText( $name );
29 $u->setName( $t->getText() );
33 /* static */ function whoIs( $id )
35 return wfGetSQL( "user", "user_name", "user_id=$id" );
38 /* static */ function idFromName( $name )
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" );
45 if ( 0 == wfNumRows( $res ) ) { return 0; }
47 $s = wfFetchObject( $res );
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);
58 /* static */ function randomPassword()
60 $pwchars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
61 $l = strlen( $pwchars ) - 1;
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 )};
71 function loadDefaults()
73 global $wgLang, $wgIP;
74 global $wgNamespacesToBeSearchedDefault;
76 $this->mId
= $this->mNewtalk
= 0;
79 $this->mPassword
= $this->mNewpassword
= "";
80 $this->mRights
= array();
81 $defOpt = $wgLang->getDefaultUserOptions() ;
82 foreach ( $defOpt as $oname => $val ) {
83 $this->mOptions
[$oname] = $val;
85 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
86 $this->mOptions
["searchNs".$nsnum] = $val;
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
= "";
95 /* private */ function getBlockedStatus()
97 global $wgIP, $wgBlockCache;
99 if ( -1 != $this->mBlockedby
) { return; }
101 $this->mBlockedby
= 0;
105 $block = new Block();
106 if ( $block->load( $wgIP , $this->mId
) ) {
107 $this->mBlockedby
= $block->mBy
;
108 $this->mBlockreason
= $block->mReason
;
113 if ( !$this->mBlockedby
) {
114 $block = $wgBlockCache->get( $wgIP );
115 if ( $block !== false ) {
116 $this->mBlockedby
= $block->mBy
;
117 $this->mBlockreason
= $block->mReason
;
124 $this->getBlockedStatus();
125 if ( 0 == $this->mBlockedby
) { return false; }
129 function blockedBy() {
130 $this->getBlockedStatus();
131 return $this->mBlockedby
;
134 function blockedFor() {
135 $this->getBlockedStatus();
136 return $this->mBlockreason
;
139 function SetupSession() {
140 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
141 if( $wgSessionsInMemcached ) {
142 include_once( "MemcachedSessions.php" );
144 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
145 session_cache_limiter( "private, must-revalidate" );
149 /* static */ function loadFromSession()
151 global $wgMemc, $wgDBname;
153 if ( isset( $_SESSION['wsUserID'] ) ) {
154 if ( 0 != $_SESSION['wsUserID'] ) {
155 $sId = $_SESSION['wsUserID'];
159 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
160 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
161 $_SESSION['wsUserID'] = $sId;
165 if ( isset( $_SESSION['wsUserName'] ) ) {
166 $sName = $_SESSION['wsUserName'];
167 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
168 $sName = $_COOKIE["{$wgDBname}UserName"];
169 $_SESSION['wsUserName'] = $sName;
174 $passwordCorrect = FALSE;
175 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
176 if($makenew = !$user) {
177 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
180 $user->loadFromDatabase();
182 wfDebug( "User::loadFromSession() got from cache!\n" );
185 if ( isset( $_SESSION['wsUserPassword'] ) ) {
186 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
187 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
188 $user->mCookiePassword
= $_COOKIE["{$wgDBname}Password"];
189 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword
);
190 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
192 return new User(); # Can't log in from session
195 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
197 if($wgMemc->set( $key, $user ))
198 wfDebug( "User::loadFromSession() successfully saved user\n" );
200 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
202 $user->spreadBlock();
205 return new User(); # Can't log in from session
208 function loadFromDatabase()
210 global $wgCommandLineMode;
211 if ( $this->mDataLoaded ||
$wgCommandLineMode ) {
216 $this->mId
= IntVal( $this->mId
);
218 # check in separate table if there are changes to the talk page
219 $this->mNewtalk
=0; # reset talk page status
221 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
222 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
224 if (wfNumRows($res)>0) {
227 wfFreeResult( $res );
230 global $wgDBname, $wgMemc;
231 $key = "$wgDBname:newtalk:ip:{$this->mName}";
232 $newtalk = $wgMemc->get( $key );
233 if( ! is_integer( $newtalk ) ){
234 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
235 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
237 $this->mNewtalk
= (wfNumRows($res)>0) ?
1 : 0;
238 wfFreeResult( $res );
240 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
242 $this->mNewtalk
= $newtalk ?
1 : 0;
246 $this->mDataLoaded
= true;
248 } # the following stuff is for non-anonymous users only
250 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
251 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
253 $res = wfQuery( $sql, DB_READ
, "User::loadFromDatabase" );
255 if ( wfNumRows( $res ) > 0 ) {
256 $s = wfFetchObject( $res );
257 $this->mName
= $s->user_name
;
258 $this->mEmail
= $s->user_email
;
259 $this->mPassword
= $s->user_password
;
260 $this->mNewpassword
= $s->user_newpassword
;
261 $this->decodeOptions( $s->user_options
);
262 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
263 $this->mTouched
= $s->user_touched
;
266 wfFreeResult( $res );
267 $this->mDataLoaded
= true;
270 function getID() { return $this->mId
; }
271 function setID( $v ) {
273 $this->mDataLoaded
= false;
277 $this->loadFromDatabase();
281 function setName( $str )
283 $this->loadFromDatabase();
287 function getNewtalk()
289 $this->loadFromDatabase();
290 return ( 0 != $this->mNewtalk
);
293 function setNewtalk( $val )
295 $this->loadFromDatabase();
296 $this->mNewtalk
= $val;
297 $this->invalidateCache();
300 function invalidateCache() {
301 $this->loadFromDatabase();
302 $this->mTouched
= wfTimestampNow();
303 # Don't forget to save the options after this or
304 # it won't take effect!
307 function validateCache( $timestamp ) {
308 $this->loadFromDatabase();
309 return ($timestamp >= $this->mTouched
);
312 function getPassword()
314 $this->loadFromDatabase();
315 return $this->mPassword
;
318 function getNewpassword()
320 $this->loadFromDatabase();
321 return $this->mNewpassword
;
324 function addSalt( $p )
326 global $wgPasswordSalt;
328 return md5( "{$this->mId}-{$p}" );
333 function encryptPassword( $p )
335 return $this->addSalt( md5( $p ) );
338 function setPassword( $str )
340 $this->loadFromDatabase();
341 $this->setCookiePassword( $str );
342 $this->mPassword
= $this->encryptPassword( $str );
343 $this->mNewpassword
= "";
346 function setCookiePassword( $str )
348 $this->loadFromDatabase();
349 $this->mCookiePassword
= md5( $str );
352 function setNewpassword( $str )
354 $this->loadFromDatabase();
355 $this->mNewpassword
= $this->encryptPassword( $str );
360 $this->loadFromDatabase();
361 return $this->mEmail
;
364 function setEmail( $str )
366 $this->loadFromDatabase();
367 $this->mEmail
= $str;
370 function getOption( $oname )
372 $this->loadFromDatabase();
373 if ( array_key_exists( $oname, $this->mOptions
) ) {
374 return $this->mOptions
[$oname];
380 function setOption( $oname, $val )
382 $this->loadFromDatabase();
383 $this->mOptions
[$oname] = $val;
384 $this->invalidateCache();
389 $this->loadFromDatabase();
390 return $this->mRights
;
393 function addRight( $rname )
395 $this->loadFromDatabase();
396 array_push( $this->mRights
, $rname );
397 $this->invalidateCache();
402 $this->loadFromDatabase();
403 if ( 0 == $this->mId
) { return false; }
405 return in_array( "sysop", $this->mRights
);
408 function isDeveloper()
410 $this->loadFromDatabase();
411 if ( 0 == $this->mId
) { return false; }
413 return in_array( "developer", $this->mRights
);
416 function isBureaucrat()
418 $this->loadFromDatabase();
419 if ( 0 == $this->mId
) { return false; }
421 return in_array( "bureaucrat", $this->mRights
);
426 $this->loadFromDatabase();
427 if ( 0 == $this->mId
) { return false; }
429 return in_array( "bot", $this->mRights
);
434 if ( ! isset( $this->mSkin
) ) {
435 $skinNames = Skin
::getSkinNames();
436 $s = $this->getOption( "skin" );
437 if ( "" == $s ) { $s = 0; }
439 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
440 else $sn = "Skin" . $skinNames[$s];
441 $this->mSkin
= new $sn;
446 function isWatched( $title ) {
447 $wl = WatchedItem
::fromUserTitle( $this, $title );
448 return $wl->isWatched();
451 function addWatch( $title ) {
452 $wl = WatchedItem
::fromUserTitle( $this, $title );
454 $this->invalidateCache();
457 function removeWatch( $title ) {
458 $wl = WatchedItem
::fromUserTitle( $this, $title );
460 $this->invalidateCache();
464 /* private */ function encodeOptions()
467 foreach ( $this->mOptions
as $oname => $oval ) {
468 array_push( $a, "{$oname}={$oval}" );
470 $s = implode( "\n", $a );
471 return wfStrencode( $s );
474 /* private */ function decodeOptions( $str )
476 $a = explode( "\n", $str );
477 foreach ( $a as $s ) {
478 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
479 $this->mOptions
[$m[1]] = $m[2];
484 function setCookies()
486 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
487 if ( 0 == $this->mId
) return;
488 $this->loadFromDatabase();
489 $exp = time() +
$wgCookieExpiration;
491 $_SESSION['wsUserID'] = $this->mId
;
492 setcookie( "{$wgDBname}UserID", $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
494 $_SESSION['wsUserName'] = $this->mName
;
495 setcookie( "{$wgDBname}UserName", $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
497 $_SESSION['wsUserPassword'] = $this->mPassword
;
498 if ( 1 == $this->getOption( "rememberpassword" ) ) {
499 setcookie( "{$wgDBname}Password", $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
501 setcookie( "{$wgDBname}Password", "", time() - 3600 );
507 global $wgCookiePath, $wgCookieDomain, $wgDBname;
510 $_SESSION['wsUserID'] = 0;
512 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
513 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
516 function saveSettings()
518 global $wgMemc, $wgDBname;
520 if ( ! $this->mNewtalk
) {
522 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
523 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
525 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
526 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
527 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
530 if ( 0 == $this->mId
) { return; }
532 $sql = "UPDATE user SET " .
533 "user_name= '" . wfStrencode( $this->mName
) . "', " .
534 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
535 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
536 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
537 "user_options= '" . $this->encodeOptions() . "', " .
538 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
539 "user_touched= '" . wfStrencode( $this->mTouched
) .
540 "' WHERE user_id={$this->mId}";
541 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
542 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
545 # Checks if a user with the given name exists
550 $s = trim( $this->mName
);
551 if ( 0 == strcmp( "", $s ) ) return 0;
553 $sql = "SELECT user_id FROM user WHERE user_name='" .
554 wfStrencode( $s ) . "'";
555 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
556 if ( 0 == wfNumRows( $res ) ) { return 0; }
558 $s = wfFetchObject( $res );
559 if ( "" == $s ) return 0;
561 $gotid = $s->user_id
;
562 wfFreeResult( $res );
566 function addToDatabase()
568 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
569 "user_email, user_rights, user_options) " .
570 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
571 wfStrencode( $this->mPassword
) . "', '" .
572 wfStrencode( $this->mNewpassword
) . "', '" .
573 wfStrencode( $this->mEmail
) . "', '" .
574 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
575 $this->encodeOptions() . "')";
576 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
577 $this->mId
= $this->idForName();
580 function spreadBlock()
583 # If the (non-anonymous) user is blocked, this function will block any IP address
584 # that they successfully log on from.
585 $fname = "User::spreadBlock";
587 wfDebug( "User:spreadBlock()\n" );
588 if ( $this->mId
== 0 ) {
592 $userblock = Block
::newFromDB( "", $this->mId
);
593 if ( !$userblock->isValid() ) {
597 # Check if this IP address is already blocked
598 $ipblock = Block
::newFromDB( $wgIP );
599 if ( $ipblock->isValid() ) {
600 # Just update the timestamp
601 $ipblock->updateTimestamp();
605 # Make a new block object with the desired properties
606 wfDebug( "Autoblocking {$this->mUserName}@{$wgIP}\n" );
607 $ipblock->mAddress
= $wgIP;
609 $ipblock->mBy
= $userblock->mBy
;
610 $ipblock->mReason
= wfMsg( "autoblocker", $this->getName(), $userblock->mReason
);
611 $ipblock->mTimestamp
= wfTimestampNow();
613 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $ipblock->mTimestamp
);
620 function getPageRenderingHash(){
621 static $hash = false;
626 // stubthreshold is only included below for completeness,
627 // it will always be 0 when this function is called by parsercache.
629 $confstr = $this->getOption( "quickbar" );
630 $confstr .= "!" . $this->getOption( "underline" );
631 $confstr .= "!" . $this->getOption( "hover" );
632 $confstr .= "!" . $this->getOption( "skin" );
633 $confstr .= "!" . $this->getOption( "math" );
634 $confstr .= "!" . $this->getOption( "highlightbroken" );
635 $confstr .= "!" . $this->getOption( "stubthreshold" );
636 $confstr .= "!" . $this->getOption( "editsection" );
637 $confstr .= "!" . $this->getOption( "editsectiononrightclick" );
638 $confstr .= "!" . $this->getOption( "showtoc" );
639 $confstr .= "!" . $this->getOption( "date" );
641 if(strlen($confstr) > 32)
642 $hash = md5($confstr);
648 function isAllowedToCreateAccount()
650 global $wgWhitelistAccount;
653 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
654 foreach ($wgWhitelistAccount as $right => $ok) {
655 $userHasRight = (!strcmp($right, "user") ||
in_array($right, $this->getRights()));
656 $allowed |
= ($ok && $userHasRight);