4 * @addtogroup SpecialPage
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 * @addtogroup SpecialPage
71 var $ip, $reason, $id;
73 function IPUnblockForm( $ip, $id, $reason ) {
74 $this->ip
= strtr( $ip, '_', ' ' );
76 $this->reason
= $reason;
80 * Generates the unblock form
81 * @param $err string: error message
82 * @return $out string: HTML form
84 function showForm( $err ) {
85 global $wgOut, $wgUser, $wgSysopUserBans, $wgContLang;
87 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
88 $wgOut->addWikiMsg( 'unblockiptext' );
90 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
91 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
92 $action = $titleObj->getLocalURL( "action=submit" );
93 $alignRight = $wgContLang->isRtl() ?
'left' : 'right';
96 $wgOut->setSubtitle( wfMsg( "formerror" ) );
97 $wgOut->addWikiText( "<span class='error'>{$err}</span>\n" );
99 $token = htmlspecialchars( $wgUser->editToken() );
101 $addressPart = false;
103 $block = Block
::newFromID( $this->id
);
105 $encName = htmlspecialchars( $block->getRedactedName() );
107 $addressPart = $encName . Xml
::hidden( 'id', $encId );
110 if ( !$addressPart ) {
111 $addressPart = Xml
::input( 'wpUnblockAddress', 20, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
115 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
116 Xml
::openElement( 'fieldset' ) .
117 Xml
::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
118 Xml
::openElement( 'table', array( 'border' => '0', 'id' => 'mw-unblock-table' ) ).
120 <td align='$alignRight'>
128 <td align='$alignRight'>" .
129 Xml
::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
132 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
138 Xml
::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
141 Xml
::closeElement( 'table' ) .
142 Xml
::closeElement( 'fieldset' ) .
143 Xml
::hidden( 'wpEditToken', $token ) .
144 Xml
::closeElement( 'form' ) . "\n"
149 const UNBLOCK_SUCCESS
= 0; // Success
150 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
151 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
152 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
153 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
156 * Backend code for unblocking. doSubmit() wraps around this.
157 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
158 * case it contains the range $ip is part of.
159 * @return array array(message key, parameters) on failure, empty array on success
162 static function doUnblock(&$id, &$ip, &$reason, &$range = null)
165 $block = Block
::newFromID( $id );
167 return array('ipb_cant_unblock', htmlspecialchars($id));
169 $ip = $block->getRedactedName();
171 $block = new Block();
173 if ( substr( $ip, 0, 1 ) == "#" ) {
174 $id = substr( $ip, 1 );
175 $block = Block
::newFromID( $id );
177 return array('ipb_cant_unblock', htmlspecialchars($id));
179 $ip = $block->getRedactedName();
181 $block = Block
::newFromDB( $ip );
183 return array('ipb_cant_unblock', htmlspecialchars($id));
185 if( $block->mRangeStart
!= $block->mRangeEnd
186 && !strstr( $ip, "/" ) ) {
187 /* If the specified IP is a single address, and the block is
188 * a range block, don't unblock the range. */
189 $range = $block->mAddress
;
190 return array('ipb_blocked_as_range', $ip, $range);
194 // Yes, this is really necessary
198 if ( !$block->delete() ) {
199 return array('ipb_cant_unblock', htmlspecialchars($id));
203 $log = new LogPage( 'block' );
204 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
208 function doSubmit() {
210 $retval = self
::doUnblock($this->id
, $this->ip
, $this->reason
, $range);
213 $key = array_shift($retval);
214 $this->showForm(wfMsgReal($key, $retval));
218 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
219 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip
) );
220 $wgOut->redirect( $success );
223 function showList( $msg ) {
224 global $wgOut, $wgUser;
226 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
228 $wgOut->setSubtitle( $msg );
231 // Purge expired entries on one in every 10 queries
232 if ( !mt_rand( 0, 10 ) ) {
233 Block
::purgeExpired();
238 // Is user allowed to see all the blocks?
239 if ( !$wgUser->isAllowed( 'oversight' ) )
240 $conds['ipb_deleted'] = 0;
241 if ( $this->ip
== '' ) {
242 // No extra conditions
243 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
244 $conds['ipb_id'] = substr( $this->ip
, 1 );
245 } elseif ( IP
::toUnsigned( $this->ip
) !== false ) {
246 $conds['ipb_address'] = $this->ip
;
247 $conds['ipb_auto'] = 0;
248 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip
, $matches ) ) {
249 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
250 $conds['ipb_auto'] = 0;
252 $user = User
::newFromName( $this->ip
);
253 if ( $user && ( $id = $user->getID() ) != 0 ) {
254 $conds['ipb_user'] = $id;
257 $conds['ipb_address'] = $this->ip
;
258 $conds['ipb_auto'] = 0;
262 $pager = new IPBlocklistPager( $this, $conds );
263 if ( $pager->getNumRows() ) {
265 $this->searchForm() .
266 $pager->getNavigationBar() .
267 Xml
::tags( 'ul', null, $pager->getBody() ) .
268 $pager->getNavigationBar()
270 } elseif ( $this->ip
!= '') {
271 $wgOut->addHTML( $this->searchForm() );
272 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
274 $wgOut->addWikiMsg( 'ipblocklist-empty' );
278 function searchForm() {
279 global $wgTitle, $wgScript, $wgRequest;
281 Xml
::tags( 'form', array( 'action' => $wgScript ),
282 Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
283 Xml
::openElement( 'fieldset' ) .
284 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
285 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
287 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
288 Xml
::closeElement( 'fieldset' )
293 * Callback function to output a block
295 function formatRow( $block ) {
296 global $wgUser, $wgLang;
298 wfProfileIn( __METHOD__
);
300 static $sk=null, $msg=null;
303 $sk = $wgUser->getSkin();
304 if( is_null( $msg ) ) {
306 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
307 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock' );
308 foreach( $keys as $key ) {
309 $msg[$key] = wfMsgHtml( $key );
311 $msg['blocklistline'] = wfMsg( 'blocklistline' );
314 # Prepare links to the blocker's user and talk pages
315 $blocker_id = $block->getBy();
316 $blocker_name = $block->getByName();
317 $blocker = $sk->userLink( $blocker_id, $blocker_name );
318 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
320 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
321 if( $block->mAuto
) {
322 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
324 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
325 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
328 $formattedTime = $wgLang->timeanddate( $block->mTimestamp
, true );
330 $properties = array();
331 if ( $block->mExpiry
=== "" ||
$block->mExpiry
=== Block
::infinity() ) {
332 $properties[] = $msg['infiniteblock'];
334 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
335 array( $wgLang->timeanddate( $block->mExpiry
, true ) ) );
337 if ( $block->mAnonOnly
) {
338 $properties[] = $msg['anononlyblock'];
340 if ( $block->mCreateAccount
) {
341 $properties[] = $msg['createaccountblock'];
343 if (!$block->mEnableAutoblock
&& $block->mUser
) {
344 $properties[] = $msg['noautoblockblock'];
347 if ( $block->mBlockEmail
&& $block->mUser
) {
348 $properties[] = $msg['emailblock'];
351 $properties = implode( ', ', $properties );
353 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
356 if ( $wgUser->isAllowed('block') ) {
357 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
358 $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId
) ) . ')';
361 $comment = $sk->commentBlock( $block->mReason
);
363 $s = "{$line} $comment";
364 if ( $block->mHideName
)
365 $s = '<span class="history-deleted">' . $s . '</span>';
367 wfProfileOut( __METHOD__
);
368 return "<li>$s $unblocklink</li>\n";
376 class IPBlocklistPager
extends ReverseChronologicalPager
{
377 public $mForm, $mConds;
379 function __construct( $form, $conds = array() ) {
380 $this->mForm
= $form;
381 $this->mConds
= $conds;
382 parent
::__construct();
385 function getStartBody() {
386 wfProfileIn( __METHOD__
);
387 # Do a link batch query
388 $this->mResult
->seek( 0 );
392 while ( $row = $this->mResult->fetchObject() ) {
393 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
394 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
395 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
396 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
399 # Usernames and titles are in fact related by a simple substitution of space -> underscore
400 # The last few lines of Title::secureAndSplit() tell the story.
401 while ( $row = $this->mResult
->fetchObject() ) {
402 $name = str_replace( ' ', '_', $row->ipb_by_text
);
403 $lb->add( NS_USER
, $name );
404 $lb->add( NS_USER_TALK
, $name );
405 $name = str_replace( ' ', '_', $row->ipb_address
);
406 $lb->add( NS_USER
, $name );
407 $lb->add( NS_USER_TALK
, $name );
410 wfProfileOut( __METHOD__
);
414 function formatRow( $row ) {
416 $block->initFromRow( $row );
417 return $this->mForm
->formatRow( $block );
420 function getQueryInfo() {
421 $conds = $this->mConds
;
422 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
424 'tables' => 'ipblocks',
430 function getIndexField() {
431 return 'ipb_timestamp';