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( $this->ip );
82 $status = IPBlockForm::checkUnblockSelf( $user );
83 if ( $status !== true ) {
84 throw new ErrorPageError( 'badaccess', $status );
88 if( $action == 'unblock' ){
90 $this->showForm( '' );
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: error message
120 * @return $out string: HTML form
122 function showForm( $err ) {
123 global $wgOut, $wgUser, $wgSysopUserBans;
125 $wgOut->addWikiMsg( 'unblockiptext' );
127 $action = $this->getTitle()->getLocalURL( 'action=submit' );
130 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
131 $wgOut->addWikiText( Html::rawElement( 'span', array( 'class' => 'error' ), $err ) . "\n" );
134 $addressPart = false;
136 $block = Block::newFromID( $this->id );
138 $encName = htmlspecialchars( $block->getRedactedName() );
140 $addressPart = $encName . Html::hidden( 'id', $encId );
141 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
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' );
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' ) ).
155 <td class='mw-label'>
158 <td class='mw-input'>
163 <td class='mw-label'>" .
164 Xml::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
166 <td class='mw-input'>" .
167 Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
172 <td class='mw-submit'>" .
173 Xml::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
176 Html::closeElement( 'table' ) .
177 Html::closeElement( 'fieldset' ) .
178 Html::hidden( 'wpEditToken', $wgUser->editToken() ) .
179 Html::closeElement( 'form' ) . "\n"
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
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
196 public static function doUnblock( &$id, &$ip, &$reason, &$range = null, $blocker = null ) {
198 $block = Block::newFromID( $id );
200 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
202 $ip = $block->getRedactedName();
205 if ( substr( $ip, 0, 1 ) == "#" ) {
206 $id = substr( $ip, 1 );
207 $block = Block::newFromID( $id );
209 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
211 $ip = $block->getRedactedName();
213 # FIXME: do proper sanitisation/cleanup here
214 $ip = str_replace( '_', ' ', $ip );
216 $block = Block::newFromDB( $ip );
218 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
220 if( $block->mRangeStart != $block->mRangeEnd && !strstr( $ip, "/" ) ) {
221 /* If the specified IP is a single address, and the block is
222 * a range block, don't unblock the range. */
223 $range = $block->mAddress;
224 return array( 'ipb_blocked_as_range', $ip, $range );
228 // Yes, this is really necessary
231 # If the name was hidden and the blocking user cannot hide
232 # names, then don't allow any block removals...
233 if( $blocker && $block->mHideName && !$blocker->isAllowed( 'hideuser' ) ) {
234 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
238 if ( !$block->delete() ) {
239 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
242 # Unset _deleted fields as needed
243 if( $block->mHideName ) {
244 IPBlockForm::unsuppressUserName( $block->mAddress, $block->mUser );
248 $log = new LogPage( 'block' );
249 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
253 function doSubmit() {
254 global $wgOut, $wgUser;
256 $retval = self::doUnblock( $this->id, $this->ip, $this->reason, $range, $wgUser );
257 if( !empty( $retval ) ) {
258 $key = array_shift( $retval );
259 $this->showForm( wfMsgReal( $key, $retval ) );
264 $success = $this->getTitle()->getFullURL( 'action=success&successip=' . urlencode( $this->ip ) );
265 $wgOut->redirect( $success );
268 function showList( $msg ) {
269 global $wgOut, $wgUser;
272 $wgOut->setSubtitle( $msg );
275 // Purge expired entries on one in every 10 queries
276 if ( !mt_rand( 0, 10 ) ) {
277 Block::purgeExpired();
281 // Is user allowed to see all the blocks?
282 if ( !$wgUser->isAllowed( 'hideuser' ) )
283 $conds['ipb_deleted'] = 0;
284 if ( $this->ip == '' ) {
285 // No extra conditions
286 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
287 $conds['ipb_id'] = substr( $this->ip, 1 );
289 } elseif ( IP::isIPAddress( $this->ip ) && strpos( $this->ip, '/' ) === false ) {
290 $iaddr = IP::toHex( $this->ip );
292 # Only scan ranges which start in this /16, this improves search speed
293 # Blocks should not cross a /16 boundary.
294 $range = substr( $iaddr, 0, 4 );
295 // Fixme -- encapsulate this sort of query-building.
296 $dbr = wfGetDB( DB_SLAVE );
297 $encIp = $dbr->addQuotes( IP::sanitizeIP( $this->ip ) );
298 $encAddr = $dbr->addQuotes( $iaddr );
299 $conds[] = "(ipb_address = $encIp) OR
300 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
301 ipb_range_start <= $encAddr
302 AND ipb_range_end >= $encAddr)";
304 $conds['ipb_address'] = IP::sanitizeIP( $this->ip );
306 $conds['ipb_auto'] = 0;
308 } elseif ( IP::isIPAddress( $this->ip ) ) {
309 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
310 $conds['ipb_auto'] = 0;
312 $user = User::newFromName( $this->ip );
313 if ( $user && ( $id = $user->getId() ) != 0 ) {
314 $conds['ipb_user'] = $id;
317 $conds['ipb_address'] = $this->ip;
318 $conds['ipb_auto'] = 0;
322 if( $this->hideuserblocks ) {
323 $conds['ipb_user'] = 0;
325 if( $this->hidetempblocks ) {
326 $conds['ipb_expiry'] = 'infinity';
328 if( $this->hideaddressblocks ) {
329 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
333 $wgOut->addHTML( $this->searchForm() );
335 // Check for other blocks, i.e. global/tor blocks
336 $otherBlockLink = array();
337 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip ) );
339 // Show additional header for the local block only when other blocks exists.
340 // Not necessary in a standard installation without such extensions enabled
341 if( count( $otherBlockLink ) ) {
343 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
346 $pager = new IPBlocklistPager( $this, $conds );
347 if ( $pager->getNumRows() ) {
349 $pager->getNavigationBar() .
350 Html::rawElement( 'ul', null, $pager->getBody() ) .
351 $pager->getNavigationBar()
353 } elseif ( $this->ip != '') {
354 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
356 $wgOut->addWikiMsg( 'ipblocklist-empty' );
359 if( count( $otherBlockLink ) ) {
361 Html::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
364 foreach( $otherBlockLink as $link ) {
365 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
367 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
372 function searchForm() {
373 global $wgScript, $wgLang;
375 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
376 $nondefaults = array();
377 if( $this->hideuserblocks ) {
378 $nondefaults['hideuserblocks'] = $this->hideuserblocks;
380 if( $this->hidetempblocks ) {
381 $nondefaults['hidetempblocks'] = $this->hidetempblocks;
383 if( $this->hideaddressblocks ) {
384 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks;
386 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks],
387 array( 'hideuserblocks' => 1-$this->hideuserblocks ), $nondefaults);
388 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks],
389 array( 'hidetempblocks' => 1-$this->hidetempblocks ), $nondefaults);
390 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks],
391 array( 'hideaddressblocks' => 1-$this->hideaddressblocks ), $nondefaults);
394 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
395 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
396 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
397 $hl = $wgLang->pipeList( $links );
400 Html::rawElement( 'form', array( 'action' => $wgScript ),
401 Html::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
402 Html::openElement( 'fieldset' ) .
403 Html::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
404 Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
406 Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
408 Html::closeElement( 'fieldset' )
413 * Makes change an option link which carries all the other options
415 * @param $title see Title
416 * @param $override Array: special query string options, will override the
418 * @param $options Array: query string options
419 * @param $active Boolean: whether to display the link in bold
421 function makeOptionsLink( $title, $override, $options, $active = false ) {
423 $sk = $wgUser->getSkin();
424 $params = $override + $options;
425 return $sk->link( $this->getTitle(), htmlspecialchars( $title ),
426 ( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
430 * Callback function to output a block
432 function formatRow( $block ) {
433 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
435 wfProfileIn( __METHOD__ );
437 static $sk=null, $msg=null;
440 $sk = $wgUser->getSkin();
441 if( is_null( $msg ) ) {
443 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
444 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
445 foreach( $keys as $key ) {
446 $msg[$key] = wfMsgHtml( $key );
450 # Prepare links to the blocker's user and talk pages
451 $blocker_id = $block->getBy();
452 $blocker_name = $block->getByName();
453 $blocker = $sk->userLink( $blocker_id, $blocker_name );
454 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
456 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
457 if( $block->mAuto ) {
458 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
460 $target = $sk->userLink( $block->mUser, $block->mAddress )
461 . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
464 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp, true ) );
466 $properties = array();
467 $properties[] = Block::formatExpiry( $block->mExpiry );
468 if ( $block->mAnonOnly ) {
469 $properties[] = $msg['anononlyblock'];
471 if ( $block->mCreateAccount ) {
472 $properties[] = $msg['createaccountblock'];
474 if (!$block->mEnableAutoblock && $block->mUser ) {
475 $properties[] = $msg['noautoblockblock'];
478 if ( $block->mBlockEmail && $block->mUser ) {
479 $properties[] = $msg['emailblock'];
482 if ( !$block->mAllowUsertalk && $wgBlockAllowsUTEdit ) {
483 $properties[] = $msg['blocklist-nousertalk'];
486 $properties = $wgLang->commaList( $properties );
488 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
490 $changeblocklink = '';
492 if ( $wgUser->isAllowed( 'block' ) ) {
493 $unblocklink = $sk->link( $this->getTitle(),
496 array( 'action' => 'unblock', 'id' => $block->mId ),
499 # Create changeblocklink for all blocks with exception of autoblocks
500 if( !$block->mAuto ) {
501 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
502 $sk->link( SpecialPage::getTitleFor( 'Blockip', $block->mAddress ),
503 $msg['change-blocklink'],
504 array(), array(), 'known' );
506 $toolLinks = "($unblocklink$changeblocklink)";
509 $comment = $sk->commentBlock( htmlspecialchars($block->mReason) );
511 $s = "{$line} $comment";
512 if ( $block->mHideName )
513 $s = '<span class="history-deleted">' . $s . '</span>';
515 wfProfileOut( __METHOD__ );
516 return "<li>$s $toolLinks</li>\n";
524 class IPBlocklistPager extends ReverseChronologicalPager {
525 public $mForm, $mConds;
527 function __construct( $form, $conds = array() ) {
528 $this->mForm = $form;
529 $this->mConds = $conds;
530 parent::__construct();
533 function getStartBody() {
534 wfProfileIn( __METHOD__ );
535 # Do a link batch query
536 $this->mResult->seek( 0 );
540 while ( $row = $this->mResult->fetchObject() ) {
541 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
542 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
543 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
544 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
547 # Usernames and titles are in fact related by a simple substitution of space -> underscore
548 # The last few lines of Title::secureAndSplit() tell the story.
549 foreach ( $this->mResult as $row ) {
550 $name = str_replace( ' ', '_', $row->ipb_by_text );
551 $lb->add( NS_USER, $name );
552 $lb->add( NS_USER_TALK, $name );
553 $name = str_replace( ' ', '_', $row->ipb_address );
554 $lb->add( NS_USER, $name );
555 $lb->add( NS_USER_TALK, $name );
558 wfProfileOut( __METHOD__ );
562 function formatRow( $row ) {
564 $block->initFromRow( $row );
565 return $this->mForm->formatRow( $block );
568 function getQueryInfo() {
569 $conds = $this->mConds;
570 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
572 'tables' => 'ipblocks',
578 function getIndexField() {
579 return 'ipb_timestamp';