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()
74 global $wgNamespacesToBeSearchedDefault;
76 $this->mId
= $this->mNewtalk
= 0;
77 $this->mName
= getenv( "REMOTE_ADDR" );
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 $wgBadRanges, $wgBadUserAgents, $wgRangeBlockUser, $wgRangeBlockReason;
99 if ( -1 != $this->mBlockedby
) { return; }
101 # Range/user-agent blocking
103 $fBlock = false; # Mmmm, Hungarian
104 if ( ( !is_array( $wgBadUserAgents ) ||
105 array_key_exists( getenv( "HTTP_USER_AGENT" ), $wgBadUserAgents ) ) &&
106 is_array( $wgBadRanges ) )
108 $iIp = ip2long( getenv( "REMOTE_ADDR" ) );
109 foreach ( $wgBadRanges as $range ) {
110 $start = ip2long( $range[0] );
111 $end = ip2long( $range[1] );
112 if ( $iIp >= $start && $iIp <= $end ) {
120 $this->mBlockedby
= $wgRangeBlockUser;
121 $this->mBlockreason
= $wgRangeBlockReason;
127 $block = new Block();
128 if ( !$block->load( getenv( "REMOTE_ADDR" ), $this->mId
) ) {
129 wfDebug( getenv( "REMOTE_ADDR" ) ." is not blocked\n" );
130 $this->mBlockedby
= 0;
134 $this->mBlockedby
= $block->mBy
;
135 $this->mBlockreason
= $block->mReason
;
140 $this->getBlockedStatus();
141 if ( 0 == $this->mBlockedby
) { return false; }
145 function blockedBy() {
146 $this->getBlockedStatus();
147 return $this->mBlockedby
;
150 function blockedFor() {
151 $this->getBlockedStatus();
152 return $this->mBlockreason
;
155 /* static */ function loadFromSession()
157 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
158 global $wgMemc, $wgDBname;
160 if ( isset( $wsUserID ) ) {
161 if ( 0 != $wsUserID ) {
166 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}UserID"] ) ) {
167 $sId = $HTTP_COOKIE_VARS["{$wgDBname}UserID"];
172 if ( isset( $wsUserName ) ) {
173 $sName = $wsUserName;
174 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}UserName"] ) ) {
175 $sName = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
176 $wsUserName = $sName;
181 $passwordCorrect = FALSE;
182 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
183 if($makenew = !$user) {
184 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
187 $user->loadFromDatabase();
189 wfDebug( "User::loadFromSession() got from cache!\n" );
192 if ( isset( $wsUserPassword ) ) {
193 $passwordCorrect = $wsUserPassword == $user->mPassword
;
194 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}Password"] ) ) {
195 $user->mCookiePassword
= $HTTP_COOKIE_VARS["{$wgDBname}Password"];
196 $wsUserPassword = $user->addSalt( $user->mCookiePassword
);
197 $passwordCorrect = $wsUserPassword == $user->mPassword
;
199 return new User(); # Can't log in from session
202 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
204 if($wgMemc->set( $key, $user ))
205 wfDebug( "User::loadFromSession() successfully saved user\n" );
207 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
209 $user->spreadBlock();
212 return new User(); # Can't log in from session
215 function loadFromDatabase()
217 if ( $this->mDataLoaded
) { return; }
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($newtalk === false) {
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
);
418 $this->loadFromDatabase();
419 if ( 0 == $this->mId
) { return false; }
421 return in_array( "bot", $this->mRights
);
426 if ( ! isset( $this->mSkin
) ) {
427 $skinNames = Skin
::getSkinNames();
428 $s = $this->getOption( "skin" );
429 if ( "" == $s ) { $s = 0; }
431 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
432 else $sn = "Skin" . $skinNames[$s];
433 $this->mSkin
= new $sn;
438 function isWatched( $title ) {
439 $wl = WatchedItem
::fromUserTitle( $this, $title );
440 return $wl->isWatched();
443 function addWatch( $title ) {
444 $wl = WatchedItem
::fromUserTitle( $this, $title );
446 $this->invalidateCache();
449 function removeWatch( $title ) {
450 $wl = WatchedItem
::fromUserTitle( $this, $title );
452 $this->invalidateCache();
456 /* private */ function encodeOptions()
459 foreach ( $this->mOptions
as $oname => $oval ) {
460 array_push( $a, "{$oname}={$oval}" );
462 $s = implode( "\n", $a );
463 return wfStrencode( $s );
466 /* private */ function decodeOptions( $str )
468 $a = explode( "\n", $str );
469 foreach ( $a as $s ) {
470 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
471 $this->mOptions
[$m[1]] = $m[2];
476 function setCookies()
478 global $wsUserID, $wsUserName, $wsUserPassword;
479 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
480 if ( 0 == $this->mId
) return;
481 $this->loadFromDatabase();
482 $exp = time() +
$wgCookieExpiration;
484 $wsUserID = $this->mId
;
485 setcookie( "{$wgDBname}UserID", $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
487 $wsUserName = $this->mName
;
488 setcookie( "{$wgDBname}UserName", $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
490 $wsUserPassword = $this->mPassword
;
491 if ( 1 == $this->getOption( "rememberpassword" ) ) {
492 setcookie( "{$wgDBname}Password", $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
494 setcookie( "{$wgDBname}Password", "", time() - 3600 );
500 global $wsUserID, $wgCookiePath, $wgCookieDomain, $wgDBname;
505 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
506 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
509 function saveSettings()
511 global $wgMemc, $wgDBname;
513 if ( ! $this->mNewtalk
) {
515 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
516 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
518 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
519 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
520 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
523 if ( 0 == $this->mId
) { return; }
525 $sql = "UPDATE user SET " .
526 "user_name= '" . wfStrencode( $this->mName
) . "', " .
527 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
528 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
529 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
530 "user_options= '" . $this->encodeOptions() . "', " .
531 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
532 "user_touched= '" . wfStrencode( $this->mTouched
) .
533 "' WHERE user_id={$this->mId}";
534 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
535 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
538 # Checks if a user with the given name exists
543 $s = trim( $this->mName
);
544 if ( 0 == strcmp( "", $s ) ) return 0;
546 $sql = "SELECT user_id FROM user WHERE user_name='" .
547 wfStrencode( $s ) . "'";
548 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
549 if ( 0 == wfNumRows( $res ) ) { return 0; }
551 $s = wfFetchObject( $res );
552 if ( "" == $s ) return 0;
554 $gotid = $s->user_id
;
555 wfFreeResult( $res );
559 function addToDatabase()
561 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
562 "user_email, user_rights, user_options) " .
563 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
564 wfStrencode( $this->mPassword
) . "', '" .
565 wfStrencode( $this->mNewpassword
) . "', '" .
566 wfStrencode( $this->mEmail
) . "', '" .
567 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
568 $this->encodeOptions() . "')";
569 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
570 $this->mId
= $this->idForName();
573 function spreadBlock()
575 # If the (non-anonymous) user is blocked, this function will block any IP address
576 # that they successfully log on from.
577 $fname = "User::spreadBlock";
579 wfDebug( "User:spreadBlock()\n" );
580 if ( $this->mId
== 0 ) {
584 $userblock = Block
::newFromDB( "", $this->mId
);
585 if ( !$userblock->isValid() ) {
589 # Check if this IP address is already blocked
590 $addr = getenv( "REMOTE_ADDR" );
591 $ipblock = Block
::newFromDB( $addr );
592 if ( $ipblock->isValid() ) {
593 # Just update the timestamp
594 $ipblock->updateTimestamp();
598 # Make a new block object with the desired properties
599 wfDebug( "Autoblocking {$this->mUserName}@{$addr}\n" );
600 $ipblock->mAddress
= $addr;
602 $ipblock->mBy
= $userblock->mBy
;
603 $ipblock->mReason
= wfMsg( "autoblocker", $this->getName(), $userblock->mReason
);
604 $ipblock->mTimestamp
= wfTimestampNow();
613 function isAllowedToCreateAccount()
615 global $wgWhitelistAccount;
618 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
619 foreach ($wgWhitelistAccount as $right => $ok) {
620 $userHasRight = (!strcmp($right, "user") ||
in_array($right, $this->getRights()));
621 $allowed |
= ($ok && $userHasRight);