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;
17 $this->loadDefaults();
20 # Static factory method
22 function newFromName( $name ) {
25 # Clean up name according to title rules
27 $t = Title
::newFromText( $name );
28 $u->setName( $t->getText() );
32 /* static */ function whoIs( $id ) {
33 return wfGetSQL( 'user', 'user_name', 'user_id='.$id );
36 /* static */ function whoIsReal( $id ) {
37 return wfGetSQL( 'user', 'user_real_name', 'user_id='.$id );
40 /* static */ function idFromName( $name ) {
41 $nt = Title
::newFromText( $name );
42 if( is_null( $nt ) ) {
46 $sql = "SELECT user_id FROM user WHERE user_name='" .
47 wfStrencode( $nt->getText() ) . "'";
48 $res = wfQuery( $sql, DB_READ
, 'User::idFromName' );
50 if ( 0 == wfNumRows( $res ) ) {
53 $s = wfFetchObject( $res );
59 # does the string match an anonymous user IP address?
60 /* static */ function isIP( $name ) {
61 return preg_match("/^\d{1,3}\.\d{1,3}.\d{1,3}\.\d{1,3}$/",$name);
65 /* static */ function randomPassword() {
66 $pwchars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz';
67 $l = strlen( $pwchars ) - 1;
70 $np = $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
71 $pwchars{mt_rand( 0, $l )} . chr( mt_rand(48, 57) ) .
72 $pwchars{mt_rand( 0, $l )} . $pwchars{mt_rand( 0, $l )} .
73 $pwchars{mt_rand( 0, $l )};
77 function loadDefaults() {
78 global $wgLang, $wgIP;
79 global $wgNamespacesToBeSearchedDefault;
81 $this->mId
= $this->mNewtalk
= 0;
84 $this->mPassword
= $this->mNewpassword
= '';
85 $this->mRights
= array();
86 $defOpt = $wgLang->getDefaultUserOptions() ;
87 foreach ( $defOpt as $oname => $val ) {
88 $this->mOptions
[$oname] = $val;
90 foreach ($wgNamespacesToBeSearchedDefault as $nsnum => $val) {
91 $this->mOptions
['searchNs'.$nsnum] = $val;
93 unset( $this->mSkin
);
94 $this->mDataLoaded
= false;
95 $this->mBlockedby
= -1; # Unset
96 $this->mTouched
= '0'; # Allow any pages to be cached
97 $this->cookiePassword
= '';
100 /* private */ function getBlockedStatus()
102 global $wgIP, $wgBlockCache, $wgProxyList;
104 if ( -1 != $this->mBlockedby
) { return; }
106 $this->mBlockedby
= 0;
110 $block = new Block();
111 if ( $block->load( $wgIP , $this->mId
) ) {
112 $this->mBlockedby
= $block->mBy
;
113 $this->mBlockreason
= $block->mReason
;
118 if ( !$this->mBlockedby
) {
119 $block = $wgBlockCache->get( $wgIP );
120 if ( $block !== false ) {
121 $this->mBlockedby
= $block->mBy
;
122 $this->mBlockreason
= $block->mReason
;
127 if ( !$this->mBlockedby
) {
128 if ( array_key_exists( $wgIP, $wgProxyList ) ) {
129 $this->mBlockreason
= wfMsg( 'proxyblockreason' );
130 $this->mBlockedby
= "Proxy blocker";
137 $this->getBlockedStatus();
138 if ( 0 === $this->mBlockedby
) { return false; }
142 function blockedBy() {
143 $this->getBlockedStatus();
144 return $this->mBlockedby
;
147 function blockedFor() {
148 $this->getBlockedStatus();
149 return $this->mBlockreason
;
152 function SetupSession() {
153 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
154 if( $wgSessionsInMemcached ) {
155 require_once( 'MemcachedSessions.php' );
157 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
158 session_cache_limiter( 'private, must-revalidate' );
162 /* static */ function loadFromSession()
164 global $wgMemc, $wgDBname;
166 if ( isset( $_SESSION['wsUserID'] ) ) {
167 if ( 0 != $_SESSION['wsUserID'] ) {
168 $sId = $_SESSION['wsUserID'];
172 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
173 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
174 $_SESSION['wsUserID'] = $sId;
178 if ( isset( $_SESSION['wsUserName'] ) ) {
179 $sName = $_SESSION['wsUserName'];
180 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
181 $sName = $_COOKIE["{$wgDBname}UserName"];
182 $_SESSION['wsUserName'] = $sName;
187 $passwordCorrect = FALSE;
188 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
189 if($makenew = !$user) {
190 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
193 $user->loadFromDatabase();
195 wfDebug( "User::loadFromSession() got from cache!\n" );
198 if ( isset( $_SESSION['wsUserPassword'] ) ) {
199 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
200 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
201 $user->mCookiePassword
= $_COOKIE["{$wgDBname}Password"];
202 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword
);
203 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
205 return new User(); # Can't log in from session
208 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
210 if($wgMemc->set( $key, $user ))
211 wfDebug( "User::loadFromSession() successfully saved user\n" );
213 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
215 $user->spreadBlock();
218 return new User(); # Can't log in from session
221 function loadFromDatabase()
223 global $wgCommandLineMode;
224 if ( $this->mDataLoaded ||
$wgCommandLineMode ) {
229 $this->mId
= IntVal( $this->mId
);
231 # check in separate table if there are changes to the talk page
232 $this->mNewtalk
=0; # reset talk page status
234 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
235 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
237 if (wfNumRows($res)>0) {
240 wfFreeResult( $res );
242 global $wgDBname, $wgMemc;
243 $key = "$wgDBname:newtalk:ip:{$this->mName}";
244 $newtalk = $wgMemc->get( $key );
245 if( ! is_integer( $newtalk ) ){
246 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
247 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
249 $this->mNewtalk
= (wfNumRows($res)>0) ?
1 : 0;
250 wfFreeResult( $res );
252 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
254 $this->mNewtalk
= $newtalk ?
1 : 0;
258 $this->mDataLoaded
= true;
260 } # the following stuff is for non-anonymous users only
262 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
263 "user_real_name,user_options,user_rights,user_touched " .
264 " FROM user WHERE user_id=" . $this->mId
;
265 $res = wfQuery( $sql, DB_READ
, "User::loadFromDatabase" );
267 if ( wfNumRows( $res ) > 0 ) {
268 $s = wfFetchObject( $res );
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 wfFreeResult( $res );
280 $this->mDataLoaded
= true;
283 function getID() { return $this->mId
; }
284 function setID( $v ) {
286 $this->mDataLoaded
= false;
290 $this->loadFromDatabase();
294 function setName( $str ) {
295 $this->loadFromDatabase();
299 function getNewtalk() {
300 $this->loadFromDatabase();
301 return ( 0 != $this->mNewtalk
);
304 function setNewtalk( $val )
306 $this->loadFromDatabase();
307 $this->mNewtalk
= $val;
308 $this->invalidateCache();
311 function invalidateCache() {
312 $this->loadFromDatabase();
313 $this->mTouched
= wfTimestampNow();
314 # Don't forget to save the options after this or
315 # it won't take effect!
318 function validateCache( $timestamp ) {
319 $this->loadFromDatabase();
320 return ($timestamp >= $this->mTouched
);
323 function getPassword() {
324 $this->loadFromDatabase();
325 return $this->mPassword
;
328 function getNewpassword() {
329 $this->loadFromDatabase();
330 return $this->mNewpassword
;
333 function addSalt( $p ) {
334 global $wgPasswordSalt;
336 return md5( "{$this->mId}-{$p}" );
341 function encryptPassword( $p ) {
342 return $this->addSalt( md5( $p ) );
345 function setPassword( $str ) {
346 $this->loadFromDatabase();
347 $this->setCookiePassword( $str );
348 $this->mPassword
= $this->encryptPassword( $str );
349 $this->mNewpassword
= '';
352 function setCookiePassword( $str ) {
353 $this->loadFromDatabase();
354 $this->mCookiePassword
= md5( $str );
357 function setNewpassword( $str ) {
358 $this->loadFromDatabase();
359 $this->mNewpassword
= $this->encryptPassword( $str );
362 function getEmail() {
363 $this->loadFromDatabase();
364 return $this->mEmail
;
367 function setEmail( $str ) {
368 $this->loadFromDatabase();
369 $this->mEmail
= $str;
372 function getRealName() {
373 $this->loadFromDatabase();
374 return $this->mRealName
;
377 function setRealName( $str ) {
378 $this->loadFromDatabase();
379 $this->mRealName
= $str;
382 function getOption( $oname ) {
383 $this->loadFromDatabase();
384 if ( array_key_exists( $oname, $this->mOptions
) ) {
385 return $this->mOptions
[$oname];
391 function setOption( $oname, $val ) {
392 $this->loadFromDatabase();
393 if ( $oname == 'skin' ) {
394 # Clear cached skin, so the new one displays immediately in Special:Preferences
395 unset( $this->mSkin
);
397 $this->mOptions
[$oname] = $val;
398 $this->invalidateCache();
401 function getRights() {
402 $this->loadFromDatabase();
403 return $this->mRights
;
406 function addRight( $rname ) {
407 $this->loadFromDatabase();
408 array_push( $this->mRights
, $rname );
409 $this->invalidateCache();
413 $this->loadFromDatabase();
414 if ( 0 == $this->mId
) { return false; }
416 return in_array( 'sysop', $this->mRights
);
419 function isDeveloper() {
420 $this->loadFromDatabase();
421 if ( 0 == $this->mId
) { return false; }
423 return in_array( 'developer', $this->mRights
);
426 function isBureaucrat() {
427 $this->loadFromDatabase();
428 if ( 0 == $this->mId
) { return false; }
430 return in_array( 'bureaucrat', $this->mRights
);
434 $this->loadFromDatabase();
436 # Why was this here? I need a UID=0 conversion script [TS]
437 # if ( 0 == $this->mId ) { return false; }
439 return in_array( 'bot', $this->mRights
);
442 function &getSkin() {
443 if ( ! isset( $this->mSkin
) ) {
444 # get all skin names available from SkinNames.php
445 $skinNames = Skin
::getSkinNames();
447 $userSkin = $this->getOption( 'skin' );
448 if ( $userSkin == '' ) { $userSkin = 'standard'; }
450 if ( !isset( $skinNames[$userSkin] ) ) {
451 # in case the user skin could not be found find a replacement
454 1 => 'SkinNostalgia',
455 2 => 'SkinCologneBlue');
456 # if phptal is enabled we should have monobook skin that superseed
457 # the good old SkinStandard.
458 if ( isset( $skinNames['monobook'] ) ) {
459 $fallback[0] = 'SkinMonoBook';
462 if(is_numeric($userSkin) && isset( $fallback[$userSkin]) ){
463 $sn = $fallback[$userSkin];
465 $sn = 'SkinStandard';
468 # The user skin is available
469 $sn = 'Skin' . $skinNames[$userSkin];
472 # only require the needed stuff
475 require_once( 'SkinPHPTal.php' );
478 require_once( 'SkinStandard.php' );
480 case 'SkinNostalgia':
481 require_once( 'SkinNostalgia.php' );
483 case 'SkinCologneBlue':
484 require_once( 'SkinCologneBlue.php' );
487 # now we can create the skin object
488 $this->mSkin
= new $sn;
493 function isWatched( $title ) {
494 $wl = WatchedItem
::fromUserTitle( $this, $title );
495 return $wl->isWatched();
498 function addWatch( $title ) {
499 $wl = WatchedItem
::fromUserTitle( $this, $title );
501 $this->invalidateCache();
504 function removeWatch( $title ) {
505 $wl = WatchedItem
::fromUserTitle( $this, $title );
507 $this->invalidateCache();
511 /* private */ function encodeOptions() {
513 foreach ( $this->mOptions
as $oname => $oval ) {
514 array_push( $a, $oname.'='.$oval );
516 $s = implode( "\n", $a );
517 return wfStrencode( $s );
520 /* private */ function decodeOptions( $str ) {
521 $a = explode( "\n", $str );
522 foreach ( $a as $s ) {
523 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
524 $this->mOptions
[$m[1]] = $m[2];
529 function setCookies() {
530 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
531 if ( 0 == $this->mId
) return;
532 $this->loadFromDatabase();
533 $exp = time() +
$wgCookieExpiration;
535 $_SESSION['wsUserID'] = $this->mId
;
536 setcookie( $wgDBname.'UserID', $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
538 $_SESSION['wsUserName'] = $this->mName
;
539 setcookie( $wgDBname.'UserName', $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
541 $_SESSION['wsUserPassword'] = $this->mPassword
;
542 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
543 setcookie( $wgDBname.'Password', $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
545 setcookie( $wgDBname.'Password', '', time() - 3600 );
550 global $wgCookiePath, $wgCookieDomain, $wgDBname;
553 $_SESSION['wsUserID'] = 0;
555 setcookie( $wgDBname.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
556 setcookie( $wgDBname.'Password', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
559 function saveSettings() {
560 global $wgMemc, $wgDBname;
562 if ( ! $this->mNewtalk
) {
564 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
565 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
567 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
568 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
569 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
572 if ( 0 == $this->mId
) { return; }
574 $sql = "UPDATE user SET " .
575 "user_name= '" . wfStrencode( $this->mName
) . "', " .
576 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
577 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
578 "user_real_name= '" . wfStrencode( $this->mRealName
) . "', " .
579 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
580 "user_options= '" . $this->encodeOptions() . "', " .
581 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
582 "user_touched= '" . wfStrencode( $this->mTouched
) .
583 "' WHERE user_id={$this->mId}";
584 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
585 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
588 # Checks if a user with the given name exists
590 function idForName() {
592 $s = trim( $this->mName
);
593 if ( 0 == strcmp( '', $s ) ) return 0;
595 $sql = "SELECT user_id FROM user WHERE user_name='" .
596 wfStrencode( $s ) . "'";
597 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
598 if ( 0 == wfNumRows( $res ) ) { return 0; }
600 $s = wfFetchObject( $res );
601 if ( '' == $s ) return 0;
603 $gotid = $s->user_id
;
604 wfFreeResult( $res );
608 function addToDatabase() {
609 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
610 "user_email, user_real_name, user_rights, user_options) " .
611 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
612 wfStrencode( $this->mPassword
) . "', '" .
613 wfStrencode( $this->mNewpassword
) . "', '" .
614 wfStrencode( $this->mEmail
) . "', '" .
615 wfStrencode( $this->mRealName
) . "', '" .
616 wfStrencode( implode( ',', $this->mRights
) ) . "', '" .
617 $this->encodeOptions() . "')";
618 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
619 $this->mId
= $this->idForName();
622 function spreadBlock()
625 # If the (non-anonymous) user is blocked, this function will block any IP address
626 # that they successfully log on from.
627 $fname = 'User::spreadBlock';
629 wfDebug( "User:spreadBlock()\n" );
630 if ( $this->mId
== 0 ) {
634 $userblock = Block
::newFromDB( '', $this->mId
);
635 if ( !$userblock->isValid() ) {
639 # Check if this IP address is already blocked
640 $ipblock = Block
::newFromDB( $wgIP );
641 if ( $ipblock->isValid() ) {
642 # Just update the timestamp
643 $ipblock->updateTimestamp();
647 # Make a new block object with the desired properties
648 wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
649 $ipblock->mAddress
= $wgIP;
651 $ipblock->mBy
= $userblock->mBy
;
652 $ipblock->mReason
= wfMsg( 'autoblocker', $this->getName(), $userblock->mReason
);
653 $ipblock->mTimestamp
= wfTimestampNow();
655 # If the user is already blocked with an expiry date, we don't
656 # want to pile on top of that!
657 if($userblock->mExpiry
) {
658 $ipblock->mExpiry
= min ( $userblock->mExpiry
, Block
::getAutoblockExpiry( $ipblock->mTimestamp
));
660 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $ipblock->mTimestamp
);
668 function getPageRenderingHash(){
669 static $hash = false;
674 // stubthreshold is only included below for completeness,
675 // it will always be 0 when this function is called by parsercache.
677 $confstr = $this->getOption( 'quickbar' );
678 $confstr .= '!' . $this->getOption( 'underline' );
679 $confstr .= '!' . $this->getOption( 'hover' );
680 $confstr .= '!' . $this->getOption( 'skin' );
681 $confstr .= '!' . $this->getOption( 'math' );
682 $confstr .= '!' . $this->getOption( 'highlightbroken' );
683 $confstr .= '!' . $this->getOption( 'stubthreshold' );
684 $confstr .= '!' . $this->getOption( 'editsection' );
685 $confstr .= '!' . $this->getOption( 'editsectiononrightclick' );
686 $confstr .= '!' . $this->getOption( 'showtoc' );
687 $confstr .= '!' . $this->getOption( 'date' );
689 if(strlen($confstr) > 32)
690 $hash = md5($confstr);
696 function isAllowedToCreateAccount() {
697 global $wgWhitelistAccount;
700 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
701 foreach ($wgWhitelistAccount as $right => $ok) {
702 $userHasRight = (!strcmp($right, 'user') ||
in_array($right, $this->getRights()));
703 $allowed |
= ($ok && $userHasRight);
708 # Set mDataLoaded, return previous value
709 # Use this to prevent DB access in command-line scripts or similar situations
710 function setLoaded( $loaded )
712 wfSetVar( $this->mDataLoaded
, $loaded );
715 function getUserPage() {
716 return Title
::makeTitle( NS_USER
, $this->mName
);
719 /* static */ function getMaxID() {
720 $row = wfGetArray( 'user', array('max(user_id) as m'), false );
724 function isNewbie() {
725 return $this->mId
> User
::getMaxID() * 0.99 && !$this->isSysop() && !$this->isBot() ||
$this->getID() == 0;