Follow-up r83909: fix error on TWN
[lhc/web/wiklou.git] / includes / specials / SpecialBlockList.php
1 <?php
2 /**
3 * Implements Special:BlockList
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that lists existing blocks
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialBlockList extends SpecialPage {
30
31 protected $target, $options;
32
33 function __construct() {
34 parent::__construct( 'BlockList' );
35 }
36
37 /**
38 * Main execution point
39 *
40 * @param $par String title fragment
41 */
42 public function execute( $par ) {
43 global $wgUser, $wgOut, $wgRequest;
44
45 $this->setHeaders();
46 $this->outputHeader();
47 $wgOut->setPageTitle( wfMsg( 'ipblocklist' ) );
48 $wgOut->addModuleStyles( 'mediawiki.special' );
49
50 $par = $wgRequest->getVal( 'ip', $par );
51 $this->target = trim( $wgRequest->getVal( 'wpTarget', $par ) );
52
53 $this->options = $wgRequest->getArray( 'wpOptions', array() );
54
55 $action = $wgRequest->getText( 'action' );
56
57 if( $action == 'unblock' || $action == 'submit' && $wgRequest->wasPosted() ) {
58 # B/C @since 1.18: Unblock interface is now at Special:Unblock
59 $title = SpecialPage::getTitleFor( 'Unblock', $this->target );
60 $wgOut->redirect( $title->getFullUrl() );
61 return;
62 }
63
64 # Just show the block list
65 $fields = array(
66 'Target' => array(
67 'type' => 'text',
68 'label-message' => 'ipaddressorusername',
69 'tabindex' => '1',
70 'size' => '45',
71 ),
72 'Options' => array(
73 'type' => 'multiselect',
74 'options' => array(
75 wfMsg( 'blocklist-userblocks' ) => 'userblocks',
76 wfMsg( 'blocklist-tempblocks' ) => 'tempblocks',
77 wfMsg( 'blocklist-addressblocks' ) => 'addressblocks',
78 ),
79 'cssclass' => 'mw-htmlform-multiselect-flatlist',
80 ),
81 );
82 $form = new HTMLForm( $fields );
83 $form->setTitle( $this->getTitle() );
84 $form->setMethod( 'get' );
85 $form->setWrapperLegend( wfMsg( 'ipblocklist-legend' ) );
86 $form->setSubmitText( wfMsg( 'ipblocklist-submit' ) );
87 $form->prepareForm();
88
89 $form->displayForm( '' );
90 $this->showList();
91 }
92
93 /**
94 * Get the component of an IP address which is certain to be the same between an IP
95 * address and a rangeblock containing that IP address.
96 * @todo: should be in IP.php??
97 * @param $ip String
98 * @return String
99 */
100 protected static function getIpFragment( $ip ){
101 global $wgBlockCIDRLimit;
102 if( IP::isIPv4( $ip ) ){
103 $hexAddress = IP::toHex( $ip );
104 return substr( $hexAddress, 0, wfBaseconvert( $wgBlockCIDRLimit['IPv4'], 10, 16 ) );
105 } elseif( IP::isIPv6( $ip ) ) {
106 $hexAddress = substr( IP::toHex( $ip ), 2 );
107 return 'v6-' . substr( $hexAddress, 0, wfBaseconvert( $wgBlockCIDRLimit['IPv6'], 10, 16 ) );
108 } else {
109 return null;
110 }
111 }
112
113 function showList() {
114 global $wgOut, $wgUser;
115
116 # Purge expired entries on one in every 10 queries
117 if ( !mt_rand( 0, 10 ) ) {
118 Block::purgeExpired();
119 }
120
121 $conds = array();
122 # Is the user allowed to see hidden blocks?
123 if ( !$wgUser->isAllowed( 'hideuser' ) ){
124 $conds['ipb_deleted'] = 0;
125 }
126
127 if ( $this->target !== '' ){
128 list( $target, $type ) = Block::parseTarget( $this->target );
129
130 switch( $type ){
131 case Block::TYPE_ID:
132 $conds['ipb_id'] = $target;
133 break;
134
135 case Block::TYPE_IP:
136 case BLock::TYPE_RANGE:
137 list( $start, $end ) = IP::parseRange( $target );
138 # Per bug 14634, we want to include relevant active rangeblocks; for
139 # rangeblocks, we want to include larger ranges which enclose the given
140 # range. We know that all blocks must be smaller than $wgBlockCIDRLimit,
141 # so we can improve performance by filtering on a LIKE clause
142 $chunk = self::getIpFragment( $start );
143 $dbr = wfGetDB( DB_SLAVE );
144 $like = $dbr->buildLike( $chunk, $dbr->anyString() );
145
146 # Fairly hard to make a malicious SQL statement out of hex characters,
147 # but stranger things have happened...
148 $safeStart = $dbr->addQuotes( IP::toHex( $start ) );
149 $safeEnd = $dbr->addQuotes( IP::toHex( $end ) );
150 $safeTarget = $dbr->addQuotes( IP::toHex( $target ) );
151
152 # TODO: abstract this away
153 $conds[] = "(ipb_address = $safeTarget) OR
154 (ipb_range_start $like AND ipb_range_start <= $safeStart AND ipb_range_end >= $safeEnd)";
155 $conds['ipb_auto'] = 0;
156 break;
157
158 case Block::TYPE_USER:
159 $conds['ipb_address'] = (string)$this->target;
160 $conds['ipb_auto'] = 0;
161 break;
162 }
163 }
164
165 # Apply filters
166 if( in_array( 'userblocks', $this->options ) ) {
167 $conds['ipb_user'] = 0;
168 }
169 if( in_array( 'tempblocks', $this->options ) ) {
170 $conds['ipb_expiry'] = 'infinity';
171 }
172 if( in_array( 'addressblocks', $this->options ) ) {
173 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
174 }
175
176 # Check for other blocks, i.e. global/tor blocks
177 $otherBlockLink = array();
178 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->target ) );
179
180 # Show additional header for the local block only when other blocks exists.
181 # Not necessary in a standard installation without such extensions enabled
182 if( count( $otherBlockLink ) ) {
183 $wgOut->addHTML(
184 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
185 );
186 }
187
188 $pager = new BlockListPager( $this, $conds );
189 if ( $pager->getNumRows() ) {
190 $wgOut->addHTML(
191 $pager->getNavigationBar() .
192 $pager->getBody().
193 $pager->getNavigationBar()
194 );
195
196 } elseif ( $this->target ) {
197 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
198
199 } else {
200 $wgOut->addWikiMsg( 'ipblocklist-empty' );
201 }
202
203 if( count( $otherBlockLink ) ) {
204 $wgOut->addHTML(
205 Html::rawElement(
206 'h2',
207 array(),
208 wfMsgExt(
209 'ipblocklist-otherblocks',
210 'parseinline',
211 count( $otherBlockLink )
212 )
213 ) . "\n"
214 );
215 $list = '';
216 foreach( $otherBlockLink as $link ) {
217 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
218 }
219 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
220 }
221 }
222 }
223
224 class BlockListPager extends TablePager {
225 protected $conds;
226 protected $page;
227
228 function __construct( $page, $conds ) {
229 $this->page = $page;
230 $this->conds = $conds;
231 $this->mDefaultDirection = true;
232 parent::__construct();
233 }
234
235 function getFieldNames() {
236 global $wgUser;
237 static $headers = null;
238
239 if ( $headers == array() ) {
240 $headers = array(
241 'ipb_timestamp' => 'blocklist-timestamp',
242 'ipb_target' => 'blocklist-target',
243 'ipb_expiry' => 'blocklist-expiry',
244 'ipb_by' => 'blocklist-by',
245 'ipb_params' => 'blocklist-params',
246 'ipb_reason' => 'blocklist-reason',
247 );
248 $headers = array_map( 'wfMsg', $headers );
249 }
250
251 return $headers;
252 }
253
254 function formatValue( $name, $value ) {
255 global $wgOut, $wgLang, $wgUser;
256
257 static $sk, $msg;
258 if ( empty( $sk ) ) {
259 $sk = $wgUser->getSkin();
260 $msg = array(
261 'anononlyblock',
262 'createaccountblock',
263 'noautoblockblock',
264 'emailblock',
265 'blocklist-nousertalk',
266 'unblocklink',
267 'change-blocklink',
268 'infiniteblock',
269 );
270 $msg = array_combine( $msg, array_map( 'wfMessage', $msg ) );
271 }
272
273 $row = $this->mCurrentRow;
274 $formatted = '';
275
276 switch( $name ) {
277 case 'ipb_timestamp':
278 $formatted = $wgLang->timeanddate( $value );
279 break;
280
281 case 'ipb_target':
282 if( $row->ipb_auto ){
283 $formatted = wfMessage( 'autoblockid', $row->ipb_id );
284 } else {
285 list( $target, $type ) = Block::parseTarget( $row->ipb_address );
286 switch( $type ){
287 case Block::TYPE_USER:
288 case Block::TYPE_IP:
289 $formatted = $sk->userLink( $target->getId(), $target );
290 $formatted .= $sk->userToolLinks(
291 $target->getId(),
292 $target,
293 false,
294 Linker::TOOL_LINKS_NOBLOCK
295 );
296 break;
297 case Block::TYPE_RANGE:
298 $formatted = htmlspecialchars( $target );
299 }
300 }
301 break;
302
303 case 'ipb_expiry':
304 $formatted = $value == Block::infinity()
305 ? $msg['infiniteblock']
306 : $wgLang->timeanddate( $value );
307 if( $wgUser->isAllowed( 'block' ) ){
308 if( $row->ipb_auto ){
309 $links[] = $sk->linkKnown(
310 SpecialPage::getTitleFor( 'Unblock' ),
311 $msg['unblocklink'],
312 array(),
313 array( 'wpTarget' => "#{$row->ipb_id}" )
314 );
315 } else {
316 $links[] = $sk->linkKnown(
317 SpecialPage::getTitleFor( 'Unblock', $row->ipb_address ),
318 $msg['unblocklink']
319 );
320 $links[] = $sk->linkKnown(
321 SpecialPage::getTitleFor( 'Block', $row->ipb_address ),
322 $msg['change-blocklink']
323 );
324 }
325 $formatted .= ' ' . Html::rawElement(
326 'span',
327 array( 'class' => 'mw-blocklist-actions' ),
328 "[{$wgLang->pipeList($links)}]"
329 );
330 }
331 break;
332
333 case 'ipb_by':
334 $user = User::newFromId( $value );
335 if( $user instanceof User ){
336 $formatted = $sk->userLink( $user->getId(), $user->getName() );
337 $formatted .= $sk->userToolLinks( $user->getId(), $user->getName() );
338 }
339 break;
340
341 case 'ipb_reason':
342 $formatted = $sk->commentBlock( $value );
343 break;
344
345 case 'ipb_params':
346 $properties = array();
347 if ( $row->ipb_anon_only ) {
348 $properties[] = $msg['anononlyblock'];
349 }
350 if ( $row->ipb_create_account ) {
351 $properties[] = $msg['createaccountblock'];
352 }
353 if ( !$row->ipb_enable_autoblock ) {
354 $properties[] = $msg['noautoblockblock'];
355 }
356
357 if ( $row->ipb_block_email ) {
358 $properties[] = $msg['emailblock'];
359 }
360
361 if ( !$row->ipb_allow_usertalk ) {
362 $properties[] = $msg['blocklist-nousertalk'];
363 }
364
365 $formatted = $wgLang->commaList( $properties );
366 break;
367
368 default:
369 $formatted = "Unable to format $name";
370 break;
371 }
372
373 return $formatted;
374 }
375
376 function getQueryInfo() {
377 $info = array(
378 'tables' => array( 'ipblocks' ),
379 'fields' => array(
380 'ipb_id',
381 'ipb_address',
382 'ipb_by',
383 'ipb_reason',
384 'ipb_timestamp',
385 'ipb_auto',
386 'ipb_anon_only',
387 'ipb_create_account',
388 'ipb_enable_autoblock',
389 'ipb_expiry',
390 'ipb_range_start',
391 'ipb_range_end',
392 'ipb_deleted',
393 'ipb_block_email',
394 'ipb_allow_usertalk',
395 ),
396 'conds' => $this->conds,
397 );
398
399 global $wgUser;
400 # Is the user allowed to see hidden blocks?
401 if ( !$wgUser->isAllowed( 'hideuser' ) ){
402 $conds['ipb_deleted'] = 0;
403 }
404
405 return $info;
406 }
407
408 public function getTableClass(){
409 return 'TablePager mw-blocklist';
410 }
411
412 function getIndexField() {
413 return 'ipb_timestamp';
414 }
415
416 function getDefaultSort() {
417 return 'ipb_timestamp';
418 }
419
420 function isFieldSortable( $name ) {
421 return false;
422 }
423
424 function getTitle() {
425 return $this->page->getTitle();
426 }
427 }