00001 <?php
00002
# Blocks and bans object
00003
#
00004
#TODO: This could be used everywhere, but it isn't.
00005
#
00006
# All the functions in this class assume the object is either explicitly
00007
# loaded or filled. It is not load-on-demand. There are no accessors.
00008
#
00009
# To use delete(), you only need to fill $mAddress
00010
00011
# Globals used: $wgBlockCache, $wgAutoblockExpiry
00012
00013 class Block
00014 {
00015 var
$mAddress,
$mUser,
$mBy,
$mReason,
$mTimestamp,
$mAuto,
$mId,
$mExpiry;
00016 var
$mNetworkBits,
$mIntegerAddr;
00017
00018 function
Block( $address = '', $user = '', $by = 0, $reason = '',
00019 $timestamp = '' , $
auto = 0, $expiry = '' )
00020 {
00021 $this->mAddress = $address;
00022 $this->mUser =
$user;
00023 $this->mBy = $by;
00024 $this->mReason = $reason;
00025 $this->mTimestamp = $timestamp;
00026 $this->mAuto = $auto;
00027 $this->mExpiry = $expiry;
00028
00029 $this->
initialiseRange();
00030 }
00031
00032 function
newFromDB( $address, $user = 0, $killExpired =
true )
00033 {
00034 $ban =
new Block();
00035 $ban->load( $address, $
user, $killExpired );
00036
return $ban;
00037 }
00038
00039 function
clear()
00040 {
00041
$mAddress =
$mReason =
$mTimestamp = '';
00042
$mUser =
$mBy = 0;
00043 }
00044
00045
# Get a ban from the DB, with either the given address or the given username
00046 function
load( $address =
"", $user = 0, $killExpired =
true )
00047 {
00048 $fname = '
Block::load';
00049 $ret =
false;
00050 $killed =
false;
00051
00052
if ( 0 ==
$user && $address==
"" ) {
00053
$sql =
"SELECT * from ipblocks";
00054 } elseif ($address==
"") {
00055
$sql =
"SELECT * FROM ipblocks WHERE ipb_user={$user}";
00056 } elseif ($
user==
"") {
00057
$sql =
"SELECT * FROM ipblocks WHERE ipb_address='" .
wfStrencode( $address ) .
"'";
00058 }
else {
00059
$sql =
"SELECT * FROM ipblocks WHERE (ipb_address='" .
wfStrencode( $address ) .
00060
"' OR ipb_user={$user})";
00061 }
00062
00063
$res =
wfQuery( $sql,
DB_READ, $fname );
00064
if ( 0 ==
wfNumRows( $res ) ) {
00065
# User is not blocked
00066
$this->
clear();
00067 }
else {
00068
# Get first block
00069
$row =
wfFetchObject( $res );
00070 $this->
initFromRow( $row );
00071
00072
if ( $killExpired ) {
00073
# If requested, delete expired rows
00074
do {
00075 $killed = $this->
deleteIfExpired();
00076
if ( $killed ) {
00077
$row =
wfFetchObject( $res );
00078
if (
$row ) {
00079 $this->
initFromRow( $row );
00080 }
00081 }
00082 }
while ( $killed &&
$row );
00083
00084
# If there were any left after the killing finished, return true
00085
if ( !
$row ) {
00086 $ret =
false;
00087 $this->
clear();
00088 }
else {
00089 $ret =
true;
00090 }
00091 }
else {
00092 $ret =
true;
00093 }
00094 }
00095
wfFreeResult( $res );
00096
return $ret;
00097 }
00098
00099 function
initFromRow( $row )
00100 {
00101 $this->mAddress =
$row->ipb_address;
00102 $this->mReason =
$row->ipb_reason;
00103 $this->mTimestamp =
$row->ipb_timestamp;
00104 $this->mUser =
$row->ipb_user;
00105 $this->mBy =
$row->ipb_by;
00106 $this->mAuto =
$row->ipb_auto;
00107 $this->mId =
$row->ipb_id;
00108 $this->mExpiry =
$row->ipb_expiry;
00109
00110 $this->
initialiseRange();
00111 }
00112
00113 function
initialiseRange()
00114 {
00115
if ( $this->mUser == 0 ) {
00116 $rangeParts = explode(
'/', $this->mAddress );
00117
if ( count( $rangeParts ) == 2 ) {
00118 $this->mNetworkBits = $rangeParts[1];
00119 }
else {
00120 $this->mNetworkBits = 32;
00121 }
00122 $this->mIntegerAddr = ip2long( $rangeParts[0] );
00123 }
else {
00124 $this->mNetworkBits =
false;
00125 $this->mIntegerAddr =
false;
00126 }
00127 }
00128
00129
# Callback with a Block object for every block
00130 function
enumBlocks( $callback, $tag, $killExpired =
true )
00131 {
00132
$sql = 'SELECT * FROM ipblocks ORDER BY ipb_timestamp DESC';
00133
$res =
wfQuery( $sql,
DB_READ, 'Block::enumBans' );
00134 $block =
new Block();
00135
00136
while (
$row =
wfFetchObject( $res ) ) {
00137 $block->initFromRow( $row );
00138
if ( $killExpired ) {
00139
if ( !$block->deleteIfExpired() ) {
00140 $callback( $block, $tag );
00141 }
00142 }
else {
00143 $callback( $block, $tag );
00144 }
00145 }
00146
wfFreeResult( $res );
00147 }
00148
00149 function
delete()
00150 {
00151 $fname = '
Block::delete';
00152
if ( $this->mAddress ==
"" ) {
00153
$sql =
"DELETE FROM ipblocks WHERE ipb_id={$this->mId}";
00154 }
else {
00155
$sql =
"DELETE FROM ipblocks WHERE ipb_address='" .
00156
wfStrencode( $this->mAddress ) .
"'";
00157 }
00158
wfQuery( $sql,
DB_WRITE, '
Block::delete' );
00159
00160 $this->
clearCache();
00161 }
00162
00163 function
insert()
00164 {
00165
$sql = 'INSERT INTO ipblocks ' .
00166 '(ipb_address, ipb_user, ipb_by, ipb_reason, ipb_timestamp, ipb_auto, ipb_expiry )' .
00167
"VALUES ('" .
wfStrencode( $this->mAddress ) .
"', {$this->mUser}, {$this->mBy}, '" .
00168
wfStrencode( $this->mReason ) .
"','{$this->mTimestamp}', {$this->mAuto}, '{$this->mExpiry}')";
00169
wfQuery( $sql,
DB_WRITE, '
Block::insert' );
00170
00171 $this->
clearCache();
00172 }
00173
00174 function
deleteIfExpired()
00175 {
00176
if ( $this->
isExpired() ) {
00177 $this->
delete();
00178
return true;
00179 }
else {
00180
return false;
00181 }
00182 }
00183
00184 function
isExpired()
00185 {
00186
if ( !$this->mExpiry ) {
00187
return false;
00188 }
else {
00189
return wfTimestampNow() > $this->mExpiry;
00190 }
00191 }
00192
00193 function
isValid()
00194 {
00195
return $this->mAddress != '';
00196 }
00197
00198 function
updateTimestamp()
00199 {
00200
if ( $this->mAuto ) {
00201 $this->mTimestamp =
wfTimestampNow();
00202 $this->mExpiry =
Block::getAutoblockExpiry( $this->mTimestamp );
00203
00204
wfQuery( 'UPDATE ipblocks SET ' .
00205
"ipb_timestamp='" . $this->mTimestamp .
"', " .
00206
"ipb_expiry='" . $this->mExpiry .
"' " .
00207
"WHERE ipb_address='" .
wfStrencode( $this->mAddress ) .
"'",
DB_WRITE, '
Block::updateTimestamp' );
00208
00209 $this->
clearCache();
00210 }
00211 }
00212
00213 function
clearCache()
00214 {
00215 global
$wgBlockCache;
00216
if ( is_object( $wgBlockCache ) ) {
00217
$wgBlockCache->clear();
00218 }
00219 }
00220
00221 function
getIntegerAddr()
00222 {
00223
return $this->mIntegerAddr;
00224 }
00225
00226 function
getNetworkBits()
00227 {
00228
return $this->mNetworkBits;
00229 }
00230
00231 function
getAutoblockExpiry( $timestamp )
00232 {
00233 global
$wgAutoblockExpiry;
00234
return wfUnix2Timestamp(
wfTimestamp2Unix( $timestamp ) + $wgAutoblockExpiry );
00235 }
00236
00237 function
normaliseRange( $range )
00238 {
00239 $parts = explode(
'/', $range );
00240
if ( count( $parts ) == 2 ) {
00241 $shift = 32 - $parts[1];
00242 $ipint = ip2long( $parts[0] );
00243 $ipint = $ipint >> $shift << $shift;
00244 $newip = long2ip( $ipint );
00245 $range =
"$newip/{$parts[1]}";
00246 }
00247
return $range;
00248 }
00249
00250 }
00251 ?>