3 * Implements Special:ipblocklist
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.
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.
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
21 * @ingroup SpecialPage
25 * A special page that lists existing blocks and allows users with the 'block'
26 * permission to remove blocks
28 * @ingroup SpecialPage
30 class IPUnblockForm
extends SpecialPage
{
31 var $ip, $reason, $id;
32 var $hideuserblocks, $hidetempblocks, $hideaddressblocks;
34 function __construct() {
35 parent
::__construct( 'Ipblocklist' );
39 * Main execution point
41 * @param $ip part of title: Special:Ipblocklist/<ip>.
43 function execute( $ip ) {
44 global $wgUser, $wgOut, $wgRequest;
47 $this->outputHeader();
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' );
57 $action = $wgRequest->getText( 'action' );
58 $successip = $wgRequest->getVal( 'successip' );
60 if( $action == 'unblock' ||
$action == 'submit' && $wgRequest->wasPosted() ) {
62 if( !$wgUser->isAllowed( 'block' ) ) {
63 $wgOut->permissionRequired( 'block' );
66 # Check for database lock
68 $wgOut->readOnlyPage();
72 # bug 15810: blocked admins should have limited access here
73 if ( $wgUser->isBlocked() ) {
75 # This doesn't pick up on autoblocks, but admins
76 # should have the ipblock-exempt permission anyway
77 $block = Block
::newFromID( $this->id
);
78 $user = User
::newFromName( $block->mAddress
);
80 $user = User
::newFromName( $ip );
82 $status = IPBlockForm
::checkUnblockSelf( $user );
83 if ( $status !== true ) {
84 throw new ErrorPageError( 'badaccess', $status );
88 if( $action == 'unblock' ){
91 } elseif( $action == 'submit'
92 && $wgRequest->wasPosted()
93 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
95 # Remove blocks and redirect user to success page
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 ) ) );
107 // A username/IP was unblocked
108 $this->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
111 # Just show the block list
112 $this->showList( '' );
117 * Generates the unblock form
119 * @param $err String, Array or null: error message name or an array if
120 * there are parameters. Null indicates no error.
121 * @return $out string: HTML form
123 function showForm( $err = null ) {
124 global $wgOut, $wgUser, $wgSysopUserBans;
126 $wgOut->addWikiMsg( 'unblockiptext' );
128 $action = $this->getTitle()->getLocalURL( 'action=submit' );
130 if ( $err !== null ) {
131 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
132 $wgOut->wrapWikiMsg( "<span class='error'>$1</span>\n", $err );
135 $addressPart = false;
137 $block = Block
::newFromID( $this->id
);
139 $encName = htmlspecialchars( $block->getRedactedName() );
141 $addressPart = $encName . Html
::hidden( 'id', $encId );
142 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
145 if ( !$addressPart ) {
146 $addressPart = Xml
::input( 'wpUnblockAddress', 40, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
147 $ipa = Xml
::label( wfMsg( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
151 Html
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
152 Html
::openElement( 'fieldset' ) .
153 Html
::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
154 Html
::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
156 <td class='mw-label'>
159 <td class='mw-input'>
164 <td class='mw-label'>" .
165 Xml
::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
167 <td class='mw-input'>" .
168 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
173 <td class='mw-submit'>" .
174 Xml
::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
177 Html
::closeElement( 'table' ) .
178 Html
::closeElement( 'fieldset' ) .
179 Html
::hidden( 'wpEditToken', $wgUser->editToken() ) .
180 Html
::closeElement( 'form' ) . "\n"
185 const UNBLOCK_SUCCESS
= 0; // Success
186 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
187 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
188 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
189 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
192 * Backend code for unblocking. doSubmit() wraps around this.
193 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
194 * case it contains the range $ip is part of.
195 * @return array array(message key, parameters) on failure, empty array on success
197 public static function doUnblock( &$id, &$ip, &$reason, &$range = null, $blocker = null ) {
199 $block = Block
::newFromID( $id );
201 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
203 $ip = $block->getRedactedName();
206 if ( substr( $ip, 0, 1 ) == "#" ) {
207 $id = substr( $ip, 1 );
208 $block = Block
::newFromID( $id );
210 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
212 $ip = $block->getRedactedName();
214 $block = Block
::newFromDB( $ip );
216 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
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 );
226 // Yes, this is really necessary
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 ) );
236 if ( !$block->delete() ) {
237 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
240 # Unset _deleted fields as needed
241 if( $block->mHideName
) {
242 IPBlockForm
::unsuppressUserName( $block->mAddress
, $block->mUser
);
246 $log = new LogPage( 'block' );
247 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
251 function doSubmit() {
252 global $wgOut, $wgUser;
254 $retval = self
::doUnblock( $this->id
, $this->ip
, $this->reason
, $range, $wgUser );
255 if ( count( $retval ) ) {
256 $this->showForm( $retval );
261 $success = $this->getTitle()->getFullURL( 'action=success&successip=' . urlencode( $this->ip
) );
262 $wgOut->redirect( $success );
265 function showList( $msg ) {
266 global $wgOut, $wgUser;
269 $wgOut->setSubtitle( $msg );
272 // Purge expired entries on one in every 10 queries
273 if ( !mt_rand( 0, 10 ) ) {
274 Block
::purgeExpired();
278 // Is user allowed to see all the blocks?
279 if ( !$wgUser->isAllowed( 'hideuser' ) )
280 $conds['ipb_deleted'] = 0;
281 if ( $this->ip
== '' ) {
282 // No extra conditions
283 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
284 $conds['ipb_id'] = substr( $this->ip
, 1 );
286 } elseif ( IP
::isIPAddress( $this->ip
) && strpos( $this->ip
, '/' ) === false ) {
287 $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)";
301 $conds['ipb_address'] = IP
::sanitizeIP( $this->ip
);
303 $conds['ipb_auto'] = 0;
305 } elseif ( IP
::isIPAddress( $this->ip
) ) {
306 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
307 $conds['ipb_auto'] = 0;
309 $user = User
::newFromName( $this->ip
);
310 if ( $user && ( $id = $user->getId() ) != 0 ) {
311 $conds['ipb_user'] = $id;
314 $conds['ipb_address'] = $this->ip
;
315 $conds['ipb_auto'] = 0;
319 if( $this->hideuserblocks
) {
320 $conds['ipb_user'] = 0;
322 if( $this->hidetempblocks
) {
323 $conds['ipb_expiry'] = 'infinity';
325 if( $this->hideaddressblocks
) {
326 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
330 $wgOut->addHTML( $this->searchForm() );
332 // Check for other blocks, i.e. global/tor blocks
333 $otherBlockLink = array();
334 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip
) );
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 ) ) {
340 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
343 $pager = new IPBlocklistPager( $this, $conds );
344 if ( $pager->getNumRows() ) {
346 $pager->getNavigationBar() .
347 Html
::rawElement( 'ul', null, $pager->getBody() ) .
348 $pager->getNavigationBar()
350 } elseif ( $this->ip
!= '') {
351 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
353 $wgOut->addWikiMsg( 'ipblocklist-empty' );
356 if( count( $otherBlockLink ) ) {
358 Html
::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
361 foreach( $otherBlockLink as $link ) {
362 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
364 $wgOut->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
369 function searchForm() {
370 global $wgScript, $wgLang;
372 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
373 $nondefaults = array();
374 if( $this->hideuserblocks
) {
375 $nondefaults['hideuserblocks'] = $this->hideuserblocks
;
377 if( $this->hidetempblocks
) {
378 $nondefaults['hidetempblocks'] = $this->hidetempblocks
;
380 if( $this->hideaddressblocks
) {
381 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks
;
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);
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 );
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
) .
403 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
405 Html
::closeElement( 'fieldset' )
410 * Makes change an option link which carries all the other options
412 * @param $title see Title
413 * @param $override Array: special query string options, will override the
415 * @param $options Array: query string options
416 * @param $active Boolean: whether to display the link in bold
418 function makeOptionsLink( $title, $override, $options, $active = false ) {
420 $sk = $wgUser->getSkin();
421 $params = $override +
$options;
422 return $sk->link( $this->getTitle(), htmlspecialchars( $title ),
423 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
427 * Callback function to output a block
429 function formatRow( $block ) {
430 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
432 wfProfileIn( __METHOD__
);
434 static $sk=null, $msg=null;
437 $sk = $wgUser->getSkin();
438 if( is_null( $msg ) ) {
440 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
441 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
442 foreach( $keys as $key ) {
443 $msg[$key] = wfMsgHtml( $key );
447 # Prepare links to the blocker's user and talk pages
448 $blocker_id = $block->getBy();
449 $blocker_name = $block->getByName();
450 $blocker = $sk->userLink( $blocker_id, $blocker_name );
451 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
453 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
454 if( $block->mAuto
) {
455 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
457 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
458 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
461 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp
, true ) );
463 $properties = array();
464 $properties[] = Block
::formatExpiry( $block->mExpiry
);
465 if ( $block->mAnonOnly
) {
466 $properties[] = $msg['anononlyblock'];
468 if ( $block->mCreateAccount
) {
469 $properties[] = $msg['createaccountblock'];
471 if (!$block->mEnableAutoblock
&& $block->mUser
) {
472 $properties[] = $msg['noautoblockblock'];
475 if ( $block->mBlockEmail
&& $block->mUser
) {
476 $properties[] = $msg['emailblock'];
479 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
480 $properties[] = $msg['blocklist-nousertalk'];
483 $properties = $wgLang->commaList( $properties );
485 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
487 $changeblocklink = '';
489 if ( $wgUser->isAllowed( 'block' ) ) {
490 $unblocklink = $sk->link( $this->getTitle(),
493 array( 'action' => 'unblock', 'id' => $block->mId
),
496 # Create changeblocklink for all blocks with exception of autoblocks
497 if( !$block->mAuto
) {
498 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
499 $sk->link( SpecialPage
::getTitleFor( 'Blockip', $block->mAddress
),
500 $msg['change-blocklink'],
501 array(), array(), 'known' );
503 $toolLinks = "($unblocklink$changeblocklink)";
506 $comment = $sk->commentBlock( htmlspecialchars($block->mReason
) );
508 $s = "{$line} $comment";
509 if ( $block->mHideName
)
510 $s = '<span class="history-deleted">' . $s . '</span>';
512 wfProfileOut( __METHOD__
);
513 return "<li>$s $toolLinks</li>\n";
521 class IPBlocklistPager
extends ReverseChronologicalPager
{
522 public $mForm, $mConds;
524 function __construct( $form, $conds = array() ) {
525 $this->mForm
= $form;
526 $this->mConds
= $conds;
527 parent
::__construct();
530 function getStartBody() {
531 wfProfileIn( __METHOD__
);
532 # Do a link batch query
533 $this->mResult
->seek( 0 );
537 while ( $row = $this->mResult->fetchObject() ) {
538 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
539 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
540 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
541 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
544 # Usernames and titles are in fact related by a simple substitution of space -> underscore
545 # The last few lines of Title::secureAndSplit() tell the story.
546 foreach ( $this->mResult
as $row ) {
547 $name = str_replace( ' ', '_', $row->ipb_by_text
);
548 $lb->add( NS_USER
, $name );
549 $lb->add( NS_USER_TALK
, $name );
550 $name = str_replace( ' ', '_', $row->ipb_address
);
551 $lb->add( NS_USER
, $name );
552 $lb->add( NS_USER_TALK
, $name );
555 wfProfileOut( __METHOD__
);
559 function formatRow( $row ) {
561 $block->initFromRow( $row );
562 return $this->mForm
->formatRow( $block );
565 function getQueryInfo() {
566 $conds = $this->mConds
;
567 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
569 'tables' => 'ipblocks',
575 function getIndexField() {
576 return 'ipb_timestamp';