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