0f688a02c498019412f3c9259b1c93450e4aedc6
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";
53 $sql = "SELECT * FROM ipblocks WHERE ipb_address='" . wfStrencode( $address ) . "'";
55 $sql = "SELECT * FROM ipblocks WHERE (ipb_address='" . wfStrencode( $address ) .
56 "' OR ipb_user={$user})";
59 $res = wfQuery( $sql, DB_READ
, $fname );
60 if ( 0 == wfNumRows( $res ) ) {
65 $row = wfFetchObject( $res );
66 $this->initFromRow( $row );
69 # If requested, delete expired rows
71 $killed = $this->deleteIfExpired();
73 $row = wfFetchObject( $res );
75 $this->initFromRow( $row );
78 } while ( $killed && $row );
80 # If there were any left after the killing finished, return true
95 function initFromRow( $row )
97 $this->mAddress
= $row->ipb_address
;
98 $this->mReason
= $row->ipb_reason
;
99 $this->mTimestamp
= $row->ipb_timestamp
;
100 $this->mUser
= $row->ipb_user
;
101 $this->mBy
= $row->ipb_by
;
102 $this->mAuto
= $row->ipb_auto
;
103 $this->mId
= $row->ipb_id
;
104 $this->mExpiry
= $row->ipb_expiry
;
106 $this->initialiseRange();
109 function initialiseRange()
111 if ( $this->mUser
== 0 ) {
112 $rangeParts = explode( "/", $this->mAddress
);
113 if ( count( $rangeParts ) == 2 ) {
114 $this->mNetworkBits
= $rangeParts[1];
116 $this->mNetworkBits
= 32;
118 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
120 $this->mNetworkBits
= false;
121 $this->mIntegerAddr
= false;
125 # Callback with a Block object for every block
126 /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
128 $sql = "SELECT * FROM ipblocks ORDER BY ipb_timestamp DESC";
129 $res = wfQuery( $sql, DB_READ
, "Block::enumBans" );
130 $block = new Block();
132 while ( $row = wfFetchObject( $res ) ) {
133 $block->initFromRow( $row );
134 if ( $killExpired ) {
135 if ( !$block->deleteIfExpired() ) {
136 $callback( $block, $tag );
139 $callback( $block, $tag );
142 wfFreeResult( $res );
147 $fname = "Block::delete";
148 if ( $this->mAddress
== "" ) {
149 $sql = "DELETE FROM ipblocks WHERE ipb_id={$this->mId}";
151 $sql = "DELETE FROM ipblocks WHERE ipb_address='" .
152 wfStrencode( $this->mAddress
) . "'";
154 wfQuery( $sql, DB_WRITE
, "Block::delete" );
161 $sql = "INSERT INTO ipblocks
162 (ipb_address, ipb_user, ipb_by, ipb_reason, ipb_timestamp, ipb_auto, ipb_expiry )
163 VALUES ('" . wfStrencode( $this->mAddress
) . "', {$this->mUser}, {$this->mBy}, '" .
164 wfStrencode( $this->mReason
) . "','{$this->mTimestamp}', {$this->mAuto}, '{$this->mExpiry}')";
165 wfQuery( $sql, DB_WRITE
, "Block::insert" );
170 function deleteIfExpired()
172 if ( $this->isExpired() ) {
182 if ( !$this->mExpiry
) {
185 return wfTimestampNow() > $this->mExpiry
;
191 return $this->mAddress
!= "";
194 function updateTimestamp() {
196 $this->mTimestamp
= wfTimestampNow();
197 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
199 wfQuery( "UPDATE ipblocks SET " .
200 "ipb_timestamp='" . $this->mTimestamp
. "', " .
201 "ipb_expiry='" . $this->mExpiry
. "' " .
202 "WHERE ipb_address='" . wfStrencode( $this->mAddress
) . "'", DB_WRITE
, "Block::updateTimestamp" );
207 /* private */ function clearCache()
209 global $wgBlockCache;
210 if ( is_object( $wgBlockCache ) ) {
211 $wgBlockCache->clear();
215 function getIntegerAddr()
217 return $this->mIntegerAddr
;
220 function getNetworkBits()
222 return $this->mNetworkBits
;
225 /* static */ function getAutoblockExpiry( $timestamp )
227 global $wgAutoblockExpiry;
228 return wfUnix2Timestamp( wfTimestamp2Unix( $timestamp ) +
$wgAutoblockExpiry );
231 /* static */ function normaliseRange( $range )
233 $parts = explode( "/", $range );
234 if ( count( $parts ) == 2 ) {
235 $shift = 32 - $parts[1];
236 $ipint = ip2long( $parts[0] );
237 $ipint = $ipint >> $shift << $shift;
238 $newip = long2ip( $ipint );
239 $range = "$newip/{$parts[1]}";