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_scalar( $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
);
433 $this->loadFromDatabase();
434 if ( 0 == $this->mId
) { return false; }
436 return in_array( "bot", $this->mRights
);
441 if ( ! isset( $this->mSkin
) ) {
442 $skinNames = Skin
::getSkinNames();
443 $s = $this->getOption( "skin" );
444 if ( "" == $s ) { $s = 0; }
446 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
447 else $sn = "Skin" . $skinNames[$s];
448 $this->mSkin
= new $sn;
453 function isWatched( $title ) {
454 $wl = WatchedItem
::fromUserTitle( $this, $title );
455 return $wl->isWatched();
458 function addWatch( $title ) {
459 $wl = WatchedItem
::fromUserTitle( $this, $title );
461 $this->invalidateCache();
464 function removeWatch( $title ) {
465 $wl = WatchedItem
::fromUserTitle( $this, $title );
467 $this->invalidateCache();
471 /* private */ function encodeOptions()
474 foreach ( $this->mOptions
as $oname => $oval ) {
475 array_push( $a, "{$oname}={$oval}" );
477 $s = implode( "\n", $a );
478 return wfStrencode( $s );
481 /* private */ function decodeOptions( $str )
483 $a = explode( "\n", $str );
484 foreach ( $a as $s ) {
485 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
486 $this->mOptions
[$m[1]] = $m[2];
491 function setCookies()
493 global $wsUserID, $wsUserName, $wsUserPassword;
494 global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
495 if ( 0 == $this->mId
) return;
496 $this->loadFromDatabase();
497 $exp = time() +
$wgCookieExpiration;
499 $wsUserID = $this->mId
;
500 setcookie( "{$wgDBname}UserID", $this->mId
, $exp, $wgCookiePath, $wgCookieDomain );
502 $wsUserName = $this->mName
;
503 setcookie( "{$wgDBname}UserName", $this->mName
, $exp, $wgCookiePath, $wgCookieDomain );
505 $wsUserPassword = $this->mPassword
;
506 if ( 1 == $this->getOption( "rememberpassword" ) ) {
507 setcookie( "{$wgDBname}Password", $this->mCookiePassword
, $exp, $wgCookiePath, $wgCookieDomain );
509 setcookie( "{$wgDBname}Password", "", time() - 3600 );
515 global $wsUserID, $wgCookiePath, $wgCookieDomain, $wgDBname;
520 setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
521 setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
524 function saveSettings()
526 global $wgMemc, $wgDBname;
528 if ( ! $this->mNewtalk
) {
530 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
531 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
533 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
534 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
535 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
538 if ( 0 == $this->mId
) { return; }
540 $sql = "UPDATE user SET " .
541 "user_name= '" . wfStrencode( $this->mName
) . "', " .
542 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
543 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
544 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
545 "user_options= '" . $this->encodeOptions() . "', " .
546 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
547 "user_touched= '" . wfStrencode( $this->mTouched
) .
548 "' WHERE user_id={$this->mId}";
549 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
550 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
553 # Checks if a user with the given name exists
558 $s = trim( $this->mName
);
559 if ( 0 == strcmp( "", $s ) ) return 0;
561 $sql = "SELECT user_id FROM user WHERE user_name='" .
562 wfStrencode( $s ) . "'";
563 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
564 if ( 0 == wfNumRows( $res ) ) { return 0; }
566 $s = wfFetchObject( $res );
567 if ( "" == $s ) return 0;
569 $gotid = $s->user_id
;
570 wfFreeResult( $res );
574 function addToDatabase()
576 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
577 "user_email, user_rights, user_options) " .
578 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
579 wfStrencode( $this->mPassword
) . "', '" .
580 wfStrencode( $this->mNewpassword
) . "', '" .
581 wfStrencode( $this->mEmail
) . "', '" .
582 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
583 $this->encodeOptions() . "')";
584 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
585 $this->mId
= $this->idForName();
588 function spreadBlock()
591 # If the (non-anonymous) user is blocked, this function will block any IP address
592 # that they successfully log on from.
593 $fname = "User::spreadBlock";
595 wfDebug( "User:spreadBlock()\n" );
596 if ( $this->mId
== 0 ) {
600 $userblock = Block
::newFromDB( "", $this->mId
);
601 if ( !$userblock->isValid() ) {
605 # Check if this IP address is already blocked
606 $ipblock = Block
::newFromDB( $wgIP );
607 if ( $ipblock->isValid() ) {
608 # Just update the timestamp
609 $ipblock->updateTimestamp();
613 # Make a new block object with the desired properties
614 wfDebug( "Autoblocking {$this->mUserName}@{$wgIP}\n" );
615 $ipblock->mAddress
= $wgIP;
617 $ipblock->mBy
= $userblock->mBy
;
618 $ipblock->mReason
= wfMsg( "autoblocker", $this->getName(), $userblock->mReason
);
619 $ipblock->mTimestamp
= wfTimestampNow();
627 function getPageRenderingHash(){
628 static $hash = false;
633 // stubthreshold is only included below for completeness,
634 // it will always be 0 when this function is called by parsercache.
636 $confstr = $this->getOption( "quickbar" );
637 $confstr .= "!" . $this->getOption( "underline" );
638 $confstr .= "!" . $this->getOption( "hover" );
639 $confstr .= "!" . $this->getOption( "skin" );
640 $confstr .= "!" . $this->getOption( "math" );
641 $confstr .= "!" . $this->getOption( "highlightbroken" );
642 $confstr .= "!" . $this->getOption( "stubthreshold" );
643 $confstr .= "!" . $this->getOption( "editsection" );
644 $confstr .= "!" . $this->getOption( "editsectiononrightclick" );
645 $confstr .= "!" . $this->getOption( "showtoc" );
646 $confstr .= "!" . $this->getOption( "date" );
648 if(strlen($confstr) > 32)
649 $hash = md5($confstr);
655 function isAllowedToCreateAccount()
657 global $wgWhitelistAccount;
660 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
661 foreach ($wgWhitelistAccount as $right => $ok) {
662 $userHasRight = (!strcmp($right, "user") ||
in_array($right, $this->getRights()));
663 $allowed |
= ($ok && $userHasRight);