7ab2583f51ba6d705e65e0d2cdf78e8e459eee68
[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 $dbw =& wfGetDB( DB_MASTER );
202
203 if ( $this->mAddress == '' ) {
204 $condition = array( 'ipb_id' => $this->mId );
205 } else {
206 $condition = array( 'ipb_address' => $this->mAddress );
207 }
208 $dbw->delete( 'ipblocks', $condition, $fname );
209 $this->clearCache();
210 }
211
212 function insert()
213 {
214 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
215 $dbw =& wfGetDB( DB_MASTER );
216 $dbw->insert( 'ipblocks',
217 array(
218 'ipb_address' => $this->mAddress,
219 'ipb_user' => $this->mUser,
220 'ipb_by' => $this->mBy,
221 'ipb_reason' => $this->mReason,
222 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
223 'ipb_auto' => $this->mAuto,
224 'ipb_expiry' => $this->mExpiry ?
225 $dbw->timestamp($this->mExpiry) :
226 $this->mExpiry,
227 ), 'Block::insert'
228 );
229
230 $this->clearCache();
231 }
232
233 function deleteIfExpired()
234 {
235 if ( $this->isExpired() ) {
236 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
237 $this->delete();
238 return true;
239 } else {
240 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
241 return false;
242 }
243 }
244
245 function isExpired()
246 {
247 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
248 if ( !$this->mExpiry ) {
249 return false;
250 } else {
251 return wfTimestampNow() > $this->mExpiry;
252 }
253 }
254
255 function isValid()
256 {
257 return $this->mAddress != '';
258 }
259
260 function updateTimestamp()
261 {
262 if ( $this->mAuto ) {
263 $this->mTimestamp = wfTimestamp();
264 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
265
266 $dbw =& wfGetDB( DB_MASTER );
267 $dbw->update( 'ipblocks',
268 array( /* SET */
269 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
270 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
271 ), array( /* WHERE */
272 'ipb_address' => $this->mAddress
273 ), 'Block::updateTimestamp'
274 );
275
276 $this->clearCache();
277 }
278 }
279
280 /* private */ function clearCache()
281 {
282 global $wgBlockCache;
283 if ( is_object( $wgBlockCache ) ) {
284 $wgBlockCache->loadFromDB();
285 }
286 }
287
288 function getIntegerAddr()
289 {
290 return $this->mIntegerAddr;
291 }
292
293 function getNetworkBits()
294 {
295 return $this->mNetworkBits;
296 }
297
298 function forUpdate( $x = NULL ) {
299 return wfSetVar( $this->mForUpdate, $x );
300 }
301
302 /* static */ function getAutoblockExpiry( $timestamp )
303 {
304 global $wgAutoblockExpiry;
305 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
306 }
307
308 /* static */ function normaliseRange( $range )
309 {
310 $parts = explode( '/', $range );
311 if ( count( $parts ) == 2 ) {
312 $shift = 32 - $parts[1];
313 $ipint = ip2long( $parts[0] );
314 $ipint = $ipint >> $shift << $shift;
315 $newip = long2ip( $ipint );
316 $range = "$newip/{$parts[1]}";
317 }
318 return $range;
319 }
320
321 }
322 ?>