10 function wfSpecialIpblocklist() {
11 global $wgUser, $wgOut, $wgRequest;
13 $ip = $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)
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
189 && !strstr( $ip, "/" ) ) {
190 /* If the specified IP is a single address, and the block is
191 * a range block, don't unblock the range. */
192 $range = $block->mAddress
;
193 return array('ipb_blocked_as_range', $ip, $range);
197 // Yes, this is really necessary
201 if ( !$block->delete() ) {
202 return array('ipb_cant_unblock', htmlspecialchars($id));
206 $log = new LogPage( 'block' );
207 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
211 function doSubmit() {
213 $retval = self
::doUnblock($this->id
, $this->ip
, $this->reason
, $range);
216 $key = array_shift($retval);
217 $this->showForm(wfMsgReal($key, $retval));
221 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
222 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip
) );
223 $wgOut->redirect( $success );
226 function showList( $msg ) {
227 global $wgOut, $wgUser;
229 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
231 $wgOut->setSubtitle( $msg );
234 // Purge expired entries on one in every 10 queries
235 if ( !mt_rand( 0, 10 ) ) {
236 Block
::purgeExpired();
241 // Is user allowed to see all the blocks?
242 if ( !$wgUser->isAllowed( 'suppress' ) )
243 $conds['ipb_deleted'] = 0;
244 if ( $this->ip
== '' ) {
245 // No extra conditions
246 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
247 $conds['ipb_id'] = substr( $this->ip
, 1 );
248 } elseif ( IP
::toUnsigned( $this->ip
) !== false ) {
249 $conds['ipb_address'] = $this->ip
;
250 $conds['ipb_auto'] = 0;
251 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip
, $matches ) ) {
252 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
253 $conds['ipb_auto'] = 0;
255 $user = User
::newFromName( $this->ip
);
256 if ( $user && ( $id = $user->getId() ) != 0 ) {
257 $conds['ipb_user'] = $id;
260 $conds['ipb_address'] = $this->ip
;
261 $conds['ipb_auto'] = 0;
265 if( $this->hideuserblocks
) {
266 $conds['ipb_user'] = 0;
268 if( $this->hidetempblocks
) {
269 $conds['ipb_expiry'] = 'infinity';
271 if( $this->hideaddressblocks
) {
272 $conds[] = "ipb_range_end > ipb_range_start";
275 $pager = new IPBlocklistPager( $this, $conds );
276 if ( $pager->getNumRows() ) {
278 $this->searchForm() .
279 $this->showhideLinks() .
280 $pager->getNavigationBar() .
281 Xml
::tags( 'ul', null, $pager->getBody() ) .
282 $pager->getNavigationBar()
284 } elseif ( $this->ip
!= '') {
285 $wgOut->addHTML( $this->searchForm() );
286 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
288 $wgOut->addWikiMsg( 'ipblocklist-empty' );
292 function searchForm() {
293 global $wgTitle, $wgScript, $wgRequest;
295 Xml
::tags( 'form', array( 'action' => $wgScript ),
296 Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
297 Xml
::openElement( 'fieldset' ) .
298 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
299 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
301 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
302 Xml
::closeElement( 'fieldset' )
306 function showhideLinks() {
307 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
308 $nondefaults = array();
309 if( $this->hideuserblocks
) {
310 $nondefaults['hideuserblocks'] = $this->hideuserblocks
;
312 if( $this->hidetempblocks
) {
313 $nondefaults['hidetempblocks'] = $this->hidetempblocks
;
315 if( $this->hideaddressblocks
) {
316 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks
;
318 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks
],
319 array( 'hideuserblocks' => 1-$this->hideuserblocks
), $nondefaults);
320 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks
],
321 array( 'hidetempblocks' => 1-$this->hidetempblocks
), $nondefaults);
322 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks
],
323 array( 'hideaddressblocks' => 1-$this->hideaddressblocks
), $nondefaults);
326 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
327 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
328 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
330 $hl = '(' . implode( ' | ', $links ) . ')<hr/>';
335 * Makes change an option link which carries all the other options
336 * @param $title see Title
340 function makeOptionsLink( $title, $override, $options, $active = false ) {
342 $sk = $wgUser->getSkin();
343 $params = wfArrayMerge( $options, $override );
344 $ipblocklist = SpecialPage
::getTitleFor( 'IPBlockList' );
345 return $sk->link( $ipblocklist, htmlspecialchars( $title ),
346 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
350 * Callback function to output a block
352 function formatRow( $block ) {
353 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
355 wfProfileIn( __METHOD__
);
357 static $sk=null, $msg=null;
360 $sk = $wgUser->getSkin();
361 if( is_null( $msg ) ) {
363 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
364 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk' );
365 foreach( $keys as $key ) {
366 $msg[$key] = wfMsgHtml( $key );
368 $msg['blocklistline'] = wfMsg( 'blocklistline' );
371 # Prepare links to the blocker's user and talk pages
372 $blocker_id = $block->getBy();
373 $blocker_name = $block->getByName();
374 $blocker = $sk->userLink( $blocker_id, $blocker_name );
375 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
377 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
378 if( $block->mAuto
) {
379 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
381 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
382 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
385 $formattedTime = $wgLang->timeanddate( $block->mTimestamp
, true );
387 $properties = array();
388 $properties[] = Block
::formatExpiry( $block->mExpiry
);
389 if ( $block->mAnonOnly
) {
390 $properties[] = $msg['anononlyblock'];
392 if ( $block->mCreateAccount
) {
393 $properties[] = $msg['createaccountblock'];
395 if (!$block->mEnableAutoblock
&& $block->mUser
) {
396 $properties[] = $msg['noautoblockblock'];
399 if ( $block->mBlockEmail
&& $block->mUser
) {
400 $properties[] = $msg['emailblock'];
403 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
404 $properties[] = $msg['blocklist-nousertalk'];
407 $properties = implode( ', ', $properties );
409 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
412 if ( $wgUser->isAllowed('block') ) {
413 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
414 $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId
) ) . ')';
417 $comment = $sk->commentBlock( $block->mReason
);
419 $s = "{$line} $comment";
420 if ( $block->mHideName
)
421 $s = '<span class="history-deleted">' . $s . '</span>';
423 wfProfileOut( __METHOD__
);
424 return "<li>$s $unblocklink</li>\n";
432 class IPBlocklistPager
extends ReverseChronologicalPager
{
433 public $mForm, $mConds;
435 function __construct( $form, $conds = array() ) {
436 $this->mForm
= $form;
437 $this->mConds
= $conds;
438 parent
::__construct();
441 function getStartBody() {
442 wfProfileIn( __METHOD__
);
443 # Do a link batch query
444 $this->mResult
->seek( 0 );
448 while ( $row = $this->mResult->fetchObject() ) {
449 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
450 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
451 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
452 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
455 # Usernames and titles are in fact related by a simple substitution of space -> underscore
456 # The last few lines of Title::secureAndSplit() tell the story.
457 while ( $row = $this->mResult
->fetchObject() ) {
458 $name = str_replace( ' ', '_', $row->ipb_by_text
);
459 $lb->add( NS_USER
, $name );
460 $lb->add( NS_USER_TALK
, $name );
461 $name = str_replace( ' ', '_', $row->ipb_address
);
462 $lb->add( NS_USER
, $name );
463 $lb->add( NS_USER_TALK
, $name );
466 wfProfileOut( __METHOD__
);
470 function formatRow( $row ) {
472 $block->initFromRow( $row );
473 return $this->mForm
->formatRow( $block );
476 function getQueryInfo() {
477 $conds = $this->mConds
;
478 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
480 'tables' => 'ipblocks',
486 function getIndexField() {
487 return 'ipb_timestamp';