2 # Blocks and bans object
4 #TODO: This could be used everywhere, but it isn't.
6 # All the functions in this class assume the object is either explicitly
7 # loaded or filled. It is not load-on-demand. There are no accessors.
9 # To use delete(), you only need to fill $mAddress
11 # Globals used: $wgBlockCache, $wgAutoblockExpiry
15 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
16 /* private */ var $mNetworkBits, $mIntegerAddr;
18 function Block( $address = '', $user = '', $by = 0, $reason = '',
19 $timestamp = '' , $auto = 0, $expiry = '' )
21 $this->mAddress
= $address;
24 $this->mReason
= $reason;
25 $this->mTimestamp
= $timestamp;
27 $this->mExpiry
= $expiry;
29 $this->initialiseRange();
32 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
35 $ban->load( $address, $user, $killExpired );
41 $mAddress = $mReason = $mTimestamp = '';
45 # Get a ban from the DB, with either the given address or the given username
46 function load( $address = "", $user = 0, $killExpired = true )
48 $fname = 'Block::load';
52 if ( 0 == $user && $address=="" ) {
53 $sql = "SELECT * from ipblocks";
54 } elseif ($address=="") {
55 $sql = "SELECT * FROM ipblocks WHERE ipb_user={$user}";
56 } elseif ($user=="") {
57 $sql = "SELECT * FROM ipblocks WHERE ipb_address='" . wfStrencode( $address ) . "'";
59 $sql = "SELECT * FROM ipblocks WHERE (ipb_address='" . wfStrencode( $address ) .
60 "' OR ipb_user={$user})";
63 $res = wfQuery( $sql, DB_READ
, $fname );
64 if ( 0 == wfNumRows( $res ) ) {
69 $row = wfFetchObject( $res );
70 $this->initFromRow( $row );
73 # If requested, delete expired rows
75 $killed = $this->deleteIfExpired();
77 $row = wfFetchObject( $res );
79 $this->initFromRow( $row );
82 } while ( $killed && $row );
84 # If there were any left after the killing finished, return true
99 function initFromRow( $row )
101 $this->mAddress
= $row->ipb_address
;
102 $this->mReason
= $row->ipb_reason
;
103 $this->mTimestamp
= $row->ipb_timestamp
;
104 $this->mUser
= $row->ipb_user
;
105 $this->mBy
= $row->ipb_by
;
106 $this->mAuto
= $row->ipb_auto
;
107 $this->mId
= $row->ipb_id
;
108 $this->mExpiry
= $row->ipb_expiry
;
110 $this->initialiseRange();
113 function initialiseRange()
115 if ( $this->mUser
== 0 ) {
116 $rangeParts = explode( '/', $this->mAddress
);
117 if ( count( $rangeParts ) == 2 ) {
118 $this->mNetworkBits
= $rangeParts[1];
120 $this->mNetworkBits
= 32;
122 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
124 $this->mNetworkBits
= false;
125 $this->mIntegerAddr
= false;
129 # Callback with a Block object for every block
130 /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
132 $sql = 'SELECT * FROM ipblocks ORDER BY ipb_timestamp DESC';
133 $res = wfQuery( $sql, DB_READ
, 'Block::enumBans' );
134 $block = new Block();
136 while ( $row = wfFetchObject( $res ) ) {
137 $block->initFromRow( $row );
138 if ( $killExpired ) {
139 if ( !$block->deleteIfExpired() ) {
140 $callback( $block, $tag );
143 $callback( $block, $tag );
146 wfFreeResult( $res );
151 $fname = 'Block::delete';
152 if ( $this->mAddress
== "" ) {
153 $sql = "DELETE FROM ipblocks WHERE ipb_id={$this->mId}";
155 $sql = "DELETE FROM ipblocks WHERE ipb_address='" .
156 wfStrencode( $this->mAddress
) . "'";
158 wfQuery( $sql, DB_WRITE
, 'Block::delete' );
165 $sql = 'INSERT INTO ipblocks ' .
166 '(ipb_address, ipb_user, ipb_by, ipb_reason, ipb_timestamp, ipb_auto, ipb_expiry )' .
167 "VALUES ('" . wfStrencode( $this->mAddress
) . "', {$this->mUser}, {$this->mBy}, '" .
168 wfStrencode( $this->mReason
) . "','{$this->mTimestamp}', {$this->mAuto}, '{$this->mExpiry}')";
169 wfQuery( $sql, DB_WRITE
, 'Block::insert' );
174 function deleteIfExpired()
176 if ( $this->isExpired() ) {
186 if ( !$this->mExpiry
) {
189 return wfTimestampNow() > $this->mExpiry
;
195 return $this->mAddress
!= '';
198 function updateTimestamp()
200 if ( $this->mAuto
) {
201 $this->mTimestamp
= wfTimestampNow();
202 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
204 wfQuery( 'UPDATE ipblocks SET ' .
205 "ipb_timestamp='" . $this->mTimestamp
. "', " .
206 "ipb_expiry='" . $this->mExpiry
. "' " .
207 "WHERE ipb_address='" . wfStrencode( $this->mAddress
) . "'", DB_WRITE
, 'Block::updateTimestamp' );
213 /* private */ function clearCache()
215 global $wgBlockCache;
216 if ( is_object( $wgBlockCache ) ) {
217 $wgBlockCache->clear();
221 function getIntegerAddr()
223 return $this->mIntegerAddr
;
226 function getNetworkBits()
228 return $this->mNetworkBits
;
231 /* static */ function getAutoblockExpiry( $timestamp )
233 global $wgAutoblockExpiry;
234 return wfUnix2Timestamp( wfTimestamp2Unix( $timestamp ) +
$wgAutoblockExpiry );
237 /* static */ function normaliseRange( $range )
239 $parts = explode( '/', $range );
240 if ( count( $parts ) == 2 ) {
241 $shift = 32 - $parts[1];
242 $ipint = ip2long( $parts[0] );
243 $ipint = $ipint >> $shift << $shift;
244 $newip = long2ip( $ipint );
245 $range = "$newip/{$parts[1]}";