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 $dbr =& wfGetDB( DB_READ
);
53 $ipblocks = $dbr->tableName( 'ipblocks' );
55 if ( 0 == $user && $address=="" ) {
56 $sql = "SELECT * from $ipblocks";
57 } elseif ($address=="") {
58 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user}";
59 } elseif ($user=="") {
60 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $dbr->strencode( $address ) . "'";
62 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $dbr->strencode( $address ) .
63 "' OR ipb_user={$user})";
66 $res = $dbr->query( $sql, $fname );
67 if ( 0 == $dbr->numRows( $res ) ) {
72 $row = $dbr->fetchObject( $res );
73 $this->initFromRow( $row );
76 # If requested, delete expired rows
78 $killed = $this->deleteIfExpired();
80 $row = $dbr->fetchObject( $res );
82 $this->initFromRow( $row );
85 } while ( $killed && $row );
87 # If there were any left after the killing finished, return true
98 $dbr->freeResult( $res );
102 function initFromRow( $row )
104 $this->mAddress
= $row->ipb_address
;
105 $this->mReason
= $row->ipb_reason
;
106 $this->mTimestamp
= $row->ipb_timestamp
;
107 $this->mUser
= $row->ipb_user
;
108 $this->mBy
= $row->ipb_by
;
109 $this->mAuto
= $row->ipb_auto
;
110 $this->mId
= $row->ipb_id
;
111 $this->mExpiry
= $row->ipb_expiry
;
113 $this->initialiseRange();
116 function initialiseRange()
118 if ( $this->mUser
== 0 ) {
119 $rangeParts = explode( '/', $this->mAddress
);
120 if ( count( $rangeParts ) == 2 ) {
121 $this->mNetworkBits
= $rangeParts[1];
123 $this->mNetworkBits
= 32;
125 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
127 $this->mNetworkBits
= false;
128 $this->mIntegerAddr
= false;
132 # Callback with a Block object for every block
133 /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
135 $dbr =& wfGetDB( DB_READ
);
136 $ipblocks = $dbr->tableName( 'ipblocks' );
138 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC";
139 $res = $dbr->query( $sql, 'Block::enumBans' );
140 $block = new Block();
142 while ( $row = wfFetchObject( $res ) ) {
143 $block->initFromRow( $row );
144 if ( $killExpired ) {
145 if ( !$block->deleteIfExpired() ) {
146 $callback( $block, $tag );
149 $callback( $block, $tag );
152 wfFreeResult( $res );
157 $fname = 'Block::delete';
158 $dbw =& wfGetDB( DB_WRITE
);
160 if ( $this->mAddress
== "" ) {
161 $condition = array( 'ipb_id' => $this->mId
);
163 $condition = array( 'ipb_address' => $this->mAddress
);
165 $dbw->delete( 'ipblocks', $condition, $fname );
171 $dbw =& wfGetDB( DB_WRITE
);
172 $dbw->insertArray( 'ipblocks',
174 'ipb_address' => $this->mAddress
,
175 'ipb_user' => $this->mUser
,
176 'ipb_by' => $this->mBy
,
177 'ipb_reason' => $this->mReason
,
178 'ipb_timestamp' => $this->mTimestamp
,
179 'ipb_auto' => $this->mAuto
,
180 'ipb_expiry' => $this->mExpiry
,
187 function deleteIfExpired()
189 if ( $this->isExpired() ) {
199 if ( !$this->mExpiry
) {
202 return wfTimestampNow() > $this->mExpiry
;
208 return $this->mAddress
!= '';
211 function updateTimestamp()
213 if ( $this->mAuto
) {
214 $this->mTimestamp
= wfTimestampNow();
215 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
217 $dbw =& wfGetDB( DB_WRITE
);
218 $dbw->updateArray( 'ipblocks',
220 'ipb_timestamp' => $this->mTimestamp
,
221 'ipb_expiry' => $this->mExpiry
,
222 ), array( /* WHERE */
223 'ipb_address' => $this->mAddress
224 ), 'Block::updateTimestamp'
231 /* private */ function clearCache()
233 global $wgBlockCache;
234 if ( is_object( $wgBlockCache ) ) {
235 $wgBlockCache->clear();
239 function getIntegerAddr()
241 return $this->mIntegerAddr
;
244 function getNetworkBits()
246 return $this->mNetworkBits
;
249 /* static */ function getAutoblockExpiry( $timestamp )
251 global $wgAutoblockExpiry;
252 return wfUnix2Timestamp( wfTimestamp2Unix( $timestamp ) +
$wgAutoblockExpiry );
255 /* static */ function normaliseRange( $range )
257 $parts = explode( '/', $range );
258 if ( count( $parts ) == 2 ) {
259 $shift = 32 - $parts[1];
260 $ipint = ip2long( $parts[0] );
261 $ipint = $ipint >> $shift << $shift;
262 $newip = long2ip( $ipint );
263 $range = "$newip/{$parts[1]}";