Fix for compatibility with short_open_tag = Off
[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: $wgIPBlockCache, $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 $ret = false;
50 $killed = false;
51
52 if ( 0 == $user ) {
53 $sql = "SELECT * FROM ipblocks WHERE ipb_address='" . wfStrencode( $address ) . "'";
54 } else {
55 $sql = "SELECT * FROM ipblocks WHERE (ipb_address='" . wfStrencode( $address ) .
56 "' OR ipb_user={$user})";
57 }
58
59 $res = wfQuery( $sql, DB_READ, $fname );
60 if ( 0 == wfNumRows( $res ) ) {
61 # User is not blocked
62 $this->clear();
63 } else {
64 # Get first block
65 $row = wfFetchObject( $res );
66 $this->initFromRow( $row );
67
68 if ( $killExpired ) {
69 # If requested, delete expired rows
70 do {
71 $killed = $this->deleteIfExpired();
72 if ( $killed ) {
73 $row = wfFetchObject( $res );
74 if ( $row ) {
75 $this->initFromRow( $row );
76 }
77 }
78 } while ( $killed && $row );
79
80 # If there were any left after the killing finished, return true
81 if ( !$row ) {
82 $ret = false;
83 $this->clear();
84 } else {
85 $ret = true;
86 }
87 } else {
88 $ret = true;
89 }
90 }
91 wfFreeResult( $res );
92 return $ret;
93 }
94
95 function initFromRow( $row )
96 {
97 $this->mAddress = $row->ipb_address;
98 $this->mReason = $row->ipb_reason;
99 $this->mTimestamp = $row->ipb_timestamp;
100 $this->mUser = $row->ipb_user;
101 $this->mBy = $row->ipb_by;
102 $this->mAuto = $row->ipb_auto;
103 $this->mId = $row->ipb_id;
104 $this->mExpiry = $row->ipb_expiry;
105
106 $this->initialiseRange();
107 }
108
109 function initialiseRange()
110 {
111 if ( $this->mUser == 0 ) {
112 $rangeParts = explode( "/", $this->mAddress );
113 if ( count( $rangeParts ) == 2 ) {
114 $this->mNetworkBits = $rangeParts[1];
115 } else {
116 $this->mNetworkBits = 32;
117 }
118 $this->mIntegerAddr = ip2long( $rangeParts[0] );
119 } else {
120 $this->mNetworkBits = false;
121 $this->mIntegerAddr = false;
122 }
123 }
124
125 # Callback with a Block object for every block
126 /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
127 {
128 $sql = "SELECT * FROM ipblocks ORDER BY ipb_timestamp DESC";
129 $res = wfQuery( $sql, DB_READ, "Block::enumBans" );
130 $block = new Block();
131
132 while ( $row = wfFetchObject( $res ) ) {
133 $block->initFromRow( $row );
134 if ( $killExpired ) {
135 if ( !$block->deleteIfExpired() ) {
136 $callback( $block, $tag );
137 }
138 } else {
139 $callback( $block, $tag );
140 }
141 }
142 wfFreeResult( $res );
143 }
144
145 function delete()
146 {
147 $fname = "Block::delete";
148 if ( $this->mAddress == "" ) {
149 $sql = "DELETE FROM ipblocks WHERE ipb_id={$this->mId}";
150 } else {
151 $sql = "DELETE FROM ipblocks WHERE ipb_address='" .
152 wfStrencode( $this->mAddress ) . "'";
153 }
154 wfQuery( $sql, DB_WRITE, "Block::delete" );
155
156 $this->clearCache();
157 }
158
159 function insert()
160 {
161 $sql = "INSERT INTO ipblocks
162 (ipb_address, ipb_user, ipb_by, ipb_reason, ipb_timestamp, ipb_auto, ipb_expiry )
163 VALUES ('" . wfStrencode( $this->mAddress ) . "', {$this->mUser}, {$this->mBy}, '" .
164 wfStrencode( $this->mReason ) . "','{$this->mTimestamp}', {$this->mAuto}, '{$this->mExpiry}')";
165 wfQuery( $sql, DB_WRITE, "Block::insert" );
166
167 $this->clearCache();
168 }
169
170 function deleteIfExpired()
171 {
172 if ( $this->isExpired() ) {
173 $this->delete();
174 return true;
175 } else {
176 return false;
177 }
178 }
179
180 function isExpired()
181 {
182 if ( !$this->mExpiry ) {
183 return false;
184 } else {
185 return wfTimestampNow() > $this->mExpiry;
186 }
187 }
188
189 function isValid()
190 {
191 return $this->mAddress != "";
192 }
193
194 function updateTimestamp() {
195
196 $this->mTimestamp = wfTimestampNow();
197 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
198
199 wfQuery( "UPDATE ipblocks SET " .
200 "ipb_timestamp='" . $this->mTimestamp . "', " .
201 "ipb_expiry='" . $this->mExpiry . "' " .
202 "WHERE ipb_address='" . wfStrencode( $this->mAddress ) . "'", DB_WRITE, "Block::updateTimestamp" );
203
204 $this->clearCache();
205 }
206
207 /* private */ function clearCache()
208 {
209 global $wgBlockCache;
210 if ( is_object( $wgBlockCache ) ) {
211 $wgBlockCache->clear();
212 }
213 }
214
215 function getIntegerAddr()
216 {
217 return $this->mIntegerAddr;
218 }
219
220 function getNetworkBits()
221 {
222 return $this->mNetworkBits;
223 }
224
225 /* static */ function getAutoblockExpiry( $timestamp )
226 {
227 global $wgAutoblockExpiry;
228 return wfUnix2Timestamp( wfTimestamp2Unix( $timestamp ) + $wgAutoblockExpiry );
229 }
230
231 /* static */ function normaliseRange( $range )
232 {
233 $parts = explode( "/", $range );
234 if ( count( $parts ) == 2 ) {
235 $shift = 32 - $parts[1];
236 $ipint = ip2long( $parts[0] );
237 $ipint = $ipint >> $shift << $shift;
238 $newip = long2ip( $ipint );
239 $range = "$newip/{$parts[1]}";
240 }
241 return $range;
242 }
243
244 }
245 ?>