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;
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
;
129 $this->getBlockedStatus();
130 if ( 0 == $this->mBlockedby
) { return false; }
134 function blockedBy() {
135 $this->getBlockedStatus();
136 return $this->mBlockedby
;
139 function blockedFor() {
140 $this->getBlockedStatus();
141 return $this->mBlockreason
;
144 function SetupSession() {
145 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
146 if( $wgSessionsInMemcached ) {
147 require_once( 'MemcachedSessions.php' );
149 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
150 session_cache_limiter( 'private, must-revalidate' );
154 /* static */ function loadFromSession()
156 global $wgMemc, $wgDBname;
158 if ( isset( $_SESSION['wsUserID'] ) ) {
159 if ( 0 != $_SESSION['wsUserID'] ) {
160 $sId = $_SESSION['wsUserID'];
164 } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
165 $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
166 $_SESSION['wsUserID'] = $sId;
170 if ( isset( $_SESSION['wsUserName'] ) ) {
171 $sName = $_SESSION['wsUserName'];
172 } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
173 $sName = $_COOKIE["{$wgDBname}UserName"];
174 $_SESSION['wsUserName'] = $sName;
179 $passwordCorrect = FALSE;
180 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
181 if($makenew = !$user) {
182 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
185 $user->loadFromDatabase();
187 wfDebug( "User::loadFromSession() got from cache!\n" );
190 if ( isset( $_SESSION['wsUserPassword'] ) ) {
191 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
192 } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
193 $user->mCookiePassword
= $_COOKIE["{$wgDBname}Password"];
194 $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword
);
195 $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword
;
197 return new User(); # Can't log in from session
200 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
202 if($wgMemc->set( $key, $user ))
203 wfDebug( "User::loadFromSession() successfully saved user\n" );
205 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
207 $user->spreadBlock();
210 return new User(); # Can't log in from session
213 function loadFromDatabase()
215 global $wgCommandLineMode;
216 if ( $this->mDataLoaded ||
$wgCommandLineMode ) {
221 $this->mId
= IntVal( $this->mId
);
223 # check in separate table if there are changes to the talk page
224 $this->mNewtalk
=0; # reset talk page status
226 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
227 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
229 if (wfNumRows($res)>0) {
232 wfFreeResult( $res );
234 global $wgDBname, $wgMemc;
235 $key = "$wgDBname:newtalk:ip:{$this->mName}";
236 $newtalk = $wgMemc->get( $key );
237 if( ! is_integer( $newtalk ) ){
238 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
239 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
241 $this->mNewtalk
= (wfNumRows($res)>0) ?
1 : 0;
242 wfFreeResult( $res );
244 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
246 $this->mNewtalk
= $newtalk ?
1 : 0;
250 $this->mDataLoaded
= true;
252 } # the following stuff is for non-anonymous users only
254 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
255 "user_real_name,user_options,user_rights,user_touched " .
256 " FROM user WHERE user_id=" . $this->mId
;
257 $res = wfQuery( $sql, DB_READ
, "User::loadFromDatabase" );
259 if ( wfNumRows( $res ) > 0 ) {
260 $s = wfFetchObject( $res );
261 $this->mName
= $s->user_name
;
262 $this->mEmail
= $s->user_email
;
263 $this->mRealName
= $s->user_real_name
;
264 $this->mPassword
= $s->user_password
;
265 $this->mNewpassword
= $s->user_newpassword
;
266 $this->decodeOptions( $s->user_options
);
267 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
268 $this->mTouched
= $s->user_touched
;
271 wfFreeResult( $res );
272 $this->mDataLoaded
= true;
275 function getID() { return $this->mId
; }
276 function setID( $v ) {
278 $this->mDataLoaded
= false;
282 $this->loadFromDatabase();
286 function setName( $str ) {
287 $this->loadFromDatabase();
291 function getNewtalk() {
292 $this->loadFromDatabase();
293 return ( 0 != $this->mNewtalk
);
296 function setNewtalk( $val )
298 $this->loadFromDatabase();
299 $this->mNewtalk
= $val;
300 $this->invalidateCache();
303 function invalidateCache() {
304 $this->loadFromDatabase();
305 $this->mTouched
= wfTimestampNow();
306 # Don't forget to save the options after this or
307 # it won't take effect!
310 function validateCache( $timestamp ) {
311 $this->loadFromDatabase();
312 return ($timestamp >= $this->mTouched
);
315 function getPassword() {
316 $this->loadFromDatabase();
317 return $this->mPassword
;
320 function getNewpassword() {
321 $this->loadFromDatabase();
322 return $this->mNewpassword
;
325 function addSalt( $p ) {
326 global $wgPasswordSalt;
328 return md5( "{$this->mId}-{$p}" );
333 function encryptPassword( $p ) {
334 return $this->addSalt( md5( $p ) );
337 function setPassword( $str ) {
338 $this->loadFromDatabase();
339 $this->setCookiePassword( $str );
340 $this->mPassword
= $this->encryptPassword( $str );
341 $this->mNewpassword
= '';
344 function setCookiePassword( $str ) {
345 $this->loadFromDatabase();
346 $this->mCookiePassword
= md5( $str );
349 function setNewpassword( $str ) {
350 $this->loadFromDatabase();
351 $this->mNewpassword
= $this->encryptPassword( $str );
354 function getEmail() {
355 $this->loadFromDatabase();
356 return $this->mEmail
;
359 function setEmail( $str ) {
360 $this->loadFromDatabase();
361 $this->mEmail
= $str;
364 function getRealName() {
365 $this->loadFromDatabase();
366 return $this->mRealName
;
369 function setRealName( $str ) {
370 $this->loadFromDatabase();
371 $this->mRealName
= $str;
374 function getOption( $oname ) {
375 $this->loadFromDatabase();
376 if ( array_key_exists( $oname, $this->mOptions
) ) {
377 return $this->mOptions
[$oname];
383 function setOption( $oname, $val ) {
384 $this->loadFromDatabase();
385 if ( $oname == 'skin' ) {
386 # Clear cached skin, so the new one displays immediately in Special:Preferences
387 unset( $this->mSkin
);
389 $this->mOptions
[$oname] = $val;
390 $this->invalidateCache();
393 function getRights() {
394 $this->loadFromDatabase();
395 return $this->mRights
;
398 function addRight( $rname ) {
399 $this->loadFromDatabase();
400 array_push( $this->mRights
, $rname );
401 $this->invalidateCache();
405 $this->loadFromDatabase();
406 if ( 0 == $this->mId
) { return false; }
408 return in_array( 'sysop', $this->mRights
);
411 function isDeveloper() {
412 $this->loadFromDatabase();
413 if ( 0 == $this->mId
) { return false; }
415 return in_array( 'developer', $this->mRights
);
418 function isBureaucrat() {
419 $this->loadFromDatabase();
420 if ( 0 == $this->mId
) { return false; }
422 return in_array( 'bureaucrat', $this->mRights
);
426 $this->loadFromDatabase();
428 # Why was this here? I need a UID=0 conversion script [TS]
429 # if ( 0 == $this->mId ) { return false; }
431 return in_array( 'bot', $this->mRights
);
434 function &getSkin() {
435 if ( ! isset( $this->mSkin
) ) {
436 # get all skin names available from SkinNames.php
437 $skinNames = Skin
::getSkinNames();
439 $userSkin = $this->getOption( 'skin' );
440 if ( $userSkin == '' ) { $userSkin = 'standard'; }
442 if ( !isset( $skinNames[$userSkin] ) ) {
443 # in case the user skin could not be found find a replacement
446 1 => 'SkinNostalgia',
447 2 => 'SkinCologneBlue');
448 # if phptal is enabled we should have monobook skin that superseed
449 # the good old SkinStandard.
450 if ( isset( $skinNames['monobook'] ) ) {
451 $fallback[0] = 'SkinMonoBook';
454 if(is_numeric($userSkin) && isset( $fallback[$userSkin]) ){
455 $sn = $fallback[$userSkin];
457 $sn = 'SkinStandard';
460 # The user skin is available
461 $sn = 'Skin' . $skinNames[$userSkin];
464 # only require the needed stuff
467 require_once( 'SkinPHPTal.php' );
470 require_once( 'SkinStandard.php' );
472 case 'SkinNostalgia':
473 require_once( 'SkinNostalgia.php' );
475 case 'SkinCologneBlue':
476 require_once( 'SkinCologneBlue.php' );
479 # now we can create the skin object
480 $this->mSkin
= new $sn;
485 function isWatched( $title ) {
486 $wl = WatchedItem
::fromUserTitle( $this, $title );
487 return $wl->isWatched();
490 function addWatch( $title ) {
491 $wl = WatchedItem
::fromUserTitle( $this, $title );
493 $this->invalidateCache();
496 function removeWatch( $title ) {
497 $wl = WatchedItem
::fromUserTitle( $this, $title );
499 $this->invalidateCache();
503 /* private */ function encodeOptions() {
505 foreach ( $this->mOptions
as $oname => $oval ) {
506 array_push( $a, $oname.'='.$oval );
508 $s = implode( "\n", $a );
509 return wfStrencode( $s );
512 /* private */ function decodeOptions( $str ) {
513 $a = explode( "\n", $str );
514 foreach ( $a as $s ) {
515 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
516 $this->mOptions
[$m[1]] = $m[2];
521 function setCookies() {
522 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
523 if ( 0 == $this->mId
) return;
524 $this->loadFromDatabase();
525 $exp = time() +
$wgCookieExpiration;
527 $_SESSION['wsUserID'] = $this->mId
;
528 setcookie( $wgDBname.'UserID', $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
530 $_SESSION['wsUserName'] = $this->mName
;
531 setcookie( $wgDBname.'UserName', $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
533 $_SESSION['wsUserPassword'] = $this->mPassword
;
534 if ( 1 == $this->getOption( 'rememberpassword' ) ) {
535 setcookie( $wgDBname.'Password', $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
537 setcookie( $wgDBname.'Password', '', time() - 3600 );
542 global $wgCookiePath, $wgCookieDomain, $wgDBname;
545 $_SESSION['wsUserID'] = 0;
547 setcookie( $wgDBname.'UserID', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
548 setcookie( $wgDBname.'Password', '', time() - 3600, $wgCookiePath, $wgCookieDomain );
551 function saveSettings() {
552 global $wgMemc, $wgDBname;
554 if ( ! $this->mNewtalk
) {
556 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
557 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
559 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
560 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
561 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
564 if ( 0 == $this->mId
) { return; }
566 $sql = "UPDATE user SET " .
567 "user_name= '" . wfStrencode( $this->mName
) . "', " .
568 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
569 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
570 "user_real_name= '" . wfStrencode( $this->mRealName
) . "', " .
571 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
572 "user_options= '" . $this->encodeOptions() . "', " .
573 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
574 "user_touched= '" . wfStrencode( $this->mTouched
) .
575 "' WHERE user_id={$this->mId}";
576 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
577 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
580 # Checks if a user with the given name exists
582 function idForName() {
584 $s = trim( $this->mName
);
585 if ( 0 == strcmp( '', $s ) ) return 0;
587 $sql = "SELECT user_id FROM user WHERE user_name='" .
588 wfStrencode( $s ) . "'";
589 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
590 if ( 0 == wfNumRows( $res ) ) { return 0; }
592 $s = wfFetchObject( $res );
593 if ( '' == $s ) return 0;
595 $gotid = $s->user_id
;
596 wfFreeResult( $res );
600 function addToDatabase() {
601 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
602 "user_email, user_real_name, user_rights, user_options) " .
603 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
604 wfStrencode( $this->mPassword
) . "', '" .
605 wfStrencode( $this->mNewpassword
) . "', '" .
606 wfStrencode( $this->mEmail
) . "', '" .
607 wfStrencode( $this->mRealName
) . "', '" .
608 wfStrencode( implode( ',', $this->mRights
) ) . "', '" .
609 $this->encodeOptions() . "')";
610 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
611 $this->mId
= $this->idForName();
614 function spreadBlock()
617 # If the (non-anonymous) user is blocked, this function will block any IP address
618 # that they successfully log on from.
619 $fname = 'User::spreadBlock';
621 wfDebug( "User:spreadBlock()\n" );
622 if ( $this->mId
== 0 ) {
626 $userblock = Block
::newFromDB( '', $this->mId
);
627 if ( !$userblock->isValid() ) {
631 # Check if this IP address is already blocked
632 $ipblock = Block
::newFromDB( $wgIP );
633 if ( $ipblock->isValid() ) {
634 # Just update the timestamp
635 $ipblock->updateTimestamp();
639 # Make a new block object with the desired properties
640 wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
641 $ipblock->mAddress
= $wgIP;
643 $ipblock->mBy
= $userblock->mBy
;
644 $ipblock->mReason
= wfMsg( 'autoblocker', $this->getName(), $userblock->mReason
);
645 $ipblock->mTimestamp
= wfTimestampNow();
647 # If the user is already blocked with an expiry date, we don't
648 # want to pile on top of that!
649 if($userblock->mExpiry
) {
650 $ipblock->mExpiry
= min ( $userblock->mExpiry
, Block
::getAutoblockExpiry( $ipblock->mTimestamp
));
652 $ipblock->mExpiry
= Block
::getAutoblockExpiry( $ipblock->mTimestamp
);
660 function getPageRenderingHash(){
661 static $hash = false;
666 // stubthreshold is only included below for completeness,
667 // it will always be 0 when this function is called by parsercache.
669 $confstr = $this->getOption( 'quickbar' );
670 $confstr .= '!' . $this->getOption( 'underline' );
671 $confstr .= '!' . $this->getOption( 'hover' );
672 $confstr .= '!' . $this->getOption( 'skin' );
673 $confstr .= '!' . $this->getOption( 'math' );
674 $confstr .= '!' . $this->getOption( 'highlightbroken' );
675 $confstr .= '!' . $this->getOption( 'stubthreshold' );
676 $confstr .= '!' . $this->getOption( 'editsection' );
677 $confstr .= '!' . $this->getOption( 'editsectiononrightclick' );
678 $confstr .= '!' . $this->getOption( 'showtoc' );
679 $confstr .= '!' . $this->getOption( 'date' );
681 if(strlen($confstr) > 32)
682 $hash = md5($confstr);
688 function isAllowedToCreateAccount() {
689 global $wgWhitelistAccount;
692 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
693 foreach ($wgWhitelistAccount as $right => $ok) {
694 $userHasRight = (!strcmp($right, 'user') ||
in_array($right, $this->getRights()));
695 $allowed |
= ($ok && $userHasRight);
700 # Set mDataLoaded, return previous value
701 # Use this to prevent DB access in command-line scripts or similar situations
702 function setLoaded( $loaded )
704 wfSetVar( $this->mDataLoaded
, $loaded );
707 function getUserPage() {
708 return Title
::makeTitle( NS_USER
, $this->mName
);