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' ) {
24 if( !$wgUser->isAllowed( 'block' ) ) {
25 $wgOut->permissionRequired( 'block' );
28 # Check for database lock
30 $wgOut->readOnlyPage();
35 } elseif( $action == 'submit' && $wgRequest->wasPosted()
36 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
38 if( !$wgUser->isAllowed( 'block' ) ) {
39 $wgOut->permissionRequired( 'block' );
42 # Check for database lock
44 $wgOut->readOnlyPage();
47 # Remove blocks and redirect user to success page
49 } elseif( $action == 'success' ) {
50 # Inform the user of a successful unblock
51 # (No need to check permissions or locks here,
52 # if something was done, then it's too late!)
53 if ( substr( $successip, 0, 1) == '#' ) {
54 // A block ID was unblocked
55 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
57 // A username/IP was unblocked
58 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
61 # Just show the block list
68 * implements Special:ipblocklist GUI
69 * @ingroup SpecialPage
72 var $ip, $reason, $id;
74 function IPUnblockForm( $ip, $id, $reason ) {
76 $this->ip
= strtr( $ip, '_', ' ' );
78 $this->reason
= $reason;
79 $this->hideuserblocks
= $wgRequest->getBool( 'hideuserblocks' );
80 $this->hidetempblocks
= $wgRequest->getBool( 'hidetempblocks' );
81 $this->hideaddressblocks
= $wgRequest->getBool( 'hideaddressblocks' );
85 * Generates the unblock form
86 * @param $err string: error message
87 * @return $out string: HTML form
89 function showForm( $err ) {
90 global $wgOut, $wgUser, $wgSysopUserBans;
92 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
93 $wgOut->addWikiMsg( 'unblockiptext' );
95 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
96 $action = $titleObj->getLocalURL( "action=submit" );
99 $wgOut->setSubtitle( wfMsg( "formerror" ) );
100 $wgOut->addWikiText( Xml
::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
103 $addressPart = false;
105 $block = Block
::newFromID( $this->id
);
107 $encName = htmlspecialchars( $block->getRedactedName() );
109 $addressPart = $encName . Xml
::hidden( 'id', $encId );
110 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
113 if ( !$addressPart ) {
114 $addressPart = Xml
::input( 'wpUnblockAddress', 40, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
115 $ipa = Xml
::label( wfMsg( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
119 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
120 Xml
::openElement( 'fieldset' ) .
121 Xml
::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
122 Xml
::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
124 <td class='mw-label'>
127 <td class='mw-input'>
132 <td class='mw-label'>" .
133 Xml
::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
135 <td class='mw-input'>" .
136 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
141 <td class='mw-submit'>" .
142 Xml
::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
145 Xml
::closeElement( 'table' ) .
146 Xml
::closeElement( 'fieldset' ) .
147 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
148 Xml
::closeElement( 'form' ) . "\n"
153 const UNBLOCK_SUCCESS
= 0; // Success
154 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
155 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
156 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
157 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
160 * Backend code for unblocking. doSubmit() wraps around this.
161 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
162 * case it contains the range $ip is part of.
163 * @return array array(message key, parameters) on failure, empty array on success
166 static function doUnblock(&$id, &$ip, &$reason, &$range = null, $blocker=null) {
168 $block = Block
::newFromID( $id );
170 return array('ipb_cant_unblock', htmlspecialchars($id));
172 $ip = $block->getRedactedName();
174 $block = new Block();
176 if ( substr( $ip, 0, 1 ) == "#" ) {
177 $id = substr( $ip, 1 );
178 $block = Block
::newFromID( $id );
180 return array('ipb_cant_unblock', htmlspecialchars($id));
182 $ip = $block->getRedactedName();
184 $block = Block
::newFromDB( $ip );
186 return array('ipb_cant_unblock', htmlspecialchars($id));
188 if( $block->mRangeStart
!= $block->mRangeEnd
&& !strstr( $ip, "/" ) ) {
189 /* If the specified IP is a single address, and the block is
190 * a range block, don't unblock the range. */
191 $range = $block->mAddress
;
192 return array('ipb_blocked_as_range', $ip, $range);
196 // Yes, this is really necessary
199 # If the name was hidden and the blocking user cannot hide
200 # names, then don't allow any block removals...
201 if( $blocker && $block->mHideName
&& !$blocker->isAllowed('hideuser') ) {
202 return array('ipb_cant_unblock', htmlspecialchars($id));
206 if ( !$block->delete() ) {
207 return array('ipb_cant_unblock', htmlspecialchars($id));
210 # Unset _deleted fields as needed
211 if( $block->mHideName
) {
212 IPBlockForm
::unsuppressUserName( $block->mAddress
, $block->mUser
);
216 $log = new LogPage( 'block' );
217 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
221 function doSubmit() {
222 global $wgOut, $wgUser;
223 $retval = self
::doUnblock($this->id
, $this->ip
, $this->reason
, $range, $wgUser);
224 if( !empty($retval) ) {
225 $key = array_shift($retval);
226 $this->showForm(wfMsgReal($key, $retval));
230 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
231 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip
) );
232 $wgOut->redirect( $success );
235 function showList( $msg ) {
236 global $wgOut, $wgUser;
238 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
240 $wgOut->setSubtitle( $msg );
243 // Purge expired entries on one in every 10 queries
244 if ( !mt_rand( 0, 10 ) ) {
245 Block
::purgeExpired();
250 // Is user allowed to see all the blocks?
251 if ( !$wgUser->isAllowed( 'hideuser' ) )
252 $conds['ipb_deleted'] = 0;
253 if ( $this->ip
== '' ) {
254 // No extra conditions
255 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
256 $conds['ipb_id'] = substr( $this->ip
, 1 );
258 } elseif ( IP
::isIPAddress($this->ip
) && strpos($this->ip
,'/') === false ) {
259 if( $iaddr = IP
::toHex($this->ip
) ) {
260 # Only scan ranges which start in this /16, this improves search speed
261 # Blocks should not cross a /16 boundary.
262 $range = substr( $iaddr, 0, 4 );
263 // Fixme -- encapsulate this sort of query-building.
264 $dbr = wfGetDB( DB_SLAVE
);
265 $encIp = $dbr->addQuotes( IP
::sanitizeIP($this->ip
) );
266 $encAddr = $dbr->addQuotes( $iaddr );
267 $conds[] = "(ipb_address = $encIp) OR
268 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
269 ipb_range_start <= $encAddr
270 AND ipb_range_end >= $encAddr)";
272 $conds['ipb_address'] = IP
::sanitizeIP($this->ip
);
274 $conds['ipb_auto'] = 0;
276 } elseif ( IP
::isIPAddress($this->ip
) ) {
277 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
278 $conds['ipb_auto'] = 0;
280 $user = User
::newFromName( $this->ip
);
281 if ( $user && ( $id = $user->getId() ) != 0 ) {
282 $conds['ipb_user'] = $id;
285 $conds['ipb_address'] = $this->ip
;
286 $conds['ipb_auto'] = 0;
290 if( $this->hideuserblocks
) {
291 $conds['ipb_user'] = 0;
293 if( $this->hidetempblocks
) {
294 $conds['ipb_expiry'] = 'infinity';
296 if( $this->hideaddressblocks
) {
297 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
301 $wgOut->addHTML( $this->searchForm() );
303 // Check for other blocks, i.e. global/tor blocks
304 $otherBlockLink = array();
305 wfRunHooks( 'getOtherBlockLogLink', array( &$otherBlockLink, $this->ip
) );
307 // Show additional header for the local block only when other blocks exists.
308 // Not necessary in a standard installation without such extensions enabled
309 if( count( $otherBlockLink ) ) {
311 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
314 $pager = new IPBlocklistPager( $this, $conds );
315 if ( $pager->getNumRows() ) {
317 $pager->getNavigationBar() .
318 Xml
::tags( 'ul', null, $pager->getBody() ) .
319 $pager->getNavigationBar()
321 } elseif ( $this->ip
!= '') {
322 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
324 $wgOut->addWikiMsg( 'ipblocklist-empty' );
327 if( count( $otherBlockLink ) ) {
329 Html
::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
332 foreach( $otherBlockLink as $link ) {
333 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
335 $wgOut->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
340 function searchForm() {
341 global $wgScript, $wgRequest, $wgLang;
343 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
344 $nondefaults = array();
345 if( $this->hideuserblocks
) {
346 $nondefaults['hideuserblocks'] = $this->hideuserblocks
;
348 if( $this->hidetempblocks
) {
349 $nondefaults['hidetempblocks'] = $this->hidetempblocks
;
351 if( $this->hideaddressblocks
) {
352 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks
;
354 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks
],
355 array( 'hideuserblocks' => 1-$this->hideuserblocks
), $nondefaults);
356 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks
],
357 array( 'hidetempblocks' => 1-$this->hidetempblocks
), $nondefaults);
358 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks
],
359 array( 'hideaddressblocks' => 1-$this->hideaddressblocks
), $nondefaults);
362 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
363 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
364 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
365 $hl = $wgLang->pipeList( $links );
368 Xml
::tags( 'form', array( 'action' => $wgScript ),
369 Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Ipblocklist' )->getPrefixedDbKey() ) .
370 Xml
::openElement( 'fieldset' ) .
371 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
372 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
374 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
376 Xml
::closeElement( 'fieldset' )
381 * Makes change an option link which carries all the other options
382 * @param $title see Title
386 function makeOptionsLink( $title, $override, $options, $active = false ) {
388 $sk = $wgUser->getSkin();
389 $params = $override +
$options;
390 $ipblocklist = SpecialPage
::getTitleFor( 'Ipblocklist' );
391 return $sk->link( $ipblocklist, htmlspecialchars( $title ),
392 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
396 * Callback function to output a block
398 function formatRow( $block ) {
399 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
401 wfProfileIn( __METHOD__
);
403 static $sk=null, $msg=null;
406 $sk = $wgUser->getSkin();
407 if( is_null( $msg ) ) {
409 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
410 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
411 foreach( $keys as $key ) {
412 $msg[$key] = wfMsgHtml( $key );
416 # Prepare links to the blocker's user and talk pages
417 $blocker_id = $block->getBy();
418 $blocker_name = $block->getByName();
419 $blocker = $sk->userLink( $blocker_id, $blocker_name );
420 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
422 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
423 if( $block->mAuto
) {
424 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
426 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
427 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
430 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp
, true ) );
432 $properties = array();
433 $properties[] = Block
::formatExpiry( $block->mExpiry
);
434 if ( $block->mAnonOnly
) {
435 $properties[] = $msg['anononlyblock'];
437 if ( $block->mCreateAccount
) {
438 $properties[] = $msg['createaccountblock'];
440 if (!$block->mEnableAutoblock
&& $block->mUser
) {
441 $properties[] = $msg['noautoblockblock'];
444 if ( $block->mBlockEmail
&& $block->mUser
) {
445 $properties[] = $msg['emailblock'];
448 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
449 $properties[] = $msg['blocklist-nousertalk'];
452 $properties = $wgLang->commaList( $properties );
454 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
457 $changeblocklink = '';
459 if ( $wgUser->isAllowed( 'block' ) ) {
460 $unblocklink = $sk->link( SpecialPage
::getTitleFor( 'Ipblocklist' ),
463 array( 'action' => 'unblock', 'id' => $block->mId
),
466 # Create changeblocklink for all blocks with exception of autoblocks
467 if( !$block->mAuto
) {
468 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
469 $sk->link( SpecialPage
::getTitleFor( 'Blockip', $block->mAddress
),
470 $msg['change-blocklink'],
471 array(), array(), 'known' );
473 $toolLinks = "($unblocklink$changeblocklink)";
476 $comment = $sk->commentBlock( htmlspecialchars($block->mReason
) );
478 $s = "{$line} $comment";
479 if ( $block->mHideName
)
480 $s = '<span class="history-deleted">' . $s . '</span>';
482 wfProfileOut( __METHOD__
);
483 return "<li>$s $toolLinks</li>\n";
491 class IPBlocklistPager
extends ReverseChronologicalPager
{
492 public $mForm, $mConds;
494 function __construct( $form, $conds = array() ) {
495 $this->mForm
= $form;
496 $this->mConds
= $conds;
497 parent
::__construct();
500 function getStartBody() {
501 wfProfileIn( __METHOD__
);
502 # Do a link batch query
503 $this->mResult
->seek( 0 );
507 while ( $row = $this->mResult->fetchObject() ) {
508 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
509 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
510 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
511 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
514 # Usernames and titles are in fact related by a simple substitution of space -> underscore
515 # The last few lines of Title::secureAndSplit() tell the story.
516 while ( $row = $this->mResult
->fetchObject() ) {
517 $name = str_replace( ' ', '_', $row->ipb_by_text
);
518 $lb->add( NS_USER
, $name );
519 $lb->add( NS_USER_TALK
, $name );
520 $name = str_replace( ' ', '_', $row->ipb_address
);
521 $lb->add( NS_USER
, $name );
522 $lb->add( NS_USER_TALK
, $name );
525 wfProfileOut( __METHOD__
);
529 function formatRow( $row ) {
531 $block->initFromRow( $row );
532 return $this->mForm
->formatRow( $block );
535 function getQueryInfo() {
536 $conds = $this->mConds
;
537 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
539 'tables' => 'ipblocks',
545 function getIndexField() {
546 return 'ipb_timestamp';