merge ORACLE_WORK. sorry, this may break some parts of MySQL, i did not test extensi...
[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 */
174 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
175 {
176 global $wgAntiLockFlags;
177
178 $block = new Block();
179 if ( $flags & EB_FOR_UPDATE ) {
180 $db =& wfGetDB( DB_MASTER );
181 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) {
182 $options = '';
183 } else {
184 $options = 'FOR UPDATE';
185 }
186 $block->forUpdate( true );
187 } else {
188 $db =& wfGetDB( DB_SLAVE );
189 $options = '';
190 }
191 $ipblocks = $db->tableName( 'ipblocks' );
192
193 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
194 $res = $db->query( $sql, 'Block::enumBans' );
195
196 while ( $row = $db->fetchObject( $res ) ) {
197 $block->initFromRow( $row );
198 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
199 if ( !$block->deleteIfExpired() ) {
200 $callback( $block, $tag );
201 }
202 } else {
203 $callback( $block, $tag );
204 }
205 }
206 wfFreeResult( $res );
207 }
208
209 function delete()
210 {
211 $fname = 'Block::delete';
212 if (wfReadOnly()) {
213 return;
214 }
215 $dbw =& wfGetDB( DB_MASTER );
216
217 if ( $this->mAddress == '' ) {
218 $condition = array( 'ipb_id' => $this->mId );
219 } else {
220 $condition = array( 'ipb_address' => $this->mAddress );
221 }
222 $dbw->delete( 'ipblocks', $condition, $fname );
223 $this->clearCache();
224 }
225
226 function insert()
227 {
228 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
229 $dbw =& wfGetDB( DB_MASTER );
230 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
231 $dbw->insert( 'ipblocks',
232 array(
233 'ipb_id' => $ipb_id,
234 'ipb_address' => $this->mAddress,
235 'ipb_user' => $this->mUser,
236 'ipb_by' => $this->mBy,
237 'ipb_reason' => $this->mReason,
238 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
239 'ipb_auto' => $this->mAuto,
240 'ipb_expiry' => $this->mExpiry ?
241 $dbw->timestamp($this->mExpiry) :
242 $this->mExpiry,
243 ), 'Block::insert'
244 );
245
246 $this->clearCache();
247 }
248
249 function deleteIfExpired()
250 {
251 if ( $this->isExpired() ) {
252 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
253 $this->delete();
254 return true;
255 } else {
256 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
257 return false;
258 }
259 }
260
261 function isExpired()
262 {
263 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
264 if ( !$this->mExpiry ) {
265 return false;
266 } else {
267 return wfTimestampNow() > $this->mExpiry;
268 }
269 }
270
271 function isValid()
272 {
273 return $this->mAddress != '';
274 }
275
276 function updateTimestamp()
277 {
278 if ( $this->mAuto ) {
279 $this->mTimestamp = wfTimestamp();
280 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
281
282 $dbw =& wfGetDB( DB_MASTER );
283 $dbw->update( 'ipblocks',
284 array( /* SET */
285 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
286 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
287 ), array( /* WHERE */
288 'ipb_address' => $this->mAddress
289 ), 'Block::updateTimestamp'
290 );
291
292 $this->clearCache();
293 }
294 }
295
296 /* private */ function clearCache()
297 {
298 global $wgBlockCache;
299 if ( is_object( $wgBlockCache ) ) {
300 $wgBlockCache->loadFromDB();
301 }
302 }
303
304 function getIntegerAddr()
305 {
306 return $this->mIntegerAddr;
307 }
308
309 function getNetworkBits()
310 {
311 return $this->mNetworkBits;
312 }
313
314 function forUpdate( $x = NULL ) {
315 return wfSetVar( $this->mForUpdate, $x );
316 }
317
318 /* static */ function getAutoblockExpiry( $timestamp )
319 {
320 global $wgAutoblockExpiry;
321 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
322 }
323
324 /* static */ function normaliseRange( $range )
325 {
326 $parts = explode( '/', $range );
327 if ( count( $parts ) == 2 ) {
328 $shift = 32 - $parts[1];
329 $ipint = ip2long( $parts[0] );
330 $ipint = $ipint >> $shift << $shift;
331 $newip = long2ip( $ipint );
332 $range = "$newip/{$parts[1]}";
333 }
334 return $range;
335 }
336
337 }
338 ?>