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 $mHash;
18 $this->loadDefaults();
21 # Static factory method
23 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 ) {
34 $dbr =& wfGetDB( DB_SLAVE
);
35 return $dbr->getField( 'user', 'user_name', array( 'user_id' => $id ) );
38 /* static */ function whoIsReal( $id ) {
39 $dbr =& wfGetDB( DB_SLAVE
);
40 return $dbr->getField( 'user', 'user_real_name', array( 'user_id' => $id ) );
43 /* static */ function idFromName( $name ) {
44 $fname = "User::idFromName";
46 $nt = Title
::newFromText( $name );
47 if( is_null( $nt ) ) {
51 $dbr =& wfGetDB( DB_SLAVE
);
52 $s = $dbr->getArray( 'user', array( 'user_id' ), array( 'user_name' => $nt->getText() ), $fname );
61 # does the string match an anonymous user IP address?
62 /* static */ function isIP( $name ) {
63 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
67 /* static */ function randomPassword() {
68 $pwchars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
69 $l = strlen( $pwchars ) - 1;
71 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
72 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
73 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
74 $pwchars{mt_rand( 0, $l )};
78 function loadDefaults() {
79 global $wgLang, $wgIP;
80 global $wgNamespacesToBeSearchedDefault;
82 $this->mId
= $this->mNewtalk
= 0;
84 $this->mRealName
= $this->mEmail
= '';
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
= '';
102 /* private */ function getBlockedStatus()
104 global $wgIP, $wgBlockCache, $wgProxyList;
106 if ( -1 != $this->mBlockedby
) { return; }
108 $this->mBlockedby
= 0;
112 $block = new Block();
113 if ( $block->load( $wgIP , $this->mId
) ) {
114 $this->mBlockedby
= $block->mBy
;
115 $this->mBlockreason
= $block->mReason
;
120 if ( !$this->mBlockedby
) {
121 $block = $wgBlockCache->get( $wgIP );
122 if ( $block !== false ) {
123 $this->mBlockedby
= $block->mBy
;
124 $this->mBlockreason
= $block->mReason
;
129 if ( !$this->mBlockedby
) {
130 if ( array_key_exists( $wgIP, $wgProxyList ) ) {
131 $this->mBlockreason
= wfMsg( 'proxyblockreason' );
132 $this->mBlockedby
= "Proxy blocker";
139 $this->getBlockedStatus();
140 if ( 0 === $this->mBlockedby
) { return false; }
144 function blockedBy() {
145 $this->getBlockedStatus();
146 return $this->mBlockedby
;
149 function blockedFor() {
150 $this->getBlockedStatus();
151 return $this->mBlockreason
;
154 function SetupSession() {
155 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
156 if( $wgSessionsInMemcached ) {
157 require_once( 'MemcachedSessions.php' );
159 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
160 session_cache_limiter( 'private, must-revalidate' );
164 /* static */ function loadFromSession()
166 global $wgMemc, $wgDBname;
168 if ( isset( $_SESSION['wsUserID'] ) ) {
169 if ( 0 != $_SESSION['wsUserID'] ) {
170 $sId = $_SESSION['wsUserID'];
174 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
175 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
176 $_SESSION['wsUserID'] = $sId;
180 if ( isset( $_SESSION['wsUserName'] ) ) {
181 $sName = $_SESSION['wsUserName'];
182 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
183 $sName = $_COOKIE["{$wgDBname}UserName"];
184 $_SESSION['wsUserName'] = $sName;
189 $passwordCorrect = FALSE;
190 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
191 if($makenew = !$user) {
192 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
195 $user->loadFromDatabase();
197 wfDebug( "User::loadFromSession() got from cache!\n" );
200 if ( isset( $_SESSION['wsUserPassword'] ) ) {
201 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
202 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
203 $user->mCookiePassword
= $_COOKIE["{$wgDBname}Password"];
204 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword
);
205 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
207 return new User(); # Can't log in from session
210 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
212 if($wgMemc->set( $key, $user ))
213 wfDebug( "User::loadFromSession() successfully saved user\n" );
215 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
217 $user->spreadBlock();
220 return new User(); # Can't log in from session
223 function loadFromDatabase()
225 global $wgCommandLineMode;
226 $fname = "User::loadFromDatabase";
227 if ( $this->mDataLoaded ||
$wgCommandLineMode ) {
232 $this->mId
= IntVal( $this->mId
);
234 # check in separate table if there are changes to the talk page
235 $this->mNewtalk
=0; # reset talk page status
236 $dbr =& wfGetDB( DB_SLAVE
);
238 $res = $dbr->select( 'user_newtalk', 1, array( 'user_id' => $this->mId
), $fname );
240 if ( $dbr->numRows($res)>0 ) {
243 $dbr->freeResult( $res );
245 global $wgDBname, $wgMemc;
246 $key = "$wgDBname:newtalk:ip:{$this->mName}";
247 $newtalk = $wgMemc->get( $key );
248 if( ! is_integer( $newtalk ) ){
249 $res = $dbr->select( 'user_newtalk', 1, array( 'user_ip' => $this->mName
), $fname );
251 $this->mNewtalk
= $dbr->numRows( $res ) > 0 ?
1 : 0;
252 $dbr->freeResult( $res );
254 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
256 $this->mNewtalk
= $newtalk ?
1 : 0;
260 $this->mDataLoaded
= true;
262 } # the following stuff is for non-anonymous users only
264 $s = $dbr->getArray( 'user', array( 'user_name','user_password','user_newpassword','user_email',
265 'user_real_name','user_options','user_rights','user_touched' ),
266 array( 'user_id' => $this->mId
), $fname );
268 if ( $s !== false ) {
269 $this->mName
= $s->user_name
;
270 $this->mEmail
= $s->user_email
;
271 $this->mRealName
= $s->user_real_name
;
272 $this->mPassword
= $s->user_password
;
273 $this->mNewpassword
= $s->user_newpassword
;
274 $this->decodeOptions( $s->user_options
);
275 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
276 $this->mTouched
= $s->user_touched
;
279 $this->mDataLoaded
= true;
282 function getID() { return $this->mId
; }
283 function setID( $v ) {
285 $this->mDataLoaded
= false;
289 $this->loadFromDatabase();
293 function setName( $str ) {
294 $this->loadFromDatabase();
298 function getNewtalk() {
299 $this->loadFromDatabase();
300 return ( 0 != $this->mNewtalk
);
303 function setNewtalk( $val )
305 $this->loadFromDatabase();
306 $this->mNewtalk
= $val;
307 $this->invalidateCache();
310 function invalidateCache() {
311 $this->loadFromDatabase();
312 $this->mTouched
= wfTimestampNow();
313 # Don't forget to save the options after this or
314 # it won't take effect!
317 function validateCache( $timestamp ) {
318 $this->loadFromDatabase();
319 return ($timestamp >= $this->mTouched
);
322 function addSalt( $p ) {
323 global $wgPasswordSalt;
325 return md5( "{$this->mId}-{$p}" );
330 function encryptPassword( $p ) {
331 return $this->addSalt( md5( $p ) );
334 function setPassword( $str ) {
335 $this->loadFromDatabase();
336 $this->setCookiePassword( $str );
337 $this->mPassword
= $this->encryptPassword( $str );
338 $this->mNewpassword
= '';
341 function setCookiePassword( $str ) {
342 $this->loadFromDatabase();
343 $this->mCookiePassword
= md5( $str );
346 function setNewpassword( $str ) {
347 $this->loadFromDatabase();
348 $this->mNewpassword
= $this->encryptPassword( $str );
351 function getEmail() {
352 $this->loadFromDatabase();
353 return $this->mEmail
;
356 function setEmail( $str ) {
357 $this->loadFromDatabase();
358 $this->mEmail
= $str;
361 function getRealName() {
362 $this->loadFromDatabase();
363 return $this->mRealName
;
366 function setRealName( $str ) {
367 $this->loadFromDatabase();
368 $this->mRealName
= $str;
371 function getOption( $oname ) {
372 $this->loadFromDatabase();
373 if ( array_key_exists( $oname, $this->mOptions
) ) {
374 return $this->mOptions
[$oname];
380 function setOption( $oname, $val ) {
381 $this->loadFromDatabase();
382 if ( $oname == 'skin' ) {
383 # Clear cached skin, so the new one displays immediately in Special:Preferences
384 unset( $this->mSkin
);
386 $this->mOptions
[$oname] = $val;
387 $this->invalidateCache();
390 function getRights() {
391 $this->loadFromDatabase();
392 return $this->mRights
;
395 function addRight( $rname ) {
396 $this->loadFromDatabase();
397 array_push( $this->mRights
, $rname );
398 $this->invalidateCache();
402 $this->loadFromDatabase();
403 if ( 0 == $this->mId
) { return false; }
405 return in_array( 'sysop', $this->mRights
);
408 function isDeveloper() {
409 $this->loadFromDatabase();
410 if ( 0 == $this->mId
) { return false; }
412 return in_array( 'developer', $this->mRights
);
415 function isBureaucrat() {
416 $this->loadFromDatabase();
417 if ( 0 == $this->mId
) { return false; }
419 return in_array( 'bureaucrat', $this->mRights
);
423 $this->loadFromDatabase();
425 # Why was this here? I need a UID=0 conversion script [TS]
426 # if ( 0 == $this->mId ) { return false; }
428 return in_array( 'bot', $this->mRights
);
431 function &getSkin() {
432 if ( ! isset( $this->mSkin
) ) {
433 # get all skin names available from SkinNames.php
434 $skinNames = Skin
::getSkinNames();
436 $userSkin = $this->getOption( 'skin' );
437 if ( $userSkin == '' ) { $userSkin = 'standard'; }
439 if ( !isset( $skinNames[$userSkin] ) ) {
440 # in case the user skin could not be found find a replacement
443 1 => 'SkinNostalgia',
444 2 => 'SkinCologneBlue');
445 # if phptal is enabled we should have monobook skin that superseed
446 # the good old SkinStandard.
447 if ( isset( $skinNames['monobook'] ) ) {
448 $fallback[0] = 'SkinMonoBook';
451 if(is_numeric($userSkin) && isset( $fallback[$userSkin]) ){
452 $sn = $fallback[$userSkin];
454 $sn = 'SkinStandard';
457 # The user skin is available
458 $sn = 'Skin' . $skinNames[$userSkin];
461 # only require the needed stuff
464 require_once( 'SkinPHPTal.php' );
467 require_once( 'SkinStandard.php' );
469 case 'SkinNostalgia':
470 require_once( 'SkinNostalgia.php' );
472 case 'SkinCologneBlue':
473 require_once( 'SkinCologneBlue.php' );
476 # now we can create the skin object
477 $this->mSkin
= new $sn;
482 function isWatched( $title ) {
483 $wl = WatchedItem
::fromUserTitle( $this, $title );
484 return $wl->isWatched();
487 function addWatch( $title ) {
488 $wl = WatchedItem
::fromUserTitle( $this, $title );
490 $this->invalidateCache();
493 function removeWatch( $title ) {
494 $wl = WatchedItem
::fromUserTitle( $this, $title );
496 $this->invalidateCache();
500 /* private */ function encodeOptions() {
502 foreach ( $this->mOptions
as $oname => $oval ) {
503 array_push( $a, $oname.'='.$oval );
505 $s = implode( "\n", $a );
509 /* private */ function decodeOptions( $str ) {
510 $a = explode( "\n", $str );
511 foreach ( $a as $s ) {
512 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
513 $this->mOptions
[$m[1]] = $m[2];
518 function setCookies() {
519 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
520 if ( 0 == $this->mId
) return;
521 $this->loadFromDatabase();
522 $exp = time() +
$wgCookieExpiration;
524 $_SESSION['wsUserID'] = $this->mId
;
525 setcookie( $wgDBname.'UserID', $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
527 $_SESSION['wsUserName'] = $this->mName
;
528 setcookie( $wgDBname.'UserName', $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
530 $_SESSION['wsUserPassword'] = $this->mPassword
;
531 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
532 setcookie( $wgDBname.'Password', $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
534 setcookie( $wgDBname.'Password', '', time() - 3600 );
539 global $wgCookiePath, $wgCookieDomain, $wgDBname;
542 $_SESSION['wsUserID'] = 0;
544 setcookie( $wgDBname.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
545 setcookie( $wgDBname.'Password', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
548 function saveSettings() {
549 global $wgMemc, $wgDBname;
550 $fname = 'User::saveSettings';
552 $dbw =& wfGetDB( DB_MASTER
);
553 if ( ! $this->mNewtalk
) {
554 # Delete user_newtalk row
556 $dbw->delete( 'user_newtalk', array( 'user_id' => $this->mId
), $fname );
558 $dbw->delete( 'user_newtalk', array( 'user_ip' => $this->mName
), $fname );
559 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
562 if ( 0 == $this->mId
) { return; }
564 $dbw->update( 'user',
566 'user_name' => $this->mName
,
567 'user_password' => $this->mPassword
,
568 'user_newpassword' => $this->mNewpassword
,
569 'user_real_name' => $this->mRealName
,
570 'user_email' => $this->mEmail
,
571 'user_options' => $this->encodeOptions(),
572 'user_rights' => implode( ",", $this->mRights
),
573 'user_touched' => $this->mTouched
574 ), array( /* WHERE */
575 'user_id' => $this->mId
578 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
581 # Checks if a user with the given name exists, returns the ID
583 function idForName() {
584 $fname = 'User::idForName';
587 $s = trim( $this->mName
);
588 if ( 0 == strcmp( '', $s ) ) return 0;
590 $dbr =& wfGetDB( DB_SLAVE
);
591 $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), $fname );
592 if ( $id === false ) {
598 function addToDatabase() {
599 $fname = 'User::addToDatabase';
600 $dbw =& wfGetDB( DB_MASTER
);
601 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
602 $dbw->insert( 'user',
604 'user_id' => $seqVal,
605 'user_name' => $this->mName
,
606 'user_password' => $this->mPassword
,
607 'user_newpassword' => $this->mNewpassword
,
608 'user_email' => $this->mEmail
,
609 'user_real_name' => $this->mRealName
,
610 'user_rights' => implode( ',', $this->mRights
),
611 'user_options' => $this->encodeOptions()
614 $this->mId
= $dbw->insertId();
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->mName}@{$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(){
668 // stubthreshold is only included below for completeness,
669 // it will always be 0 when this function is called by parsercache.
671 $confstr = $this->getOption( 'math' );
672 $confstr .= '!' . $this->getOption( 'highlightbroken' );
673 $confstr .= '!' . $this->getOption( 'stubthreshold' );
674 $confstr .= '!' . $this->getOption( 'editsection' );
675 $confstr .= '!' . $this->getOption( 'editsectiononrightclick' );
676 $confstr .= '!' . $this->getOption( 'showtoc' );
677 $confstr .= '!' . $this->getOption( 'date' );
679 $this->mHash
= $confstr;
683 function isAllowedToCreateAccount() {
684 global $wgWhitelistAccount;
687 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
688 foreach ($wgWhitelistAccount as $right => $ok) {
689 $userHasRight = (!strcmp($right, 'user') ||
in_array($right, $this->getRights()));
690 $allowed |
= ($ok && $userHasRight);
695 # Set mDataLoaded, return previous value
696 # Use this to prevent DB access in command-line scripts or similar situations
697 function setLoaded( $loaded )
699 return wfSetVar( $this->mDataLoaded
, $loaded );
702 function getUserPage() {
703 return Title
::makeTitle( NS_USER
, $this->mName
);
706 /* static */ function getMaxID() {
707 $dbr =& wfGetDB( DB_SLAVE
);
708 return $dbr->selectField( 'user', 'max(user_id)', false );
711 function isNewbie() {
712 return $this->mId
> User
::getMaxID() * 0.99 && !$this->isSysop() && !$this->isBot() ||
$this->getID() == 0;
715 # Check to see if the given clear-text password is one of the accepted passwords
716 function checkPassword( $password ) {
717 $this->loadFromDatabase();
718 $ep = $this->encryptPassword( $password );
719 if ( 0 == strcmp( $ep, $this->mPassword
) ) {
721 } elseif ( 0 == strcmp( $ep, $this->mNewpassword
) ) {
723 } elseif ( function_exists( 'iconv' ) ) {
724 # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
725 # Check for this with iconv
726 /* $cp1252hash = $this->encryptPassword( iconv( 'UTF-8', 'WINDOWS-1252', $password ) );
727 if ( 0 == strcmp( $cp1252hash, $this->mPassword ) ) {