SpecialIpblocklist.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * @todo document
  8. */
  9. function wfSpecialIpblocklist() {
  10. global $wgUser, $wgOut, $wgRequest;
  11. $ip = trim( $wgRequest->getVal( 'wpUnblockAddress', $wgRequest->getVal( 'ip' ) ) );
  12. $id = $wgRequest->getVal( 'id' );
  13. $reason = $wgRequest->getText( 'wpUnblockReason' );
  14. $action = $wgRequest->getText( 'action' );
  15. $successip = $wgRequest->getVal( 'successip' );
  16. $ipu = new IPUnblockForm( $ip, $id, $reason );
  17. if( $action == 'unblock' ) {
  18. # Check permissions
  19. if( !$wgUser->isAllowed( 'block' ) ) {
  20. $wgOut->permissionRequired( 'block' );
  21. return;
  22. }
  23. # Check for database lock
  24. if( wfReadOnly() ) {
  25. $wgOut->readOnlyPage();
  26. return;
  27. }
  28. # Show unblock form
  29. $ipu->showForm( '' );
  30. } elseif( $action == 'submit' && $wgRequest->wasPosted()
  31. && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
  32. # Check permissions
  33. if( !$wgUser->isAllowed( 'block' ) ) {
  34. $wgOut->permissionRequired( 'block' );
  35. return;
  36. }
  37. # Check for database lock
  38. if( wfReadOnly() ) {
  39. $wgOut->readOnlyPage();
  40. return;
  41. }
  42. # Remove blocks and redirect user to success page
  43. $ipu->doSubmit();
  44. } elseif( $action == 'success' ) {
  45. # Inform the user of a successful unblock
  46. # (No need to check permissions or locks here,
  47. # if something was done, then it's too late!)
  48. if ( substr( $successip, 0, 1) == '#' ) {
  49. // A block ID was unblocked
  50. $ipu->showList( $wgOut->parse( wfMsg( 'unblocked-id', $successip ) ) );
  51. } else {
  52. // A username/IP was unblocked
  53. $ipu->showList( $wgOut->parse( wfMsg( 'unblocked', $successip ) ) );
  54. }
  55. } else {
  56. # Just show the block list
  57. $ipu->showList( '' );
  58. }
  59. }
  60. /**
  61. * implements Special:ipblocklist GUI
  62. * @ingroup SpecialPage
  63. */
  64. class IPUnblockForm {
  65. var $ip, $reason, $id;
  66. function IPUnblockForm( $ip, $id, $reason ) {
  67. global $wgRequest;
  68. $this->ip = strtr( $ip, '_', ' ' );
  69. $this->id = $id;
  70. $this->reason = $reason;
  71. $this->hideuserblocks = $wgRequest->getBool( 'hideuserblocks' );
  72. $this->hidetempblocks = $wgRequest->getBool( 'hidetempblocks' );
  73. $this->hideaddressblocks = $wgRequest->getBool( 'hideaddressblocks' );
  74. }
  75. /**
  76. * Generates the unblock form
  77. * @param $err string: error message
  78. * @return $out string: HTML form
  79. */
  80. function showForm( $err ) {
  81. global $wgOut, $wgUser, $wgSysopUserBans;
  82. $wgOut->setPagetitle( wfMsg( 'unblockip' ) );
  83. $wgOut->addWikiMsg( 'unblockiptext' );
  84. $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
  85. $action = $titleObj->getLocalURL( "action=submit" );
  86. if ( "" != $err ) {
  87. $wgOut->setSubtitle( wfMsg( "formerror" ) );
  88. $wgOut->addWikiText( Xml::tags( 'span', array( 'class' => 'error' ), $err ) . "\n" );
  89. }
  90. $addressPart = false;
  91. if ( $this->id ) {
  92. $block = Block::newFromID( $this->id );
  93. if ( $block ) {
  94. $encName = htmlspecialchars( $block->getRedactedName() );
  95. $encId = $this->id;
  96. $addressPart = $encName . Xml::hidden( 'id', $encId );
  97. $ipa = wfMsgHtml( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' );
  98. }
  99. }
  100. if ( !$addressPart ) {
  101. $addressPart = Xml::input( 'wpUnblockAddress', 40, $this->ip, array( 'type' => 'text', 'tabindex' => '1' ) );
  102. $ipa = Xml::label( wfMsg( $wgSysopUserBans ? 'ipadressorusername' : 'ipaddress' ), 'wpUnblockAddress' );
  103. }
  104. $wgOut->addHTML(
  105. Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'unblockip' ) ) .
  106. Xml::openElement( 'fieldset' ) .
  107. Xml::element( 'legend', null, wfMsg( 'ipb-unblock' ) ) .
  108. Xml::openElement( 'table', array( 'id' => 'mw-unblock-table' ) ).
  109. "<tr>
  110. <td class='mw-label'>
  111. {$ipa}
  112. </td>
  113. <td class='mw-input'>
  114. {$addressPart}
  115. </td>
  116. </tr>
  117. <tr>
  118. <td class='mw-label'>" .
  119. Xml::label( wfMsg( 'ipbreason' ), 'wpUnblockReason' ) .
  120. "</td>
  121. <td class='mw-input'>" .
  122. Xml::input( 'wpUnblockReason', 40, $this->reason, array( 'type' => 'text', 'tabindex' => '2' ) ) .
  123. "</td>
  124. </tr>
  125. <tr>
  126. <td>&nbsp;</td>
  127. <td class='mw-submit'>" .
  128. Xml::submitButton( wfMsg( 'ipusubmit' ), array( 'name' => 'wpBlock', 'tabindex' => '3' ) ) .
  129. "</td>
  130. </tr>" .
  131. Xml::closeElement( 'table' ) .
  132. Xml::closeElement( 'fieldset' ) .
  133. Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
  134. Xml::closeElement( 'form' ) . "\n"
  135. );
  136. }
  137. const UNBLOCK_SUCCESS = 0; // Success
  138. const UNBLOCK_NO_SUCH_ID = 1; // No such block ID
  139. const UNBLOCK_USER_NOT_BLOCKED = 2; // IP wasn't blocked
  140. const UNBLOCK_BLOCKED_AS_RANGE = 3; // IP is part of a range block
  141. const UNBLOCK_UNKNOWNERR = 4; // Unknown error
  142. /**
  143. * Backend code for unblocking. doSubmit() wraps around this.
  144. * $range is only used when UNBLOCK_BLOCKED_AS_RANGE is returned, in which
  145. * case it contains the range $ip is part of.
  146. * @return array array(message key, parameters) on failure, empty array on success
  147. */
  148. static function doUnblock(&$id, &$ip, &$reason, &$range = null, $blocker=null) {
  149. if ( $id ) {
  150. $block = Block::newFromID( $id );
  151. if ( !$block ) {
  152. return array('ipb_cant_unblock', htmlspecialchars($id));
  153. }
  154. $ip = $block->getRedactedName();
  155. } else {
  156. $block = new Block();
  157. $ip = trim( $ip );
  158. if ( substr( $ip, 0, 1 ) == "#" ) {
  159. $id = substr( $ip, 1 );
  160. $block = Block::newFromID( $id );
  161. if( !$block ) {
  162. return array('ipb_cant_unblock', htmlspecialchars($id));
  163. }
  164. $ip = $block->getRedactedName();
  165. } else {
  166. $block = Block::newFromDB( $ip );
  167. if ( !$block ) {
  168. return array('ipb_cant_unblock', htmlspecialchars($id));
  169. }
  170. if( $block->mRangeStart != $block->mRangeEnd
  171. && !strstr( $ip, "/" ) ) {
  172. /* If the specified IP is a single address, and the block is
  173. * a range block, don't unblock the range. */
  174. $range = $block->mAddress;
  175. return array('ipb_blocked_as_range', $ip, $range);
  176. }
  177. }
  178. }
  179. // Yes, this is really necessary
  180. $id = $block->mId;
  181. # If the name was hidden and the blocking user cannot hide
  182. # names, then don't allow any block removals...
  183. if( $blocker && $block->mHideName && !$blocker->isAllowed('hideuser') ) {
  184. return array('ipb_cant_unblock', htmlspecialchars($id));
  185. }
  186. # Delete block
  187. if ( !$block->delete() ) {
  188. return array('ipb_cant_unblock', htmlspecialchars($id));
  189. }
  190. # Unset _deleted fields as needed
  191. if( $block->mHideName ) {
  192. IPBlockForm::unsuppressUserName( $block->mAddress, $block->mUser );
  193. }
  194. # Make log entry
  195. $log = new LogPage( 'block' );
  196. $log->addEntry( 'unblock', Title::makeTitle( NS_USER, $ip ), $reason );
  197. return array();
  198. }
  199. function doSubmit() {
  200. global $wgOut, $wgUser;
  201. $retval = self::doUnblock($this->id, $this->ip, $this->reason, $range, $wgUser);
  202. if(!empty($retval))
  203. {
  204. $key = array_shift($retval);
  205. $this->showForm(wfMsgReal($key, $retval));
  206. return;
  207. }
  208. # Report to the user
  209. $titleObj = SpecialPage::getTitleFor( "Ipblocklist" );
  210. $success = $titleObj->getFullURL( "action=success&successip=" . urlencode( $this->ip ) );
  211. $wgOut->redirect( $success );
  212. }
  213. function showList( $msg ) {
  214. global $wgOut, $wgUser;
  215. $wgOut->setPagetitle( wfMsg( "ipblocklist" ) );
  216. if ( "" != $msg ) {
  217. $wgOut->setSubtitle( $msg );
  218. }
  219. // Purge expired entries on one in every 10 queries
  220. if ( !mt_rand( 0, 10 ) ) {
  221. Block::purgeExpired();
  222. }
  223. $conds = array();
  224. $matches = array();
  225. // Is user allowed to see all the blocks?
  226. if ( !$wgUser->isAllowed( 'hideuser' ) )
  227. $conds['ipb_deleted'] = 0;
  228. if ( $this->ip == '' ) {
  229. // No extra conditions
  230. } elseif ( substr( $this->ip, 0, 1 ) == '#' ) {
  231. $conds['ipb_id'] = substr( $this->ip, 1 );
  232. // Single IPs
  233. } elseif ( IP::isIPAddress($this->ip) && strpos($this->ip,'/') === false ) {
  234. if( $iaddr = IP::toHex($this->ip) ) {
  235. # Only scan ranges which start in this /16, this improves search speed
  236. # Blocks should not cross a /16 boundary.
  237. $range = substr( $iaddr, 0, 4 );
  238. // Fixme -- encapsulate this sort of query-building.
  239. $dbr = wfGetDB( DB_SLAVE );
  240. $encIp = $dbr->addQuotes( IP::sanitizeIP($this->ip) );
  241. $encRange = $dbr->addQuotes( "$range%" );
  242. $encAddr = $dbr->addQuotes( $iaddr );
  243. $conds[] = "(ipb_address = $encIp) OR
  244. (ipb_range_start LIKE $encRange AND
  245. ipb_range_start <= $encAddr
  246. AND ipb_range_end >= $encAddr)";
  247. } else {
  248. $conds['ipb_address'] = IP::sanitizeIP($this->ip);
  249. }
  250. $conds['ipb_auto'] = 0;
  251. // IP range
  252. } elseif ( IP::isIPAddress($this->ip) ) {
  253. $conds['ipb_address'] = Block::normaliseRange( $this->ip );
  254. $conds['ipb_auto'] = 0;
  255. } else {
  256. $user = User::newFromName( $this->ip );
  257. if ( $user && ( $id = $user->getId() ) != 0 ) {
  258. $conds['ipb_user'] = $id;
  259. } else {
  260. // Uh...?
  261. $conds['ipb_address'] = $this->ip;
  262. $conds['ipb_auto'] = 0;
  263. }
  264. }
  265. // Apply filters
  266. if( $this->hideuserblocks ) {
  267. $conds['ipb_user'] = 0;
  268. }
  269. if( $this->hidetempblocks ) {
  270. $conds['ipb_expiry'] = 'infinity';
  271. }
  272. if( $this->hideaddressblocks ) {
  273. $conds[] = "ipb_user != 0 OR ipb_range_end > ipb_range_start";
  274. }
  275. $pager = new IPBlocklistPager( $this, $conds );
  276. if ( $pager->getNumRows() ) {
  277. $wgOut->addHTML(
  278. $this->searchForm() .
  279. $pager->getNavigationBar() .
  280. Xml::tags( 'ul', null, $pager->getBody() ) .
  281. $pager->getNavigationBar()
  282. );
  283. } elseif ( $this->ip != '') {
  284. $wgOut->addHTML( $this->searchForm() );
  285. $wgOut->addWikiMsg( 'ipblocklist-no-results' );
  286. } else {
  287. $wgOut->addHTML( $this->searchForm() );
  288. $wgOut->addWikiMsg( 'ipblocklist-empty' );
  289. }
  290. }
  291. function searchForm() {
  292. global $wgTitle, $wgScript, $wgRequest, $wgLang;
  293. $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
  294. $nondefaults = array();
  295. if( $this->hideuserblocks ) {
  296. $nondefaults['hideuserblocks'] = $this->hideuserblocks;
  297. }
  298. if( $this->hidetempblocks ) {
  299. $nondefaults['hidetempblocks'] = $this->hidetempblocks;
  300. }
  301. if( $this->hideaddressblocks ) {
  302. $nondefaults['hideaddressblocks'] = $this->hideaddressblocks;
  303. }
  304. $ubLink = $this->makeOptionsLink( $showhide[1-$this->hideuserblocks],
  305. array( 'hideuserblocks' => 1-$this->hideuserblocks ), $nondefaults);
  306. $tbLink = $this->makeOptionsLink( $showhide[1-$this->hidetempblocks],
  307. array( 'hidetempblocks' => 1-$this->hidetempblocks ), $nondefaults);
  308. $sipbLink = $this->makeOptionsLink( $showhide[1-$this->hideaddressblocks],
  309. array( 'hideaddressblocks' => 1-$this->hideaddressblocks ), $nondefaults);
  310. $links = array();
  311. $links[] = wfMsgHtml( 'ipblocklist-sh-userblocks', $ubLink );
  312. $links[] = wfMsgHtml( 'ipblocklist-sh-tempblocks', $tbLink );
  313. $links[] = wfMsgHtml( 'ipblocklist-sh-addressblocks', $sipbLink );
  314. $hl = $wgLang->pipeList( $links );
  315. return
  316. Xml::tags( 'form', array( 'action' => $wgScript ),
  317. Xml::hidden( 'title', $wgTitle->getPrefixedDbKey() ) .
  318. Xml::openElement( 'fieldset' ) .
  319. Xml::element( 'legend', null, wfMsg( 'ipblocklist-legend' ) ) .
  320. Xml::inputLabel( wfMsg( 'ipblocklist-username' ), 'ip', 'ip', /* size */ false, $this->ip ) .
  321. '&nbsp;' .
  322. Xml::submitButton( wfMsg( 'ipblocklist-submit' ) ) . '<br />' .
  323. $hl .
  324. Xml::closeElement( 'fieldset' )
  325. );
  326. }
  327. /**
  328. * Makes change an option link which carries all the other options
  329. * @param $title see Title
  330. * @param $override
  331. * @param $options
  332. */
  333. function makeOptionsLink( $title, $override, $options, $active = false ) {
  334. global $wgUser;
  335. $sk = $wgUser->getSkin();
  336. $params = $override + $options;
  337. $ipblocklist = SpecialPage::getTitleFor( 'IPBlockList' );
  338. return $sk->link( $ipblocklist, htmlspecialchars( $title ),
  339. ( $active ? array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
  340. }
  341. /**
  342. * Callback function to output a block
  343. */
  344. function formatRow( $block ) {
  345. global $wgUser, $wgLang, $wgBlockAllowsUTEdit;
  346. wfProfileIn( __METHOD__ );
  347. static $sk=null, $msg=null;
  348. if( is_null( $sk ) )
  349. $sk = $wgUser->getSkin();
  350. if( is_null( $msg ) ) {
  351. $msg = array();
  352. $keys = array( 'infiniteblock', 'expiringblock', 'unblocklink', 'change-blocklink',
  353. 'anononlyblock', 'createaccountblock', 'noautoblockblock', 'emailblock', 'blocklist-nousertalk' );
  354. foreach( $keys as $key ) {
  355. $msg[$key] = wfMsgHtml( $key );
  356. }
  357. $msg['blocklistline'] = wfMsg( 'blocklistline' );
  358. }
  359. # Prepare links to the blocker's user and talk pages
  360. $blocker_id = $block->getBy();
  361. $blocker_name = $block->getByName();
  362. $blocker = $sk->userLink( $blocker_id, $blocker_name );
  363. $blocker .= $sk->userToolLinks( $blocker_id, $blocker_name );
  364. # Prepare links to the block target's user and contribs. pages (as applicable, don't do it for autoblocks)
  365. if( $block->mAuto ) {
  366. $target = $block->getRedactedName(); # Hide the IP addresses of auto-blocks; privacy
  367. } else {
  368. $target = $sk->userLink( $block->mUser, $block->mAddress )
  369. . $sk->userToolLinks( $block->mUser, $block->mAddress, false, Linker::TOOL_LINKS_NOBLOCK );
  370. }
  371. $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
  372. $properties = array();
  373. $properties[] = Block::formatExpiry( $block->mExpiry );
  374. if ( $block->mAnonOnly ) {
  375. $properties[] = $msg['anononlyblock'];
  376. }
  377. if ( $block->mCreateAccount ) {
  378. $properties[] = $msg['createaccountblock'];
  379. }
  380. if (!$block->mEnableAutoblock && $block->mUser ) {
  381. $properties[] = $msg['noautoblockblock'];
  382. }
  383. if ( $block->mBlockEmail && $block->mUser ) {
  384. $properties[] = $msg['emailblock'];
  385. }
  386. if ( !$block->mAllowUsertalk && $wgBlockAllowsUTEdit ) {
  387. $properties[] = $msg['blocklist-nousertalk'];
  388. }
  389. $properties = $wgLang->commaList( $properties );
  390. $line = wfMsgReplaceArgs( $msg['blocklistline'], array( $formattedTime, $blocker, $target, $properties ) );
  391. $unblocklink = '';
  392. $changeblocklink = '';
  393. $toolLinks = '';
  394. if ( $wgUser->isAllowed( 'block' ) ) {
  395. $unblocklink = $sk->link( SpecialPage::getTitleFor( 'Ipblocklist' ),
  396. $msg['unblocklink'],
  397. array(),
  398. array( 'action' => 'unblock', 'id' => $block->mId ),
  399. 'known' );
  400. # Create changeblocklink for all blocks with exception of autoblocks
  401. if( !$block->mAuto ) {
  402. $changeblocklink = wfMsg( 'pipe-separator' ) .
  403. $sk->link( SpecialPage::getTitleFor( 'Blockip', $block->mAddress ),
  404. $msg['change-blocklink'],
  405. array(), array(), 'known' );
  406. }
  407. $toolLinks = "($unblocklink$changeblocklink)";
  408. }
  409. $comment = $sk->commentBlock( $block->mReason );
  410. $s = "{$line} $comment";
  411. if ( $block->mHideName )
  412. $s = '<span class="history-deleted">' . $s . '</span>';
  413. wfProfileOut( __METHOD__ );
  414. return "<li>$s $toolLinks</li>\n";
  415. }
  416. }
  417. /**
  418. * @todo document
  419. * @ingroup Pager
  420. */
  421. class IPBlocklistPager extends ReverseChronologicalPager {
  422. public $mForm, $mConds;
  423. function __construct( $form, $conds = array() ) {
  424. $this->mForm = $form;
  425. $this->mConds = $conds;
  426. parent::__construct();
  427. }
  428. function getStartBody() {
  429. wfProfileIn( __METHOD__ );
  430. # Do a link batch query
  431. $this->mResult->seek( 0 );
  432. $lb = new LinkBatch;
  433. /*
  434. while ( $row = $this->mResult->fetchObject() ) {
  435. $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
  436. $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
  437. $lb->addObj( Title::makeTitleSafe( NS_USER, $row->ipb_address ) );
  438. $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ipb_address ) );
  439. }*/
  440. # Faster way
  441. # Usernames and titles are in fact related by a simple substitution of space -> underscore
  442. # The last few lines of Title::secureAndSplit() tell the story.
  443. while ( $row = $this->mResult->fetchObject() ) {
  444. $name = str_replace( ' ', '_', $row->ipb_by_text );
  445. $lb->add( NS_USER, $name );
  446. $lb->add( NS_USER_TALK, $name );
  447. $name = str_replace( ' ', '_', $row->ipb_address );
  448. $lb->add( NS_USER, $name );
  449. $lb->add( NS_USER_TALK, $name );
  450. }
  451. $lb->execute();
  452. wfProfileOut( __METHOD__ );
  453. return '';
  454. }
  455. function formatRow( $row ) {
  456. $block = new Block;
  457. $block->initFromRow( $row );
  458. return $this->mForm->formatRow( $block );
  459. }
  460. function getQueryInfo() {
  461. $conds = $this->mConds;
  462. $conds[] = 'ipb_expiry>' . $this->mDb->addQuotes( $this->mDb->timestamp() );
  463. return array(
  464. 'tables' => 'ipblocks',
  465. 'fields' => '*',
  466. 'conds' => $conds,
  467. );
  468. }
  469. function getIndexField() {
  470. return 'ipb_timestamp';
  471. }
  472. }