4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup SpecialPage
26 * @param $ip part of title: Special:Ipblocklist/<ip>.
29 function wfSpecialIpblocklist( $ip = '' ) {
30 global $wgUser, $wgOut, $wgRequest;
31 $ip = $wgRequest->getVal( 'ip', $ip );
32 $ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $ip ) );
33 $id = $wgRequest->getVal( 'id' );
34 $reason = $wgRequest->getText( 'wpUnblockReason' );
35 $action = $wgRequest->getText( 'action' );
36 $successip = $wgRequest->getVal( 'successip' );
38 $ipu = new IPUnblockForm( $ip, $id, $reason );
40 if( $action == 'unblock' ||
$action == 'submit' && $wgRequest->wasPosted() ) {
42 if( !$wgUser->isAllowed( 'block' ) ) {
43 $wgOut->permissionRequired( 'block' );
46 # Check for database lock
48 $wgOut->readOnlyPage();
52 # bug 15810: blocked admins should have limited access here
53 if ( $wgUser->isBlocked() ) {
55 # This doesn't pick up on autoblocks, but admins
56 # should have the ipblock-exempt permission anyway
57 $block = Block
::newFromID( $id );
58 $user = User
::newFromName( $block->mAddress
);
60 $user = User
::newFromName( $ip );
62 $status = IPBlockForm
::checkUnblockSelf( $user );
63 if ( $status !== true ) {
64 throw new ErrorPageError( 'badaccess', $status );
68 if( $action == 'unblock' ){
71 } elseif( $action == 'submit'
72 && $wgRequest->wasPosted()
73 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
75 # Remove blocks and redirect user to success page
79 } elseif( $action == 'success' ) {
80 # Inform the user of a successful unblock
81 # (No need to check permissions or locks here,
82 # if something was done, then it's too late!)
83 if ( substr( $successip, 0, 1) == '#' ) {
84 // A block ID was unblocked
85 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
87 // A username/IP was unblocked
88 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
91 # Just show the block list
98 * implements Special:ipblocklist GUI
99 * @ingroup SpecialPage
101 class IPUnblockForm
{
102 var $ip, $reason, $id;
104 function IPUnblockForm( $ip, $id, $reason ) {
106 $this->ip
= strtr( $ip, '_', ' ' );
108 $this->reason
= $reason;
109 $this->hideuserblocks
= $wgRequest->getBool( 'hideuserblocks' );
110 $this->hidetempblocks
= $wgRequest->getBool( 'hidetempblocks' );
111 $this->hideaddressblocks
= $wgRequest->getBool( 'hideaddressblocks' );
115 * Generates the unblock form
116 * @param $err string: error message
117 * @return $out string: HTML form
119 function showForm( $err ) {
120 global $wgOut, $wgUser, $wgSysopUserBans;
122 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
123 $wgOut->addWikiMsg( 'unblockiptext' );
125 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
126 $action = $titleObj->getLocalURL( "action=submit" );
129 $wgOut->setSubtitle( wfMsg( "formerror" ) );
130 $wgOut->addWikiText( Xml
::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
133 $addressPart = false;
135 $block = Block
::newFromID( $this->id
);
137 $encName = htmlspecialchars( $block->getRedactedName() );
139 $addressPart = $encName . Xml
::hidden( 'id', $encId );
140 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
143 if ( !$addressPart ) {
144 $addressPart = Xml
::input( 'wpUnblockAddress', 40, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
145 $ipa = Xml
::label( wfMsg( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
149 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
150 Xml
::openElement( 'fieldset' ) .
151 Xml
::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
152 Xml
::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
154 <td class='mw-label'>
157 <td class='mw-input'>
162 <td class='mw-label'>" .
163 Xml
::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
165 <td class='mw-input'>" .
166 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
171 <td class='mw-submit'>" .
172 Xml
::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
175 Xml
::closeElement( 'table' ) .
176 Xml
::closeElement( 'fieldset' ) .
177 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
178 Xml
::closeElement( 'form' ) . "\n"
183 const UNBLOCK_SUCCESS
= 0; // Success
184 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
185 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
186 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
187 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
190 * Backend code for unblocking. doSubmit() wraps around this.
191 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
192 * case it contains the range $ip is part of.
193 * @return array array(message key, parameters) on failure, empty array on success
196 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();
204 $block = new Block();
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;
253 $retval = self
::doUnblock($this->id
, $this->ip
, $this->reason
, $range, $wgUser);
254 if( !empty($retval) ) {
255 $key = array_shift($retval);
256 $this->showForm(wfMsgReal($key, $retval));
260 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
261 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip
) );
262 $wgOut->redirect( $success );
265 function showList( $msg ) {
266 global $wgOut, $wgUser;
268 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
270 $wgOut->setSubtitle( $msg );
273 // Purge expired entries on one in every 10 queries
274 if ( !mt_rand( 0, 10 ) ) {
275 Block
::purgeExpired();
280 // Is user allowed to see all the blocks?
281 if ( !$wgUser->isAllowed( 'hideuser' ) )
282 $conds['ipb_deleted'] = 0;
283 if ( $this->ip
== '' ) {
284 // No extra conditions
285 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
286 $conds['ipb_id'] = substr( $this->ip
, 1 );
288 } elseif ( IP
::isIPAddress($this->ip
) && strpos($this->ip
,'/') === false ) {
289 if( $iaddr = IP
::toHex($this->ip
) ) {
290 # Only scan ranges which start in this /16, this improves search speed
291 # Blocks should not cross a /16 boundary.
292 $range = substr( $iaddr, 0, 4 );
293 // Fixme -- encapsulate this sort of query-building.
294 $dbr = wfGetDB( DB_SLAVE
);
295 $encIp = $dbr->addQuotes( IP
::sanitizeIP($this->ip
) );
296 $encAddr = $dbr->addQuotes( $iaddr );
297 $conds[] = "(ipb_address = $encIp) OR
298 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
299 ipb_range_start <= $encAddr
300 AND ipb_range_end >= $encAddr)";
302 $conds['ipb_address'] = IP
::sanitizeIP($this->ip
);
304 $conds['ipb_auto'] = 0;
306 } elseif ( IP
::isIPAddress($this->ip
) ) {
307 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
308 $conds['ipb_auto'] = 0;
310 $user = User
::newFromName( $this->ip
);
311 if ( $user && ( $id = $user->getId() ) != 0 ) {
312 $conds['ipb_user'] = $id;
315 $conds['ipb_address'] = $this->ip
;
316 $conds['ipb_auto'] = 0;
320 if( $this->hideuserblocks
) {
321 $conds['ipb_user'] = 0;
323 if( $this->hidetempblocks
) {
324 $conds['ipb_expiry'] = 'infinity';
326 if( $this->hideaddressblocks
) {
327 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
331 $wgOut->addHTML( $this->searchForm() );
333 // Check for other blocks, i.e. global/tor blocks
334 $otherBlockLink = array();
335 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip
) );
337 // Show additional header for the local block only when other blocks exists.
338 // Not necessary in a standard installation without such extensions enabled
339 if( count( $otherBlockLink ) ) {
341 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
344 $pager = new IPBlocklistPager( $this, $conds );
345 if ( $pager->getNumRows() ) {
347 $pager->getNavigationBar() .
348 Xml
::tags( 'ul', null, $pager->getBody() ) .
349 $pager->getNavigationBar()
351 } elseif ( $this->ip
!= '') {
352 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
354 $wgOut->addWikiMsg( 'ipblocklist-empty' );
357 if( count( $otherBlockLink ) ) {
359 Html
::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
362 foreach( $otherBlockLink as $link ) {
363 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
365 $wgOut->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
370 function searchForm() {
371 global $wgScript, $wgRequest, $wgLang;
373 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
374 $nondefaults = array();
375 if( $this->hideuserblocks
) {
376 $nondefaults['hideuserblocks'] = $this->hideuserblocks
;
378 if( $this->hidetempblocks
) {
379 $nondefaults['hidetempblocks'] = $this->hidetempblocks
;
381 if( $this->hideaddressblocks
) {
382 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks
;
384 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks
],
385 array( 'hideuserblocks' => 1-$this->hideuserblocks
), $nondefaults);
386 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks
],
387 array( 'hidetempblocks' => 1-$this->hidetempblocks
), $nondefaults);
388 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks
],
389 array( 'hideaddressblocks' => 1-$this->hideaddressblocks
), $nondefaults);
392 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
393 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
394 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
395 $hl = $wgLang->pipeList( $links );
398 Xml
::tags( 'form', array( 'action' => $wgScript ),
399 Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Ipblocklist' )->getPrefixedDbKey() ) .
400 Xml
::openElement( 'fieldset' ) .
401 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
402 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
404 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
406 Xml
::closeElement( 'fieldset' )
411 * Makes change an option link which carries all the other options
412 * @param $title see Title
416 function makeOptionsLink( $title, $override, $options, $active = false ) {
418 $sk = $wgUser->getSkin();
419 $params = $override +
$options;
420 $ipblocklist = SpecialPage
::getTitleFor( 'Ipblocklist' );
421 return $sk->link( $ipblocklist, htmlspecialchars( $title ),
422 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
426 * Callback function to output a block
428 function formatRow( $block ) {
429 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
431 wfProfileIn( __METHOD__
);
433 static $sk=null, $msg=null;
436 $sk = $wgUser->getSkin();
437 if( is_null( $msg ) ) {
439 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
440 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
441 foreach( $keys as $key ) {
442 $msg[$key] = wfMsgHtml( $key );
446 # Prepare links to the blocker's user and talk pages
447 $blocker_id = $block->getBy();
448 $blocker_name = $block->getByName();
449 $blocker = $sk->userLink( $blocker_id, $blocker_name );
450 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
452 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
453 if( $block->mAuto
) {
454 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
456 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
457 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
460 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp
, true ) );
462 $properties = array();
463 $properties[] = Block
::formatExpiry( $block->mExpiry
);
464 if ( $block->mAnonOnly
) {
465 $properties[] = $msg['anononlyblock'];
467 if ( $block->mCreateAccount
) {
468 $properties[] = $msg['createaccountblock'];
470 if (!$block->mEnableAutoblock
&& $block->mUser
) {
471 $properties[] = $msg['noautoblockblock'];
474 if ( $block->mBlockEmail
&& $block->mUser
) {
475 $properties[] = $msg['emailblock'];
478 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
479 $properties[] = $msg['blocklist-nousertalk'];
482 $properties = $wgLang->commaList( $properties );
484 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
487 $changeblocklink = '';
489 if ( $wgUser->isAllowed( 'block' ) ) {
490 $unblocklink = $sk->link( SpecialPage
::getTitleFor( 'Ipblocklist' ),
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 while ( $row = $this->mResult
->fetchObject() ) {
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';