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