8 * @param $ip part of title: Special:Ipblocklist/<ip>.
11 function wfSpecialIpblocklist( $ip = '' ) {
12 global $wgUser, $wgOut, $wgRequest;
13 $ip = $wgRequest->getVal( 'ip', $ip );
14 $ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $ip ) );
15 $id = $wgRequest->getVal( 'id' );
16 $reason = $wgRequest->getText( 'wpUnblockReason' );
17 $action = $wgRequest->getText( 'action' );
18 $successip = $wgRequest->getVal( 'successip' );
20 $ipu = new IPUnblockForm( $ip, $id, $reason );
22 if( $action == 'unblock' ||
$action == 'submit' && $wgRequest->wasPosted() ) {
24 if( !$wgUser->isAllowed( 'block' ) ) {
25 $wgOut->permissionRequired( 'block' );
28 # Check for database lock
30 $wgOut->readOnlyPage();
34 # bug 15810: blocked admins should have limited access here
35 if ( $wgUser->isBlocked() ) {
37 # This doesn't pick up on autoblocks, but admins
38 # should have the ipblock-exempt permission anyway
39 $block = Block
::newFromID( $id );
40 $user = User
::newFromName( $block->mAddress
);
42 $user = User
::newFromName( $ip );
44 $status = IPBlockForm
::checkUnblockSelf( $user );
45 if ( $status !== true ) {
46 throw new ErrorPageError( 'badaccess', $status );
50 if( $action == 'unblock' ){
53 } elseif( $action == 'submit'
54 && $wgRequest->wasPosted()
55 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
57 # Remove blocks and redirect user to success page
61 } elseif( $action == 'success' ) {
62 # Inform the user of a successful unblock
63 # (No need to check permissions or locks here,
64 # if something was done, then it's too late!)
65 if ( substr( $successip, 0, 1) == '#' ) {
66 // A block ID was unblocked
67 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
69 // A username/IP was unblocked
70 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
73 # Just show the block list
80 * implements Special:ipblocklist GUI
81 * @ingroup SpecialPage
84 var $ip, $reason, $id;
86 function IPUnblockForm( $ip, $id, $reason ) {
88 $this->ip
= strtr( $ip, '_', ' ' );
90 $this->reason
= $reason;
91 $this->hideuserblocks
= $wgRequest->getBool( 'hideuserblocks' );
92 $this->hidetempblocks
= $wgRequest->getBool( 'hidetempblocks' );
93 $this->hideaddressblocks
= $wgRequest->getBool( 'hideaddressblocks' );
97 * Generates the unblock form
98 * @param $err string: error message
99 * @return $out string: HTML form
101 function showForm( $err ) {
102 global $wgOut, $wgUser, $wgSysopUserBans;
104 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
105 $wgOut->addWikiMsg( 'unblockiptext' );
107 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
108 $action = $titleObj->getLocalURL( "action=submit" );
111 $wgOut->setSubtitle( wfMsg( "formerror" ) );
112 $wgOut->addWikiText( Xml
::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
115 $addressPart = false;
117 $block = Block
::newFromID( $this->id
);
119 $encName = htmlspecialchars( $block->getRedactedName() );
121 $addressPart = $encName . Xml
::hidden( 'id', $encId );
122 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
125 if ( !$addressPart ) {
126 $addressPart = Xml
::input( 'wpUnblockAddress', 40, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
127 $ipa = Xml
::label( wfMsg( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
131 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
132 Xml
::openElement( 'fieldset' ) .
133 Xml
::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
134 Xml
::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
136 <td class='mw-label'>
139 <td class='mw-input'>
144 <td class='mw-label'>" .
145 Xml
::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
147 <td class='mw-input'>" .
148 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
153 <td class='mw-submit'>" .
154 Xml
::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
157 Xml
::closeElement( 'table' ) .
158 Xml
::closeElement( 'fieldset' ) .
159 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
160 Xml
::closeElement( 'form' ) . "\n"
165 const UNBLOCK_SUCCESS
= 0; // Success
166 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
167 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
168 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
169 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
172 * Backend code for unblocking. doSubmit() wraps around this.
173 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
174 * case it contains the range $ip is part of.
175 * @return array array(message key, parameters) on failure, empty array on success
178 static function doUnblock(&$id, &$ip, &$reason, &$range = null, $blocker=null) {
180 $block = Block
::newFromID( $id );
182 return array('ipb_cant_unblock', htmlspecialchars($id));
184 $ip = $block->getRedactedName();
186 $block = new Block();
188 if ( substr( $ip, 0, 1 ) == "#" ) {
189 $id = substr( $ip, 1 );
190 $block = Block
::newFromID( $id );
192 return array('ipb_cant_unblock', htmlspecialchars($id));
194 $ip = $block->getRedactedName();
196 $block = Block
::newFromDB( $ip );
198 return array('ipb_cant_unblock', htmlspecialchars($id));
200 if( $block->mRangeStart
!= $block->mRangeEnd
&& !strstr( $ip, "/" ) ) {
201 /* If the specified IP is a single address, and the block is
202 * a range block, don't unblock the range. */
203 $range = $block->mAddress
;
204 return array('ipb_blocked_as_range', $ip, $range);
208 // Yes, this is really necessary
211 # If the name was hidden and the blocking user cannot hide
212 # names, then don't allow any block removals...
213 if( $blocker && $block->mHideName
&& !$blocker->isAllowed('hideuser') ) {
214 return array('ipb_cant_unblock', htmlspecialchars($id));
218 if ( !$block->delete() ) {
219 return array('ipb_cant_unblock', htmlspecialchars($id));
222 # Unset _deleted fields as needed
223 if( $block->mHideName
) {
224 IPBlockForm
::unsuppressUserName( $block->mAddress
, $block->mUser
);
228 $log = new LogPage( 'block' );
229 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
233 function doSubmit() {
234 global $wgOut, $wgUser;
235 $retval = self
::doUnblock($this->id
, $this->ip
, $this->reason
, $range, $wgUser);
236 if( !empty($retval) ) {
237 $key = array_shift($retval);
238 $this->showForm(wfMsgReal($key, $retval));
242 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
243 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip
) );
244 $wgOut->redirect( $success );
247 function showList( $msg ) {
248 global $wgOut, $wgUser;
250 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
252 $wgOut->setSubtitle( $msg );
255 // Purge expired entries on one in every 10 queries
256 if ( !mt_rand( 0, 10 ) ) {
257 Block
::purgeExpired();
262 // Is user allowed to see all the blocks?
263 if ( !$wgUser->isAllowed( 'hideuser' ) )
264 $conds['ipb_deleted'] = 0;
265 if ( $this->ip
== '' ) {
266 // No extra conditions
267 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
268 $conds['ipb_id'] = substr( $this->ip
, 1 );
270 } elseif ( IP
::isIPAddress($this->ip
) && strpos($this->ip
,'/') === false ) {
271 if( $iaddr = IP
::toHex($this->ip
) ) {
272 # Only scan ranges which start in this /16, this improves search speed
273 # Blocks should not cross a /16 boundary.
274 $range = substr( $iaddr, 0, 4 );
275 // Fixme -- encapsulate this sort of query-building.
276 $dbr = wfGetDB( DB_SLAVE
);
277 $encIp = $dbr->addQuotes( IP
::sanitizeIP($this->ip
) );
278 $encAddr = $dbr->addQuotes( $iaddr );
279 $conds[] = "(ipb_address = $encIp) OR
280 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
281 ipb_range_start <= $encAddr
282 AND ipb_range_end >= $encAddr)";
284 $conds['ipb_address'] = IP
::sanitizeIP($this->ip
);
286 $conds['ipb_auto'] = 0;
288 } elseif ( IP
::isIPAddress($this->ip
) ) {
289 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
290 $conds['ipb_auto'] = 0;
292 $user = User
::newFromName( $this->ip
);
293 if ( $user && ( $id = $user->getId() ) != 0 ) {
294 $conds['ipb_user'] = $id;
297 $conds['ipb_address'] = $this->ip
;
298 $conds['ipb_auto'] = 0;
302 if( $this->hideuserblocks
) {
303 $conds['ipb_user'] = 0;
305 if( $this->hidetempblocks
) {
306 $conds['ipb_expiry'] = 'infinity';
308 if( $this->hideaddressblocks
) {
309 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
313 $wgOut->addHTML( $this->searchForm() );
315 // Check for other blocks, i.e. global/tor blocks
316 $otherBlockLink = array();
317 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip
) );
319 // Show additional header for the local block only when other blocks exists.
320 // Not necessary in a standard installation without such extensions enabled
321 if( count( $otherBlockLink ) ) {
323 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
326 $pager = new IPBlocklistPager( $this, $conds );
327 if ( $pager->getNumRows() ) {
329 $pager->getNavigationBar() .
330 Xml
::tags( 'ul', null, $pager->getBody() ) .
331 $pager->getNavigationBar()
333 } elseif ( $this->ip
!= '') {
334 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
336 $wgOut->addWikiMsg( 'ipblocklist-empty' );
339 if( count( $otherBlockLink ) ) {
341 Html
::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
344 foreach( $otherBlockLink as $link ) {
345 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
347 $wgOut->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
352 function searchForm() {
353 global $wgScript, $wgRequest, $wgLang;
355 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
356 $nondefaults = array();
357 if( $this->hideuserblocks
) {
358 $nondefaults['hideuserblocks'] = $this->hideuserblocks
;
360 if( $this->hidetempblocks
) {
361 $nondefaults['hidetempblocks'] = $this->hidetempblocks
;
363 if( $this->hideaddressblocks
) {
364 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks
;
366 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks
],
367 array( 'hideuserblocks' => 1-$this->hideuserblocks
), $nondefaults);
368 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks
],
369 array( 'hidetempblocks' => 1-$this->hidetempblocks
), $nondefaults);
370 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks
],
371 array( 'hideaddressblocks' => 1-$this->hideaddressblocks
), $nondefaults);
374 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
375 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
376 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
377 $hl = $wgLang->pipeList( $links );
380 Xml
::tags( 'form', array( 'action' => $wgScript ),
381 Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Ipblocklist' )->getPrefixedDbKey() ) .
382 Xml
::openElement( 'fieldset' ) .
383 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
384 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
386 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
388 Xml
::closeElement( 'fieldset' )
393 * Makes change an option link which carries all the other options
394 * @param $title see Title
398 function makeOptionsLink( $title, $override, $options, $active = false ) {
400 $sk = $wgUser->getSkin();
401 $params = $override +
$options;
402 $ipblocklist = SpecialPage
::getTitleFor( 'Ipblocklist' );
403 return $sk->link( $ipblocklist, htmlspecialchars( $title ),
404 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
408 * Callback function to output a block
410 function formatRow( $block ) {
411 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
413 wfProfileIn( __METHOD__
);
415 static $sk=null, $msg=null;
418 $sk = $wgUser->getSkin();
419 if( is_null( $msg ) ) {
421 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
422 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
423 foreach( $keys as $key ) {
424 $msg[$key] = wfMsgHtml( $key );
428 # Prepare links to the blocker's user and talk pages
429 $blocker_id = $block->getBy();
430 $blocker_name = $block->getByName();
431 $blocker = $sk->userLink( $blocker_id, $blocker_name );
432 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
434 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
435 if( $block->mAuto
) {
436 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
438 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
439 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
442 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp
, true ) );
444 $properties = array();
445 $properties[] = Block
::formatExpiry( $block->mExpiry
);
446 if ( $block->mAnonOnly
) {
447 $properties[] = $msg['anononlyblock'];
449 if ( $block->mCreateAccount
) {
450 $properties[] = $msg['createaccountblock'];
452 if (!$block->mEnableAutoblock
&& $block->mUser
) {
453 $properties[] = $msg['noautoblockblock'];
456 if ( $block->mBlockEmail
&& $block->mUser
) {
457 $properties[] = $msg['emailblock'];
460 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
461 $properties[] = $msg['blocklist-nousertalk'];
464 $properties = $wgLang->commaList( $properties );
466 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
469 $changeblocklink = '';
471 if ( $wgUser->isAllowed( 'block' ) ) {
472 $unblocklink = $sk->link( SpecialPage
::getTitleFor( 'Ipblocklist' ),
475 array( 'action' => 'unblock', 'id' => $block->mId
),
478 # Create changeblocklink for all blocks with exception of autoblocks
479 if( !$block->mAuto
) {
480 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
481 $sk->link( SpecialPage
::getTitleFor( 'Blockip', $block->mAddress
),
482 $msg['change-blocklink'],
483 array(), array(), 'known' );
485 $toolLinks = "($unblocklink$changeblocklink)";
488 $comment = $sk->commentBlock( htmlspecialchars($block->mReason
) );
490 $s = "{$line} $comment";
491 if ( $block->mHideName
)
492 $s = '<span class="history-deleted">' . $s . '</span>';
494 wfProfileOut( __METHOD__
);
495 return "<li>$s $toolLinks</li>\n";
503 class IPBlocklistPager
extends ReverseChronologicalPager
{
504 public $mForm, $mConds;
506 function __construct( $form, $conds = array() ) {
507 $this->mForm
= $form;
508 $this->mConds
= $conds;
509 parent
::__construct();
512 function getStartBody() {
513 wfProfileIn( __METHOD__
);
514 # Do a link batch query
515 $this->mResult
->seek( 0 );
519 while ( $row = $this->mResult->fetchObject() ) {
520 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
521 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
522 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
523 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
526 # Usernames and titles are in fact related by a simple substitution of space -> underscore
527 # The last few lines of Title::secureAndSplit() tell the story.
528 while ( $row = $this->mResult
->fetchObject() ) {
529 $name = str_replace( ' ', '_', $row->ipb_by_text
);
530 $lb->add( NS_USER
, $name );
531 $lb->add( NS_USER_TALK
, $name );
532 $name = str_replace( ' ', '_', $row->ipb_address
);
533 $lb->add( NS_USER
, $name );
534 $lb->add( NS_USER_TALK
, $name );
537 wfProfileOut( __METHOD__
);
541 function formatRow( $row ) {
543 $block->initFromRow( $row );
544 return $this->mForm
->formatRow( $block );
547 function getQueryInfo() {
548 $conds = $this->mConds
;
549 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
551 'tables' => 'ipblocks',
557 function getIndexField() {
558 return 'ipb_timestamp';