Standardised file description headers; first path
[lhc/web/wiklou.git] / includes / specials / SpecialIpblocklist.php
1 <?php
2 /**
3 * Implements Special:ipblocklist
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 and allows users with the 'block'
26 * permission to remove blocks
27 *
28 * @ingroup SpecialPage
29 */
30 class IPUnblockForm extends SpecialPage {
31 var $ip, $reason, $id;
32 var $hideuserblocks, $hidetempblocks, $hideaddressblocks;
33
34 function __construct() {
35 parent::__construct( 'Ipblocklist' );
36 }
37
38 /**
39 * Main execution point
40 *
41 * @param $ip part of title: Special:Ipblocklist/<ip>.
42 */
43 function execute( $ip ) {
44 global $wgUser, $wgOut, $wgRequest;
45
46 $this->setHeaders();
47 $this->outputHeader();
48
49 $ip = $wgRequest->getVal( 'ip', $ip );
50 $this->ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $ip ) );
51 $this->id = $wgRequest->getVal( 'id' );
52 $this->reason = $wgRequest->getText( 'wpUnblockReason' );
53 $this->hideuserblocks = $wgRequest->getBool( 'hideuserblocks' );
54 $this->hidetempblocks = $wgRequest->getBool( 'hidetempblocks' );
55 $this->hideaddressblocks = $wgRequest->getBool( 'hideaddressblocks' );
56
57 $action = $wgRequest->getText( 'action' );
58 $successip = $wgRequest->getVal( 'successip' );
59
60 if( $action == 'unblock' || $action == 'submit' && $wgRequest->wasPosted() ) {
61 # Check permissions
62 if( !$wgUser->isAllowed( 'block' ) ) {
63 $wgOut->permissionRequired( 'block' );
64 return;
65 }
66 # Check for database lock
67 if( wfReadOnly() ) {
68 $wgOut->readOnlyPage();
69 return;
70 }
71
72 # bug 15810: blocked admins should have limited access here
73 if ( $wgUser->isBlocked() ) {
74 if ( $id ) {
75 # This doesn't pick up on autoblocks, but admins
76 # should have the ipblock-exempt permission anyway
77 $block = Block::newFromID( $id );
78 $user = User::newFromName( $block->mAddress );
79 } else {
80 $user = User::newFromName( $ip );
81 }
82 $status = IPBlockForm::checkUnblockSelf( $user );
83 if ( $status !== true ) {
84 throw new ErrorPageError( 'badaccess', $status );
85 }
86 }
87
88 if( $action == 'unblock' ){
89 # Show unblock form
90 $this->showForm( '' );
91 } elseif( $action == 'submit'
92 && $wgRequest->wasPosted()
93 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
94 {
95 # Remove blocks and redirect user to success page
96 $this->doSubmit();
97 }
98
99 } elseif( $action == 'success' ) {
100 # Inform the user of a successful unblock
101 # (No need to check permissions or locks here,
102 # if something was done, then it's too late!)
103 if ( substr( $successip, 0, 1) == '#' ) {
104 // A block ID was unblocked
105 $this->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
106 } else {
107 // A username/IP was unblocked
108 $this->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
109 }
110 } else {
111 # Just show the block list
112 $this->showList( '' );
113 }
114 }
115
116 /**
117 * Generates the unblock form
118 *
119 * @param $err string: error message
120 * @return $out string: HTML form
121 */
122 function showForm( $err ) {
123 global $wgOut, $wgUser, $wgSysopUserBans;
124
125 $wgOut->addWikiMsg( 'unblockiptext' );
126
127 $action = $this->getTitle()->getLocalURL( 'action=submit' );
128
129 if ( $err != '' ) {
130 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
131 $wgOut->addWikiText( Html::rawElement( 'span', array( 'class' => 'error' ), $err ) . "\n" );
132 }
133
134 $addressPart = false;
135 if ( $this->id ) {
136 $block = Block::newFromID( $this->id );
137 if ( $block ) {
138 $encName = htmlspecialchars( $block->getRedactedName() );
139 $encId = $this->id;
140 $addressPart = $encName . Html::hidden( 'id', $encId );
141 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
142 }
143 }
144 if ( !$addressPart ) {
145 $addressPart = Xml::input( 'wpUnblockAddress', 40, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
146 $ipa = Xml::label( wfMsg( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
147 }
148
149 $wgOut->addHTML(
150 Html::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
151 Html::openElement( 'fieldset' ) .
152 Html::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
153 Html::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
154 "<tr>
155 <td class='mw-label'>
156 {$ipa}
157 </td>
158 <td class='mw-input'>
159 {$addressPart}
160 </td>
161 </tr>
162 <tr>
163 <td class='mw-label'>" .
164 Xml::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
165 "</td>
166 <td class='mw-input'>" .
167 Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
168 "</td>
169 </tr>
170 <tr>
171 <td>&#160;</td>
172 <td class='mw-submit'>" .
173 Xml::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
174 "</td>
175 </tr>" .
176 Html::closeElement( 'table' ) .
177 Html::closeElement( 'fieldset' ) .
178 Html::hidden( 'wpEditToken', $wgUser->editToken() ) .
179 Html::closeElement( 'form' ) . "\n"
180 );
181
182 }
183
184 const UNBLOCK_SUCCESS = 0; // Success
185 const UNBLOCK_NO_SUCH_ID = 1; // No such block ID
186 const UNBLOCK_USER_NOT_BLOCKED = 2; // IP wasn't blocked
187 const UNBLOCK_BLOCKED_AS_RANGE = 3; // IP is part of a range block
188 const UNBLOCK_UNKNOWNERR = 4; // Unknown error
189
190 /**
191 * Backend code for unblocking. doSubmit() wraps around this.
192 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
193 * case it contains the range $ip is part of.
194 * @return array array(message key, parameters) on failure, empty array on success
195 */
196 public static function doUnblock( &$id, &$ip, &$reason, &$range = null, $blocker = null ) {
197 if ( $id ) {
198 $block = Block::newFromID( $id );
199 if ( !$block ) {
200 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
201 }
202 $ip = $block->getRedactedName();
203 } else {
204 $block = new Block();
205 $ip = trim( $ip );
206 if ( substr( $ip, 0, 1 ) == "#" ) {
207 $id = substr( $ip, 1 );
208 $block = Block::newFromID( $id );
209 if( !$block ) {
210 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
211 }
212 $ip = $block->getRedactedName();
213 } else {
214 $block = Block::newFromDB( $ip );
215 if ( !$block ) {
216 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
217 }
218 if( $block->mRangeStart != $block->mRangeEnd && !strstr( $ip, "/" ) ) {
219 /* If the specified IP is a single address, and the block is
220 * a range block, don't unblock the range. */
221 $range = $block->mAddress;
222 return array( 'ipb_blocked_as_range', $ip, $range );
223 }
224 }
225 }
226 // Yes, this is really necessary
227 $id = $block->mId;
228
229 # If the name was hidden and the blocking user cannot hide
230 # names, then don't allow any block removals...
231 if( $blocker && $block->mHideName && !$blocker->isAllowed( 'hideuser' ) ) {
232 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
233 }
234
235 # Delete block
236 if ( !$block->delete() ) {
237 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
238 }
239
240 # Unset _deleted fields as needed
241 if( $block->mHideName ) {
242 IPBlockForm::unsuppressUserName( $block->mAddress, $block->mUser );
243 }
244
245 # Make log entry
246 $log = new LogPage( 'block' );
247 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
248 return array();
249 }
250
251 function doSubmit() {
252 global $wgOut, $wgUser;
253
254 $retval = self::doUnblock( $this->id, $this->ip, $this->reason, $range, $wgUser );
255 if( !empty( $retval ) ) {
256 $key = array_shift( $retval );
257 $this->showForm( wfMsgReal( $key, $retval ) );
258 return;
259 }
260
261 # Report to the user
262 $success = $this->getTitle()->getFullURL( 'action=success&successip=' . urlencode( $this->ip ) );
263 $wgOut->redirect( $success );
264 }
265
266 function showList( $msg ) {
267 global $wgOut, $wgUser;
268
269 if ( $msg != '' ) {
270 $wgOut->setSubtitle( $msg );
271 }
272
273 // Purge expired entries on one in every 10 queries
274 if ( !mt_rand( 0, 10 ) ) {
275 Block::purgeExpired();
276 }
277
278 $conds = array();
279 // Is user allowed to see all the blocks?
280 if ( !$wgUser->isAllowed( 'hideuser' ) )
281 $conds['ipb_deleted'] = 0;
282 if ( $this->ip == '' ) {
283 // No extra conditions
284 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
285 $conds['ipb_id'] = substr( $this->ip, 1 );
286 // Single IPs
287 } elseif ( IP::isIPAddress( $this->ip ) && strpos( $this->ip, '/' ) === false ) {
288 if( $iaddr = IP::toHex( $this->ip ) ) {
289 # Only scan ranges which start in this /16, this improves search speed
290 # Blocks should not cross a /16 boundary.
291 $range = substr( $iaddr, 0, 4 );
292 // Fixme -- encapsulate this sort of query-building.
293 $dbr = wfGetDB( DB_SLAVE );
294 $encIp = $dbr->addQuotes( IP::sanitizeIP( $this->ip ) );
295 $encAddr = $dbr->addQuotes( $iaddr );
296 $conds[] = "(ipb_address = $encIp) OR
297 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
298 ipb_range_start <= $encAddr
299 AND ipb_range_end >= $encAddr)";
300 } else {
301 $conds['ipb_address'] = IP::sanitizeIP( $this->ip );
302 }
303 $conds['ipb_auto'] = 0;
304 // IP range
305 } elseif ( IP::isIPAddress( $this->ip ) ) {
306 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
307 $conds['ipb_auto'] = 0;
308 } else {
309 $user = User::newFromName( $this->ip );
310 if ( $user && ( $id = $user->getId() ) != 0 ) {
311 $conds['ipb_user'] = $id;
312 } else {
313 // Uh...?
314 $conds['ipb_address'] = $this->ip;
315 $conds['ipb_auto'] = 0;
316 }
317 }
318 // Apply filters
319 if( $this->hideuserblocks ) {
320 $conds['ipb_user'] = 0;
321 }
322 if( $this->hidetempblocks ) {
323 $conds['ipb_expiry'] = 'infinity';
324 }
325 if( $this->hideaddressblocks ) {
326 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
327 }
328
329 // Search form
330 $wgOut->addHTML( $this->searchForm() );
331
332 // Check for other blocks, i.e. global/tor blocks
333 $otherBlockLink = array();
334 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip ) );
335
336 // Show additional header for the local block only when other blocks exists.
337 // Not necessary in a standard installation without such extensions enabled
338 if( count( $otherBlockLink ) ) {
339 $wgOut->addHTML(
340 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
341 );
342 }
343 $pager = new IPBlocklistPager( $this, $conds );
344 if ( $pager->getNumRows() ) {
345 $wgOut->addHTML(
346 $pager->getNavigationBar() .
347 Html::rawElement( 'ul', null, $pager->getBody() ) .
348 $pager->getNavigationBar()
349 );
350 } elseif ( $this->ip != '') {
351 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
352 } else {
353 $wgOut->addWikiMsg( 'ipblocklist-empty' );
354 }
355
356 if( count( $otherBlockLink ) ) {
357 $wgOut->addHTML(
358 Html::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
359 );
360 $list = '';
361 foreach( $otherBlockLink as $link ) {
362 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
363 }
364 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
365 }
366
367 }
368
369 function searchForm() {
370 global $wgScript, $wgLang;
371
372 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
373 $nondefaults = array();
374 if( $this->hideuserblocks ) {
375 $nondefaults['hideuserblocks'] = $this->hideuserblocks;
376 }
377 if( $this->hidetempblocks ) {
378 $nondefaults['hidetempblocks'] = $this->hidetempblocks;
379 }
380 if( $this->hideaddressblocks ) {
381 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks;
382 }
383 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks],
384 array( 'hideuserblocks' => 1-$this->hideuserblocks ), $nondefaults);
385 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks],
386 array( 'hidetempblocks' => 1-$this->hidetempblocks ), $nondefaults);
387 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks],
388 array( 'hideaddressblocks' => 1-$this->hideaddressblocks ), $nondefaults);
389
390 $links = array();
391 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
392 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
393 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
394 $hl = $wgLang->pipeList( $links );
395
396 return
397 Html::rawElement( 'form', array( 'action' => $wgScript ),
398 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
399 Html::openElement( 'fieldset' ) .
400 Html::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
401 Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
402 '&#160;' .
403 Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
404 $hl .
405 Html::closeElement( 'fieldset' )
406 );
407 }
408
409 /**
410 * Makes change an option link which carries all the other options
411 * @param $title see Title
412 * @param $override
413 * @param $options
414 */
415 function makeOptionsLink( $title, $override, $options, $active = false ) {
416 global $wgUser;
417 $sk = $wgUser->getSkin();
418 $params = $override + $options;
419 return $sk->link( $this->getTitle(), htmlspecialchars( $title ),
420 ( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
421 }
422
423 /**
424 * Callback function to output a block
425 */
426 function formatRow( $block ) {
427 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
428
429 wfProfileIn( __METHOD__ );
430
431 static $sk=null, $msg=null;
432
433 if( is_null( $sk ) )
434 $sk = $wgUser->getSkin();
435 if( is_null( $msg ) ) {
436 $msg = array();
437 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
438 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
439 foreach( $keys as $key ) {
440 $msg[$key] = wfMsgHtml( $key );
441 }
442 }
443
444 # Prepare links to the blocker's user and talk pages
445 $blocker_id = $block->getBy();
446 $blocker_name = $block->getByName();
447 $blocker = $sk->userLink( $blocker_id, $blocker_name );
448 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
449
450 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
451 if( $block->mAuto ) {
452 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
453 } else {
454 $target = $sk->userLink( $block->mUser, $block->mAddress )
455 . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
456 }
457
458 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp, true ) );
459
460 $properties = array();
461 $properties[] = Block::formatExpiry( $block->mExpiry );
462 if ( $block->mAnonOnly ) {
463 $properties[] = $msg['anononlyblock'];
464 }
465 if ( $block->mCreateAccount ) {
466 $properties[] = $msg['createaccountblock'];
467 }
468 if (!$block->mEnableAutoblock && $block->mUser ) {
469 $properties[] = $msg['noautoblockblock'];
470 }
471
472 if ( $block->mBlockEmail && $block->mUser ) {
473 $properties[] = $msg['emailblock'];
474 }
475
476 if ( !$block->mAllowUsertalk && $wgBlockAllowsUTEdit ) {
477 $properties[] = $msg['blocklist-nousertalk'];
478 }
479
480 $properties = $wgLang->commaList( $properties );
481
482 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
483
484 $unblocklink = '';
485 $changeblocklink = '';
486 $toolLinks = '';
487 if ( $wgUser->isAllowed( 'block' ) ) {
488 $unblocklink = $sk->link( $this->getTitle(),
489 $msg['unblocklink'],
490 array(),
491 array( 'action' => 'unblock', 'id' => $block->mId ),
492 'known' );
493
494 # Create changeblocklink for all blocks with exception of autoblocks
495 if( !$block->mAuto ) {
496 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
497 $sk->link( SpecialPage::getTitleFor( 'Blockip', $block->mAddress ),
498 $msg['change-blocklink'],
499 array(), array(), 'known' );
500 }
501 $toolLinks = "($unblocklink$changeblocklink)";
502 }
503
504 $comment = $sk->commentBlock( htmlspecialchars($block->mReason) );
505
506 $s = "{$line} $comment";
507 if ( $block->mHideName )
508 $s = '<span class="history-deleted">' . $s . '</span>';
509
510 wfProfileOut( __METHOD__ );
511 return "<li>$s $toolLinks</li>\n";
512 }
513 }
514
515 /**
516 * @todo document
517 * @ingroup Pager
518 */
519 class IPBlocklistPager extends ReverseChronologicalPager {
520 public $mForm, $mConds;
521
522 function __construct( $form, $conds = array() ) {
523 $this->mForm = $form;
524 $this->mConds = $conds;
525 parent::__construct();
526 }
527
528 function getStartBody() {
529 wfProfileIn( __METHOD__ );
530 # Do a link batch query
531 $this->mResult->seek( 0 );
532 $lb = new LinkBatch;
533
534 /*
535 while ( $row = $this->mResult->fetchObject() ) {
536 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
537 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
538 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
539 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
540 }*/
541 # Faster way
542 # Usernames and titles are in fact related by a simple substitution of space -> underscore
543 # The last few lines of Title::secureAndSplit() tell the story.
544 while ( $row = $this->mResult->fetchObject() ) {
545 $name = str_replace( ' ', '_', $row->ipb_by_text );
546 $lb->add( NS_USER, $name );
547 $lb->add( NS_USER_TALK, $name );
548 $name = str_replace( ' ', '_', $row->ipb_address );
549 $lb->add( NS_USER, $name );
550 $lb->add( NS_USER_TALK, $name );
551 }
552 $lb->execute();
553 wfProfileOut( __METHOD__ );
554 return '';
555 }
556
557 function formatRow( $row ) {
558 $block = new Block;
559 $block->initFromRow( $row );
560 return $this->mForm->formatRow( $block );
561 }
562
563 function getQueryInfo() {
564 $conds = $this->mConds;
565 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
566 return array(
567 'tables' => 'ipblocks',
568 'fields' => '*',
569 'conds' => $conds,
570 );
571 }
572
573 function getIndexField() {
574 return 'ipb_timestamp';
575 }
576 }