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