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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 * Implements Special:ipblocklist
22 * @ingroup SpecialPage
24 class IPUnblockForm
extends SpecialPage
{
25 var $ip, $reason, $id;
26 var $hideuserblocks, $hidetempblocks, $hideaddressblocks;
28 function __construct() {
29 parent
::__construct( 'Ipblocklist' );
33 * Main execution point
35 * @param $ip part of title: Special:Ipblocklist/<ip>.
37 function execute( $ip ) {
38 global $wgUser, $wgOut, $wgRequest;
41 $this->outputHeader();
43 $ip = $wgRequest->getVal( 'ip', $ip );
44 $this->ip
= trim( $wgRequest->getVal( 'wpUnblockAddress', $ip ) );
45 $this->id
= $wgRequest->getVal( 'id' );
46 $this->reason
= $wgRequest->getText( 'wpUnblockReason' );
47 $this->hideuserblocks
= $wgRequest->getBool( 'hideuserblocks' );
48 $this->hidetempblocks
= $wgRequest->getBool( 'hidetempblocks' );
49 $this->hideaddressblocks
= $wgRequest->getBool( 'hideaddressblocks' );
51 $action = $wgRequest->getText( 'action' );
52 $successip = $wgRequest->getVal( 'successip' );
54 if( $action == 'unblock' ||
$action == 'submit' && $wgRequest->wasPosted() ) {
56 if( !$wgUser->isAllowed( 'block' ) ) {
57 $wgOut->permissionRequired( 'block' );
60 # Check for database lock
62 $wgOut->readOnlyPage();
66 # bug 15810: blocked admins should have limited access here
67 if ( $wgUser->isBlocked() ) {
69 # This doesn't pick up on autoblocks, but admins
70 # should have the ipblock-exempt permission anyway
71 $block = Block
::newFromID( $id );
72 $user = User
::newFromName( $block->mAddress
);
74 $user = User
::newFromName( $ip );
76 $status = IPBlockForm
::checkUnblockSelf( $user );
77 if ( $status !== true ) {
78 throw new ErrorPageError( 'badaccess', $status );
82 if( $action == 'unblock' ){
84 $this->showForm( '' );
85 } elseif( $action == 'submit'
86 && $wgRequest->wasPosted()
87 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
89 # Remove blocks and redirect user to success page
93 } elseif( $action == 'success' ) {
94 # Inform the user of a successful unblock
95 # (No need to check permissions or locks here,
96 # if something was done, then it's too late!)
97 if ( substr( $successip, 0, 1) == '#' ) {
98 // A block ID was unblocked
99 $this->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
101 // A username/IP was unblocked
102 $this->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
105 # Just show the block list
106 $this->showList( '' );
111 * Generates the unblock form
113 * @param $err string: error message
114 * @return $out string: HTML form
116 function showForm( $err ) {
117 global $wgOut, $wgUser, $wgSysopUserBans;
119 $wgOut->addWikiMsg( 'unblockiptext' );
121 $action = $this->getTitle()->getLocalURL( 'action=submit' );
124 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
125 $wgOut->addWikiText( Html
::rawElement( 'span', array( 'class' => 'error' ), $err ) . "\n" );
128 $addressPart = false;
130 $block = Block
::newFromID( $this->id
);
132 $encName = htmlspecialchars( $block->getRedactedName() );
134 $addressPart = $encName . Html
::hidden( 'id', $encId );
135 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
138 if ( !$addressPart ) {
139 $addressPart = Xml
::input( 'wpUnblockAddress', 40, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
140 $ipa = Xml
::label( wfMsg( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
144 Html
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
145 Html
::openElement( 'fieldset' ) .
146 Html
::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
147 Html
::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
149 <td class='mw-label'>
152 <td class='mw-input'>
157 <td class='mw-label'>" .
158 Xml
::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
160 <td class='mw-input'>" .
161 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
166 <td class='mw-submit'>" .
167 Xml
::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
170 Html
::closeElement( 'table' ) .
171 Html
::closeElement( 'fieldset' ) .
172 Html
::hidden( 'wpEditToken', $wgUser->editToken() ) .
173 Html
::closeElement( 'form' ) . "\n"
178 const UNBLOCK_SUCCESS
= 0; // Success
179 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
180 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
181 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
182 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
185 * Backend code for unblocking. doSubmit() wraps around this.
186 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
187 * case it contains the range $ip is part of.
188 * @return array array(message key, parameters) on failure, empty array on success
190 public static function doUnblock( &$id, &$ip, &$reason, &$range = null, $blocker = null ) {
192 $block = Block
::newFromID( $id );
194 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
196 $ip = $block->getRedactedName();
198 $block = new Block();
200 if ( substr( $ip, 0, 1 ) == "#" ) {
201 $id = substr( $ip, 1 );
202 $block = Block
::newFromID( $id );
204 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
206 $ip = $block->getRedactedName();
208 $block = Block
::newFromDB( $ip );
210 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
212 if( $block->mRangeStart
!= $block->mRangeEnd
&& !strstr( $ip, "/" ) ) {
213 /* If the specified IP is a single address, and the block is
214 * a range block, don't unblock the range. */
215 $range = $block->mAddress
;
216 return array( 'ipb_blocked_as_range', $ip, $range );
220 // Yes, this is really necessary
223 # If the name was hidden and the blocking user cannot hide
224 # names, then don't allow any block removals...
225 if( $blocker && $block->mHideName
&& !$blocker->isAllowed( 'hideuser' ) ) {
226 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
230 if ( !$block->delete() ) {
231 return array( 'ipb_cant_unblock', htmlspecialchars( $id ) );
234 # Unset _deleted fields as needed
235 if( $block->mHideName
) {
236 IPBlockForm
::unsuppressUserName( $block->mAddress
, $block->mUser
);
240 $log = new LogPage( 'block' );
241 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
245 function doSubmit() {
246 global $wgOut, $wgUser;
248 $retval = self
::doUnblock( $this->id
, $this->ip
, $this->reason
, $range, $wgUser );
249 if( !empty( $retval ) ) {
250 $key = array_shift( $retval );
251 $this->showForm( wfMsgReal( $key, $retval ) );
256 $success = $this->getTitle()->getFullURL( 'action=success&successip=' . urlencode( $this->ip
) );
257 $wgOut->redirect( $success );
260 function showList( $msg ) {
261 global $wgOut, $wgUser;
264 $wgOut->setSubtitle( $msg );
267 // Purge expired entries on one in every 10 queries
268 if ( !mt_rand( 0, 10 ) ) {
269 Block
::purgeExpired();
273 // Is user allowed to see all the blocks?
274 if ( !$wgUser->isAllowed( 'hideuser' ) )
275 $conds['ipb_deleted'] = 0;
276 if ( $this->ip
== '' ) {
277 // No extra conditions
278 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
279 $conds['ipb_id'] = substr( $this->ip
, 1 );
281 } elseif ( IP
::isIPAddress( $this->ip
) && strpos( $this->ip
, '/' ) === false ) {
282 if( $iaddr = IP
::toHex( $this->ip
) ) {
283 # Only scan ranges which start in this /16, this improves search speed
284 # Blocks should not cross a /16 boundary.
285 $range = substr( $iaddr, 0, 4 );
286 // Fixme -- encapsulate this sort of query-building.
287 $dbr = wfGetDB( DB_SLAVE
);
288 $encIp = $dbr->addQuotes( IP
::sanitizeIP( $this->ip
) );
289 $encAddr = $dbr->addQuotes( $iaddr );
290 $conds[] = "(ipb_address = $encIp) OR
291 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
292 ipb_range_start <= $encAddr
293 AND ipb_range_end >= $encAddr)";
295 $conds['ipb_address'] = IP
::sanitizeIP( $this->ip
);
297 $conds['ipb_auto'] = 0;
299 } elseif ( IP
::isIPAddress( $this->ip
) ) {
300 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
301 $conds['ipb_auto'] = 0;
303 $user = User
::newFromName( $this->ip
);
304 if ( $user && ( $id = $user->getId() ) != 0 ) {
305 $conds['ipb_user'] = $id;
308 $conds['ipb_address'] = $this->ip
;
309 $conds['ipb_auto'] = 0;
313 if( $this->hideuserblocks
) {
314 $conds['ipb_user'] = 0;
316 if( $this->hidetempblocks
) {
317 $conds['ipb_expiry'] = 'infinity';
319 if( $this->hideaddressblocks
) {
320 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
324 $wgOut->addHTML( $this->searchForm() );
326 // Check for other blocks, i.e. global/tor blocks
327 $otherBlockLink = array();
328 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip
) );
330 // Show additional header for the local block only when other blocks exists.
331 // Not necessary in a standard installation without such extensions enabled
332 if( count( $otherBlockLink ) ) {
334 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
337 $pager = new IPBlocklistPager( $this, $conds );
338 if ( $pager->getNumRows() ) {
340 $pager->getNavigationBar() .
341 Html
::rawElement( 'ul', null, $pager->getBody() ) .
342 $pager->getNavigationBar()
344 } elseif ( $this->ip
!= '') {
345 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
347 $wgOut->addWikiMsg( 'ipblocklist-empty' );
350 if( count( $otherBlockLink ) ) {
352 Html
::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
355 foreach( $otherBlockLink as $link ) {
356 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
358 $wgOut->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
363 function searchForm() {
364 global $wgScript, $wgLang;
366 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
367 $nondefaults = array();
368 if( $this->hideuserblocks
) {
369 $nondefaults['hideuserblocks'] = $this->hideuserblocks
;
371 if( $this->hidetempblocks
) {
372 $nondefaults['hidetempblocks'] = $this->hidetempblocks
;
374 if( $this->hideaddressblocks
) {
375 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks
;
377 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks
],
378 array( 'hideuserblocks' => 1-$this->hideuserblocks
), $nondefaults);
379 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks
],
380 array( 'hidetempblocks' => 1-$this->hidetempblocks
), $nondefaults);
381 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks
],
382 array( 'hideaddressblocks' => 1-$this->hideaddressblocks
), $nondefaults);
385 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
386 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
387 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
388 $hl = $wgLang->pipeList( $links );
391 Html
::rawElement( 'form', array( 'action' => $wgScript ),
392 Html
::hidden( 'title', $this->getTitle()->getPrefixedDbKey() ) .
393 Html
::openElement( 'fieldset' ) .
394 Html
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
395 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
397 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
399 Html
::closeElement( 'fieldset' )
404 * Makes change an option link which carries all the other options
405 * @param $title see Title
409 function makeOptionsLink( $title, $override, $options, $active = false ) {
411 $sk = $wgUser->getSkin();
412 $params = $override +
$options;
413 return $sk->link( $this->getTitle(), htmlspecialchars( $title ),
414 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
418 * Callback function to output a block
420 function formatRow( $block ) {
421 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
423 wfProfileIn( __METHOD__
);
425 static $sk=null, $msg=null;
428 $sk = $wgUser->getSkin();
429 if( is_null( $msg ) ) {
431 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
432 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
433 foreach( $keys as $key ) {
434 $msg[$key] = wfMsgHtml( $key );
438 # Prepare links to the blocker's user and talk pages
439 $blocker_id = $block->getBy();
440 $blocker_name = $block->getByName();
441 $blocker = $sk->userLink( $blocker_id, $blocker_name );
442 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
444 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
445 if( $block->mAuto
) {
446 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
448 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
449 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
452 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp
, true ) );
454 $properties = array();
455 $properties[] = Block
::formatExpiry( $block->mExpiry
);
456 if ( $block->mAnonOnly
) {
457 $properties[] = $msg['anononlyblock'];
459 if ( $block->mCreateAccount
) {
460 $properties[] = $msg['createaccountblock'];
462 if (!$block->mEnableAutoblock
&& $block->mUser
) {
463 $properties[] = $msg['noautoblockblock'];
466 if ( $block->mBlockEmail
&& $block->mUser
) {
467 $properties[] = $msg['emailblock'];
470 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
471 $properties[] = $msg['blocklist-nousertalk'];
474 $properties = $wgLang->commaList( $properties );
476 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
479 $changeblocklink = '';
481 if ( $wgUser->isAllowed( 'block' ) ) {
482 $unblocklink = $sk->link( $this->getTitle(),
485 array( 'action' => 'unblock', 'id' => $block->mId
),
488 # Create changeblocklink for all blocks with exception of autoblocks
489 if( !$block->mAuto
) {
490 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
491 $sk->link( SpecialPage
::getTitleFor( 'Blockip', $block->mAddress
),
492 $msg['change-blocklink'],
493 array(), array(), 'known' );
495 $toolLinks = "($unblocklink$changeblocklink)";
498 $comment = $sk->commentBlock( htmlspecialchars($block->mReason
) );
500 $s = "{$line} $comment";
501 if ( $block->mHideName
)
502 $s = '<span class="history-deleted">' . $s . '</span>';
504 wfProfileOut( __METHOD__
);
505 return "<li>$s $toolLinks</li>\n";
513 class IPBlocklistPager
extends ReverseChronologicalPager
{
514 public $mForm, $mConds;
516 function __construct( $form, $conds = array() ) {
517 $this->mForm
= $form;
518 $this->mConds
= $conds;
519 parent
::__construct();
522 function getStartBody() {
523 wfProfileIn( __METHOD__
);
524 # Do a link batch query
525 $this->mResult
->seek( 0 );
529 while ( $row = $this->mResult->fetchObject() ) {
530 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
531 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
532 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
533 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
536 # Usernames and titles are in fact related by a simple substitution of space -> underscore
537 # The last few lines of Title::secureAndSplit() tell the story.
538 while ( $row = $this->mResult
->fetchObject() ) {
539 $name = str_replace( ' ', '_', $row->ipb_by_text
);
540 $lb->add( NS_USER
, $name );
541 $lb->add( NS_USER_TALK
, $name );
542 $name = str_replace( ' ', '_', $row->ipb_address
);
543 $lb->add( NS_USER
, $name );
544 $lb->add( NS_USER_TALK
, $name );
547 wfProfileOut( __METHOD__
);
551 function formatRow( $row ) {
553 $block->initFromRow( $row );
554 return $this->mForm
->formatRow( $block );
557 function getQueryInfo() {
558 $conds = $this->mConds
;
559 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
561 'tables' => 'ipblocks',
567 function getIndexField() {
568 return 'ipb_timestamp';