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;
79 function showForm( $err ) {
80 global $wgOut, $wgUser, $wgSysopUserBans, $wgContLang;
82 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
83 $wgOut->addWikiMsg( 'unblockiptext' );
85 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
86 $ipr = wfMsgHtml( 'ipbreason' );
87 $ipus = wfMsgHtml( 'ipusubmit' );
88 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
89 $action = $titleObj->getLocalURL( "action=submit" );
90 $alignRight = $wgContLang->isRtl() ?
'left' : 'right';
93 $wgOut->setSubtitle( wfMsg( "formerror" ) );
94 $wgOut->addWikiText( "<span class='error'>{$err}</span>\n" );
96 $token = htmlspecialchars( $wgUser->editToken() );
100 $block = Block
::newFromID( $this->id
);
102 $encName = htmlspecialchars( $block->getRedactedName() );
104 $addressPart = $encName . Xml
::hidden( 'id', $encId );
107 if ( !$addressPart ) {
108 $addressPart = Xml
::input( 'wpUnblockAddress', 20, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
112 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
113 Xml
::openElement( 'table', array( 'border' => '0' ) ).
115 <td align='$alignRight'>
123 <td align='$alignRight'>
127 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
133 Xml
::submitButton( $ipus, array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
136 Xml
::closeElement( 'table' ) .
137 Xml
::hidden( 'wpEditToken', $token ) .
138 Xml
::closeElement( 'form' ) . "\n"
143 const UNBLOCK_SUCCESS
= 0; // Success
144 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
145 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
146 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
147 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
150 * Backend code for unblocking. doSubmit() wraps around this.
151 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
152 * case it contains the range $ip is part of.
153 * @return array array(message key, parameters) on failure, empty array on success
156 static function doUnblock(&$id, &$ip, &$reason, &$range = null)
159 $block = Block
::newFromID( $id );
161 return array('ipb_cant_unblock', htmlspecialchars($id));
163 $ip = $block->getRedactedName();
165 $block = new Block();
167 if ( substr( $ip, 0, 1 ) == "#" ) {
168 $id = substr( $ip, 1 );
169 $block = Block
::newFromID( $id );
171 return array('ipb_cant_unblock', htmlspecialchars($id));
173 $ip = $block->getRedactedName();
175 $block = Block
::newFromDB( $ip );
177 return array('ipb_cant_unblock', htmlspecialchars($id));
179 if( $block->mRangeStart
!= $block->mRangeEnd
180 && !strstr( $ip, "/" ) ) {
181 /* If the specified IP is a single address, and the block is
182 * a range block, don't unblock the range. */
183 $range = $block->mAddress
;
184 return array('ipb_blocked_as_range', $ip, $range);
188 // Yes, this is really necessary
192 if ( !$block->delete() ) {
193 return array('ipb_cant_unblock', htmlspecialchars($id));
197 $log = new LogPage( 'block' );
198 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
202 function doSubmit() {
204 $retval = self
::doUnblock($this->id
, $this->ip
, $this->reason
, $range);
207 $key = array_shift($retval);
208 $this->showForm(wfMsgReal($key, $retval));
212 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
213 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip
) );
214 $wgOut->redirect( $success );
217 function showList( $msg ) {
218 global $wgOut, $wgUser;
220 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
222 $wgOut->setSubtitle( $msg );
225 // Purge expired entries on one in every 10 queries
226 if ( !mt_rand( 0, 10 ) ) {
227 Block
::purgeExpired();
232 // Is user allowed to see all the blocks?
233 if ( !$wgUser->isAllowed( 'oversight' ) )
234 $conds['ipb_deleted'] = 0;
235 if ( $this->ip
== '' ) {
236 // No extra conditions
237 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
238 $conds['ipb_id'] = substr( $this->ip
, 1 );
239 } elseif ( IP
::toUnsigned( $this->ip
) !== false ) {
240 $conds['ipb_address'] = $this->ip
;
241 $conds['ipb_auto'] = 0;
242 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip
, $matches ) ) {
243 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
244 $conds['ipb_auto'] = 0;
246 $user = User
::newFromName( $this->ip
);
247 if ( $user && ( $id = $user->getID() ) != 0 ) {
248 $conds['ipb_user'] = $id;
251 $conds['ipb_address'] = $this->ip
;
252 $conds['ipb_auto'] = 0;
256 $pager = new IPBlocklistPager( $this, $conds );
257 if ( $pager->getNumRows() ) {
259 $this->searchForm() .
260 $pager->getNavigationBar() .
261 Xml
::tags( 'ul', null, $pager->getBody() ) .
262 $pager->getNavigationBar()
264 } elseif ( $this->ip
!= '') {
265 $wgOut->addHTML( $this->searchForm() );
266 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
268 $wgOut->addWikiMsg( 'ipblocklist-empty' );
272 function searchForm() {
273 global $wgTitle, $wgScript, $wgRequest;
275 Xml
::tags( 'form', array( 'action' => $wgScript ),
276 Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
277 Xml
::openElement( 'fieldset' ) .
278 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
279 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
281 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
282 Xml
::closeElement( 'fieldset' )
287 * Callback function to output a block
289 function formatRow( $block ) {
290 global $wgUser, $wgLang;
292 wfProfileIn( __METHOD__
);
294 static $sk=null, $msg=null;
297 $sk = $wgUser->getSkin();
298 if( is_null( $msg ) ) {
300 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
301 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock' );
302 foreach( $keys as $key ) {
303 $msg[$key] = wfMsgHtml( $key );
305 $msg['blocklistline'] = wfMsg( 'blocklistline' );
308 # Prepare links to the blocker's user and talk pages
309 $blocker_id = $block->getBy();
310 $blocker_name = $block->getByName();
311 $blocker = $sk->userLink( $blocker_id, $blocker_name );
312 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
314 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
315 if( $block->mAuto
) {
316 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
318 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
319 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
322 $formattedTime = $wgLang->timeanddate( $block->mTimestamp
, true );
324 $properties = array();
325 if ( $block->mExpiry
=== "" ||
$block->mExpiry
=== Block
::infinity() ) {
326 $properties[] = $msg['infiniteblock'];
328 $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
329 array( $wgLang->timeanddate( $block->mExpiry
, true ) ) );
331 if ( $block->mAnonOnly
) {
332 $properties[] = $msg['anononlyblock'];
334 if ( $block->mCreateAccount
) {
335 $properties[] = $msg['createaccountblock'];
337 if (!$block->mEnableAutoblock
&& $block->mUser
) {
338 $properties[] = $msg['noautoblockblock'];
341 if ( $block->mBlockEmail
&& $block->mUser
) {
342 $properties[] = $msg['emailblock'];
345 $properties = implode( ', ', $properties );
347 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
350 if ( $wgUser->isAllowed('block') ) {
351 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
352 $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId
) ) . ')';
355 $comment = $sk->commentBlock( $block->mReason
);
357 $s = "{$line} $comment";
358 if ( $block->mHideName
)
359 $s = '<span class="history-deleted">' . $s . '</span>';
361 wfProfileOut( __METHOD__
);
362 return "<li>$s $unblocklink</li>\n";
370 class IPBlocklistPager
extends ReverseChronologicalPager
{
371 public $mForm, $mConds;
373 function __construct( $form, $conds = array() ) {
374 $this->mForm
= $form;
375 $this->mConds
= $conds;
376 parent
::__construct();
379 function getStartBody() {
380 wfProfileIn( __METHOD__
);
381 # Do a link batch query
382 $this->mResult
->seek( 0 );
386 while ( $row = $this->mResult->fetchObject() ) {
387 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
388 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
389 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
390 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
393 # Usernames and titles are in fact related by a simple substitution of space -> underscore
394 # The last few lines of Title::secureAndSplit() tell the story.
395 while ( $row = $this->mResult
->fetchObject() ) {
396 $name = str_replace( ' ', '_', $row->ipb_by_text
);
397 $lb->add( NS_USER
, $name );
398 $lb->add( NS_USER_TALK
, $name );
399 $name = str_replace( ' ', '_', $row->ipb_address
);
400 $lb->add( NS_USER
, $name );
401 $lb->add( NS_USER_TALK
, $name );
404 wfProfileOut( __METHOD__
);
408 function formatRow( $row ) {
410 $block->initFromRow( $row );
411 return $this->mForm
->formatRow( $block );
414 function getQueryInfo() {
415 $conds = $this->mConds
;
416 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
418 'tables' => 'ipblocks',
424 function getIndexField() {
425 return 'ipb_timestamp';