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' );
316 function searchForm() {
317 global $wgScript, $wgRequest, $wgLang;
319 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
320 $nondefaults = array();
321 if( $this->hideuserblocks
) {
322 $nondefaults['hideuserblocks'] = $this->hideuserblocks
;
324 if( $this->hidetempblocks
) {
325 $nondefaults['hidetempblocks'] = $this->hidetempblocks
;
327 if( $this->hideaddressblocks
) {
328 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks
;
330 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks
],
331 array( 'hideuserblocks' => 1-$this->hideuserblocks
), $nondefaults);
332 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks
],
333 array( 'hidetempblocks' => 1-$this->hidetempblocks
), $nondefaults);
334 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks
],
335 array( 'hideaddressblocks' => 1-$this->hideaddressblocks
), $nondefaults);
338 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
339 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
340 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
341 $hl = $wgLang->pipeList( $links );
344 Xml
::tags( 'form', array( 'action' => $wgScript ),
345 Xml
::hidden( 'title', SpecialPage
::getTitleFor( 'Ipblocklist' )->getPrefixedDbKey() ) .
346 Xml
::openElement( 'fieldset' ) .
347 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
348 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
350 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
352 Xml
::closeElement( 'fieldset' )
357 * Makes change an option link which carries all the other options
358 * @param $title see Title
362 function makeOptionsLink( $title, $override, $options, $active = false ) {
364 $sk = $wgUser->getSkin();
365 $params = $override +
$options;
366 $ipblocklist = SpecialPage
::getTitleFor( 'Ipblocklist' );
367 return $sk->link( $ipblocklist, htmlspecialchars( $title ),
368 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
372 * Callback function to output a block
374 function formatRow( $block ) {
375 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
377 wfProfileIn( __METHOD__
);
379 static $sk=null, $msg=null;
382 $sk = $wgUser->getSkin();
383 if( is_null( $msg ) ) {
385 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
386 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
387 foreach( $keys as $key ) {
388 $msg[$key] = wfMsgHtml( $key );
392 # Prepare links to the blocker's user and talk pages
393 $blocker_id = $block->getBy();
394 $blocker_name = $block->getByName();
395 $blocker = $sk->userLink( $blocker_id, $blocker_name );
396 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
398 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
399 if( $block->mAuto
) {
400 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
402 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
403 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
406 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp
, true ) );
408 $properties = array();
409 $properties[] = Block
::formatExpiry( $block->mExpiry
);
410 if ( $block->mAnonOnly
) {
411 $properties[] = $msg['anononlyblock'];
413 if ( $block->mCreateAccount
) {
414 $properties[] = $msg['createaccountblock'];
416 if (!$block->mEnableAutoblock
&& $block->mUser
) {
417 $properties[] = $msg['noautoblockblock'];
420 if ( $block->mBlockEmail
&& $block->mUser
) {
421 $properties[] = $msg['emailblock'];
424 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
425 $properties[] = $msg['blocklist-nousertalk'];
428 $properties = $wgLang->commaList( $properties );
430 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
433 $changeblocklink = '';
435 if ( $wgUser->isAllowed( 'block' ) ) {
436 $unblocklink = $sk->link( SpecialPage
::getTitleFor( 'Ipblocklist' ),
439 array( 'action' => 'unblock', 'id' => $block->mId
),
442 # Create changeblocklink for all blocks with exception of autoblocks
443 if( !$block->mAuto
) {
444 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
445 $sk->link( SpecialPage
::getTitleFor( 'Blockip', $block->mAddress
),
446 $msg['change-blocklink'],
447 array(), array(), 'known' );
449 $toolLinks = "($unblocklink$changeblocklink)";
452 $comment = $sk->commentBlock( htmlspecialchars($block->mReason
) );
454 $s = "{$line} $comment";
455 if ( $block->mHideName
)
456 $s = '<span class="history-deleted">' . $s . '</span>';
458 wfProfileOut( __METHOD__
);
459 return "<li>$s $toolLinks</li>\n";
467 class IPBlocklistPager
extends ReverseChronologicalPager
{
468 public $mForm, $mConds;
470 function __construct( $form, $conds = array() ) {
471 $this->mForm
= $form;
472 $this->mConds
= $conds;
473 parent
::__construct();
476 function getStartBody() {
477 wfProfileIn( __METHOD__
);
478 # Do a link batch query
479 $this->mResult
->seek( 0 );
483 while ( $row = $this->mResult->fetchObject() ) {
484 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
485 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
486 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
487 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
490 # Usernames and titles are in fact related by a simple substitution of space -> underscore
491 # The last few lines of Title::secureAndSplit() tell the story.
492 while ( $row = $this->mResult
->fetchObject() ) {
493 $name = str_replace( ' ', '_', $row->ipb_by_text
);
494 $lb->add( NS_USER
, $name );
495 $lb->add( NS_USER_TALK
, $name );
496 $name = str_replace( ' ', '_', $row->ipb_address
);
497 $lb->add( NS_USER
, $name );
498 $lb->add( NS_USER_TALK
, $name );
501 wfProfileOut( __METHOD__
);
505 function formatRow( $row ) {
507 $block->initFromRow( $row );
508 return $this->mForm
->formatRow( $block );
511 function getQueryInfo() {
512 $conds = $this->mConds
;
513 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
515 'tables' => 'ipblocks',
521 function getIndexField() {
522 return 'ipb_timestamp';