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