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