2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* Blocks and bans object
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2004-02-14 12:37:25 +00:00
|
|
|
|
2004-09-02 23:28:24 +00:00
|
|
|
/**
|
|
|
|
|
* The block class
|
2005-08-02 13:35:19 +00:00
|
|
|
* All the functions in this class assume the object is either explicitly
|
2004-09-02 23:28:24 +00:00
|
|
|
* loaded or filled. It is not load-on-demand. There are no accessors.
|
2005-08-02 13:35:19 +00:00
|
|
|
*
|
2005-12-01 10:37:47 +00:00
|
|
|
* Globals used: $wgAutoblockExpiry, $wgAntiLockFlags
|
2004-09-03 23:00:01 +00:00
|
|
|
*
|
2004-09-02 23:28:24 +00:00
|
|
|
* @todo This could be used everywhere, but it isn't.
|
2004-09-03 23:00:01 +00:00
|
|
|
* @package MediaWiki
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2003-09-01 13:13:56 +00:00
|
|
|
class Block
|
|
|
|
|
{
|
2006-05-11 22:40:38 +00:00
|
|
|
/* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
|
2006-07-10 06:30:03 +00:00
|
|
|
$mRangeStart, $mRangeEnd, $mAnonOnly;
|
2006-05-11 22:40:38 +00:00
|
|
|
/* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mFromMaster, $mByName;
|
2006-06-08 13:21:42 +00:00
|
|
|
|
|
|
|
|
const EB_KEEP_EXPIRED = 1;
|
|
|
|
|
const EB_FOR_UPDATE = 2;
|
|
|
|
|
const EB_RANGE_ONLY = 4;
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2006-07-10 08:38:22 +00:00
|
|
|
function Block( $address = '', $user = 0, $by = 0, $reason = '',
|
2006-07-10 06:30:03 +00:00
|
|
|
$timestamp = '' , $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0 )
|
2003-09-01 13:13:56 +00:00
|
|
|
{
|
2006-07-10 06:30:03 +00:00
|
|
|
$this->mId = 0;
|
2003-09-01 13:13:56 +00:00
|
|
|
$this->mAddress = $address;
|
|
|
|
|
$this->mUser = $user;
|
|
|
|
|
$this->mBy = $by;
|
|
|
|
|
$this->mReason = $reason;
|
2004-09-07 06:20:51 +00:00
|
|
|
$this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
|
2003-09-07 13:56:25 +00:00
|
|
|
$this->mAuto = $auto;
|
2006-07-10 06:30:03 +00:00
|
|
|
$this->mAnonOnly = $anonOnly;
|
|
|
|
|
$this->mCreateAccount = $createAccount;
|
|
|
|
|
$this->mExpiry = self::decodeExpiry( $expiry );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-08-15 07:23:39 +00:00
|
|
|
$this->mForUpdate = false;
|
2005-12-01 10:37:47 +00:00
|
|
|
$this->mFromMaster = false;
|
2005-08-23 16:52:42 +00:00
|
|
|
$this->mByName = false;
|
2004-02-14 12:37:25 +00:00
|
|
|
$this->initialiseRange();
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
static function newFromDB( $address, $user = 0, $killExpired = true )
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2006-07-10 06:30:03 +00:00
|
|
|
$block = new Block();
|
|
|
|
|
$block->load( $address, $user, $killExpired );
|
|
|
|
|
if ( $block->isValid() ) {
|
|
|
|
|
return $block;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function newFromID( $id )
|
|
|
|
|
{
|
|
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
|
|
|
|
$res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
|
|
|
|
|
array( 'ipb_id' => $id ), __METHOD__ ) );
|
|
|
|
|
$block = new Block;
|
|
|
|
|
if ( $block->loadFromResult( $res ) ) {
|
|
|
|
|
return $block;
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
|
|
|
|
function clear()
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2005-08-23 16:52:42 +00:00
|
|
|
$this->mAddress = $this->mReason = $this->mTimestamp = '';
|
2006-07-10 06:30:03 +00:00
|
|
|
$this->mId = $this->mAnonOnly = $this->mCreateAccount =
|
|
|
|
|
$this->mAuto = $this->mUser = $this->mBy = 0;
|
2005-08-23 16:52:42 +00:00
|
|
|
$this->mByName = false;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2005-01-22 08:30:39 +00:00
|
|
|
/**
|
2005-12-01 10:37:47 +00:00
|
|
|
* Get the DB object and set the reference parameter to the query options
|
2005-01-22 08:30:39 +00:00
|
|
|
*/
|
2006-01-07 13:09:30 +00:00
|
|
|
function &getDBOptions( &$options )
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2005-08-26 23:02:54 +00:00
|
|
|
global $wgAntiLockFlags;
|
2005-12-01 10:37:47 +00:00
|
|
|
if ( $this->mForUpdate || $this->mFromMaster ) {
|
2004-08-15 07:23:39 +00:00
|
|
|
$db =& wfGetDB( DB_MASTER );
|
2005-12-01 10:37:47 +00:00
|
|
|
if ( !$this->mForUpdate || ($wgAntiLockFlags & ALF_NO_BLOCK_LOCK) ) {
|
2006-07-10 06:30:03 +00:00
|
|
|
$options = array();
|
2005-07-25 06:57:12 +00:00
|
|
|
} else {
|
2006-07-10 06:30:03 +00:00
|
|
|
$options = array( 'FOR UPDATE' );
|
2005-07-25 06:57:12 +00:00
|
|
|
}
|
2004-08-15 07:23:39 +00:00
|
|
|
} else {
|
|
|
|
|
$db =& wfGetDB( DB_SLAVE );
|
2006-07-10 06:30:03 +00:00
|
|
|
$options = array();
|
2004-08-15 07:23:39 +00:00
|
|
|
}
|
2005-12-01 10:37:47 +00:00
|
|
|
return $db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a ban from the DB, with either the given address or the given username
|
2006-07-10 06:30:03 +00:00
|
|
|
*
|
|
|
|
|
* @param string $address The IP address of the user, or blank to skip IP blocks
|
|
|
|
|
* @param integer $user The user ID, or zero for anonymous users
|
|
|
|
|
* @param bool $killExpired Whether to delete expired rows while loading
|
|
|
|
|
*
|
2005-12-01 10:37:47 +00:00
|
|
|
*/
|
|
|
|
|
function load( $address = '', $user = 0, $killExpired = true )
|
|
|
|
|
{
|
|
|
|
|
wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
|
|
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
$options = array();
|
2005-12-01 10:37:47 +00:00
|
|
|
$db =& $this->getDBOptions( $options );
|
|
|
|
|
|
|
|
|
|
$ret = false;
|
|
|
|
|
$killed = false;
|
2004-07-10 03:09:26 +00:00
|
|
|
|
2005-12-01 10:37:47 +00:00
|
|
|
if ( 0 == $user && $address == '' ) {
|
|
|
|
|
# Invalid user specification, not blocked
|
|
|
|
|
$this->clear();
|
|
|
|
|
return false;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
# Try user block
|
|
|
|
|
if ( $user ) {
|
|
|
|
|
$res = $db->resultObject( $db->select( 'ipblocks', '*', array( 'ipb_user' => $user ),
|
|
|
|
|
__METHOD__, $options ) );
|
|
|
|
|
if ( $this->loadFromResult( $res, $killExpired ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Try IP block
|
2006-07-11 05:30:35 +00:00
|
|
|
# TODO: improve performance by merging this query with the autoblock one
|
|
|
|
|
# Slightly tricky while handling killExpired as well
|
2006-07-10 06:30:03 +00:00
|
|
|
if ( $address ) {
|
2006-07-11 05:30:35 +00:00
|
|
|
$conds = array( 'ipb_address' => $address, 'ipb_auto' => 0 );
|
2006-07-10 06:30:03 +00:00
|
|
|
$res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
|
|
|
|
|
if ( $this->loadFromResult( $res, $killExpired ) ) {
|
2006-07-11 05:30:35 +00:00
|
|
|
if ( $user && $this->mAnonOnly ) {
|
|
|
|
|
# Block is marked anon-only
|
|
|
|
|
# Whitelist this IP address against autoblocks and range blocks
|
|
|
|
|
$this->clear();
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2006-07-10 06:30:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Try range block
|
|
|
|
|
if ( $this->loadRange( $address, $killExpired, $user == 0 ) ) {
|
2006-07-11 05:30:35 +00:00
|
|
|
if ( $user && $this->mAnonOnly ) {
|
|
|
|
|
$this->clear();
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2006-07-10 06:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-11 05:30:35 +00:00
|
|
|
# Try autoblock
|
|
|
|
|
if ( $address ) {
|
|
|
|
|
$conds = array( 'ipb_address' => $address, 'ipb_auto' => 1 );
|
|
|
|
|
if ( $user ) {
|
|
|
|
|
$conds['ipb_anon_only'] = 0;
|
|
|
|
|
}
|
|
|
|
|
$res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
|
|
|
|
|
if ( $this->loadFromResult( $res, $killExpired ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
# Give up
|
|
|
|
|
$this->clear();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fill in member variables from a result wrapper
|
|
|
|
|
*/
|
|
|
|
|
function loadFromResult( ResultWrapper $res, $killExpired = true ) {
|
|
|
|
|
$ret = false;
|
|
|
|
|
if ( 0 != $res->numRows() ) {
|
2003-09-01 13:13:56 +00:00
|
|
|
# Get first block
|
2006-07-10 06:30:03 +00:00
|
|
|
$row = $res->fetchObject();
|
2003-09-01 13:13:56 +00:00
|
|
|
$this->initFromRow( $row );
|
|
|
|
|
|
|
|
|
|
if ( $killExpired ) {
|
|
|
|
|
# If requested, delete expired rows
|
|
|
|
|
do {
|
|
|
|
|
$killed = $this->deleteIfExpired();
|
2003-09-07 13:56:25 +00:00
|
|
|
if ( $killed ) {
|
2006-07-10 06:30:03 +00:00
|
|
|
$row = $res->fetchObject();
|
2003-09-07 13:56:25 +00:00
|
|
|
if ( $row ) {
|
|
|
|
|
$this->initFromRow( $row );
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-09-01 13:13:56 +00:00
|
|
|
} while ( $killed && $row );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2003-09-01 13:13:56 +00:00
|
|
|
# If there were any left after the killing finished, return true
|
2006-07-10 06:30:03 +00:00
|
|
|
if ( $row ) {
|
2003-09-01 13:13:56 +00:00
|
|
|
$ret = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$ret = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-07-10 06:30:03 +00:00
|
|
|
$res->free();
|
2003-09-01 13:13:56 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-12-01 10:37:47 +00:00
|
|
|
/**
|
2006-01-07 13:09:30 +00:00
|
|
|
* Search the database for any range blocks matching the given address, and
|
2005-12-01 10:37:47 +00:00
|
|
|
* load the row if one is found.
|
|
|
|
|
*/
|
2006-07-11 05:30:35 +00:00
|
|
|
function loadRange( $address, $killExpired = true )
|
2005-12-01 10:37:47 +00:00
|
|
|
{
|
2006-07-12 18:33:21 +00:00
|
|
|
$iaddr = IP::ToHex( $address );
|
2005-12-01 10:37:47 +00:00
|
|
|
if ( $iaddr === false ) {
|
|
|
|
|
# Invalid address
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Only scan ranges which start in this /16, this improves search speed
|
|
|
|
|
# Blocks should not cross a /16 boundary.
|
|
|
|
|
$range = substr( $iaddr, 0, 4 );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
$options = array();
|
2005-12-01 10:37:47 +00:00
|
|
|
$db =& $this->getDBOptions( $options );
|
2006-07-10 06:30:03 +00:00
|
|
|
$conds = array(
|
|
|
|
|
"ipb_range_start LIKE '$range%'",
|
|
|
|
|
"ipb_range_start <= '$iaddr'",
|
|
|
|
|
"ipb_range_end >= '$iaddr'"
|
|
|
|
|
);
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
$res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
|
|
|
|
|
$success = $this->loadFromResult( $res, $killExpired );
|
2005-12-01 10:37:47 +00:00
|
|
|
return $success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if a given integer IPv4 address is in a given CIDR network
|
|
|
|
|
*/
|
|
|
|
|
function isAddressInRange( $addr, $range ) {
|
2006-06-01 08:19:02 +00:00
|
|
|
list( $network, $bits ) = wfParseCIDR( $range );
|
2005-12-01 10:37:47 +00:00
|
|
|
if ( $network !== false && $addr >> ( 32 - $bits ) == $network >> ( 32 - $bits ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
function initFromRow( $row )
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2003-09-01 13:13:56 +00:00
|
|
|
$this->mAddress = $row->ipb_address;
|
|
|
|
|
$this->mReason = $row->ipb_reason;
|
2004-09-07 06:20:51 +00:00
|
|
|
$this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
|
2003-09-01 13:13:56 +00:00
|
|
|
$this->mUser = $row->ipb_user;
|
|
|
|
|
$this->mBy = $row->ipb_by;
|
2003-09-07 13:56:25 +00:00
|
|
|
$this->mAuto = $row->ipb_auto;
|
2006-07-10 06:30:03 +00:00
|
|
|
$this->mAnonOnly = $row->ipb_anon_only;
|
|
|
|
|
$this->mCreateAccount = $row->ipb_create_account;
|
2003-09-07 13:56:25 +00:00
|
|
|
$this->mId = $row->ipb_id;
|
2006-07-10 06:30:03 +00:00
|
|
|
$this->mExpiry = self::decodeExpiry( $row->ipb_expiry );
|
2005-08-23 16:52:42 +00:00
|
|
|
if ( isset( $row->user_name ) ) {
|
|
|
|
|
$this->mByName = $row->user_name;
|
|
|
|
|
} else {
|
|
|
|
|
$this->mByName = false;
|
|
|
|
|
}
|
2005-12-01 10:37:47 +00:00
|
|
|
$this->mRangeStart = $row->ipb_range_start;
|
|
|
|
|
$this->mRangeEnd = $row->ipb_range_end;
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2003-09-01 13:13:56 +00:00
|
|
|
|
2004-02-14 12:37:25 +00:00
|
|
|
function initialiseRange()
|
|
|
|
|
{
|
2005-12-01 10:37:47 +00:00
|
|
|
$this->mRangeStart = '';
|
|
|
|
|
$this->mRangeEnd = '';
|
2004-02-14 12:37:25 +00:00
|
|
|
if ( $this->mUser == 0 ) {
|
2006-06-01 08:19:02 +00:00
|
|
|
list( $network, $bits ) = wfParseCIDR( $this->mAddress );
|
2005-12-01 10:37:47 +00:00
|
|
|
if ( $network !== false ) {
|
|
|
|
|
$this->mRangeStart = sprintf( '%08X', $network );
|
|
|
|
|
$this->mRangeEnd = sprintf( '%08X', $network + (1 << (32 - $bits)) - 1 );
|
2004-02-14 12:37:25 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
2004-02-14 12:37:25 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-01-22 08:30:39 +00:00
|
|
|
/**
|
|
|
|
|
* Callback with a Block object for every block
|
2005-08-11 05:23:04 +00:00
|
|
|
* @return integer number of blocks;
|
2005-01-22 08:30:39 +00:00
|
|
|
*/
|
2005-08-02 13:35:19 +00:00
|
|
|
/*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2005-07-25 06:57:12 +00:00
|
|
|
global $wgAntiLockFlags;
|
|
|
|
|
|
2003-09-01 13:13:56 +00:00
|
|
|
$block = new Block();
|
2006-06-08 13:21:42 +00:00
|
|
|
if ( $flags & Block::EB_FOR_UPDATE ) {
|
2004-08-15 07:23:39 +00:00
|
|
|
$db =& wfGetDB( DB_MASTER );
|
2005-07-25 06:57:12 +00:00
|
|
|
if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
|
|
|
|
|
$options = '';
|
|
|
|
|
} else {
|
|
|
|
|
$options = 'FOR UPDATE';
|
|
|
|
|
}
|
2004-08-15 07:23:39 +00:00
|
|
|
$block->forUpdate( true );
|
|
|
|
|
} else {
|
|
|
|
|
$db =& wfGetDB( DB_SLAVE );
|
|
|
|
|
$options = '';
|
2005-08-02 13:35:19 +00:00
|
|
|
}
|
2006-06-08 13:21:42 +00:00
|
|
|
if ( $flags & Block::EB_RANGE_ONLY ) {
|
2005-12-01 10:37:47 +00:00
|
|
|
$cond = " AND ipb_range_start <> ''";
|
2005-08-21 06:07:48 +00:00
|
|
|
} else {
|
|
|
|
|
$cond = '';
|
|
|
|
|
}
|
|
|
|
|
|
2005-09-11 13:43:49 +00:00
|
|
|
$now = wfTimestampNow();
|
|
|
|
|
|
2005-08-23 16:52:42 +00:00
|
|
|
extract( $db->tableNames( 'ipblocks', 'user' ) );
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-08-23 20:38:46 +00:00
|
|
|
$sql = "SELECT $ipblocks.*,user_name FROM $ipblocks,$user " .
|
2005-08-23 16:52:42 +00:00
|
|
|
"WHERE user_id=ipb_by $cond ORDER BY ipb_timestamp DESC $options";
|
2005-09-11 13:43:49 +00:00
|
|
|
$res = $db->query( $sql, 'Block::enumBlocks' );
|
2005-08-11 05:23:04 +00:00
|
|
|
$num_rows = $db->numRows( $res );
|
2003-09-01 13:13:56 +00:00
|
|
|
|
2004-08-15 07:23:39 +00:00
|
|
|
while ( $row = $db->fetchObject( $res ) ) {
|
2003-09-01 13:13:56 +00:00
|
|
|
$block->initFromRow( $row );
|
2006-06-08 13:21:42 +00:00
|
|
|
if ( ( $flags & Block::EB_RANGE_ONLY ) && $block->mRangeStart == '' ) {
|
2005-08-21 06:07:48 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-08 13:21:42 +00:00
|
|
|
if ( !( $flags & Block::EB_KEEP_EXPIRED ) ) {
|
2005-09-11 13:43:49 +00:00
|
|
|
if ( $block->mExpiry && $now > $block->mExpiry ) {
|
|
|
|
|
$block->delete();
|
|
|
|
|
} else {
|
2005-08-27 23:51:29 +00:00
|
|
|
call_user_func( $callback, $block, $tag );
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2005-08-27 23:51:29 +00:00
|
|
|
call_user_func( $callback, $block, $tag );
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
wfFreeResult( $res );
|
2005-08-11 05:23:04 +00:00
|
|
|
return $num_rows;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
function delete()
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2005-03-08 02:45:25 +00:00
|
|
|
if (wfReadOnly()) {
|
2006-07-10 06:30:03 +00:00
|
|
|
return false;
|
2005-03-08 02:45:25 +00:00
|
|
|
}
|
2006-07-10 06:30:03 +00:00
|
|
|
if ( !$this->mId ) {
|
|
|
|
|
throw new MWException( "Block::delete() now requires that the mId member be filled\n" );
|
2003-09-07 13:56:25 +00:00
|
|
|
}
|
2006-07-10 06:30:03 +00:00
|
|
|
|
|
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->delete( 'ipblocks', array( 'ipb_id' => $this->mId ), __METHOD__ );
|
|
|
|
|
return $dbw->affectedRows() > 0;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
function insert()
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2005-01-11 09:29:29 +00:00
|
|
|
wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
2006-07-10 06:30:03 +00:00
|
|
|
$dbw->begin();
|
|
|
|
|
|
2006-07-10 08:38:22 +00:00
|
|
|
# Unset ipb_anon_only and ipb_create_account for user blocks, makes no sense
|
|
|
|
|
if ( $this->mUser ) {
|
|
|
|
|
$this->mAnonOnly = 0;
|
|
|
|
|
$this->mCreateAccount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
# Don't collide with expired blocks
|
|
|
|
|
Block::purgeExpired();
|
|
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
$ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
|
2004-10-24 07:10:33 +00:00
|
|
|
$dbw->insert( 'ipblocks',
|
2004-07-10 03:09:26 +00:00
|
|
|
array(
|
2005-08-02 13:35:19 +00:00
|
|
|
'ipb_id' => $ipb_id,
|
2004-07-10 03:09:26 +00:00
|
|
|
'ipb_address' => $this->mAddress,
|
|
|
|
|
'ipb_user' => $this->mUser,
|
|
|
|
|
'ipb_by' => $this->mBy,
|
|
|
|
|
'ipb_reason' => $this->mReason,
|
2004-09-07 06:20:51 +00:00
|
|
|
'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
|
2004-07-10 03:09:26 +00:00
|
|
|
'ipb_auto' => $this->mAuto,
|
2006-07-10 06:30:03 +00:00
|
|
|
'ipb_anon_only' => $this->mAnonOnly,
|
|
|
|
|
'ipb_create_account' => $this->mCreateAccount,
|
|
|
|
|
'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
|
2005-12-01 10:37:47 +00:00
|
|
|
'ipb_range_start' => $this->mRangeStart,
|
|
|
|
|
'ipb_range_end' => $this->mRangeEnd,
|
2006-07-10 06:30:03 +00:00
|
|
|
), 'Block::insert', array( 'IGNORE' )
|
2004-07-10 03:09:26 +00:00
|
|
|
);
|
2006-07-10 06:30:03 +00:00
|
|
|
$affected = $dbw->affectedRows();
|
|
|
|
|
$dbw->commit();
|
|
|
|
|
return $affected;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
function deleteIfExpired()
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2005-10-22 20:52:30 +00:00
|
|
|
$fname = 'Block::deleteIfExpired';
|
|
|
|
|
wfProfileIn( $fname );
|
2003-09-01 13:13:56 +00:00
|
|
|
if ( $this->isExpired() ) {
|
2005-01-11 08:05:22 +00:00
|
|
|
wfDebug( "Block::deleteIfExpired() -- deleting\n" );
|
2003-09-01 13:13:56 +00:00
|
|
|
$this->delete();
|
2005-10-22 20:52:30 +00:00
|
|
|
$retVal = true;
|
2003-09-01 13:13:56 +00:00
|
|
|
} else {
|
2005-01-11 08:05:22 +00:00
|
|
|
wfDebug( "Block::deleteIfExpired() -- not expired\n" );
|
2005-10-22 20:52:30 +00:00
|
|
|
$retVal = false;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
2005-10-22 20:52:30 +00:00
|
|
|
wfProfileOut( $fname );
|
|
|
|
|
return $retVal;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
function isExpired()
|
|
|
|
|
{
|
2005-01-11 08:05:22 +00:00
|
|
|
wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
|
2004-02-16 00:05:25 +00:00
|
|
|
if ( !$this->mExpiry ) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2005-01-11 08:05:22 +00:00
|
|
|
return wfTimestampNow() > $this->mExpiry;
|
2004-02-16 00:05:25 +00:00
|
|
|
}
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
|
2005-08-02 13:35:19 +00:00
|
|
|
function isValid()
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2004-06-08 23:56:09 +00:00
|
|
|
return $this->mAddress != '';
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
|
|
|
|
function updateTimestamp()
|
2004-02-27 08:25:56 +00:00
|
|
|
{
|
|
|
|
|
if ( $this->mAuto ) {
|
2004-09-07 06:20:51 +00:00
|
|
|
$this->mTimestamp = wfTimestamp();
|
2004-02-27 08:25:56 +00:00
|
|
|
$this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
|
|
|
|
|
|
2004-07-18 08:48:43 +00:00
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
2005-08-02 13:35:19 +00:00
|
|
|
$dbw->update( 'ipblocks',
|
|
|
|
|
array( /* SET */
|
2004-09-07 06:20:51 +00:00
|
|
|
'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
|
2004-09-07 06:26:15 +00:00
|
|
|
'ipb_expiry' => $dbw->timestamp($this->mExpiry),
|
2004-07-10 03:09:26 +00:00
|
|
|
), array( /* WHERE */
|
|
|
|
|
'ipb_address' => $this->mAddress
|
2005-08-02 13:35:19 +00:00
|
|
|
), 'Block::updateTimestamp'
|
2004-07-10 03:09:26 +00:00
|
|
|
);
|
2004-02-14 12:37:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2005-12-01 10:37:47 +00:00
|
|
|
/*
|
2004-02-14 12:37:25 +00:00
|
|
|
function getIntegerAddr()
|
|
|
|
|
{
|
|
|
|
|
return $this->mIntegerAddr;
|
|
|
|
|
}
|
2005-08-02 13:35:19 +00:00
|
|
|
|
2004-02-14 12:37:25 +00:00
|
|
|
function getNetworkBits()
|
|
|
|
|
{
|
|
|
|
|
return $this->mNetworkBits;
|
2005-12-01 10:37:47 +00:00
|
|
|
}*/
|
2004-02-14 12:37:25 +00:00
|
|
|
|
2005-08-23 16:52:42 +00:00
|
|
|
function getByName()
|
|
|
|
|
{
|
|
|
|
|
if ( $this->mByName === false ) {
|
|
|
|
|
$this->mByName = User::whoIs( $this->mBy );
|
|
|
|
|
}
|
|
|
|
|
return $this->mByName;
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-15 07:23:39 +00:00
|
|
|
function forUpdate( $x = NULL ) {
|
|
|
|
|
return wfSetVar( $this->mForUpdate, $x );
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-01 10:37:47 +00:00
|
|
|
function fromMaster( $x = NULL ) {
|
|
|
|
|
return wfSetVar( $this->mFromMaster, $x );
|
|
|
|
|
}
|
|
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
function getRedactedName() {
|
|
|
|
|
if ( $this->mAuto ) {
|
|
|
|
|
return '#' . $this->mId;
|
|
|
|
|
} else {
|
|
|
|
|
return $this->mAddress;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Encode expiry for DB
|
|
|
|
|
*/
|
|
|
|
|
static function encodeExpiry( $expiry, $db ) {
|
|
|
|
|
if ( $expiry == '' || $expiry == Block::infinity() ) {
|
|
|
|
|
return Block::infinity();
|
|
|
|
|
} else {
|
|
|
|
|
return $db->timestamp( $expiry );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decode expiry which has come from the DB
|
|
|
|
|
*/
|
|
|
|
|
static function decodeExpiry( $expiry ) {
|
|
|
|
|
if ( $expiry == '' || $expiry == Block::infinity() ) {
|
|
|
|
|
return Block::infinity();
|
|
|
|
|
} else {
|
|
|
|
|
return wfTimestamp( TS_MW, $expiry );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function getAutoblockExpiry( $timestamp )
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
|
|
|
|
global $wgAutoblockExpiry;
|
2004-09-07 06:26:15 +00:00
|
|
|
return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
|
2004-02-14 12:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
static function normaliseRange( $range )
|
2004-02-14 12:37:25 +00:00
|
|
|
{
|
2004-06-08 23:56:09 +00:00
|
|
|
$parts = explode( '/', $range );
|
2004-02-14 12:37:25 +00:00
|
|
|
if ( count( $parts ) == 2 ) {
|
|
|
|
|
$shift = 32 - $parts[1];
|
2006-07-12 18:33:21 +00:00
|
|
|
$ipint = IP::ToUnsigned( $parts[0] );
|
2004-02-14 12:37:25 +00:00
|
|
|
$ipint = $ipint >> $shift << $shift;
|
|
|
|
|
$newip = long2ip( $ipint );
|
|
|
|
|
$range = "$newip/{$parts[1]}";
|
|
|
|
|
}
|
|
|
|
|
return $range;
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
2003-09-07 13:56:25 +00:00
|
|
|
|
2006-07-10 06:30:03 +00:00
|
|
|
/**
|
|
|
|
|
* Purge expired blocks from the ipblocks table
|
|
|
|
|
*/
|
|
|
|
|
static function purgeExpired() {
|
|
|
|
|
$dbw =& wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->delete( 'ipblocks', array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function infinity() {
|
|
|
|
|
# This is a special keyword for timestamps in PostgreSQL, and
|
|
|
|
|
# works with CHAR(14) as well because "i" sorts after all numbers.
|
|
|
|
|
return 'infinity';
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
static $infinity;
|
|
|
|
|
if ( !isset( $infinity ) ) {
|
|
|
|
|
$dbr =& wfGetDB( DB_SLAVE );
|
|
|
|
|
$infinity = $dbr->bigTimestamp();
|
|
|
|
|
}
|
|
|
|
|
return $infinity;
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
2003-09-01 13:13:56 +00:00
|
|
|
}
|
|
|
|
|
?>
|