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()
74 global $wgNamespacesToBeSearchedDefault;
76 $this->mId
= $this->mNewtalk
= 0;
77 $this->mName
= getenv( "REMOTE_ADDR" );
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 if ( -1 != $this->mBlockedby
) { return; }
100 if ( !$block->load( getenv( "REMOTE_ADDR" ), $this->mId
) ) {
101 wfDebug( getenv( "REMOTE_ADDR" ) ." is not blocked\n" );
102 $this->mBlockedby
= 0;
106 $this->mBlockedby
= $block->mBy
;
107 $this->mBlockreason
= $block->mReason
;
112 $this->getBlockedStatus();
113 if ( 0 == $this->mBlockedby
) { return false; }
117 function blockedBy() {
118 $this->getBlockedStatus();
119 return $this->mBlockedby
;
122 function blockedFor() {
123 $this->getBlockedStatus();
124 return $this->mBlockreason
;
127 /* static */ function loadFromSession()
129 global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
130 global $wgMemc, $wgDBname;
132 if ( isset( $wsUserID ) ) {
133 if ( 0 != $wsUserID ) {
138 } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
139 $sId = $HTTP_COOKIE_VARS["wcUserID"];
144 if ( isset( $wsUserName ) ) {
145 $sName = $wsUserName;
146 } else if ( isset( $HTTP_COOKIE_VARS["wcUserName"] ) ) {
147 $sName = $HTTP_COOKIE_VARS["wcUserName"];
148 $wsUserName = $sName;
153 $passwordCorrect = FALSE;
154 $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
155 if($makenew = !$user) {
156 wfDebug( "User::loadFromSession() unable to load from memcached\n" );
159 $user->loadFromDatabase();
161 wfDebug( "User::loadFromSession() got from cache!\n" );
164 if ( isset( $wsUserPassword ) ) {
165 $passwordCorrect = $wsUserPassword == $user->mPassword
;
166 } else if ( isset( $HTTP_COOKIE_VARS["wcUserPassword"] ) ) {
167 $user->mCookiePassword
= $HTTP_COOKIE_VARS["wcUserPassword"];
168 $wsUserPassword = $user->addSalt( $user->mCookiePassword
);
169 $passwordCorrect = $wsUserPassword == $user->mPassword
;
171 return new User(); # Can't log in from session
174 if ( ( $sName == $user->mName
) && $passwordCorrect ) {
176 if($wgMemc->set( $key, $user ))
177 wfDebug( "User::loadFromSession() successfully saved user\n" );
179 wfDebug( "User::loadFromSession() unable to save to memcached\n" );
181 $user->spreadBlock();
184 return new User(); # Can't log in from session
187 function loadFromDatabase()
189 if ( $this->mDataLoaded
) { return; }
190 # check in separate table if there are changes to the talk page
191 $this->mNewtalk
=0; # reset talk page status
193 $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
194 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
196 if (wfNumRows($res)>0) {
199 wfFreeResult( $res );
202 global $wgDBname, $wgMemc;
203 $key = "$wgDBname:newtalk:ip:{$this->mName}";
204 $newtalk = $wgMemc->get( $key );
205 if($newtalk === false) {
206 $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
207 $res = wfQuery ($sql, DB_READ
, "User::loadFromDatabase" );
209 $this->mNewtalk
= (wfNumRows($res)>0) ?
1 : 0;
210 wfFreeResult( $res );
212 $wgMemc->set( $key, $this->mNewtalk
, time() ); // + 1800 );
214 $this->mNewtalk
= $newtalk ?
1 : 0;
218 $this->mDataLoaded
= true;
220 } # the following stuff is for non-anonymous users only
222 $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
223 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
225 $res = wfQuery( $sql, DB_READ
, "User::loadFromDatabase" );
227 if ( wfNumRows( $res ) > 0 ) {
228 $s = wfFetchObject( $res );
229 $this->mName
= $s->user_name
;
230 $this->mEmail
= $s->user_email
;
231 $this->mPassword
= $s->user_password
;
232 $this->mNewpassword
= $s->user_newpassword
;
233 $this->decodeOptions( $s->user_options
);
234 $this->mRights
= explode( ",", strtolower( $s->user_rights
) );
235 $this->mTouched
= $s->user_touched
;
238 wfFreeResult( $res );
239 $this->mDataLoaded
= true;
242 function getID() { return $this->mId
; }
243 function setID( $v ) {
245 $this->mDataLoaded
= false;
249 $this->loadFromDatabase();
253 function setName( $str )
255 $this->loadFromDatabase();
259 function getNewtalk()
261 $this->loadFromDatabase();
262 return ( 0 != $this->mNewtalk
);
265 function setNewtalk( $val )
267 $this->loadFromDatabase();
268 $this->mNewtalk
= $val;
269 $this->invalidateCache();
272 function invalidateCache() {
273 $this->loadFromDatabase();
274 $this->mTouched
= wfTimestampNow();
275 # Don't forget to save the options after this or
276 # it won't take effect!
279 function validateCache( $timestamp ) {
280 $this->loadFromDatabase();
281 return ($timestamp >= $this->mTouched
);
284 function getPassword()
286 $this->loadFromDatabase();
287 return $this->mPassword
;
290 function getNewpassword()
292 $this->loadFromDatabase();
293 return $this->mNewpassword
;
296 function addSalt( $p )
298 global $wgPasswordSalt;
300 return md5( "{$this->mId}-{$p}" );
305 function encryptPassword( $p )
307 return $this->addSalt( md5( $p ) );
310 function setPassword( $str )
312 $this->loadFromDatabase();
313 $this->setCookiePassword( $str );
314 $this->mPassword
= $this->encryptPassword( $str );
315 $this->mNewpassword
= "";
318 function setCookiePassword( $str )
320 $this->loadFromDatabase();
321 $this->mCookiePassword
= md5( $str );
324 function setNewpassword( $str )
326 $this->loadFromDatabase();
327 $this->mNewpassword
= $this->encryptPassword( $str );
332 $this->loadFromDatabase();
333 return $this->mEmail
;
336 function setEmail( $str )
338 $this->loadFromDatabase();
339 $this->mEmail
= $str;
342 function getOption( $oname )
344 $this->loadFromDatabase();
345 if ( array_key_exists( $oname, $this->mOptions
) ) {
346 return $this->mOptions
[$oname];
352 function setOption( $oname, $val )
354 $this->loadFromDatabase();
355 $this->mOptions
[$oname] = $val;
356 $this->invalidateCache();
361 $this->loadFromDatabase();
362 return $this->mRights
;
365 function addRight( $rname )
367 $this->loadFromDatabase();
368 array_push( $this->mRights
, $rname );
369 $this->invalidateCache();
374 $this->loadFromDatabase();
375 if ( 0 == $this->mId
) { return false; }
377 return in_array( "sysop", $this->mRights
);
380 function isDeveloper()
382 $this->loadFromDatabase();
383 if ( 0 == $this->mId
) { return false; }
385 return in_array( "developer", $this->mRights
);
390 $this->loadFromDatabase();
391 if ( 0 == $this->mId
) { return false; }
393 return in_array( "bot", $this->mRights
);
398 if ( ! isset( $this->mSkin
) ) {
399 $skinNames = Skin
::getSkinNames();
400 $s = $this->getOption( "skin" );
401 if ( "" == $s ) { $s = 0; }
403 if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
404 else $sn = "Skin" . $skinNames[$s];
405 $this->mSkin
= new $sn;
410 function isWatched( $title ) {
411 $wl = WatchedItem
::fromUserTitle( $this, $title );
412 return $wl->isWatched();
415 function addWatch( $title ) {
416 $wl = WatchedItem
::fromUserTitle( $this, $title );
418 $this->invalidateCache();
421 function removeWatch( $title ) {
422 $wl = WatchedItem
::fromUserTitle( $this, $title );
424 $this->invalidateCache();
428 /* private */ function encodeOptions()
431 foreach ( $this->mOptions
as $oname => $oval ) {
432 array_push( $a, "{$oname}={$oval}" );
434 $s = implode( "\n", $a );
435 return wfStrencode( $s );
438 /* private */ function decodeOptions( $str )
440 $a = explode( "\n", $str );
441 foreach ( $a as $s ) {
442 if ( preg_match( "/^(.[^=]*)=(.*)$/", $s, $m ) ) {
443 $this->mOptions
[$m[1]] = $m[2];
448 function setCookies()
450 global $wsUserID, $wsUserName, $wsUserPassword;
451 global $wgCookieExpiration;
452 if ( 0 == $this->mId
) return;
453 $this->loadFromDatabase();
454 $exp = time() +
$wgCookieExpiration;
456 $wsUserID = $this->mId
;
457 setcookie( "wcUserID", $this->mId
, $exp, "/" );
459 $wsUserName = $this->mName
;
460 setcookie( "wcUserName", $this->mName
, $exp, "/" );
462 $wsUserPassword = $this->mPassword
;
463 if ( 1 == $this->getOption( "rememberpassword" ) ) {
464 setcookie( "wcUserPassword", $this->mCookiePassword
, $exp, "/" );
466 setcookie( "wcUserPassword", "", time() - 3600 );
477 setcookie( "wcUserID", "", time() - 3600 );
478 setcookie( "wcUserPassword", "", time() - 3600 );
481 function saveSettings()
483 global $wgMemc, $wgDBname;
485 if ( ! $this->mNewtalk
) {
487 $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
488 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
490 $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
491 wfQuery ($sql, DB_WRITE
, "User::saveSettings");
492 $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
495 if ( 0 == $this->mId
) { return; }
497 $sql = "UPDATE user SET " .
498 "user_name= '" . wfStrencode( $this->mName
) . "', " .
499 "user_password= '" . wfStrencode( $this->mPassword
) . "', " .
500 "user_newpassword= '" . wfStrencode( $this->mNewpassword
) . "', " .
501 "user_email= '" . wfStrencode( $this->mEmail
) . "', " .
502 "user_options= '" . $this->encodeOptions() . "', " .
503 "user_rights= '" . wfStrencode( implode( ",", $this->mRights
) ) . "', " .
504 "user_touched= '" . wfStrencode( $this->mTouched
) .
505 "' WHERE user_id={$this->mId}";
506 wfQuery( $sql, DB_WRITE
, "User::saveSettings" );
507 $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
510 # Checks if a user with the given name exists
515 $s = trim( $this->mName
);
516 if ( 0 == strcmp( "", $s ) ) return 0;
518 $sql = "SELECT user_id FROM user WHERE user_name='" .
519 wfStrencode( $s ) . "'";
520 $res = wfQuery( $sql, DB_READ
, "User::idForName" );
521 if ( 0 == wfNumRows( $res ) ) { return 0; }
523 $s = wfFetchObject( $res );
524 if ( "" == $s ) return 0;
526 $gotid = $s->user_id
;
527 wfFreeResult( $res );
531 function addToDatabase()
533 $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
534 "user_email, user_rights, user_options) " .
535 " VALUES ('" . wfStrencode( $this->mName
) . "', '" .
536 wfStrencode( $this->mPassword
) . "', '" .
537 wfStrencode( $this->mNewpassword
) . "', '" .
538 wfStrencode( $this->mEmail
) . "', '" .
539 wfStrencode( implode( ",", $this->mRights
) ) . "', '" .
540 $this->encodeOptions() . "')";
541 wfQuery( $sql, DB_WRITE
, "User::addToDatabase" );
542 $this->mId
= $this->idForName();
545 function spreadBlock()
547 # If the (non-anonymous) user is blocked, this function will block any IP address
548 # that they successfully log on from.
549 $fname = "User::spreadBlock";
551 wfDebug( "User:spreadBlock()\n" );
552 if ( $this->mId
== 0 ) {
556 $userblock = Block
::newFromDB( "", $this->mId
);
557 if ( !$userblock->isValid() ) {
561 # Check if this IP address is already blocked
562 $addr = getenv( "REMOTE_ADDR" );
563 $ipblock = Block
::newFromDB( $addr );
564 if ( $ipblock->isValid() ) {
565 # Just update the timestamp
566 $ipblock->updateTimestamp();
570 # Make a new block object with the desired properties
571 wfDebug( "Autoblocking {$this->mUserName}@{$addr}\n" );
572 $ipblock->mAddress
= $addr;
574 $ipblock->mBy
= $userblock->mBy
;
575 $ipblock->mReason
= str_replace( "$1", $this->getName(), wfMsg( "autoblocker" ) );
576 $ipblock->mReason
= str_replace( "$2", $userblock->mReason
, $ipblock->mReason
);
577 $ipblock->mTimestamp
= wfTimestampNow();
586 function isAllowedToCreateAccount()
588 global $wgWhitelistAccount;
591 if (!$wgWhitelistAccount) { return 1; }; // default behaviour
592 foreach ($wgWhitelistAccount as $right => $ok) {
593 $userHasRight = (!strcmp($right, "user") ||
in_array($right, $this->getRights()));
594 $allowed |
= ($ok && $userHasRight);