* td background set to White to avoid header border shining through
[mediawiki.git] / includes / Block.php
blobd98b07baebb7ce241a00eb1d781e3c980926b5a7
1 <?php
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
13 class Block
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;
22 $this->mUser = $user;
23 $this->mBy = $by;
24 $this->mReason = $reason;
25 $this->mTimestamp = $timestamp;
26 $this->mAuto = $auto;
27 $this->mExpiry = $expiry;
29 $this->initialiseRange();
32 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
34 $ban = new Block();
35 $ban->load( $address, $user, $killExpired );
36 return $ban;
39 function clear()
41 $mAddress = $mReason = $mTimestamp = "";
42 $mUser = $mBy = 0;
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";
49 $ret = false;
50 $killed = false;
52 if ( 0 == $user ) {
53 $sql = "SELECT * FROM ipblocks WHERE ipb_address='" . wfStrencode( $address ) . "'";
54 } else {
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 ) ) {
61 # User is not blocked
62 $this->clear();
63 } else {
64 # Get first block
65 $row = wfFetchObject( $res );
66 $this->initFromRow( $row );
68 if ( $killExpired ) {
69 # If requested, delete expired rows
70 do {
71 $killed = $this->deleteIfExpired();
72 if ( $killed ) {
73 $row = wfFetchObject( $res );
74 if ( $row ) {
75 $this->initFromRow( $row );
78 } while ( $killed && $row );
80 # If there were any left after the killing finished, return true
81 if ( !$row ) {
82 $ret = false;
83 $this->clear();
84 } else {
85 $ret = true;
87 } else {
88 $ret = true;
91 wfFreeResult( $res );
92 return $ret;
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];
115 } else {
116 $this->mNetworkBits = 32;
118 $this->mIntegerAddr = ip2long( $rangeParts[0] );
119 } else {
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 );
138 } else {
139 $callback( $block, $tag );
142 wfFreeResult( $res );
145 function delete()
147 $fname = "Block::delete";
148 if ( $this->mAddress == "" ) {
149 $sql = "DELETE FROM ipblocks WHERE ipb_id={$this->mId}";
150 } else {
151 $sql = "DELETE FROM ipblocks WHERE ipb_address='" .
152 wfStrencode( $this->mAddress ) . "'";
154 wfQuery( $sql, DB_WRITE, "Block::delete" );
156 $this->clearCache();
159 function insert()
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" );
167 $this->clearCache();
170 function deleteIfExpired()
172 if ( $this->isExpired() ) {
173 $this->delete();
174 return true;
175 } else {
176 return false;
180 function isExpired()
182 if ( !$this->mExpiry ) {
183 return false;
184 } else {
185 return wfTimestampNow() > $this->mExpiry;
189 function isValid()
191 return $this->mAddress != "";
194 function updateTimestamp()
196 if ( $this->mAuto ) {
197 $this->mTimestamp = wfTimestampNow();
198 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
200 wfQuery( "UPDATE ipblocks SET " .
201 "ipb_timestamp='" . $this->mTimestamp . "', " .
202 "ipb_expiry='" . $this->mExpiry . "' " .
203 "WHERE ipb_address='" . wfStrencode( $this->mAddress ) . "'", DB_WRITE, "Block::updateTimestamp" );
205 $this->clearCache();
209 /* private */ function clearCache()
211 global $wgBlockCache;
212 if ( is_object( $wgBlockCache ) ) {
213 $wgBlockCache->clear();
217 function getIntegerAddr()
219 return $this->mIntegerAddr;
222 function getNetworkBits()
224 return $this->mNetworkBits;
227 /* static */ function getAutoblockExpiry( $timestamp )
229 global $wgAutoblockExpiry;
230 return wfUnix2Timestamp( wfTimestamp2Unix( $timestamp ) + $wgAutoblockExpiry );
233 /* static */ function normaliseRange( $range )
235 $parts = explode( "/", $range );
236 if ( count( $parts ) == 2 ) {
237 $shift = 32 - $parts[1];
238 $ipint = ip2long( $parts[0] );
239 $ipint = $ipint >> $shift << $shift;
240 $newip = long2ip( $ipint );
241 $range = "$newip/{$parts[1]}";
243 return $range;