00001 <?php
00011 function wfSpecialIpblocklist( $ip = '' ) {
00012 global $wgUser, $wgOut, $wgRequest;
00013 $ip = $wgRequest->getVal( 'ip', $ip );
00014 $ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $ip ) );
00015 $id = $wgRequest->getVal( 'id' );
00016 $reason = $wgRequest->getText( 'wpUnblockReason' );
00017 $action = $wgRequest->getText( 'action' );
00018 $successip = $wgRequest->getVal( 'successip' );
00019
00020 $ipu = new IPUnblockForm( $ip, $id, $reason );
00021
00022 if( $action == 'unblock' ) {
00023 # Check permissions
00024 if( !$wgUser->isAllowed( 'block' ) ) {
00025 $wgOut->permissionRequired( 'block' );
00026 return;
00027 }
00028 # Check for database lock
00029 if( wfReadOnly() ) {
00030 $wgOut->readOnlyPage();
00031 return;
00032 }
00033 # Show unblock form
00034 $ipu->showForm( '' );
00035 } elseif( $action == 'submit' && $wgRequest->wasPosted()
00036 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
00037 # Check permissions
00038 if( !$wgUser->isAllowed( 'block' ) ) {
00039 $wgOut->permissionRequired( 'block' );
00040 return;
00041 }
00042 # Check for database lock
00043 if( wfReadOnly() ) {
00044 $wgOut->readOnlyPage();
00045 return;
00046 }
00047 # Remove blocks and redirect user to success page
00048 $ipu->doSubmit();
00049 } elseif( $action == 'success' ) {
00050 # Inform the user of a successful unblock
00051 # (No need to check permissions or locks here,
00052 # if something was done, then it's too late!)
00053 if ( substr( $successip, 0, 1) == '#' ) {
00054
00055 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
00056 } else {
00057
00058 $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
00059 }
00060 } else {
00061 # Just show the block list
00062 $ipu->showList( '' );
00063 }
00064
00065 }
00066
00071 class IPUnblockForm {
00072 var $ip, $reason, $id;
00073
00074 function IPUnblockForm( $ip, $id, $reason ) {
00075 global $wgRequest;
00076 $this->ip = strtr( $ip, '_', ' ' );
00077 $this->id = $id;
00078 $this->reason = $reason;
00079 $this->hideuserblocks = $wgRequest->getBool( 'hideuserblocks' );
00080 $this->hidetempblocks = $wgRequest->getBool( 'hidetempblocks' );
00081 $this->hideaddressblocks = $wgRequest->getBool( 'hideaddressblocks' );
00082 }
00083
00089 function showForm( $err ) {
00090 global $wgOut, $wgUser, $wgSysopUserBans;
00091
00092 $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
00093 $wgOut->addWikiMsg( 'unblockiptext' );
00094
00095 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
00096 $action = $titleObj->getLocalURL( "action=submit" );
00097
00098 if ( $err != "" ) {
00099 $wgOut->setSubtitle( wfMsg( "formerror" ) );
00100 $wgOut->addWikiText( Xml::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
00101 }
00102
00103 $addressPart = false;
00104 if ( $this->id ) {
00105 $block = Block::newFromID( $this->id );
00106 if ( $block ) {
00107 $encName = htmlspecialchars( $block->getRedactedName() );
00108 $encId = $this->id;
00109 $addressPart = $encName . Xml::hidden( 'id', $encId );
00110 $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
00111 }
00112 }
00113 if ( !$addressPart ) {
00114 $addressPart = Xml::input( 'wpUnblockAddress', 40, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
00115 $ipa = Xml::label( wfMsg( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
00116 }
00117
00118 $wgOut->addHTML(
00119 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
00120 Xml::openElement( 'fieldset' ) .
00121 Xml::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
00122 Xml::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
00123 "<tr>
00124 <td class='mw-label'>
00125 {$ipa}
00126 </td>
00127 <td class='mw-input'>
00128 {$addressPart}
00129 </td>
00130 </tr>
00131 <tr>
00132 <td class='mw-label'>" .
00133 Xml::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
00134 "</td>
00135 <td class='mw-input'>" .
00136 Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
00137 "</td>
00138 </tr>
00139 <tr>
00140 <td> </td>
00141 <td class='mw-submit'>" .
00142 Xml::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
00143 "</td>
00144 </tr>" .
00145 Xml::closeElement( 'table' ) .
00146 Xml::closeElement( 'fieldset' ) .
00147 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
00148 Xml::closeElement( 'form' ) . "\n"
00149 );
00150
00151 }
00152
00153 const UNBLOCK_SUCCESS = 0;
00154 const UNBLOCK_NO_SUCH_ID = 1;
00155 const UNBLOCK_USER_NOT_BLOCKED = 2;
00156 const UNBLOCK_BLOCKED_AS_RANGE = 3;
00157 const UNBLOCK_UNKNOWNERR = 4;
00158
00166 static function doUnblock(&$id, &$ip, &$reason, &$range = null, $blocker=null) {
00167 if ( $id ) {
00168 $block = Block::newFromID( $id );
00169 if ( !$block ) {
00170 return array('ipb_cant_unblock', htmlspecialchars($id));
00171 }
00172 $ip = $block->getRedactedName();
00173 } else {
00174 $block = new Block();
00175 $ip = trim( $ip );
00176 if ( substr( $ip, 0, 1 ) == "#" ) {
00177 $id = substr( $ip, 1 );
00178 $block = Block::newFromID( $id );
00179 if( !$block ) {
00180 return array('ipb_cant_unblock', htmlspecialchars($id));
00181 }
00182 $ip = $block->getRedactedName();
00183 } else {
00184 $block = Block::newFromDB( $ip );
00185 if ( !$block ) {
00186 return array('ipb_cant_unblock', htmlspecialchars($id));
00187 }
00188 if( $block->mRangeStart != $block->mRangeEnd && !strstr( $ip, "/" ) ) {
00189
00190
00191 $range = $block->mAddress;
00192 return array('ipb_blocked_as_range', $ip, $range);
00193 }
00194 }
00195 }
00196
00197 $id = $block->mId;
00198
00199 # If the name was hidden and the blocking user cannot hide
00200 # names, then don't allow any block removals...
00201 if( $blocker && $block->mHideName && !$blocker->isAllowed('hideuser') ) {
00202 return array('ipb_cant_unblock', htmlspecialchars($id));
00203 }
00204
00205 # Delete block
00206 if ( !$block->delete() ) {
00207 return array('ipb_cant_unblock', htmlspecialchars($id));
00208 }
00209
00210 # Unset _deleted fields as needed
00211 if( $block->mHideName ) {
00212 IPBlockForm::unsuppressUserName( $block->mAddress, $block->mUser );
00213 }
00214
00215 # Make log entry
00216 $log = new LogPage( 'block' );
00217 $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
00218 return array();
00219 }
00220
00221 function doSubmit() {
00222 global $wgOut, $wgUser;
00223 $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range, $wgUser);
00224 if( !empty($retval) ) {
00225 $key = array_shift($retval);
00226 $this->showForm(wfMsgReal($key, $retval));
00227 return;
00228 }
00229 # Report to the user
00230 $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
00231 $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
00232 $wgOut->redirect( $success );
00233 }
00234
00235 function showList( $msg ) {
00236 global $wgOut, $wgUser;
00237
00238 $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
00239 if ( $msg != "" ) {
00240 $wgOut->setSubtitle( $msg );
00241 }
00242
00243
00244 if ( !mt_rand( 0, 10 ) ) {
00245 Block::purgeExpired();
00246 }
00247
00248 $conds = array();
00249 $matches = array();
00250
00251 if ( !$wgUser->isAllowed( 'hideuser' ) )
00252 $conds['ipb_deleted'] = 0;
00253 if ( $this->ip == '' ) {
00254
00255 } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
00256 $conds['ipb_id'] = substr( $this->ip, 1 );
00257
00258 } elseif ( IP::isIPAddress($this->ip) && strpos($this->ip,'/') === false ) {
00259 if( $iaddr = IP::toHex($this->ip) ) {
00260 # Only scan ranges which start in this /16, this improves search speed
00261 # Blocks should not cross a /16 boundary.
00262 $range = substr( $iaddr, 0, 4 );
00263
00264 $dbr = wfGetDB( DB_SLAVE );
00265 $encIp = $dbr->addQuotes( IP::sanitizeIP($this->ip) );
00266 $encAddr = $dbr->addQuotes( $iaddr );
00267 $conds[] = "(ipb_address = $encIp) OR
00268 (ipb_range_start" . $dbr->buildLike( $range, $dbr->anyString() ) . " AND
00269 ipb_range_start <= $encAddr
00270 AND ipb_range_end >= $encAddr)";
00271 } else {
00272 $conds['ipb_address'] = IP::sanitizeIP($this->ip);
00273 }
00274 $conds['ipb_auto'] = 0;
00275
00276 } elseif ( IP::isIPAddress($this->ip) ) {
00277 $conds['ipb_address'] = Block::normaliseRange( $this->ip );
00278 $conds['ipb_auto'] = 0;
00279 } else {
00280 $user = User::newFromName( $this->ip );
00281 if ( $user && ( $id = $user->getId() ) != 0 ) {
00282 $conds['ipb_user'] = $id;
00283 } else {
00284
00285 $conds['ipb_address'] = $this->ip;
00286 $conds['ipb_auto'] = 0;
00287 }
00288 }
00289
00290 if( $this->hideuserblocks ) {
00291 $conds['ipb_user'] = 0;
00292 }
00293 if( $this->hidetempblocks ) {
00294 $conds['ipb_expiry'] = 'infinity';
00295 }
00296 if( $this->hideaddressblocks ) {
00297 $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
00298 }
00299
00300
00301 $wgOut->addHTML( $this->searchForm() );
00302
00303
00304 $otherBlockLink = array();
00305 wfRunHooks( 'OtherBlockLogLink', array( &$otherBlockLink, $this->ip ) );
00306
00307
00308
00309 if( count( $otherBlockLink ) ) {
00310 $wgOut->addHTML(
00311 Html::rawElement( 'h2', array(), wfMsg( 'ipblocklist-localblock' ) ) . "\n"
00312 );
00313 }
00314 $pager = new IPBlocklistPager( $this, $conds );
00315 if ( $pager->getNumRows() ) {
00316 $wgOut->addHTML(
00317 $pager->getNavigationBar() .
00318 Xml::tags( 'ul', null, $pager->getBody() ) .
00319 $pager->getNavigationBar()
00320 );
00321 } elseif ( $this->ip != '') {
00322 $wgOut->addWikiMsg( 'ipblocklist-no-results' );
00323 } else {
00324 $wgOut->addWikiMsg( 'ipblocklist-empty' );
00325 }
00326
00327 if( count( $otherBlockLink ) ) {
00328 $wgOut->addHTML(
00329 Html::rawElement( 'h2', array(), wfMsgExt( 'ipblocklist-otherblocks', 'parseinline', count( $otherBlockLink ) ) ) . "\n"
00330 );
00331 $list = '';
00332 foreach( $otherBlockLink as $link ) {
00333 $list .= Html::rawElement( 'li', array(), $link ) . "\n";
00334 }
00335 $wgOut->addHTML( Html::rawElement( 'ul', array( 'class' => 'mw-ipblocklist-otherblocks' ), $list ) . "\n" );
00336 }
00337
00338 }
00339
00340 function searchForm() {
00341 global $wgScript, $wgRequest, $wgLang;
00342
00343 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
00344 $nondefaults = array();
00345 if( $this->hideuserblocks ) {
00346 $nondefaults['hideuserblocks'] = $this->hideuserblocks;
00347 }
00348 if( $this->hidetempblocks ) {
00349 $nondefaults['hidetempblocks'] = $this->hidetempblocks;
00350 }
00351 if( $this->hideaddressblocks ) {
00352 $nondefaults['hideaddressblocks'] = $this->hideaddressblocks;
00353 }
00354 $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks],
00355 array( 'hideuserblocks' => 1-$this->hideuserblocks ), $nondefaults);
00356 $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks],
00357 array( 'hidetempblocks' => 1-$this->hidetempblocks ), $nondefaults);
00358 $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks],
00359 array( 'hideaddressblocks' => 1-$this->hideaddressblocks ), $nondefaults);
00360
00361 $links = array();
00362 $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
00363 $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
00364 $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
00365 $hl = $wgLang->pipeList( $links );
00366
00367 return
00368 Xml::tags( 'form', array( 'action' => $wgScript ),
00369 Xml::hidden( 'title', SpecialPage::getTitleFor( 'Ipblocklist' )->getPrefixedDbKey() ) .
00370 Xml::openElement( 'fieldset' ) .
00371 Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
00372 Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', false, $this->ip ) .
00373 ' ' .
00374 Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
00375 $hl .
00376 Xml::closeElement( 'fieldset' )
00377 );
00378 }
00379
00386 function makeOptionsLink( $title, $override, $options, $active = false ) {
00387 global $wgUser;
00388 $sk = $wgUser->getSkin();
00389 $params = $override + $options;
00390 $ipblocklist = SpecialPage::getTitleFor( 'Ipblocklist' );
00391 return $sk->link( $ipblocklist, htmlspecialchars( $title ),
00392 ( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
00393 }
00394
00398 function formatRow( $block ) {
00399 global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
00400
00401 wfProfileIn( __METHOD__ );
00402
00403 static $sk=null, $msg=null;
00404
00405 if( is_null( $sk ) )
00406 $sk = $wgUser->getSkin();
00407 if( is_null( $msg ) ) {
00408 $msg = array();
00409 $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
00410 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk', 'blocklistline' );
00411 foreach( $keys as $key ) {
00412 $msg[$key] = wfMsgHtml( $key );
00413 }
00414 }
00415
00416 # Prepare links to the blocker's user and talk pages
00417 $blocker_id = $block->getBy();
00418 $blocker_name = $block->getByName();
00419 $blocker = $sk->userLink( $blocker_id, $blocker_name );
00420 $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
00421
00422 # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
00423 if( $block->mAuto ) {
00424 $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
00425 } else {
00426 $target = $sk->userLink( $block->mUser, $block->mAddress )
00427 . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
00428 }
00429
00430 $formattedTime = htmlspecialchars( $wgLang->timeanddate( $block->mTimestamp, true ) );
00431
00432 $properties = array();
00433 $properties[] = Block::formatExpiry( $block->mExpiry );
00434 if ( $block->mAnonOnly ) {
00435 $properties[] = $msg['anononlyblock'];
00436 }
00437 if ( $block->mCreateAccount ) {
00438 $properties[] = $msg['createaccountblock'];
00439 }
00440 if (!$block->mEnableAutoblock && $block->mUser ) {
00441 $properties[] = $msg['noautoblockblock'];
00442 }
00443
00444 if ( $block->mBlockEmail && $block->mUser ) {
00445 $properties[] = $msg['emailblock'];
00446 }
00447
00448 if ( !$block->mAllowUsertalk && $wgBlockAllowsUTEdit ) {
00449 $properties[] = $msg['blocklist-nousertalk'];
00450 }
00451
00452 $properties = $wgLang->commaList( $properties );
00453
00454 $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
00455
00456 $unblocklink = '';
00457 $changeblocklink = '';
00458 $toolLinks = '';
00459 if ( $wgUser->isAllowed( 'block' ) ) {
00460 $unblocklink = $sk->link( SpecialPage::getTitleFor( 'Ipblocklist' ),
00461 $msg['unblocklink'],
00462 array(),
00463 array( 'action' => 'unblock', 'id' => $block->mId ),
00464 'known' );
00465
00466 # Create changeblocklink for all blocks with exception of autoblocks
00467 if( !$block->mAuto ) {
00468 $changeblocklink = wfMsgExt( 'pipe-separator', 'escapenoentities' ) .
00469 $sk->link( SpecialPage::getTitleFor( 'Blockip', $block->mAddress ),
00470 $msg['change-blocklink'],
00471 array(), array(), 'known' );
00472 }
00473 $toolLinks = "($unblocklink$changeblocklink)";
00474 }
00475
00476 $comment = $sk->commentBlock( htmlspecialchars($block->mReason) );
00477
00478 $s = "{$line} $comment";
00479 if ( $block->mHideName )
00480 $s = '<span class="history-deleted">' . $s . '</span>';
00481
00482 wfProfileOut( __METHOD__ );
00483 return "<li>$s $toolLinks</li>\n";
00484 }
00485 }
00486
00491 class IPBlocklistPager extends ReverseChronologicalPager {
00492 public $mForm, $mConds;
00493
00494 function __construct( $form, $conds = array() ) {
00495 $this->mForm = $form;
00496 $this->mConds = $conds;
00497 parent::__construct();
00498 }
00499
00500 function getStartBody() {
00501 wfProfileIn( __METHOD__ );
00502 # Do a link batch query
00503 $this->mResult->seek( 0 );
00504 $lb = new LinkBatch;
00505
00506
00507
00508
00509
00510
00511
00512
00513 # Faster way
00514 # Usernames and titles are in fact related by a simple substitution of space -> underscore
00515 # The last few lines of Title::secureAndSplit() tell the story.
00516 while ( $row = $this->mResult->fetchObject() ) {
00517 $name = str_replace( ' ', '_', $row->ipb_by_text );
00518 $lb->add( NS_USER, $name );
00519 $lb->add( NS_USER_TALK, $name );
00520 $name = str_replace( ' ', '_', $row->ipb_address );
00521 $lb->add( NS_USER, $name );
00522 $lb->add( NS_USER_TALK, $name );
00523 }
00524 $lb->execute();
00525 wfProfileOut( __METHOD__ );
00526 return '';
00527 }
00528
00529 function formatRow( $row ) {
00530 $block = new Block;
00531 $block->initFromRow( $row );
00532 return $this->mForm->formatRow( $block );
00533 }
00534
00535 function getQueryInfo() {
00536 $conds = $this->mConds;
00537 $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
00538 return array(
00539 'tables' => 'ipblocks',
00540 'fields' => '*',
00541 'conds' => $conds,
00542 );
00543 }
00544
00545 function getIndexField() {
00546 return 'ipb_timestamp';
00547 }
00548 }