5ea64f60f0db3ea19aa0d02655ee6d3707b412ec
[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 $fname = 'Block::load';
67 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
68
69 $ret = false;
70 $killed = false;
71 if ( $this->forUpdate() ) {
72 $db =& wfGetDB( DB_MASTER );
73 $options = 'FOR UPDATE';
74 } else {
75 $db =& wfGetDB( DB_SLAVE );
76 $options = '';
77 }
78 $ipblocks = $db->tableName( 'ipblocks' );
79
80 if ( 0 == $user && $address=='' ) {
81 $sql = "SELECT * from $ipblocks $options";
82 } elseif ($address=="") {
83 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
84 } elseif ($user=="") {
85 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
86 } elseif ( $options=='' ) {
87 # If there are no optiones (e.g. FOR UPDATE), use a UNION
88 # so that the query can make efficient use of indices
89 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
90 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
91 } else {
92 # If there are options, a UNION can not be used, use one
93 # SELECT instead. Will do a full table scan.
94 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
95 "' OR ipb_user={$user}) $options";
96 }
97
98 $res = $db->query( $sql, $fname );
99 if ( 0 == $db->numRows( $res ) ) {
100 # User is not blocked
101 $this->clear();
102 } else {
103 # Get first block
104 $row = $db->fetchObject( $res );
105 $this->initFromRow( $row );
106
107 if ( $killExpired ) {
108 # If requested, delete expired rows
109 do {
110 $killed = $this->deleteIfExpired();
111 if ( $killed ) {
112 $row = $db->fetchObject( $res );
113 if ( $row ) {
114 $this->initFromRow( $row );
115 }
116 }
117 } while ( $killed && $row );
118
119 # If there were any left after the killing finished, return true
120 if ( !$row ) {
121 $ret = false;
122 $this->clear();
123 } else {
124 $ret = true;
125 }
126 } else {
127 $ret = true;
128 }
129 }
130 $db->freeResult( $res );
131 return $ret;
132 }
133
134 function initFromRow( $row )
135 {
136 $this->mAddress = $row->ipb_address;
137 $this->mReason = $row->ipb_reason;
138 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
139 $this->mUser = $row->ipb_user;
140 $this->mBy = $row->ipb_by;
141 $this->mAuto = $row->ipb_auto;
142 $this->mId = $row->ipb_id;
143 $this->mExpiry = $row->ipb_expiry ?
144 wfTimestamp(TS_MW,$row->ipb_expiry) :
145 $row->ipb_expiry;
146
147 $this->initialiseRange();
148 }
149
150 function initialiseRange()
151 {
152 if ( $this->mUser == 0 ) {
153 $rangeParts = explode( '/', $this->mAddress );
154 if ( count( $rangeParts ) == 2 ) {
155 $this->mNetworkBits = $rangeParts[1];
156 } else {
157 $this->mNetworkBits = 32;
158 }
159 $this->mIntegerAddr = ip2long( $rangeParts[0] );
160 } else {
161 $this->mNetworkBits = false;
162 $this->mIntegerAddr = false;
163 }
164 }
165
166 /**
167 * Callback with a Block object for every block
168 */
169 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
170 {
171 $block = new Block();
172 if ( $flags & EB_FOR_UPDATE ) {
173 $db =& wfGetDB( DB_MASTER );
174 $options = 'FOR UPDATE';
175 $block->forUpdate( true );
176 } else {
177 $db =& wfGetDB( DB_SLAVE );
178 $options = '';
179 }
180 $ipblocks = $db->tableName( 'ipblocks' );
181
182 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
183 $res = $db->query( $sql, 'Block::enumBans' );
184
185 while ( $row = $db->fetchObject( $res ) ) {
186 $block->initFromRow( $row );
187 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
188 if ( !$block->deleteIfExpired() ) {
189 $callback( $block, $tag );
190 }
191 } else {
192 $callback( $block, $tag );
193 }
194 }
195 wfFreeResult( $res );
196 }
197
198 function delete()
199 {
200 $fname = 'Block::delete';
201 if (wfReadOnly()) {
202 return;
203 }
204 $dbw =& wfGetDB( DB_MASTER );
205
206 if ( $this->mAddress == '' ) {
207 $condition = array( 'ipb_id' => $this->mId );
208 } else {
209 $condition = array( 'ipb_address' => $this->mAddress );
210 }
211 $dbw->delete( 'ipblocks', $condition, $fname );
212 $this->clearCache();
213 }
214
215 function insert()
216 {
217 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
218 $dbw =& wfGetDB( DB_MASTER );
219 $dbw->insert( 'ipblocks',
220 array(
221 'ipb_address' => $this->mAddress,
222 'ipb_user' => $this->mUser,
223 'ipb_by' => $this->mBy,
224 'ipb_reason' => $this->mReason,
225 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
226 'ipb_auto' => $this->mAuto,
227 'ipb_expiry' => $this->mExpiry ?
228 $dbw->timestamp($this->mExpiry) :
229 $this->mExpiry,
230 ), 'Block::insert'
231 );
232
233 $this->clearCache();
234 }
235
236 function deleteIfExpired()
237 {
238 if ( $this->isExpired() ) {
239 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
240 $this->delete();
241 return true;
242 } else {
243 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
244 return false;
245 }
246 }
247
248 function isExpired()
249 {
250 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
251 if ( !$this->mExpiry ) {
252 return false;
253 } else {
254 return wfTimestampNow() > $this->mExpiry;
255 }
256 }
257
258 function isValid()
259 {
260 return $this->mAddress != '';
261 }
262
263 function updateTimestamp()
264 {
265 if ( $this->mAuto ) {
266 $this->mTimestamp = wfTimestamp();
267 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
268
269 $dbw =& wfGetDB( DB_MASTER );
270 $dbw->update( 'ipblocks',
271 array( /* SET */
272 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
273 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
274 ), array( /* WHERE */
275 'ipb_address' => $this->mAddress
276 ), 'Block::updateTimestamp'
277 );
278
279 $this->clearCache();
280 }
281 }
282
283 /* private */ function clearCache()
284 {
285 global $wgBlockCache;
286 if ( is_object( $wgBlockCache ) ) {
287 $wgBlockCache->loadFromDB();
288 }
289 }
290
291 function getIntegerAddr()
292 {
293 return $this->mIntegerAddr;
294 }
295
296 function getNetworkBits()
297 {
298 return $this->mNetworkBits;
299 }
300
301 function forUpdate( $x = NULL ) {
302 return wfSetVar( $this->mForUpdate, $x );
303 }
304
305 /* static */ function getAutoblockExpiry( $timestamp )
306 {
307 global $wgAutoblockExpiry;
308 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
309 }
310
311 /* static */ function normaliseRange( $range )
312 {
313 $parts = explode( '/', $range );
314 if ( count( $parts ) == 2 ) {
315 $shift = 32 - $parts[1];
316 $ipint = ip2long( $parts[0] );
317 $ipint = $ipint >> $shift << $shift;
318 $newip = long2ip( $ipint );
319 $range = "$newip/{$parts[1]}";
320 }
321 return $range;
322 }
323
324 }
325 ?>