API: Don't display range info for autoblocks (effectively disclosing the IP address...
[lhc/web/wiklou.git] / includes / api / ApiQueryBlocks.php
1 <?php
2
3 /*
4 * Created on Sep 10, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to enumerate all available pages.
33 *
34 * @ingroup API
35 */
36 class ApiQueryBlocks extends ApiQueryBase {
37
38 var $users;
39
40 public function __construct( $query, $moduleName ) {
41 parent :: __construct( $query, $moduleName, 'bk' );
42 }
43
44 public function execute() {
45 global $wgUser;
46
47 $params = $this->extractRequestParams();
48 if ( isset( $params['users'] ) && isset( $params['ip'] ) )
49 $this->dieUsage( 'bkusers and bkip cannot be used together', 'usersandip' );
50
51 $prop = array_flip( $params['prop'] );
52 $fld_id = isset( $prop['id'] );
53 $fld_user = isset( $prop['user'] );
54 $fld_by = isset( $prop['by'] );
55 $fld_timestamp = isset( $prop['timestamp'] );
56 $fld_expiry = isset( $prop['expiry'] );
57 $fld_reason = isset( $prop['reason'] );
58 $fld_range = isset( $prop['range'] );
59 $fld_flags = isset( $prop['flags'] );
60
61 $result = $this->getResult();
62 $pageSet = $this->getPageSet();
63 $titles = $pageSet->getTitles();
64 $data = array();
65
66 $this->addTables( 'ipblocks' );
67 if ( $fld_id )
68 $this->addFields( 'ipb_id' );
69 if ( $fld_user )
70 $this->addFields( array( 'ipb_address', 'ipb_user', 'ipb_auto' ) );
71 if ( $fld_by )
72 {
73 $this->addTables( 'user' );
74 $this->addFields( array( 'ipb_by', 'user_name' ) );
75 $this->addWhere( 'user_id = ipb_by' );
76 }
77 if ( $fld_timestamp )
78 $this->addFields( 'ipb_timestamp' );
79 if ( $fld_expiry )
80 $this->addFields( 'ipb_expiry' );
81 if ( $fld_reason )
82 $this->addFields( 'ipb_reason' );
83 if ( $fld_range )
84 $this->addFields( array( 'ipb_range_start', 'ipb_range_end' ) );
85 if ( $fld_flags )
86 $this->addFields( array( 'ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ) );
87
88 $this->addOption( 'LIMIT', $params['limit'] + 1 );
89 $this->addWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
90 if ( isset( $params['ids'] ) )
91 $this->addWhereFld( 'ipb_id', $params['ids'] );
92 if ( isset( $params['users'] ) )
93 {
94 foreach ( (array)$params['users'] as $u )
95 $this->prepareUsername( $u );
96 $this->addWhereFld( 'ipb_address', $this->usernames );
97 $this->addWhereFld( 'ipb_auto', 0 );
98 }
99 if ( isset( $params['ip'] ) )
100 {
101 list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
102 if ( $ip && $range )
103 {
104 // We got a CIDR range
105 if ( $range < 16 )
106 $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
107 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
108 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
109 }
110 else
111 $lower = $upper = IP::toHex( $params['ip'] );
112 $prefix = substr( $lower, 0, 4 );
113
114 $db = $this->getDB();
115 $this->addWhere( array(
116 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
117 "ipb_range_start <= '$lower'",
118 "ipb_range_end >= '$upper'",
119 'ipb_auto' => 0
120 ) );
121 }
122 if ( !$wgUser->isAllowed( 'hideuser' ) )
123 $this->addWhereFld( 'ipb_deleted', 0 );
124
125 // Purge expired entries on one in every 10 queries
126 if ( !mt_rand( 0, 10 ) )
127 Block::purgeExpired();
128
129 $res = $this->select( __METHOD__ );
130
131 $count = 0;
132 while ( $row = $res->fetchObject() )
133 {
134 if ( ++$count > $params['limit'] )
135 {
136 // We've had enough
137 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
138 break;
139 }
140 $block = array();
141 if ( $fld_id )
142 $block['id'] = $row->ipb_id;
143 if ( $fld_user && !$row->ipb_auto )
144 $block['user'] = $row->ipb_address;
145 if ( $fld_by )
146 $block['by'] = $row->user_name;
147 if ( $fld_timestamp )
148 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
149 if ( $fld_expiry )
150 $block['expiry'] = Block::decodeExpiry( $row->ipb_expiry, TS_ISO_8601 );
151 if ( $fld_reason )
152 $block['reason'] = $row->ipb_reason;
153 if ( $fld_range && !$row->ipb_auto )
154 {
155 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start );
156 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end );
157 }
158 if ( $fld_flags )
159 {
160 // For clarity, these flags use the same names as their action=block counterparts
161 if ( $row->ipb_auto )
162 $block['automatic'] = '';
163 if ( $row->ipb_anon_only )
164 $block['anononly'] = '';
165 if ( $row->ipb_create_account )
166 $block['nocreate'] = '';
167 if ( $row->ipb_enable_autoblock )
168 $block['autoblock'] = '';
169 if ( $row->ipb_block_email )
170 $block['noemail'] = '';
171 if ( $row->ipb_deleted )
172 $block['hidden'] = '';
173 if ( $row->ipb_allow_usertalk )
174 $block['allowusertalk'] = '';
175 }
176 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
177 if ( !$fit )
178 {
179 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
180 break;
181 }
182 }
183 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
184 }
185
186 protected function prepareUsername( $user )
187 {
188 if ( !$user )
189 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
190 $name = User::isIP( $user )
191 ? $user
192 : User::getCanonicalName( $user, 'valid' );
193 if ( $name === false )
194 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
195 $this->usernames[] = $name;
196 }
197
198 public function getAllowedParams() {
199 return array (
200 'start' => array(
201 ApiBase :: PARAM_TYPE => 'timestamp'
202 ),
203 'end' => array(
204 ApiBase :: PARAM_TYPE => 'timestamp',
205 ),
206 'dir' => array(
207 ApiBase :: PARAM_TYPE => array(
208 'newer',
209 'older'
210 ),
211 ApiBase :: PARAM_DFLT => 'older'
212 ),
213 'ids' => array(
214 ApiBase :: PARAM_TYPE => 'integer',
215 ApiBase :: PARAM_ISMULTI => true
216 ),
217 'users' => array(
218 ApiBase :: PARAM_ISMULTI => true
219 ),
220 'ip' => null,
221 'limit' => array(
222 ApiBase :: PARAM_DFLT => 10,
223 ApiBase :: PARAM_TYPE => 'limit',
224 ApiBase :: PARAM_MIN => 1,
225 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
226 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
227 ),
228 'prop' => array(
229 ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
230 ApiBase :: PARAM_TYPE => array(
231 'id',
232 'user',
233 'by',
234 'timestamp',
235 'expiry',
236 'reason',
237 'range',
238 'flags'
239 ),
240 ApiBase :: PARAM_ISMULTI => true
241 )
242 );
243 }
244
245 public function getParamDescription() {
246 return array (
247 'start' => 'The timestamp to start enumerating from',
248 'end' => 'The timestamp to stop enumerating at',
249 'dir' => 'The direction in which to enumerate',
250 'ids' => 'Pipe-separated list of block IDs to list (optional)',
251 'users' => 'Pipe-separated list of users to search for (optional)',
252 'ip' => array( 'Get all blocks applying to this IP or CIDR range, including range blocks.',
253 'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted.' ),
254 'limit' => 'The maximum amount of blocks to list',
255 'prop' => 'Which properties to get',
256 );
257 }
258
259 public function getDescription() {
260 return 'List all blocked users and IP addresses.';
261 }
262
263 public function getPossibleErrors() {
264 return array_merge( parent::getPossibleErrors(), array(
265 array( 'code' => 'usersandip', 'info' => 'bkusers and bkip cannot be used together' ),
266 array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
267 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
268 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
269 ) );
270 }
271
272 protected function getExamples() {
273 return array ( 'api.php?action=query&list=blocks',
274 'api.php?action=query&list=blocks&bkusers=Alice|Bob'
275 );
276 }
277
278 public function getVersion() {
279 return __CLASS__ . ': $Id$';
280 }
281 }