b5264ab96489344291557658e221a5f9dd9ece62
[lhc/web/wiklou.git] / includes / Block.php
1 <?php
2 /**
3 * Blocks and bans object
4 * @package MediaWiki
5 */
6
7 /**
8 * Some globals
9 */
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
12
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.
17 *
18 * To use delete(), you only need to fill $mAddress
19 * Globals used: $wgBlockCache, $wgAutoblockExpiry
20 *
21 * @todo This could be used everywhere, but it isn't.
22 * @package MediaWiki
23 */
24 class Block
25 {
26 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
27 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
28
29 function Block( $address = '', $user = '', $by = 0, $reason = '',
30 $timestamp = '' , $auto = 0, $expiry = '' )
31 {
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 if( empty( $expiry ) ) {
39 $this->mExpiry = $expiry;
40 } else {
41 $this->mExpiry = wfTimestamp( TS_MW, $expiry );
42 }
43
44 $this->mForUpdate = false;
45 $this->initialiseRange();
46 }
47
48 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
49 {
50 $ban = new Block();
51 $ban->load( $address, $user, $killExpired );
52 return $ban;
53 }
54
55 function clear()
56 {
57 $mAddress = $mReason = $mTimestamp = '';
58 $mUser = $mBy = 0;
59 }
60
61 /**
62 * Get a ban from the DB, with either the given address or the given username
63 */
64 function load( $address = '', $user = 0, $killExpired = true )
65 {
66 global $wgDBmysql4, $wgAntiLockFlags;
67 $fname = 'Block::load';
68 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
69
70 $ret = false;
71 $killed = false;
72 if ( $this->forUpdate() ) {
73 $db =& wfGetDB( DB_MASTER );
74 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
75 $options = '';
76 } else {
77 $options = 'FOR UPDATE';
78 }
79 } else {
80 $db =& wfGetDB( DB_SLAVE );
81 $options = '';
82 }
83 $ipblocks = $db->tableName( 'ipblocks' );
84
85 if ( 0 == $user && $address=='' ) {
86 $sql = "SELECT * from $ipblocks $options";
87 } elseif ($address=="") {
88 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
89 } elseif ($user=="") {
90 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
91 } elseif ( $options=='' && $wgDBmysql4 ) {
92 # If there are no optiones (e.g. FOR UPDATE), use a UNION
93 # so that the query can make efficient use of indices
94 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
95 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
96 } else {
97 # If there are options, a UNION can not be used, use one
98 # SELECT instead. Will do a full table scan.
99 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
100 "' OR ipb_user={$user}) $options";
101 }
102
103 $res = $db->query( $sql, $fname );
104 if ( 0 == $db->numRows( $res ) ) {
105 # User is not blocked
106 $this->clear();
107 } else {
108 # Get first block
109 $row = $db->fetchObject( $res );
110 $this->initFromRow( $row );
111
112 if ( $killExpired ) {
113 # If requested, delete expired rows
114 do {
115 $killed = $this->deleteIfExpired();
116 if ( $killed ) {
117 $row = $db->fetchObject( $res );
118 if ( $row ) {
119 $this->initFromRow( $row );
120 }
121 }
122 } while ( $killed && $row );
123
124 # If there were any left after the killing finished, return true
125 if ( !$row ) {
126 $ret = false;
127 $this->clear();
128 } else {
129 $ret = true;
130 }
131 } else {
132 $ret = true;
133 }
134 }
135 $db->freeResult( $res );
136 return $ret;
137 }
138
139 function initFromRow( $row )
140 {
141 $this->mAddress = $row->ipb_address;
142 $this->mReason = $row->ipb_reason;
143 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
144 $this->mUser = $row->ipb_user;
145 $this->mBy = $row->ipb_by;
146 $this->mAuto = $row->ipb_auto;
147 $this->mId = $row->ipb_id;
148 $this->mExpiry = $row->ipb_expiry ?
149 wfTimestamp(TS_MW,$row->ipb_expiry) :
150 $row->ipb_expiry;
151
152 $this->initialiseRange();
153 }
154
155 function initialiseRange()
156 {
157 if ( $this->mUser == 0 ) {
158 $rangeParts = explode( '/', $this->mAddress );
159 if ( count( $rangeParts ) == 2 ) {
160 $this->mNetworkBits = $rangeParts[1];
161 } else {
162 $this->mNetworkBits = 32;
163 }
164 $this->mIntegerAddr = ip2long( $rangeParts[0] );
165 } else {
166 $this->mNetworkBits = false;
167 $this->mIntegerAddr = false;
168 }
169 }
170
171 /**
172 * Callback with a Block object for every block
173 * @return integer number of blocks;
174 */
175 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
176 {
177 global $wgAntiLockFlags;
178
179 $block = new Block();
180 if ( $flags & EB_FOR_UPDATE ) {
181 $db =& wfGetDB( DB_MASTER );
182 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
183 $options = '';
184 } else {
185 $options = 'FOR UPDATE';
186 }
187 $block->forUpdate( true );
188 } else {
189 $db =& wfGetDB( DB_SLAVE );
190 $options = '';
191 }
192 $ipblocks = $db->tableName( 'ipblocks' );
193
194 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
195 $res = $db->query( $sql, 'Block::enumBans' );
196 $num_rows = $db->numRows( $res );
197
198 while ( $row = $db->fetchObject( $res ) ) {
199 $block->initFromRow( $row );
200 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
201 if ( !$block->deleteIfExpired() ) {
202 $callback( $block, $tag );
203 }
204 } else {
205 $callback( $block, $tag );
206 }
207 }
208 wfFreeResult( $res );
209 return $num_rows;
210 }
211
212 function delete()
213 {
214 $fname = 'Block::delete';
215 if (wfReadOnly()) {
216 return;
217 }
218 $dbw =& wfGetDB( DB_MASTER );
219
220 if ( $this->mAddress == '' ) {
221 $condition = array( 'ipb_id' => $this->mId );
222 } else {
223 $condition = array( 'ipb_address' => $this->mAddress );
224 }
225 $dbw->delete( 'ipblocks', $condition, $fname );
226 $this->clearCache();
227 }
228
229 function insert()
230 {
231 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
232 $dbw =& wfGetDB( DB_MASTER );
233 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
234 $dbw->insert( 'ipblocks',
235 array(
236 'ipb_id' => $ipb_id,
237 'ipb_address' => $this->mAddress,
238 'ipb_user' => $this->mUser,
239 'ipb_by' => $this->mBy,
240 'ipb_reason' => $this->mReason,
241 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
242 'ipb_auto' => $this->mAuto,
243 'ipb_expiry' => $this->mExpiry ?
244 $dbw->timestamp($this->mExpiry) :
245 $this->mExpiry,
246 ), 'Block::insert'
247 );
248
249 $this->clearCache();
250 }
251
252 function deleteIfExpired()
253 {
254 if ( $this->isExpired() ) {
255 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
256 $this->delete();
257 return true;
258 } else {
259 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
260 return false;
261 }
262 }
263
264 function isExpired()
265 {
266 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
267 if ( !$this->mExpiry ) {
268 return false;
269 } else {
270 return wfTimestampNow() > $this->mExpiry;
271 }
272 }
273
274 function isValid()
275 {
276 return $this->mAddress != '';
277 }
278
279 function updateTimestamp()
280 {
281 if ( $this->mAuto ) {
282 $this->mTimestamp = wfTimestamp();
283 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
284
285 $dbw =& wfGetDB( DB_MASTER );
286 $dbw->update( 'ipblocks',
287 array( /* SET */
288 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
289 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
290 ), array( /* WHERE */
291 'ipb_address' => $this->mAddress
292 ), 'Block::updateTimestamp'
293 );
294
295 $this->clearCache();
296 }
297 }
298
299 /* private */ function clearCache()
300 {
301 global $wgBlockCache;
302 if ( is_object( $wgBlockCache ) ) {
303 $wgBlockCache->loadFromDB();
304 }
305 }
306
307 function getIntegerAddr()
308 {
309 return $this->mIntegerAddr;
310 }
311
312 function getNetworkBits()
313 {
314 return $this->mNetworkBits;
315 }
316
317 function forUpdate( $x = NULL ) {
318 return wfSetVar( $this->mForUpdate, $x );
319 }
320
321 /* static */ function getAutoblockExpiry( $timestamp )
322 {
323 global $wgAutoblockExpiry;
324 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
325 }
326
327 /* static */ function normaliseRange( $range )
328 {
329 $parts = explode( '/', $range );
330 if ( count( $parts ) == 2 ) {
331 $shift = 32 - $parts[1];
332 $ipint = ip2long( $parts[0] );
333 $ipint = $ipint >> $shift << $shift;
334 $newip = long2ip( $ipint );
335 $range = "$newip/{$parts[1]}";
336 }
337 return $range;
338 }
339
340 }
341 ?>