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 ) {
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;
87 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
88 $wgOut->addWikiMsg( 'unblockiptext' );
90 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
91 $action = $titleObj->getLocalURL( "action=submit" );
94 $wgOut->setSubtitle( wfMsg( "formerror" ) );
95 $wgOut->addWikiText( Xml
::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
100 $block = Block
::newFromID( $this->id
);
102 $encName = htmlspecialchars( $block->getRedactedName() );
104 $addressPart = $encName . Xml
::hidden( 'id', $encId );
105 $ipa = wfMsgHtml( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' );
108 if ( !$addressPart ) {
109 $addressPart = Xml
::input( 'wpUnblockAddress', 40, $this->ip
, array( 'type' => 'text', 'tabindex' => '1' ) );
110 $ipa = Xml
::label( wfMsg( $wgSysopUserBans ?
'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
114 Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
115 Xml
::openElement( 'fieldset' ) .
116 Xml
::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
117 Xml
::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
119 <td class='mw-label'>
122 <td class='mw-input'>
127 <td class='mw-label'>" .
128 Xml
::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
130 <td class='mw-input'>" .
131 Xml
::input( 'wpUnblockReason', 40, $this->reason
, array( 'type' => 'text', 'tabindex' => '2' ) ) .
136 <td class='mw-submit'>" .
137 Xml
::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
140 Xml
::closeElement( 'table' ) .
141 Xml
::closeElement( 'fieldset' ) .
142 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
143 Xml
::closeElement( 'form' ) . "\n"
148 const UNBLOCK_SUCCESS
= 0; // Success
149 const UNBLOCK_NO_SUCH_ID
= 1; // No such block ID
150 const UNBLOCK_USER_NOT_BLOCKED
= 2; // IP wasn't blocked
151 const UNBLOCK_BLOCKED_AS_RANGE
= 3; // IP is part of a range block
152 const UNBLOCK_UNKNOWNERR
= 4; // Unknown error
155 * Backend code for unblocking. doSubmit() wraps around this.
156 * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
157 * case it contains the range $ip is part of.
158 * @return array array(message key, parameters) on failure, empty array on success
161 static function doUnblock(&$id, &$ip, &$reason, &$range = null)
164 $block = Block
::newFromID( $id );
166 return array('ipb_cant_unblock', htmlspecialchars($id));
168 $ip = $block->getRedactedName();
170 $block = new Block();
172 if ( substr( $ip, 0, 1 ) == "#" ) {
173 $id = substr( $ip, 1 );
174 $block = Block
::newFromID( $id );
176 return array('ipb_cant_unblock', htmlspecialchars($id));
178 $ip = $block->getRedactedName();
180 $block = Block
::newFromDB( $ip );
182 return array('ipb_cant_unblock', htmlspecialchars($id));
184 if( $block->mRangeStart
!= $block->mRangeEnd
185 && !strstr( $ip, "/" ) ) {
186 /* If the specified IP is a single address, and the block is
187 * a range block, don't unblock the range. */
188 $range = $block->mAddress
;
189 return array('ipb_blocked_as_range', $ip, $range);
193 // Yes, this is really necessary
197 if ( !$block->delete() ) {
198 return array('ipb_cant_unblock', htmlspecialchars($id));
202 $log = new LogPage( 'block' );
203 $log->addEntry( 'unblock', Title
::makeTitle( NS_USER
, $ip ), $reason );
207 function doSubmit() {
209 $retval = self
::doUnblock($this->id
, $this->ip
, $this->reason
, $range);
212 $key = array_shift($retval);
213 $this->showForm(wfMsgReal($key, $retval));
217 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
218 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip
) );
219 $wgOut->redirect( $success );
222 function showList( $msg ) {
223 global $wgOut, $wgUser;
225 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
227 $wgOut->setSubtitle( $msg );
230 // Purge expired entries on one in every 10 queries
231 if ( !mt_rand( 0, 10 ) ) {
232 Block
::purgeExpired();
237 // Is user allowed to see all the blocks?
238 if ( !$wgUser->isAllowed( 'suppress' ) )
239 $conds['ipb_deleted'] = 0;
240 if ( $this->ip
== '' ) {
241 // No extra conditions
242 } elseif ( substr( $this->ip
, 0, 1 ) == '#' ) {
243 $conds['ipb_id'] = substr( $this->ip
, 1 );
244 } elseif ( IP
::toUnsigned( $this->ip
) !== false ) {
245 $conds['ipb_address'] = $this->ip
;
246 $conds['ipb_auto'] = 0;
247 } elseif( preg_match( '/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\\/(\\d{1,2})$/', $this->ip
, $matches ) ) {
248 $conds['ipb_address'] = Block
::normaliseRange( $this->ip
);
249 $conds['ipb_auto'] = 0;
251 $user = User
::newFromName( $this->ip
);
252 if ( $user && ( $id = $user->getId() ) != 0 ) {
253 $conds['ipb_user'] = $id;
256 $conds['ipb_address'] = $this->ip
;
257 $conds['ipb_auto'] = 0;
261 $pager = new IPBlocklistPager( $this, $conds );
262 if ( $pager->getNumRows() ) {
264 $this->searchForm() .
265 $pager->getNavigationBar() .
266 Xml
::tags( 'ul', null, $pager->getBody() ) .
267 $pager->getNavigationBar()
269 } elseif ( $this->ip
!= '') {
270 $wgOut->addHTML( $this->searchForm() );
271 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
273 $wgOut->addWikiMsg( 'ipblocklist-empty' );
277 function searchForm() {
278 global $wgTitle, $wgScript, $wgRequest;
280 Xml
::tags( 'form', array( 'action' => $wgScript ),
281 Xml
::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
282 Xml
::openElement( 'fieldset' ) .
283 Xml
::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
284 Xml
::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip
) .
286 Xml
::submitButton( wfMsg( 'ipblocklist-submit' ) ) .
287 Xml
::closeElement( 'fieldset' )
292 * Callback function to output a block
294 function formatRow( $block ) {
295 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
297 wfProfileIn( __METHOD__
);
299 static $sk=null, $msg=null;
302 $sk = $wgUser->getSkin();
303 if( is_null( $msg ) ) {
305 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink',
306 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk' );
307 foreach( $keys as $key ) {
308 $msg[$key] = wfMsgHtml( $key );
310 $msg['blocklistline'] = wfMsg( 'blocklistline' );
313 # Prepare links to the blocker's user and talk pages
314 $blocker_id = $block->getBy();
315 $blocker_name = $block->getByName();
316 $blocker = $sk->userLink( $blocker_id, $blocker_name );
317 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
319 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
320 if( $block->mAuto
) {
321 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
323 $target = $sk->userLink( $block->mUser
, $block->mAddress
)
324 . $sk->userToolLinks( $block->mUser
, $block->mAddress
, false, Linker
::TOOL_LINKS_NOBLOCK
);
327 $formattedTime = $wgLang->timeanddate( $block->mTimestamp
, true );
329 $properties = array();
330 $properties[] = Block
::formatExpiry( $block->mExpiry
);
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 if ( !$block->mAllowUsertalk
&& $wgBlockAllowsUTEdit ) {
346 $properties[] = $msg['blocklist-nousertalk'];
349 $properties = implode( ', ', $properties );
351 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
354 if ( $wgUser->isAllowed('block') ) {
355 $titleObj = SpecialPage
::getTitleFor( "Ipblocklist" );
356 $unblocklink = ' (' . $sk->makeKnownLinkObj($titleObj, $msg['unblocklink'], 'action=unblock&id=' . urlencode( $block->mId
) ) . ')';
359 $comment = $sk->commentBlock( $block->mReason
);
361 $s = "{$line} $comment";
362 if ( $block->mHideName
)
363 $s = '<span class="history-deleted">' . $s . '</span>';
365 wfProfileOut( __METHOD__
);
366 return "<li>$s $unblocklink</li>\n";
374 class IPBlocklistPager
extends ReverseChronologicalPager
{
375 public $mForm, $mConds;
377 function __construct( $form, $conds = array() ) {
378 $this->mForm
= $form;
379 $this->mConds
= $conds;
380 parent
::__construct();
383 function getStartBody() {
384 wfProfileIn( __METHOD__
);
385 # Do a link batch query
386 $this->mResult
->seek( 0 );
390 while ( $row = $this->mResult->fetchObject() ) {
391 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
392 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
393 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
394 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
397 # Usernames and titles are in fact related by a simple substitution of space -> underscore
398 # The last few lines of Title::secureAndSplit() tell the story.
399 while ( $row = $this->mResult
->fetchObject() ) {
400 $name = str_replace( ' ', '_', $row->ipb_by_text
);
401 $lb->add( NS_USER
, $name );
402 $lb->add( NS_USER_TALK
, $name );
403 $name = str_replace( ' ', '_', $row->ipb_address
);
404 $lb->add( NS_USER
, $name );
405 $lb->add( NS_USER_TALK
, $name );
408 wfProfileOut( __METHOD__
);
412 function formatRow( $row ) {
414 $block->initFromRow( $row );
415 return $this->mForm
->formatRow( $block );
418 function getQueryInfo() {
419 $conds = $this->mConds
;
420 $conds[] = 'ipb_expiry>' . $this->mDb
->addQuotes( $this->mDb
->timestamp() );
422 'tables' => 'ipblocks',
428 function getIndexField() {
429 return 'ipb_timestamp';