Somewhat less hacky fix to the French l''''homme''' problem.
[mediawiki.git] / includes / Block.php
blob427fda22eeaccb1b78c305238102848aac3d3aa9
1 <?php
2 # Blocks and bans object
4 #TODO: This could be used everywhere, but it isn't.
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.
9 # To use delete(), you only need to fill $mAddress
11 # Globals used: $wgBlockCache, $wgAutoblockExpiry
13 class Block
15 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
16 /* private */ var $mNetworkBits, $mIntegerAddr;
18 function Block( $address = '', $user = '', $by = 0, $reason = '',
19 $timestamp = '' , $auto = 0, $expiry = '' )
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;
29 $this->initialiseRange();
32 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
34 $ban = new Block();
35 $ban->load( $address, $user, $killExpired );
36 return $ban;
39 function clear()
41 $mAddress = $mReason = $mTimestamp = '';
42 $mUser = $mBy = 0;
45 # Get a ban from the DB, with either the given address or the given username
46 function load( $address = "", $user = 0, $killExpired = true )
48 $fname = 'Block::load';
50 $ret = false;
51 $killed = false;
52 $dbr =& wfGetDB( DB_SLAVE );
53 $ipblocks = $dbr->tableName( 'ipblocks' );
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})";
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 );
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 );
85 } while ( $killed && $row );
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;
94 } else {
95 $ret = true;
98 $dbr->freeResult( $res );
99 return $ret;
102 function initFromRow( $row )
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;
113 $this->initialiseRange();
116 function initialiseRange()
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;
125 $this->mIntegerAddr = ip2long( $rangeParts[0] );
126 } else {
127 $this->mNetworkBits = false;
128 $this->mIntegerAddr = false;
132 # Callback with a Block object for every block
133 /*static*/ function enumBlocks( $callback, $tag, $killExpired = true )
135 $dbr =& wfGetDB( DB_SLAVE );
136 $ipblocks = $dbr->tableName( 'ipblocks' );
138 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC";
139 $res = $dbr->query( $sql, 'Block::enumBans' );
140 $block = new Block();
142 while ( $row = $dbr->fetchObject( $res ) ) {
143 $block->initFromRow( $row );
144 if ( $killExpired ) {
145 if ( !$block->deleteIfExpired() ) {
146 $callback( $block, $tag );
148 } else {
149 $callback( $block, $tag );
152 wfFreeResult( $res );
155 function delete()
157 $fname = 'Block::delete';
158 $dbw =& wfGetDB( DB_MASTER );
160 if ( $this->mAddress == "" ) {
161 $condition = array( 'ipb_id' => $this->mId );
162 } else {
163 $condition = array( 'ipb_address' => $this->mAddress );
165 $dbw->delete( 'ipblocks', $condition, $fname );
166 $this->clearCache();
169 function insert()
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'
184 $this->clearCache();
187 function deleteIfExpired()
189 if ( $this->isExpired() ) {
190 $this->delete();
191 return true;
192 } else {
193 return false;
197 function isExpired()
199 if ( !$this->mExpiry ) {
200 return false;
201 } else {
202 return wfTimestampNow() > $this->mExpiry;
206 function isValid()
208 return $this->mAddress != '';
211 function updateTimestamp()
213 if ( $this->mAuto ) {
214 $this->mTimestamp = wfTimestampNow();
215 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
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'
227 $this->clearCache();
231 /* private */ function clearCache()
233 global $wgBlockCache;
234 if ( is_object( $wgBlockCache ) ) {
235 $wgBlockCache->clear();
239 function getIntegerAddr()
241 return $this->mIntegerAddr;
244 function getNetworkBits()
246 return $this->mNetworkBits;
249 /* static */ function getAutoblockExpiry( $timestamp )
251 global $wgAutoblockExpiry;
252 return wfUnix2Timestamp( wfTimestamp2Unix( $timestamp ) + $wgAutoblockExpiry );
255 /* static */ function normaliseRange( $range )
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]}";
265 return $range;