*** empty log message ***
[mediawiki.git] / includes / Block.php
blob3f0ed4871797abe37ab4d90cafa85aea3194eb83
1 <?php
2 /**
3 * Blocks and bans object
4 * @package MediaWiki
5 */
7 /**
8 * Some globals
9 */
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
13 /**
14 * The block class
15 * All the functions in this class assume the object is either explicitly
16 * loaded or filled. It is not load-on-demand. There are no accessors.
18 * To use delete(), you only need to fill $mAddress
19 * Globals used: $wgBlockCache, $wgAutoblockExpiry
21 * @todo This could be used everywhere, but it isn't.
22 * @package MediaWiki
24 class Block
26 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
27 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
29 function Block( $address = '', $user = '', $by = 0, $reason = '',
30 $timestamp = '' , $auto = 0, $expiry = '' )
32 $this->mAddress = $address;
33 $this->mUser = $user;
34 $this->mBy = $by;
35 $this->mReason = $reason;
36 $this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
37 $this->mAuto = $auto;
38 $this->mExpiry = wfTimestamp(TS_MW,$expiry);
40 $this->mForUpdate = false;
41 $this->initialiseRange();
44 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
46 $ban = new Block();
47 $ban->load( $address, $user, $killExpired );
48 return $ban;
51 function clear()
53 $mAddress = $mReason = $mTimestamp = '';
54 $mUser = $mBy = 0;
57 # Get a ban from the DB, with either the given address or the given username
58 function load( $address = '', $user = 0, $killExpired = true )
60 $fname = 'Block::load';
62 $ret = false;
63 $killed = false;
64 if ( $this->forUpdate() ) {
65 $db =& wfGetDB( DB_MASTER );
66 $options = 'FOR UPDATE';
67 } else {
68 $db =& wfGetDB( DB_SLAVE );
69 $options = '';
71 $ipblocks = $db->tableName( 'ipblocks' );
73 if ( 0 == $user && $address=='' ) {
74 $sql = "SELECT * from $ipblocks $options";
75 } elseif ($address=="") {
76 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
77 } elseif ($user=="") {
78 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
79 } elseif ( $options=='' ) {
80 # If there are no optiones (e.g. FOR UPDATE), use a UNION
81 # so that the query can make efficient use of indices
82 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
83 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
84 } else {
85 # If there are options, a UNION can not be used, use one
86 # SELECT instead. Will do a full table scan.
87 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
88 "' OR ipb_user={$user}) $options";
91 $res = $db->query( $sql, $fname );
92 if ( 0 == $db->numRows( $res ) ) {
93 # User is not blocked
94 $this->clear();
95 } else {
96 # Get first block
97 $row = $db->fetchObject( $res );
98 $this->initFromRow( $row );
100 if ( $killExpired ) {
101 # If requested, delete expired rows
102 do {
103 $killed = $this->deleteIfExpired();
104 if ( $killed ) {
105 $row = $db->fetchObject( $res );
106 if ( $row ) {
107 $this->initFromRow( $row );
110 } while ( $killed && $row );
112 # If there were any left after the killing finished, return true
113 if ( !$row ) {
114 $ret = false;
115 $this->clear();
116 } else {
117 $ret = true;
119 } else {
120 $ret = true;
123 $db->freeResult( $res );
124 return $ret;
127 function initFromRow( $row )
129 $this->mAddress = $row->ipb_address;
130 $this->mReason = $row->ipb_reason;
131 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
132 $this->mUser = $row->ipb_user;
133 $this->mBy = $row->ipb_by;
134 $this->mAuto = $row->ipb_auto;
135 $this->mId = $row->ipb_id;
136 $this->mExpiry = wfTimestamp(TS_MW,$row->ipb_expiry);
138 $this->initialiseRange();
141 function initialiseRange()
143 if ( $this->mUser == 0 ) {
144 $rangeParts = explode( '/', $this->mAddress );
145 if ( count( $rangeParts ) == 2 ) {
146 $this->mNetworkBits = $rangeParts[1];
147 } else {
148 $this->mNetworkBits = 32;
150 $this->mIntegerAddr = ip2long( $rangeParts[0] );
151 } else {
152 $this->mNetworkBits = false;
153 $this->mIntegerAddr = false;
157 # Callback with a Block object for every block
158 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
160 $block = new Block();
161 if ( $flags & EB_FOR_UPDATE ) {
162 $db =& wfGetDB( DB_MASTER );
163 $options = 'FOR UPDATE';
164 $block->forUpdate( true );
165 } else {
166 $db =& wfGetDB( DB_SLAVE );
167 $options = '';
169 $ipblocks = $db->tableName( 'ipblocks' );
171 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
172 $res = $db->query( $sql, 'Block::enumBans' );
174 while ( $row = $db->fetchObject( $res ) ) {
175 $block->initFromRow( $row );
176 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
177 if ( !$block->deleteIfExpired() ) {
178 $callback( $block, $tag );
180 } else {
181 $callback( $block, $tag );
184 wfFreeResult( $res );
187 function delete()
189 $fname = 'Block::delete';
190 $dbw =& wfGetDB( DB_MASTER );
192 if ( $this->mAddress == '' ) {
193 $condition = array( 'ipb_id' => $this->mId );
194 } else {
195 $condition = array( 'ipb_address' => $this->mAddress );
197 $dbw->delete( 'ipblocks', $condition, $fname );
198 $this->clearCache();
201 function insert()
203 $dbw =& wfGetDB( DB_MASTER );
204 $dbw->insert( 'ipblocks',
205 array(
206 'ipb_address' => $this->mAddress,
207 'ipb_user' => $this->mUser,
208 'ipb_by' => $this->mBy,
209 'ipb_reason' => $this->mReason,
210 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
211 'ipb_auto' => $this->mAuto,
212 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
213 ), 'Block::insert'
216 $this->clearCache();
219 function deleteIfExpired()
221 if ( $this->isExpired() ) {
222 $this->delete();
223 return true;
224 } else {
225 return false;
229 function isExpired()
231 if ( !$this->mExpiry ) {
232 return false;
233 } else {
234 return wfTimestamp() > $this->mExpiry;
238 function isValid()
240 return $this->mAddress != '';
243 function updateTimestamp()
245 if ( $this->mAuto ) {
246 $this->mTimestamp = wfTimestamp();
247 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
249 $dbw =& wfGetDB( DB_MASTER );
250 $dbw->update( 'ipblocks',
251 array( /* SET */
252 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
253 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
254 ), array( /* WHERE */
255 'ipb_address' => $this->mAddress
256 ), 'Block::updateTimestamp'
259 $this->clearCache();
263 /* private */ function clearCache()
265 global $wgBlockCache;
266 if ( is_object( $wgBlockCache ) ) {
267 $wgBlockCache->loadFromDB();
271 function getIntegerAddr()
273 return $this->mIntegerAddr;
276 function getNetworkBits()
278 return $this->mNetworkBits;
281 function forUpdate( $x = NULL ) {
282 return wfSetVar( $this->mForUpdate, $x );
285 /* static */ function getAutoblockExpiry( $timestamp )
287 global $wgAutoblockExpiry;
288 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
291 /* static */ function normaliseRange( $range )
293 $parts = explode( '/', $range );
294 if ( count( $parts ) == 2 ) {
295 $shift = 32 - $parts[1];
296 $ipint = ip2long( $parts[0] );
297 $ipint = $ipint >> $shift << $shift;
298 $newip = long2ip( $ipint );
299 $range = "$newip/{$parts[1]}";
301 return $range;