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()
73 global $wgLang, $wgIP;
74 global $wgNamespacesToBeSearchedDefault;
76 $this->mId
= $this->mNewtalk
= 0;
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, $wgIP;
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( $wgIP );
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( $wgIP , $this->mId
) ) {
129 wfDebug( $wgIP ." 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 function SetupSession() {
156 global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
157 global $wsUserID, $wsUserName, $wsUserPassword, $wsUploadFiles;
158 if( $wgSessionsInMemcached ) {
159 include_once( "MemcachedSessions.php" );
161 session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
162 session_cache_limiter( "private, must-revalidate" );
164 session_register( "wsUserID" );
165 session_register( "wsUserName" );
166 session_register( "wsUserPassword" );
167 session_register( "wsUploadFiles" );
170 /* static */ function loadFromSession()
172 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
173 global $wgMemc, $wgDBname;
175 if ( isset( $wsUserID ) ) {
176 if ( 0 != $wsUserID ) {
181 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}UserID"] ) ) {
182 $sId = $HTTP_COOKIE_VARS["{$wgDBname}UserID"];
187 if ( isset( $wsUserName ) ) {
188 $sName = $wsUserName;
189 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}UserName"] ) ) {
190 $sName = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
191 $wsUserName = $sName;
196 $passwordCorrect = FALSE;
197 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
198 if($makenew = !$user) {
199 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
202 $user->loadFromDatabase();
204 wfDebug( "User::loadFromSession() got from cache!\n" );
207 if ( isset( $wsUserPassword ) ) {
208 $passwordCorrect = $wsUserPassword == $user->mPassword
;
209 } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}Password"] ) ) {
210 $user->mCookiePassword
= $HTTP_COOKIE_VARS["{$wgDBname}Password"];
211 $wsUserPassword = $user->addSalt( $user->mCookiePassword
);
212 $passwordCorrect = $wsUserPassword == $user->mPassword
;
214 return new User(); # Can't log in from session
217 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
219 if($wgMemc->set( $key, $user ))
220 wfDebug( "User::loadFromSession() successfully saved user\n" );
222 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
224 $user->spreadBlock();
227 return new User(); # Can't log in from session
230 function loadFromDatabase()
232 if ( $this->mDataLoaded
) { return; }
233 # check in separate table if there are changes to the talk page
234 $this->mNewtalk
=0; # reset talk page status
236 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
237 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
239 if (wfNumRows($res)>0) {
242 wfFreeResult( $res );
245 global $wgDBname, $wgMemc;
246 $key = "$wgDBname:newtalk:ip:{$this->mName}";
247 $newtalk = $wgMemc->get( $key );
248 if( ! is_integer( $newtalk ) ){
249 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
250 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
252 $this->mNewtalk
= (wfNumRows($res)>0) ?
1 : 0;
253 wfFreeResult( $res );
255 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
257 $this->mNewtalk
= $newtalk ?
1 : 0;
261 $this->mDataLoaded
= true;
263 } # the following stuff is for non-anonymous users only
265 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
266 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
268 $res = wfQuery( $sql, DB_READ
, "User::loadFromDatabase" );
270 if ( wfNumRows( $res ) > 0 ) {
271 $s = wfFetchObject( $res );
272 $this->mName
= $s->user_name
;
273 $this->mEmail
= $s->user_email
;
274 $this->mPassword
= $s->user_password
;
275 $this->mNewpassword
= $s->user_newpassword
;
276 $this->decodeOptions( $s->user_options
);
277 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
278 $this->mTouched
= $s->user_touched
;
281 wfFreeResult( $res );
282 $this->mDataLoaded
= true;
285 function getID() { return $this->mId
; }
286 function setID( $v ) {
288 $this->mDataLoaded
= false;
292 $this->loadFromDatabase();
296 function setName( $str )
298 $this->loadFromDatabase();
302 function getNewtalk()
304 $this->loadFromDatabase();
305 return ( 0 != $this->mNewtalk
);
308 function setNewtalk( $val )
310 $this->loadFromDatabase();
311 $this->mNewtalk
= $val;
312 $this->invalidateCache();
315 function invalidateCache() {
316 $this->loadFromDatabase();
317 $this->mTouched
= wfTimestampNow();
318 # Don't forget to save the options after this or
319 # it won't take effect!
322 function validateCache( $timestamp ) {
323 $this->loadFromDatabase();
324 return ($timestamp >= $this->mTouched
);
327 function getPassword()
329 $this->loadFromDatabase();
330 return $this->mPassword
;
333 function getNewpassword()
335 $this->loadFromDatabase();
336 return $this->mNewpassword
;
339 function addSalt( $p )
341 global $wgPasswordSalt;
343 return md5( "{$this->mId}-{$p}" );
348 function encryptPassword( $p )
350 return $this->addSalt( md5( $p ) );
353 function setPassword( $str )
355 $this->loadFromDatabase();
356 $this->setCookiePassword( $str );
357 $this->mPassword
= $this->encryptPassword( $str );
358 $this->mNewpassword
= "";
361 function setCookiePassword( $str )
363 $this->loadFromDatabase();
364 $this->mCookiePassword
= md5( $str );
367 function setNewpassword( $str )
369 $this->loadFromDatabase();
370 $this->mNewpassword
= $this->encryptPassword( $str );
375 $this->loadFromDatabase();
376 return $this->mEmail
;
379 function setEmail( $str )
381 $this->loadFromDatabase();
382 $this->mEmail
= $str;
385 function getOption( $oname )
387 $this->loadFromDatabase();
388 if ( array_key_exists( $oname, $this->mOptions
) ) {
389 return $this->mOptions
[$oname];
395 function setOption( $oname, $val )
397 $this->loadFromDatabase();
398 $this->mOptions
[$oname] = $val;
399 $this->invalidateCache();
404 $this->loadFromDatabase();
405 return $this->mRights
;
408 function addRight( $rname )
410 $this->loadFromDatabase();
411 array_push( $this->mRights
, $rname );
412 $this->invalidateCache();
417 $this->loadFromDatabase();
418 if ( 0 == $this->mId
) { return false; }
420 return in_array( "sysop", $this->mRights
);
423 function isDeveloper()
425 $this->loadFromDatabase();
426 if ( 0 == $this->mId
) { return false; }
428 return in_array( "developer", $this->mRights
);
431 function isBureaucrat()
433 $this->loadFromDatabase();
434 if ( 0 == $this->mId
) { return false; }
436 return in_array( "bureaucrat", $this->mRights
);
441 $this->loadFromDatabase();
442 if ( 0 == $this->mId
) { return false; }
444 return in_array( "bot", $this->mRights
);
449 if ( ! isset( $this->mSkin
) ) {
450 $skinNames = Skin
::getSkinNames();
451 $s = $this->getOption( "skin" );
452 if ( "" == $s ) { $s = 0; }
454 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
455 else $sn = "Skin" . $skinNames[$s];
456 $this->mSkin
= new $sn;
461 function isWatched( $title ) {
462 $wl = WatchedItem
::fromUserTitle( $this, $title );
463 return $wl->isWatched();
466 function addWatch( $title ) {
467 $wl = WatchedItem
::fromUserTitle( $this, $title );
469 $this->invalidateCache();
472 function removeWatch( $title ) {
473 $wl = WatchedItem
::fromUserTitle( $this, $title );
475 $this->invalidateCache();
479 /* private */ function encodeOptions()
482 foreach ( $this->mOptions
as $oname => $oval ) {
483 array_push( $a, "{$oname}={$oval}" );
485 $s = implode( "\n", $a );
486 return wfStrencode( $s );
489 /* private */ function decodeOptions( $str )
491 $a = explode( "\n", $str );
492 foreach ( $a as $s ) {
493 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
494 $this->mOptions
[$m[1]] = $m[2];
499 function setCookies()
501 global $wsUserID, $wsUserName, $wsUserPassword;
502 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
503 if ( 0 == $this->mId
) return;
504 $this->loadFromDatabase();
505 $exp = time() +
$wgCookieExpiration;
507 $wsUserID = $this->mId
;
508 setcookie( "{$wgDBname}UserID", $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
510 $wsUserName = $this->mName
;
511 setcookie( "{$wgDBname}UserName", $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
513 $wsUserPassword = $this->mPassword
;
514 if ( 1 == $this->getOption( "rememberpassword" ) ) {
515 setcookie( "{$wgDBname}Password", $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
517 setcookie( "{$wgDBname}Password", "", time() - 3600 );
523 global $wsUserID, $wgCookiePath, $wgCookieDomain, $wgDBname;
528 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
529 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
532 function saveSettings()
534 global $wgMemc, $wgDBname;
536 if ( ! $this->mNewtalk
) {
538 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
539 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
541 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
542 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
543 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
546 if ( 0 == $this->mId
) { return; }
548 $sql = "UPDATE user SET " .
549 "user_name= '" . wfStrencode( $this->mName
) . "', " .
550 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
551 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
552 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
553 "user_options= '" . $this->encodeOptions() . "', " .
554 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
555 "user_touched= '" . wfStrencode( $this->mTouched
) .
556 "' WHERE user_id={$this->mId}";
557 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
558 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
561 # Checks if a user with the given name exists
566 $s = trim( $this->mName
);
567 if ( 0 == strcmp( "", $s ) ) return 0;
569 $sql = "SELECT user_id FROM user WHERE user_name='" .
570 wfStrencode( $s ) . "'";
571 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
572 if ( 0 == wfNumRows( $res ) ) { return 0; }
574 $s = wfFetchObject( $res );
575 if ( "" == $s ) return 0;
577 $gotid = $s->user_id
;
578 wfFreeResult( $res );
582 function addToDatabase()
584 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
585 "user_email, user_rights, user_options) " .
586 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
587 wfStrencode( $this->mPassword
) . "', '" .
588 wfStrencode( $this->mNewpassword
) . "', '" .
589 wfStrencode( $this->mEmail
) . "', '" .
590 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
591 $this->encodeOptions() . "')";
592 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
593 $this->mId
= $this->idForName();
596 function spreadBlock()
599 # If the (non-anonymous) user is blocked, this function will block any IP address
600 # that they successfully log on from.
601 $fname = "User::spreadBlock";
603 wfDebug( "User:spreadBlock()\n" );
604 if ( $this->mId
== 0 ) {
608 $userblock = Block
::newFromDB( "", $this->mId
);
609 if ( !$userblock->isValid() ) {
613 # Check if this IP address is already blocked
614 $ipblock = Block
::newFromDB( $wgIP );
615 if ( $ipblock->isValid() ) {
616 # Just update the timestamp
617 $ipblock->updateTimestamp();
621 # Make a new block object with the desired properties
622 wfDebug( "Autoblocking {$this->mUserName}@{$wgIP}\n" );
623 $ipblock->mAddress
= $wgIP;
625 $ipblock->mBy
= $userblock->mBy
;
626 $ipblock->mReason
= wfMsg( "autoblocker", $this->getName(), $userblock->mReason
);
627 $ipblock->mTimestamp
= wfTimestampNow();
635 function getPageRenderingHash(){
636 static $hash = false;
641 // stubthreshold is only included below for completeness,
642 // it will always be 0 when this function is called by parsercache.
644 $confstr = $this->getOption( "quickbar" );
645 $confstr .= "!" . $this->getOption( "underline" );
646 $confstr .= "!" . $this->getOption( "hover" );
647 $confstr .= "!" . $this->getOption( "skin" );
648 $confstr .= "!" . $this->getOption( "math" );
649 $confstr .= "!" . $this->getOption( "highlightbroken" );
650 $confstr .= "!" . $this->getOption( "stubthreshold" );
651 $confstr .= "!" . $this->getOption( "editsection" );
652 $confstr .= "!" . $this->getOption( "editsectiononrightclick" );
653 $confstr .= "!" . $this->getOption( "showtoc" );
654 $confstr .= "!" . $this->getOption( "date" );
656 if(strlen($confstr) > 32)
657 $hash = md5($confstr);
663 function isAllowedToCreateAccount()
665 global $wgWhitelistAccount;
668 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
669 foreach ($wgWhitelistAccount as $right => $ok) {
670 $userHasRight = (!strcmp($right, "user") ||
in_array($right, $this->getRights()));
671 $allowed |
= ($ok && $userHasRight);