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' );
158 } elseif( 'files' != ini_get( 'session.save_handler' ) ) {
159 # If it's left on 'user' or another setting from another
160 # application, it will end up failing. Try to recover.
161 ini_set ( 'session.save_handler', 'files' );
163 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
164 session_cache_limiter( 'private, must-revalidate' );
168 /* static */ function loadFromSession()
170 global $wgMemc, $wgDBname;
172 if ( isset( $_SESSION['wsUserID'] ) ) {
173 if ( 0 != $_SESSION['wsUserID'] ) {
174 $sId = $_SESSION['wsUserID'];
178 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
179 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
180 $_SESSION['wsUserID'] = $sId;
184 if ( isset( $_SESSION['wsUserName'] ) ) {
185 $sName = $_SESSION['wsUserName'];
186 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
187 $sName = $_COOKIE["{$wgDBname}UserName"];
188 $_SESSION['wsUserName'] = $sName;
193 $passwordCorrect = FALSE;
194 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
195 if($makenew = !$user) {
196 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
199 $user->loadFromDatabase();
201 wfDebug( "User::loadFromSession() got from cache!\n" );
204 if ( isset( $_SESSION['wsUserPassword'] ) ) {
205 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
206 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
207 $user->mCookiePassword
= $_COOKIE["{$wgDBname}Password"];
208 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword
);
209 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
211 return new User(); # Can't log in from session
214 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
216 if($wgMemc->set( $key, $user ))
217 wfDebug( "User::loadFromSession() successfully saved user\n" );
219 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
221 $user->spreadBlock();
224 return new User(); # Can't log in from session
227 function loadFromDatabase()
229 global $wgCommandLineMode;
230 $fname = "User::loadFromDatabase";
231 if ( $this->mDataLoaded ||
$wgCommandLineMode ) {
236 $this->mId
= IntVal( $this->mId
);
238 # check in separate table if there are changes to the talk page
239 $this->mNewtalk
=0; # reset talk page status
240 $dbr =& wfGetDB( DB_SLAVE
);
242 $res = $dbr->select( 'user_newtalk', 1, array( 'user_id' => $this->mId
), $fname );
244 if ( $dbr->numRows($res)>0 ) {
247 $dbr->freeResult( $res );
249 global $wgDBname, $wgMemc;
250 $key = "$wgDBname:newtalk:ip:{$this->mName}";
251 $newtalk = $wgMemc->get( $key );
252 if( ! is_integer( $newtalk ) ){
253 $res = $dbr->select( 'user_newtalk', 1, array( 'user_ip' => $this->mName
), $fname );
255 $this->mNewtalk
= $dbr->numRows( $res ) > 0 ?
1 : 0;
256 $dbr->freeResult( $res );
258 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
260 $this->mNewtalk
= $newtalk ?
1 : 0;
264 $this->mDataLoaded
= true;
266 } # the following stuff is for non-anonymous users only
268 $s = $dbr->getArray( 'user', array( 'user_name','user_password','user_newpassword','user_email',
269 'user_real_name','user_options','user_touched' ),
270 array( 'user_id' => $this->mId
), $fname );
272 if ( $s !== false ) {
273 $this->mName
= $s->user_name
;
274 $this->mEmail
= $s->user_email
;
275 $this->mRealName
= $s->user_real_name
;
276 $this->mPassword
= $s->user_password
;
277 $this->mNewpassword
= $s->user_newpassword
;
278 $this->decodeOptions( $s->user_options
);
279 $this->mTouched
= wfTimestamp(TS_MW
,$s->user_touched
);
280 $this->mRights
= explode( ",", strtolower(
281 $dbr->getField( 'user_rights', 'user_rights', array( 'user_id' => $this->mId
) )
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 ) {
300 $this->loadFromDatabase();
304 function getNewtalk() {
305 $this->loadFromDatabase();
306 return ( 0 != $this->mNewtalk
);
309 function setNewtalk( $val )
311 $this->loadFromDatabase();
312 $this->mNewtalk
= $val;
313 $this->invalidateCache();
316 function invalidateCache() {
317 $this->loadFromDatabase();
318 $this->mTouched
= wfTimestampNow();
319 # Don't forget to save the options after this or
320 # it won't take effect!
323 function validateCache( $timestamp ) {
324 $this->loadFromDatabase();
325 return ($timestamp >= $this->mTouched
);
328 function addSalt( $p ) {
329 global $wgPasswordSalt;
331 return md5( "{$this->mId}-{$p}" );
336 function encryptPassword( $p ) {
337 return $this->addSalt( md5( $p ) );
340 function setPassword( $str ) {
341 $this->loadFromDatabase();
342 $this->setCookiePassword( $str );
343 $this->mPassword
= $this->encryptPassword( $str );
344 $this->mNewpassword
= '';
347 function setCookiePassword( $str ) {
348 $this->loadFromDatabase();
349 $this->mCookiePassword
= md5( $str );
352 function setNewpassword( $str ) {
353 $this->loadFromDatabase();
354 $this->mNewpassword
= $this->encryptPassword( $str );
357 function getEmail() {
358 $this->loadFromDatabase();
359 return $this->mEmail
;
362 function setEmail( $str ) {
363 $this->loadFromDatabase();
364 $this->mEmail
= $str;
367 function getRealName() {
368 $this->loadFromDatabase();
369 return $this->mRealName
;
372 function setRealName( $str ) {
373 $this->loadFromDatabase();
374 $this->mRealName
= $str;
377 function getOption( $oname ) {
378 $this->loadFromDatabase();
379 if ( array_key_exists( $oname, $this->mOptions
) ) {
380 return $this->mOptions
[$oname];
386 function setOption( $oname, $val ) {
387 $this->loadFromDatabase();
388 if ( $oname == 'skin' ) {
389 # Clear cached skin, so the new one displays immediately in Special:Preferences
390 unset( $this->mSkin
);
392 $this->mOptions
[$oname] = $val;
393 $this->invalidateCache();
396 function getRights() {
397 $this->loadFromDatabase();
398 return $this->mRights
;
401 function addRight( $rname ) {
402 $this->loadFromDatabase();
403 array_push( $this->mRights
, $rname );
404 $this->invalidateCache();
408 $this->loadFromDatabase();
409 if ( 0 == $this->mId
) { return false; }
411 return in_array( 'sysop', $this->mRights
);
414 function isDeveloper() {
415 $this->loadFromDatabase();
416 if ( 0 == $this->mId
) { return false; }
418 return in_array( 'developer', $this->mRights
);
421 function isBureaucrat() {
422 $this->loadFromDatabase();
423 if ( 0 == $this->mId
) { return false; }
425 return in_array( 'bureaucrat', $this->mRights
);
429 $this->loadFromDatabase();
431 # Why was this here? I need a UID=0 conversion script [TS]
432 # if ( 0 == $this->mId ) { return false; }
434 return in_array( 'bot', $this->mRights
);
437 function &getSkin() {
438 if ( ! isset( $this->mSkin
) ) {
439 # get all skin names available from SkinNames.php
440 $skinNames = Skin
::getSkinNames();
442 $userSkin = $this->getOption( 'skin' );
443 if ( $userSkin == '' ) { $userSkin = 'standard'; }
445 if ( !isset( $skinNames[$userSkin] ) ) {
446 # in case the user skin could not be found find a replacement
449 1 => 'SkinNostalgia',
450 2 => 'SkinCologneBlue');
451 # if phptal is enabled we should have monobook skin that superseed
452 # the good old SkinStandard.
453 if ( isset( $skinNames['monobook'] ) ) {
454 $fallback[0] = 'SkinMonoBook';
457 if(is_numeric($userSkin) && isset( $fallback[$userSkin]) ){
458 $sn = $fallback[$userSkin];
460 $sn = 'SkinStandard';
463 # The user skin is available
464 $sn = 'Skin' . $skinNames[$userSkin];
467 # only require the needed stuff
470 require_once( 'SkinPHPTal.php' );
473 require_once( 'SkinStandard.php' );
475 case 'SkinNostalgia':
476 require_once( 'SkinNostalgia.php' );
478 case 'SkinCologneBlue':
479 require_once( 'SkinCologneBlue.php' );
482 # now we can create the skin object
483 $this->mSkin
= new $sn;
488 function isWatched( $title ) {
489 $wl = WatchedItem
::fromUserTitle( $this, $title );
490 return $wl->isWatched();
493 function addWatch( $title ) {
494 $wl = WatchedItem
::fromUserTitle( $this, $title );
496 $this->invalidateCache();
499 function removeWatch( $title ) {
500 $wl = WatchedItem
::fromUserTitle( $this, $title );
502 $this->invalidateCache();
506 /* private */ function encodeOptions() {
508 foreach ( $this->mOptions
as $oname => $oval ) {
509 array_push( $a, $oname.'='.$oval );
511 $s = implode( "\n", $a );
515 /* private */ function decodeOptions( $str ) {
516 $a = explode( "\n", $str );
517 foreach ( $a as $s ) {
518 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
519 $this->mOptions
[$m[1]] = $m[2];
524 function setCookies() {
525 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
526 if ( 0 == $this->mId
) return;
527 $this->loadFromDatabase();
528 $exp = time() +
$wgCookieExpiration;
530 $_SESSION['wsUserID'] = $this->mId
;
531 setcookie( $wgDBname.'UserID', $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
533 $_SESSION['wsUserName'] = $this->mName
;
534 setcookie( $wgDBname.'UserName', $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
536 $_SESSION['wsUserPassword'] = $this->mPassword
;
537 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
538 setcookie( $wgDBname.'Password', $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
540 setcookie( $wgDBname.'Password', '', time() - 3600 );
545 global $wgCookiePath, $wgCookieDomain, $wgDBname;
548 $_SESSION['wsUserID'] = 0;
550 setcookie( $wgDBname.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
551 setcookie( $wgDBname.'Password', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
554 function saveSettings() {
555 global $wgMemc, $wgDBname;
556 $fname = 'User::saveSettings';
558 $dbw =& wfGetDB( DB_MASTER
);
559 if ( ! $this->mNewtalk
) {
560 # Delete user_newtalk row
562 $dbw->delete( 'user_newtalk', array( 'user_id' => $this->mId
), $fname );
564 $dbw->delete( 'user_newtalk', array( 'user_ip' => $this->mName
), $fname );
565 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
568 if ( 0 == $this->mId
) { return; }
570 $dbw->update( 'user',
572 'user_name' => $this->mName
,
573 'user_password' => $this->mPassword
,
574 'user_newpassword' => $this->mNewpassword
,
575 'user_real_name' => $this->mRealName
,
576 'user_email' => $this->mEmail
,
577 'user_options' => $this->encodeOptions(),
578 'user_touched' => $dbw->timestamp($this->mTouched
)
579 ), array( /* WHERE */
580 'user_id' => $this->mId
583 $dbw->set( 'user_rights', 'user_rights', implode( ",", $this->mRights
),
584 'user_id='. $this->mId
, $fname );
585 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
588 # Checks if a user with the given name exists, returns the ID
590 function idForName() {
591 $fname = 'User::idForName';
594 $s = trim( $this->mName
);
595 if ( 0 == strcmp( '', $s ) ) return 0;
597 $dbr =& wfGetDB( DB_SLAVE
);
598 $id = $dbr->selectField( 'user', 'user_id', array( 'user_name' => $s ), $fname );
599 if ( $id === false ) {
605 function addToDatabase() {
606 $fname = 'User::addToDatabase';
607 $dbw =& wfGetDB( DB_MASTER
);
608 $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
609 $dbw->insert( 'user',
611 'user_id' => $seqVal,
612 'user_name' => $this->mName
,
613 'user_password' => $this->mPassword
,
614 'user_newpassword' => $this->mNewpassword
,
615 'user_email' => $this->mEmail
,
616 'user_real_name' => $this->mRealName
,
617 'user_options' => $this->encodeOptions()
620 $this->mId
= $dbw->insertId();
621 $dbw->insert( 'user_rights',
623 'user_id' => $this->mId
,
624 'user_rights' => implode( ',', $this->mRights
)
630 function spreadBlock()
633 # If the (non-anonymous) user is blocked, this function will block any IP address
634 # that they successfully log on from.
635 $fname = 'User::spreadBlock';
637 wfDebug( "User:spreadBlock()\n" );
638 if ( $this->mId
== 0 ) {
642 $userblock = Block
::newFromDB( '', $this->mId
);
643 if ( !$userblock->isValid() ) {
647 # Check if this IP address is already blocked
648 $ipblock = Block
::newFromDB( $wgIP );
649 if ( $ipblock->isValid() ) {
650 # Just update the timestamp
651 $ipblock->updateTimestamp();
655 # Make a new block object with the desired properties
656 wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
657 $ipblock->mAddress
= $wgIP;
659 $ipblock->mBy
= $userblock->mBy
;
660 $ipblock->mReason
= wfMsg( 'autoblocker', $this->getName(), $userblock->mReason
);
661 $ipblock->mTimestamp
= wfTimestampNow();
663 # If the user is already blocked with an expiry date, we don't
664 # want to pile on top of that!
665 if($userblock->mExpiry
) {
666 $ipblock->mExpiry
= min ( $userblock->mExpiry
, Block
::getAutoblockExpiry( $ipblock->mTimestamp
));
668 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $ipblock->mTimestamp
);
676 function getPageRenderingHash(){
681 // stubthreshold is only included below for completeness,
682 // it will always be 0 when this function is called by parsercache.
684 $confstr = $this->getOption( 'math' );
685 $confstr .= '!' . $this->getOption( 'highlightbroken' );
686 $confstr .= '!' . $this->getOption( 'stubthreshold' );
687 $confstr .= '!' . $this->getOption( 'editsection' );
688 $confstr .= '!' . $this->getOption( 'editsectiononrightclick' );
689 $confstr .= '!' . $this->getOption( 'showtoc' );
690 $confstr .= '!' . $this->getOption( 'date' );
691 $confstr .= '!' . $this->getOption( 'numberheadings' );
693 $this->mHash
= $confstr;
697 function isAllowedToCreateAccount() {
698 global $wgWhitelistAccount;
701 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
702 foreach ($wgWhitelistAccount as $right => $ok) {
703 $userHasRight = (!strcmp($right, 'user') ||
in_array($right, $this->getRights()));
704 $allowed |
= ($ok && $userHasRight);
709 # Set mDataLoaded, return previous value
710 # Use this to prevent DB access in command-line scripts or similar situations
711 function setLoaded( $loaded )
713 return wfSetVar( $this->mDataLoaded
, $loaded );
716 function getUserPage() {
717 return Title
::makeTitle( NS_USER
, $this->mName
);
720 /* static */ function getMaxID() {
721 $dbr =& wfGetDB( DB_SLAVE
);
722 return $dbr->selectField( 'user', 'max(user_id)', false );
725 function isNewbie() {
726 return $this->mId
> User
::getMaxID() * 0.99 && !$this->isSysop() && !$this->isBot() ||
$this->getID() == 0;
729 # Check to see if the given clear-text password is one of the accepted passwords
730 function checkPassword( $password ) {
731 $this->loadFromDatabase();
732 $ep = $this->encryptPassword( $password );
733 if ( 0 == strcmp( $ep, $this->mPassword
) ) {
735 } elseif ( 0 == strcmp( $ep, $this->mNewpassword
) ) {
737 } elseif ( function_exists( 'iconv' ) ) {
738 # Some wikis were converted from ISO 8859-1 to UTF-8, the passwords can't be converted
739 # Check for this with iconv
740 /* $cp1252hash = $this->encryptPassword( iconv( 'UTF-8', 'WINDOWS-1252', $password ) );
741 if ( 0 == strcmp( $cp1252hash, $this->mPassword ) ) {