10 function wfSpecialIpblocklist() {
11 global $wgUser, $wgOut, $wgRequest;
13 $ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) ) );
14 $id = $wgRequest->getVal( 'id' );
15 $reason = $wgRequest->getText( 'wpUnblockReason' );
16 $action = $wgRequest->getText( 'action' );
17 $successip = $wgRequest->getVal( 'successip' );
19 $ipu = new IPUnblockForm( $ip, $id, $reason );
21 if( $action == 'unblock' ) {
23 if( !$wgUser->isAllowed( 'block' ) ) {
24 $wgOut->permissionRequired( 'block' );
27 # Check for database lock
29 $wgOut->readOnlyPage();
34 } elseif( $action == 'submit' && $wgRequest->wasPosted()
35 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
37 if( !$wgUser->isAllowed( 'block' ) ) {
38 $wgOut->permissionRequired( 'block' );
41 # Check for database lock
43 $wgOut->readOnlyPage();
46 # Remove blocks and redirect user to success page
48 } elseif( $action == 'success' ) {
49 # Inform the user of a successful unblock
50 # (No need to check permissions or locks here,
51 # if something was done, then it's too late!)
52 if ( substr( $successip, 0, 1) == '#' ) {
53 // A block ID was unblocked
54 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
56 // A username/IP was unblocked
57 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
60 # Just show the block list
67 * implements Special:ipblocklist GUI
68 * @ingroup SpecialPage
71 var $ip, $reason, $id;
73 function IPUnblockForm( $ip, $id, $reason ) {
75 $this->ip
= strtr( $ip, '_', ' ' );
77 $this->reason
= $reason;
78 $this->hideuserblocks
= $wgRequest->getBool( 'hideuserblocks' );
79 $this->hidetempblocks
= $wgRequest->getBool( 'hidetempblocks' );
80 $this->hideaddressblocks
= $wgRequest->getBool( 'hideaddressblocks' );
84 * Generates the unblock form
85 * @param $err string: error message
86 * @return $out string: HTML form
88 function showForm( $err ) {
89 global $wgOut, $wgUser, $wgSysopUserBans;
91 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
92 $wgOut->addWikiMsg( 'unblockiptext' );
94 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
95 $action = $titleObj->getLocalURL( "action=submit" );
98 $wgOut->setSubtitle( wfMsg( "formerror" ) );
99 $wgOut->addWikiText( Xml
::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
102 $addressPart = false;
104 $block = Block
::newFromID( $this->id
);
106 $encName = htmlspecialchars( $block->getRedactedName() );
108 $addressPart = $encName . Xml
::hidden( 'id', $encId );
109 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
112 if ( !$addressPart ) {
113 $addressPart = Xml
::input( 'wpUnblockAddress', 40, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
114 $ipa = Xml
::label( wfMsg( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
118 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
119 Xml
::openElement( 'fieldset' ) .
120 Xml
::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
121 Xml
::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
123 <td class='mw-label'>
126 <td class='mw-input'>
131 <td class='mw-label'>" .
132 Xml
::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
134 <td class='mw-input'>" .
135 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
140 <td class='mw-submit'>" .
141 Xml
::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
144 Xml
::closeElement( 'table' ) .
145 Xml
::closeElement( 'fieldset' ) .
146 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
147 Xml
::closeElement( 'form' ) . "\n"
152 const UNBLOCK_SUCCESS
= 0; // Success
153 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
154 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
155 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
156 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
159 * Backend code for unblocking. doSubmit() wraps around this.
160 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
161 * case it contains the range $ip is part of.
162 * @return array array(message key, parameters) on failure, empty array on success
165 static function doUnblock(&$id, &$ip, &$reason, &$range = null, $blocker=null) {
167 $block = Block
::newFromID( $id );
169 return array('ipb_cant_unblock', htmlspecialchars($id));
171 $ip = $block->getRedactedName();
173 $block = new Block();
175 if ( substr( $ip, 0, 1 ) == "#" ) {
176 $id = substr( $ip, 1 );
177 $block = Block
::newFromID( $id );
179 return array('ipb_cant_unblock', htmlspecialchars($id));
181 $ip = $block->getRedactedName();
183 $block = Block
::newFromDB( $ip );
185 return array('ipb_cant_unblock', htmlspecialchars($id));
187 if( $block->mRangeStart
!= $block->mRangeEnd
&& !strstr( $ip, "/" ) ) {
188 /* If the specified IP is a single address, and the block is
189 * a range block, don't unblock the range. */
190 $range = $block->mAddress
;
191 return array('ipb_blocked_as_range', $ip, $range);
195 // Yes, this is really necessary
198 # If the name was hidden and the blocking user cannot hide
199 # names, then don't allow any block removals...
200 if( $blocker && $block->mHideName
&& !$blocker->isAllowed('hideuser') ) {
201 return array('ipb_cant_unblock', htmlspecialchars($id));
205 if ( !$block->delete() ) {
206 return array('ipb_cant_unblock', htmlspecialchars($id));
209 # Unset _deleted fields as needed
210 if( $block->mHideName
) {
211 IPBlockForm
::unsuppressUserName( $block->mAddress
, $block->mUser
);
215 $log = new LogPage( 'block' );
216 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
220 function doSubmit() {
221 global $wgOut, $wgUser;
222 $retval = self
::doUnblock($this->id
, $this->ip
, $this->reason
, $range, $wgUser);
223 if( !empty($retval) ) {
224 $key = array_shift($retval);
225 $this->showForm(wfMsgReal($key, $retval));
229 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
230 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip
) );
231 $wgOut->redirect( $success );
234 function showList( $msg ) {
235 global $wgOut, $wgUser;
237 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
239 $wgOut->setSubtitle( $msg );
242 // Purge expired entries on one in every 10 queries
243 if ( !mt_rand( 0, 10 ) ) {
244 Block
::purgeExpired();
249 // Is user allowed to see all the blocks?
250 if ( !$wgUser->isAllowed( 'hideuser' ) )
251 $conds['ipb_deleted'] = 0;
252 if ( $this->ip
== '' ) {
253 // No extra conditions
254 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
255 $conds['ipb_id'] = substr( $this->ip
, 1 );
257 } elseif ( IP
::isIPAddress($this->ip
) && strpos($this->ip
,'/') === false ) {
258 if( $iaddr = IP
::toHex($this->ip
) ) {
259 # Only scan ranges which start in this /16, this improves search speed
260 # Blocks should not cross a /16 boundary.
261 $range = substr( $iaddr, 0, 4 );
262 // Fixme -- encapsulate this sort of query-building.
263 $dbr = wfGetDB( DB_SLAVE
);
264 $encIp = $dbr->addQuotes( IP
::sanitizeIP($this->ip
) );
265 $encAddr = $dbr->addQuotes( $iaddr );
266 $conds[] = "(ipb_address = $encIp) OR
267 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
268 ipb_range_start <= $encAddr
269 AND ipb_range_end >= $encAddr)";
271 $conds['ipb_address'] = IP
::sanitizeIP($this->ip
);
273 $conds['ipb_auto'] = 0;
275 } elseif ( IP
::isIPAddress($this->ip
) ) {
276 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
277 $conds['ipb_auto'] = 0;
279 $user = User
::newFromName( $this->ip
);
280 if ( $user && ( $id = $user->getId() ) != 0 ) {
281 $conds['ipb_user'] = $id;
284 $conds['ipb_address'] = $this->ip
;
285 $conds['ipb_auto'] = 0;
289 if( $this->hideuserblocks
) {
290 $conds['ipb_user'] = 0;
292 if( $this->hidetempblocks
) {
293 $conds['ipb_expiry'] = 'infinity';
295 if( $this->hideaddressblocks
) {
296 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
299 $pager = new IPBlocklistPager( $this, $conds );
300 if ( $pager->getNumRows() ) {
302 $this->searchForm() .
303 $pager->getNavigationBar() .
304 Xml
::tags( 'ul', null, $pager->getBody() ) .
305 $pager->getNavigationBar()
307 } elseif ( $this->ip
!= '') {
308 $wgOut->addHTML( $this->searchForm() );
309 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
311 $wgOut->addHTML( $this->searchForm() );
312 $wgOut->addWikiMsg( 'ipblocklist-empty' );
315 $otherBlockLink = array();
316 wfRunHooks( 'getOtherBlockLogLink', array( &$otherBlockLink, $this->ip
) );
317 if( count( $otherBlockLink ) ) {
319 Html
::rawElement( 'h2', array(), wfMsg( 'ipblocklist-otherblocks' ) ) . "\n"
322 foreach( $otherBlockLink as $link ) {
323 $list .= Html
::rawElement( 'li', array(), $link ) . "\n";
325 $wgOut->addHTML( Html
::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
330 function searchForm() {
331 global $wgScript, $wgRequest, $wgLang;
333 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
334 $nondefaults = array();
335 if( $this->hideuserblocks
) {
336 $nondefaults['hideuserblocks'] = $this->hideuserblocks
;
338 if( $this->hidetempblocks
) {
339 $nondefaults['hidetempblocks'] = $this->hidetempblocks
;
341 if( $this->hideaddressblocks
) {
342 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks
;
344 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks
],
345 array( 'hideuserblocks' => 1-$this->hideuserblocks
), $nondefaults);
346 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks
],
347 array( 'hidetempblocks' => 1-$this->hidetempblocks
), $nondefaults);
348 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks
],
349 array( 'hideaddressblocks' => 1-$this->hideaddressblocks
), $nondefaults);
352 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
353 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
354 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
355 $hl = $wgLang->pipeList( $links );
358 Xml
::tags( 'form', array( 'action' => $wgScript ),
359 Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Ipblocklist' )->getPrefixedDbKey() ) .
360 Xml
::openElement( 'fieldset' ) .
361 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
362 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
364 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
366 Xml
::closeElement( 'fieldset' )
371 * Makes change an option link which carries all the other options
372 * @param $title see Title
376 function makeOptionsLink( $title, $override, $options, $active = false ) {
378 $sk = $wgUser->getSkin();
379 $params = $override +
$options;
380 $ipblocklist = SpecialPage
::getTitleFor( 'Ipblocklist' );
381 return $sk->link( $ipblocklist, htmlspecialchars( $title ),
382 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
386 * Callback function to output a block
388 function formatRow( $block ) {
389 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
391 wfProfileIn( __METHOD__
);
393 static $sk=null, $msg=null;
396 $sk = $wgUser->getSkin();
397 if( is_null( $msg ) ) {
399 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
400 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
401 foreach( $keys as $key ) {
402 $msg[$key] = wfMsgHtml( $key );
406 # Prepare links to the blocker's user and talk pages
407 $blocker_id = $block->getBy();
408 $blocker_name = $block->getByName();
409 $blocker = $sk->userLink( $blocker_id, $blocker_name );
410 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
412 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
413 if( $block->mAuto
) {
414 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
416 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
417 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
420 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp
, true ) );
422 $properties = array();
423 $properties[] = Block
::formatExpiry( $block->mExpiry
);
424 if ( $block->mAnonOnly
) {
425 $properties[] = $msg['anononlyblock'];
427 if ( $block->mCreateAccount
) {
428 $properties[] = $msg['createaccountblock'];
430 if (!$block->mEnableAutoblock
&& $block->mUser
) {
431 $properties[] = $msg['noautoblockblock'];
434 if ( $block->mBlockEmail
&& $block->mUser
) {
435 $properties[] = $msg['emailblock'];
438 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
439 $properties[] = $msg['blocklist-nousertalk'];
442 $properties = $wgLang->commaList( $properties );
444 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
447 $changeblocklink = '';
449 if ( $wgUser->isAllowed( 'block' ) ) {
450 $unblocklink = $sk->link( SpecialPage
::getTitleFor( 'Ipblocklist' ),
453 array( 'action' => 'unblock', 'id' => $block->mId
),
456 # Create changeblocklink for all blocks with exception of autoblocks
457 if( !$block->mAuto
) {
458 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
459 $sk->link( SpecialPage
::getTitleFor( 'Blockip', $block->mAddress
),
460 $msg['change-blocklink'],
461 array(), array(), 'known' );
463 $toolLinks = "($unblocklink$changeblocklink)";
466 $comment = $sk->commentBlock( htmlspecialchars($block->mReason
) );
468 $s = "{$line} $comment";
469 if ( $block->mHideName
)
470 $s = '<span class="history-deleted">' . $s . '</span>';
472 wfProfileOut( __METHOD__
);
473 return "<li>$s $toolLinks</li>\n";
481 class IPBlocklistPager
extends ReverseChronologicalPager
{
482 public $mForm, $mConds;
484 function __construct( $form, $conds = array() ) {
485 $this->mForm
= $form;
486 $this->mConds
= $conds;
487 parent
::__construct();
490 function getStartBody() {
491 wfProfileIn( __METHOD__
);
492 # Do a link batch query
493 $this->mResult
->seek( 0 );
497 while ( $row = $this->mResult->fetchObject() ) {
498 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
499 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
500 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
501 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
504 # Usernames and titles are in fact related by a simple substitution of space -> underscore
505 # The last few lines of Title::secureAndSplit() tell the story.
506 while ( $row = $this->mResult
->fetchObject() ) {
507 $name = str_replace( ' ', '_', $row->ipb_by_text
);
508 $lb->add( NS_USER
, $name );
509 $lb->add( NS_USER_TALK
, $name );
510 $name = str_replace( ' ', '_', $row->ipb_address
);
511 $lb->add( NS_USER
, $name );
512 $lb->add( NS_USER_TALK
, $name );
515 wfProfileOut( __METHOD__
);
519 function formatRow( $row ) {
521 $block->initFromRow( $row );
522 return $this->mForm
->formatRow( $block );
525 function getQueryInfo() {
526 $conds = $this->mConds
;
527 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
529 'tables' => 'ipblocks',
535 function getIndexField() {
536 return 'ipb_timestamp';