SpecialBlockip.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. <?php
  2. /**
  3. * Constructor for Special:Blockip page
  4. *
  5. * @file
  6. * @ingroup SpecialPage
  7. */
  8. /**
  9. * Constructor
  10. */
  11. function wfSpecialBlockip( $par ) {
  12. global $wgUser, $wgOut, $wgRequest;
  13. # Can't block when the database is locked
  14. if( wfReadOnly() ) {
  15. $wgOut->readOnlyPage();
  16. return;
  17. }
  18. # Permission check
  19. if( !$wgUser->isAllowed( 'block' ) ) {
  20. $wgOut->permissionRequired( 'block' );
  21. return;
  22. }
  23. $ipb = new IPBlockForm( $par );
  24. $action = $wgRequest->getVal( 'action' );
  25. if ( 'success' == $action ) {
  26. $ipb->showSuccess();
  27. } else if ( $wgRequest->wasPosted() && 'submit' == $action &&
  28. $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
  29. $ipb->doSubmit();
  30. } else {
  31. $ipb->showForm( '' );
  32. }
  33. }
  34. /**
  35. * Form object for the Special:Blockip page.
  36. *
  37. * @ingroup SpecialPage
  38. */
  39. class IPBlockForm {
  40. var $BlockAddress, $BlockExpiry, $BlockReason;
  41. # var $BlockEmail;
  42. // The maximum number of edits a user can have and still be hidden
  43. const HIDEUSER_CONTRIBLIMIT = 1000;
  44. function IPBlockForm( $par ) {
  45. global $wgRequest, $wgUser, $wgBlockAllowsUTEdit;
  46. $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip', $par ) );
  47. $this->BlockAddress = strtr( $this->BlockAddress, '_', ' ' );
  48. $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
  49. $this->BlockReasonList = $wgRequest->getText( 'wpBlockReasonList' );
  50. $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry', wfMsg('ipbotheroption') );
  51. $this->BlockOther = $wgRequest->getVal( 'wpBlockOther', '' );
  52. # Unchecked checkboxes are not included in the form data at all, so having one
  53. # that is true by default is a bit tricky
  54. $byDefault = !$wgRequest->wasPosted();
  55. $this->BlockAnonOnly = $wgRequest->getBool( 'wpAnonOnly', $byDefault );
  56. $this->BlockCreateAccount = $wgRequest->getBool( 'wpCreateAccount', $byDefault );
  57. $this->BlockEnableAutoblock = $wgRequest->getBool( 'wpEnableAutoblock', $byDefault );
  58. $this->BlockEmail = $wgRequest->getBool( 'wpEmailBan', false );
  59. $this->BlockWatchUser = $wgRequest->getBool( 'wpWatchUser', false );
  60. # Re-check user's rights to hide names, very serious, defaults to 0
  61. $this->BlockHideName = ( $wgRequest->getBool( 'wpHideName', 0 ) && $wgUser->isAllowed( 'hideuser' ) ) ? 1 : 0;
  62. $this->BlockAllowUsertalk = ( $wgRequest->getBool( 'wpAllowUsertalk', $byDefault ) && $wgBlockAllowsUTEdit );
  63. $this->BlockReblock = $wgRequest->getBool( 'wpChangeBlock', false );
  64. }
  65. function showForm( $err ) {
  66. global $wgOut, $wgUser, $wgSysopUserBans;
  67. $wgOut->setPagetitle( wfMsg( 'blockip' ) );
  68. $wgOut->addWikiMsg( 'blockiptext' );
  69. if($wgSysopUserBans) {
  70. $mIpaddress = Xml::label( wfMsg( 'ipadressorusername' ), 'mw-bi-target' );
  71. } else {
  72. $mIpaddress = Xml::label( wfMsg( 'ipaddress' ), 'mw-bi-target' );
  73. }
  74. $mIpbexpiry = Xml::label( wfMsg( 'ipbexpiry' ), 'wpBlockExpiry' );
  75. $mIpbother = Xml::label( wfMsg( 'ipbother' ), 'mw-bi-other' );
  76. $mIpbreasonother = Xml::label( wfMsg( 'ipbreason' ), 'wpBlockReasonList' );
  77. $mIpbreason = Xml::label( wfMsg( 'ipbotherreason' ), 'mw-bi-reason' );
  78. $titleObj = SpecialPage::getTitleFor( 'Blockip' );
  79. $user = User::newFromName( $this->BlockAddress );
  80. $alreadyBlocked = false;
  81. if ( $err && $err[0] != 'ipb_already_blocked' ) {
  82. $key = array_shift($err);
  83. $msg = wfMsgReal($key, $err);
  84. $wgOut->setSubtitle( wfMsgHtml( 'formerror' ) );
  85. $wgOut->addHTML( Xml::tags( 'p', array( 'class' => 'error' ), $msg ) );
  86. } elseif ( $this->BlockAddress ) {
  87. $userId = 0;
  88. if ( is_object( $user ) )
  89. $userId = $user->getId();
  90. $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
  91. if ( !is_null($currentBlock) && !$currentBlock->mAuto && # The block exists and isn't an autoblock
  92. ( $currentBlock->mRangeStart == $currentBlock->mRangeEnd || # The block isn't a rangeblock
  93. # or if it is, the range is what we're about to block
  94. ( $currentBlock->mAddress == $this->BlockAddress ) ) ) {
  95. $wgOut->addWikiMsg( 'ipb-needreblock', $this->BlockAddress );
  96. $alreadyBlocked = true;
  97. # Set the block form settings to the existing block
  98. $this->BlockAnonOnly = $currentBlock->mAnonOnly;
  99. $this->BlockCreateAccount = $currentBlock->mCreateAccount;
  100. $this->BlockEnableAutoblock = $currentBlock->mEnableAutoblock;
  101. $this->BlockEmail = $currentBlock->mBlockEmail;
  102. $this->BlockHideName = $currentBlock->mHideName;
  103. $this->BlockAllowUsertalk = $currentBlock->mAllowUsertalk;
  104. if( $currentBlock->mExpiry == 'infinity' ) {
  105. $this->BlockOther = 'indefinite';
  106. } else {
  107. $this->BlockOther = wfTimestamp( TS_ISO_8601, $currentBlock->mExpiry );
  108. }
  109. $this->BlockReason = $currentBlock->mReason;
  110. }
  111. }
  112. $scBlockExpiryOptions = wfMsgForContent( 'ipboptions' );
  113. $showblockoptions = $scBlockExpiryOptions != '-';
  114. if (!$showblockoptions)
  115. $mIpbother = $mIpbexpiry;
  116. $blockExpiryFormOptions = Xml::option( wfMsg( 'ipbotheroption' ), 'other' );
  117. foreach (explode(',', $scBlockExpiryOptions) as $option) {
  118. if ( strpos($option, ":") === false ) $option = "$option:$option";
  119. list($show, $value) = explode(":", $option);
  120. $show = htmlspecialchars($show);
  121. $value = htmlspecialchars($value);
  122. $blockExpiryFormOptions .= Xml::option( $show, $value, $this->BlockExpiry === $value ? true : false ) . "\n";
  123. }
  124. $reasonDropDown = Xml::listDropDown( 'wpBlockReasonList',
  125. wfMsgForContent( 'ipbreason-dropdown' ),
  126. wfMsgForContent( 'ipbreasonotherlist' ), $this->BlockReasonList, 'wpBlockDropDown', 4 );
  127. global $wgStylePath, $wgStyleVersion;
  128. $wgOut->addHTML(
  129. Xml::tags( 'script', array( 'type' => 'text/javascript', 'src' => "$wgStylePath/common/block.js?$wgStyleVersion" ), '' ) .
  130. Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( "action=submit" ), 'id' => 'blockip' ) ) .
  131. Xml::openElement( 'fieldset' ) .
  132. Xml::element( 'legend', null, wfMsg( 'blockip-legend' ) ) .
  133. Xml::openElement( 'table', array ( 'border' => '0', 'id' => 'mw-blockip-table' ) ) .
  134. "<tr>
  135. <td class='mw-label'>
  136. {$mIpaddress}
  137. </td>
  138. <td class='mw-input'>" .
  139. Xml::input( 'wpBlockAddress', 45, $this->BlockAddress,
  140. array(
  141. 'tabindex' => '1',
  142. 'id' => 'mw-bi-target',
  143. 'onchange' => 'updateBlockOptions()' ) ). "
  144. </td>
  145. </tr>
  146. <tr>"
  147. );
  148. if ( $showblockoptions ) {
  149. $wgOut->addHTML("
  150. <td class='mw-label'>
  151. {$mIpbexpiry}
  152. </td>
  153. <td class='mw-input'>" .
  154. Xml::tags( 'select',
  155. array(
  156. 'id' => 'wpBlockExpiry',
  157. 'name' => 'wpBlockExpiry',
  158. 'onchange' => 'considerChangingExpiryFocus()',
  159. 'tabindex' => '2' ),
  160. $blockExpiryFormOptions ) .
  161. "</td>"
  162. );
  163. }
  164. $wgOut->addHTML("
  165. </tr>
  166. <tr id='wpBlockOther'>
  167. <td class='mw-label'>
  168. {$mIpbother}
  169. </td>
  170. <td class='mw-input'>" .
  171. Xml::input( 'wpBlockOther', 45, $this->BlockOther,
  172. array( 'tabindex' => '3', 'id' => 'mw-bi-other' ) ) . "
  173. </td>
  174. </tr>
  175. <tr>
  176. <td class='mw-label'>
  177. {$mIpbreasonother}
  178. </td>
  179. <td class='mw-input'>
  180. {$reasonDropDown}
  181. </td>
  182. </tr>
  183. <tr id=\"wpBlockReason\">
  184. <td class='mw-label'>
  185. {$mIpbreason}
  186. </td>
  187. <td class='mw-input'>" .
  188. Xml::input( 'wpBlockReason', 45, $this->BlockReason,
  189. array( 'tabindex' => '5', 'id' => 'mw-bi-reason', 'maxlength'=> '200' ) ) . "
  190. </td>
  191. </tr>
  192. <tr id='wpAnonOnlyRow'>
  193. <td>&nbsp;</td>
  194. <td class='mw-input'>" .
  195. Xml::checkLabel( wfMsg( 'ipbanononly' ),
  196. 'wpAnonOnly', 'wpAnonOnly', $this->BlockAnonOnly,
  197. array( 'tabindex' => '6' ) ) . "
  198. </td>
  199. </tr>
  200. <tr id='wpCreateAccountRow'>
  201. <td>&nbsp;</td>
  202. <td class='mw-input'>" .
  203. Xml::checkLabel( wfMsg( 'ipbcreateaccount' ),
  204. 'wpCreateAccount', 'wpCreateAccount', $this->BlockCreateAccount,
  205. array( 'tabindex' => '7' ) ) . "
  206. </td>
  207. </tr>
  208. <tr id='wpEnableAutoblockRow'>
  209. <td>&nbsp;</td>
  210. <td class='mw-input'>" .
  211. Xml::checkLabel( wfMsg( 'ipbenableautoblock' ),
  212. 'wpEnableAutoblock', 'wpEnableAutoblock', $this->BlockEnableAutoblock,
  213. array( 'tabindex' => '8' ) ) . "
  214. </td>
  215. </tr>"
  216. );
  217. global $wgSysopEmailBans, $wgBlockAllowsUTEdit;
  218. if ( $wgSysopEmailBans && $wgUser->isAllowed( 'blockemail' ) ) {
  219. $wgOut->addHTML("
  220. <tr id='wpEnableEmailBan'>
  221. <td>&nbsp;</td>
  222. <td class='mw-input'>" .
  223. Xml::checkLabel( wfMsg( 'ipbemailban' ),
  224. 'wpEmailBan', 'wpEmailBan', $this->BlockEmail,
  225. array( 'tabindex' => '9' )) . "
  226. </td>
  227. </tr>"
  228. );
  229. }
  230. // Allow some users to hide name from block log, blocklist and listusers
  231. if ( $wgUser->isAllowed( 'hideuser' ) ) {
  232. $wgOut->addHTML("
  233. <tr id='wpEnableHideUser'>
  234. <td>&nbsp;</td>
  235. <td class='mw-input'><strong>" .
  236. Xml::checkLabel( wfMsg( 'ipbhidename' ),
  237. 'wpHideName', 'wpHideName', $this->BlockHideName,
  238. array( 'tabindex' => '10' ) ) . "
  239. </strong></td>
  240. </tr>"
  241. );
  242. }
  243. # Watchlist their user page?
  244. $wgOut->addHTML("
  245. <tr id='wpEnableWatchUser'>
  246. <td>&nbsp;</td>
  247. <td class='mw-input'>" .
  248. Xml::checkLabel( wfMsg( 'ipbwatchuser' ),
  249. 'wpWatchUser', 'wpWatchUser', $this->BlockWatchUser,
  250. array( 'tabindex' => '11' ) ) . "
  251. </td>
  252. </tr>"
  253. );
  254. if( $wgBlockAllowsUTEdit ){
  255. $wgOut->addHTML("
  256. <tr id='wpAllowUsertalkRow'>
  257. <td>&nbsp;</td>
  258. <td class='mw-input'>" .
  259. Xml::checkLabel( wfMsg( 'ipballowusertalk' ),
  260. 'wpAllowUsertalk', 'wpAllowUsertalk', $this->BlockAllowUsertalk,
  261. array( 'tabindex' => '12' ) ) . "
  262. </td>
  263. </tr>"
  264. );
  265. }
  266. $wgOut->addHTML("
  267. <tr>
  268. <td style='padding-top: 1em'>&nbsp;</td>
  269. <td class='mw-submit' style='padding-top: 1em'>" .
  270. Xml::submitButton( wfMsg( $alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit' ),
  271. array( 'name' => 'wpBlock', 'tabindex' => '13', 'accesskey' => 's' ) ) . "
  272. </td>
  273. </tr>" .
  274. Xml::closeElement( 'table' ) .
  275. Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
  276. ( $alreadyBlocked ? Xml::hidden( 'wpChangeBlock', 1 ) : "" ) .
  277. Xml::closeElement( 'fieldset' ) .
  278. Xml::closeElement( 'form' ) .
  279. Xml::tags( 'script', array( 'type' => 'text/javascript' ), 'updateBlockOptions()' ) . "\n"
  280. );
  281. $wgOut->addHTML( $this->getConvenienceLinks() );
  282. if( is_object( $user ) ) {
  283. $this->showLogFragment( $wgOut, $user->getUserPage() );
  284. } elseif( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $this->BlockAddress ) ) {
  285. $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
  286. } elseif( preg_match( '/^\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}/', $this->BlockAddress ) ) {
  287. $this->showLogFragment( $wgOut, Title::makeTitle( NS_USER, $this->BlockAddress ) );
  288. }
  289. }
  290. /**
  291. * Backend block code.
  292. * $userID and $expiry will be filled accordingly
  293. * @return array(message key, arguments) on failure, empty array on success
  294. */
  295. function doBlock( &$userId = null, &$expiry = null ) {
  296. global $wgUser, $wgSysopUserBans, $wgSysopRangeBans, $wgBlockAllowsUTEdit;
  297. $userId = 0;
  298. # Expand valid IPv6 addresses, usernames are left as is
  299. $this->BlockAddress = IP::sanitizeIP( $this->BlockAddress );
  300. # isIPv4() and IPv6() are used for final validation
  301. $rxIP4 = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
  302. $rxIP6 = '\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}:\w{1,4}';
  303. $rxIP = "($rxIP4|$rxIP6)";
  304. # Check for invalid specifications
  305. if ( !preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
  306. $matches = array();
  307. if ( preg_match( "/^($rxIP4)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
  308. # IPv4
  309. if ( $wgSysopRangeBans ) {
  310. if ( !IP::isIPv4( $this->BlockAddress ) || $matches[2] < 16 || $matches[2] > 32 ) {
  311. return array('ip_range_invalid');
  312. }
  313. $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
  314. } else {
  315. # Range block illegal
  316. return array('range_block_disabled');
  317. }
  318. } else if ( preg_match( "/^($rxIP6)\\/(\\d{1,3})$/", $this->BlockAddress, $matches ) ) {
  319. # IPv6
  320. if ( $wgSysopRangeBans ) {
  321. if ( !IP::isIPv6( $this->BlockAddress ) || $matches[2] < 64 || $matches[2] > 128 ) {
  322. return array('ip_range_invalid');
  323. }
  324. $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
  325. } else {
  326. # Range block illegal
  327. return array('range_block_disabled');
  328. }
  329. } else {
  330. # Username block
  331. if ( $wgSysopUserBans ) {
  332. $user = User::newFromName( $this->BlockAddress );
  333. if( !is_null( $user ) && $user->getId() ) {
  334. # Use canonical name
  335. $userId = $user->getId();
  336. $this->BlockAddress = $user->getName();
  337. } else {
  338. return array('nosuchusershort', htmlspecialchars( $user ? $user->getName() : $this->BlockAddress ) );
  339. }
  340. } else {
  341. return array('badipaddress');
  342. }
  343. }
  344. }
  345. if ( $wgUser->isBlocked() && ( $wgUser->getId() !== $userId ) ) {
  346. return array( 'cant-block-while-blocked' );
  347. }
  348. $reasonstr = $this->BlockReasonList;
  349. if ( $reasonstr != 'other' && $this->BlockReason != '' ) {
  350. // Entry from drop down menu + additional comment
  351. $reasonstr .= wfMsgForContent( 'colon-separator' ) . $this->BlockReason;
  352. } elseif ( $reasonstr == 'other' ) {
  353. $reasonstr = $this->BlockReason;
  354. }
  355. $expirestr = $this->BlockExpiry;
  356. if( $expirestr == 'other' )
  357. $expirestr = $this->BlockOther;
  358. if ( ( strlen( $expirestr ) == 0) || ( strlen( $expirestr ) > 50) ) {
  359. return array('ipb_expiry_invalid');
  360. }
  361. if ( false === ($expiry = Block::parseExpiryInput( $expirestr )) ) {
  362. // Bad expiry.
  363. return array('ipb_expiry_invalid');
  364. }
  365. if( $this->BlockHideName ) {
  366. if( !$userId ) {
  367. // IP users should not be hidden
  368. $this->BlockHideName = false;
  369. } else if( $expiry !== 'infinity' ) {
  370. // Bad expiry.
  371. return array('ipb_expiry_temp');
  372. } else if( User::edits($userId) > self::HIDEUSER_CONTRIBLIMIT ) {
  373. // Typically, the user should have a handful of edits.
  374. // Disallow hiding users with many edits for performance.
  375. return array('ipb_hide_invalid');
  376. }
  377. }
  378. # Create block
  379. # Note: for a user block, ipb_address is only for display purposes
  380. $block = new Block( $this->BlockAddress, $userId, $wgUser->getId(),
  381. $reasonstr, wfTimestampNow(), 0, $expiry, $this->BlockAnonOnly,
  382. $this->BlockCreateAccount, $this->BlockEnableAutoblock, $this->BlockHideName,
  383. $this->BlockEmail, isset( $this->BlockAllowUsertalk ) ? $this->BlockAllowUsertalk : $wgBlockAllowsUTEdit
  384. );
  385. # Should this be privately logged?
  386. $suppressLog = (bool)$this->BlockHideName;
  387. if ( wfRunHooks('BlockIp', array(&$block, &$wgUser)) ) {
  388. # Try to insert block. Is there a conflicting block?
  389. if ( !$block->insert() ) {
  390. # Show form unless the user is already aware of this...
  391. if ( !$this->BlockReblock ) {
  392. return array( 'ipb_already_blocked' );
  393. # Otherwise, try to update the block...
  394. } else {
  395. # This returns direct blocks before autoblocks/rangeblocks, since we should
  396. # be sure the user is blocked by now it should work for our purposes
  397. $currentBlock = Block::newFromDB( $this->BlockAddress, $userId );
  398. if( $block->equals( $currentBlock ) ) {
  399. return array( 'ipb_already_blocked' );
  400. }
  401. # If the name was hidden and the blocking user cannot hide
  402. # names, then don't allow any block changes...
  403. if( $currentBlock->mHideName && !$wgUser->isAllowed('hideuser') ) {
  404. return array( 'hookaborted' );
  405. }
  406. $currentBlock->delete();
  407. $block->insert();
  408. # If hiding/unhiding a name, this should go in the private logs
  409. $suppressLog = $suppressLog || (bool)$currentBlock->mHideName;
  410. $log_action = 'reblock';
  411. # Unset _deleted fields if requested
  412. if( $currentBlock->mHideName && !$this->BlockHideName ) {
  413. self::unsuppressUserName( $this->BlockAddress, $userId );
  414. }
  415. }
  416. } else {
  417. $log_action = 'block';
  418. }
  419. wfRunHooks('BlockIpComplete', array($block, $wgUser));
  420. # Set *_deleted fields if requested
  421. if( $this->BlockHideName ) {
  422. self::suppressUserName( $this->BlockAddress, $userId );
  423. }
  424. if ( $this->BlockWatchUser &&
  425. # Only show watch link when this is no range block
  426. $block->mRangeStart == $block->mRangeEnd) {
  427. $wgUser->addWatch ( Title::makeTitle( NS_USER, $this->BlockAddress ) );
  428. }
  429. # Block constructor sanitizes certain block options on insert
  430. $this->BlockEmail = $block->mBlockEmail;
  431. $this->BlockEnableAutoblock = $block->mEnableAutoblock;
  432. # Prepare log parameters
  433. $logParams = array();
  434. $logParams[] = $expirestr;
  435. $logParams[] = $this->blockLogFlags();
  436. # Make log entry, if the name is hidden, put it in the oversight log
  437. $log_type = $suppressLog ? 'suppress' : 'block';
  438. $log = new LogPage( $log_type );
  439. $log->addEntry( $log_action, Title::makeTitle( NS_USER, $this->BlockAddress ),
  440. $reasonstr, $logParams );
  441. # Report to the user
  442. return array();
  443. } else {
  444. return array('hookaborted');
  445. }
  446. }
  447. public static function suppressUserName( $name, $userId ) {
  448. $op = '|'; // bitwise OR
  449. return self::setUsernameBitfields( $name, $userId, $op );
  450. }
  451. public static function unsuppressUserName( $name, $userId ) {
  452. $op = '&'; // bitwise AND
  453. return self::setUsernameBitfields( $name, $userId, $op );
  454. }
  455. private static function setUsernameBitfields( $name, $userId, $op ) {
  456. if( $op !== '|' && $op !== '&' )
  457. return false; // sanity check
  458. $dbw = wfGetDB( DB_MASTER );
  459. $delUser = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
  460. $delAction = LogPage::DELETED_ACTION | Revision::DELETED_RESTRICTED;
  461. # Normalize user name
  462. $userTitle = Title::makeTitleSafe( NS_USER, $name );
  463. $userDbKey = $userTitle->getDBKey();
  464. # To suppress, we OR the current bitfields with Revision::DELETED_USER
  465. # to put a 1 in the username *_deleted bit. To unsuppress we AND the
  466. # current bitfields with the inverse of Revision::DELETED_USER. The
  467. # username bit is made to 0 (x & 0 = 0), while others are unchanged (x & 1 = x).
  468. # The same goes for the sysop-restricted *_deleted bit.
  469. if( $op == '&' ) {
  470. $delUser = "~{$delUser}";
  471. $delAction = "~{$delAction}";
  472. }
  473. # Hide name from live edits
  474. $dbw->update( 'revision', array("rev_deleted = rev_deleted $op $delUser"),
  475. array('rev_user' => $userId), __METHOD__ );
  476. # Hide name from deleted edits
  477. $dbw->update( 'archive', array("ar_deleted = ar_deleted $op $delUser"),
  478. array('ar_user_text' => $name), __METHOD__ );
  479. # Hide name from logs
  480. $dbw->update( 'logging', array("log_deleted = log_deleted $op $delUser"),
  481. array('log_user' => $userId, "log_type != 'suppress'"), __METHOD__ );
  482. $dbw->update( 'logging', array("log_deleted = log_deleted $op $delAction"),
  483. array('log_namespace' => NS_USER, 'log_title' => $userDbKey,
  484. "log_type != 'suppress'"), __METHOD__ );
  485. # Hide name from RC
  486. $dbw->update( 'recentchanges', array("rc_deleted = rc_deleted $op $delUser"),
  487. array('rc_user_text' => $name), __METHOD__ );
  488. # Hide name from live images
  489. $dbw->update( 'oldimage', array("oi_deleted = oi_deleted $op $delUser"),
  490. array('oi_user_text' => $name), __METHOD__ );
  491. # Hide name from deleted images
  492. # WMF - schema change pending
  493. # $dbw->update( 'filearchive', array("fa_deleted = fa_deleted $op $delUser"),
  494. # array('fa_user_text' => $name), __METHOD__ );
  495. # Done!
  496. return true;
  497. }
  498. /**
  499. * UI entry point for blocking
  500. * Wraps around doBlock()
  501. */
  502. function doSubmit()
  503. {
  504. global $wgOut;
  505. $retval = $this->doBlock();
  506. if(empty($retval)) {
  507. $titleObj = SpecialPage::getTitleFor( 'Blockip' );
  508. $wgOut->redirect( $titleObj->getFullURL( 'action=success&ip=' .
  509. urlencode( $this->BlockAddress ) ) );
  510. return;
  511. }
  512. $this->showForm( $retval );
  513. }
  514. function showSuccess() {
  515. global $wgOut;
  516. $wgOut->setPagetitle( wfMsg( 'blockip' ) );
  517. $wgOut->setSubtitle( wfMsg( 'blockipsuccesssub' ) );
  518. $text = wfMsgExt( 'blockipsuccesstext', array( 'parse' ), $this->BlockAddress );
  519. $wgOut->addHTML( $text );
  520. }
  521. function showLogFragment( $out, $title ) {
  522. global $wgUser;
  523. $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'block' ) ) );
  524. $count = LogEventsList::showLogExtract( $out, 'block', $title->getPrefixedText(), '', 10 );
  525. if($count > 10){
  526. $out->addHTML( $wgUser->getSkin()->link(
  527. SpecialPage::getTitleFor( 'Log' ),
  528. wfMsgHtml( 'blocklog-fulllog' ),
  529. array(),
  530. array(
  531. 'type' => 'block',
  532. 'page' => $title->getPrefixedText() ) ) );
  533. }
  534. }
  535. /**
  536. * Return a comma-delimited list of "flags" to be passed to the log
  537. * reader for this block, to provide more information in the logs
  538. *
  539. * @return array
  540. */
  541. private function blockLogFlags() {
  542. global $wgBlockAllowsUTEdit;
  543. $flags = array();
  544. if( $this->BlockAnonOnly && IP::isIPAddress( $this->BlockAddress ) )
  545. // when blocking a user the option 'anononly' is not available/has no effect -> do not write this into log
  546. $flags[] = 'anononly';
  547. if( $this->BlockCreateAccount )
  548. $flags[] = 'nocreate';
  549. if( !$this->BlockEnableAutoblock )
  550. $flags[] = 'noautoblock';
  551. if ( $this->BlockEmail )
  552. $flags[] = 'noemail';
  553. if ( !$this->BlockAllowUsertalk && $wgBlockAllowsUTEdit )
  554. $flags[] = 'nousertalk';
  555. if ( $this->BlockHideName )
  556. $flags[] = 'hiddenname';
  557. return implode( ',', $flags );
  558. }
  559. /**
  560. * Builds unblock and block list links
  561. *
  562. * @return string
  563. */
  564. private function getConvenienceLinks() {
  565. global $wgUser, $wgLang;
  566. $skin = $wgUser->getSkin();
  567. if( $this->BlockAddress )
  568. $links[] = $this->getContribsLink( $skin );
  569. $links[] = $this->getUnblockLink( $skin );
  570. $links[] = $this->getBlockListLink( $skin );
  571. $links[] = $skin->makeLink ( 'MediaWiki:Ipbreason-dropdown', wfMsgHtml( 'ipb-edit-dropdown' ) );
  572. return '<p class="mw-ipb-conveniencelinks">' . $wgLang->pipeList( $links ) . '</p>';
  573. }
  574. /**
  575. * Build a convenient link to a user or IP's contribs
  576. * form
  577. *
  578. * @param $skin Skin to use
  579. * @return string
  580. */
  581. private function getContribsLink( $skin ) {
  582. $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->BlockAddress );
  583. return $skin->link( $contribsPage, wfMsgExt( 'ipb-blocklist-contribs', 'escape', $this->BlockAddress ) );
  584. }
  585. /**
  586. * Build a convenient link to unblock the given username or IP
  587. * address, if available; otherwise link to a blank unblock
  588. * form
  589. *
  590. * @param $skin Skin to use
  591. * @return string
  592. */
  593. private function getUnblockLink( $skin ) {
  594. $list = SpecialPage::getTitleFor( 'Ipblocklist' );
  595. if( $this->BlockAddress ) {
  596. $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
  597. return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock-addr', $addr ),
  598. 'action=unblock&ip=' . urlencode( $this->BlockAddress ) );
  599. } else {
  600. return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-unblock' ), 'action=unblock' );
  601. }
  602. }
  603. /**
  604. * Build a convenience link to the block list
  605. *
  606. * @param $skin Skin to use
  607. * @return string
  608. */
  609. private function getBlockListLink( $skin ) {
  610. $list = SpecialPage::getTitleFor( 'Ipblocklist' );
  611. if( $this->BlockAddress ) {
  612. $addr = htmlspecialchars( strtr( $this->BlockAddress, '_', ' ' ) );
  613. return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist-addr', $addr ),
  614. 'ip=' . urlencode( $this->BlockAddress ) );
  615. } else {
  616. return $skin->makeKnownLinkObj( $list, wfMsgHtml( 'ipb-blocklist' ) );
  617. }
  618. }
  619. /**
  620. * Block a list of selected users
  621. * @param array $users
  622. * @param string $reason
  623. * @param string $tag replaces user pages
  624. * @param string $talkTag replaces user talk pages
  625. * @returns array, list of html-safe usernames
  626. */
  627. public static function doMassUserBlock( $users, $reason = '', $tag = '', $talkTag = '' ) {
  628. global $wgUser;
  629. $counter = $blockSize = 0;
  630. $safeUsers = array();
  631. $log = new LogPage( 'block' );
  632. foreach( $users as $name ) {
  633. # Enforce limits
  634. $counter++;
  635. $blockSize++;
  636. # Lets not go *too* fast
  637. if( $blockSize >= 20 ) {
  638. $blockSize = 0;
  639. wfWaitForSlaves( 5 );
  640. }
  641. $u = User::newFromName( $name, false );
  642. // If user doesn't exist, it ought to be an IP then
  643. if( is_null($u) || (!$u->getId() && !IP::isIPAddress( $u->getName() )) ) {
  644. continue;
  645. }
  646. $userTitle = $u->getUserPage();
  647. $userTalkTitle = $u->getTalkPage();
  648. $userpage = new Article( $userTitle );
  649. $usertalk = new Article( $userTalkTitle );
  650. $safeUsers[] = '[[' . $userTitle->getPrefixedText() . '|' . $userTitle->getText() . ']]';
  651. $expirestr = $u->getId() ? 'indefinite' : '1 week';
  652. $expiry = Block::parseExpiryInput( $expirestr );
  653. $anonOnly = IP::isIPAddress( $u->getName() ) ? 1 : 0;
  654. // Create the block
  655. $block = new Block( $u->getName(), // victim
  656. $u->getId(), // uid
  657. $wgUser->getId(), // blocker
  658. $reason, // comment
  659. wfTimestampNow(), // block time
  660. 0, // auto ?
  661. $expiry, // duration
  662. $anonOnly, // anononly?
  663. 1, // block account creation?
  664. 1, // autoblocking?
  665. 0, // suppress name?
  666. 0 // block from sending email?
  667. );
  668. $oldblock = Block::newFromDB( $u->getName(), $u->getId() );
  669. if( !$oldblock ) {
  670. $block->insert();
  671. # Prepare log parameters
  672. $logParams = array();
  673. $logParams[] = $expirestr;
  674. if( $anonOnly ) {
  675. $logParams[] = 'anononly';
  676. }
  677. $logParams[] = 'nocreate';
  678. # Add log entry
  679. $log->addEntry( 'block', $userTitle, $reason, $logParams );
  680. }
  681. # Tag userpage! (check length to avoid mistakes)
  682. if( strlen($tag) > 2 ) {
  683. $userpage->doEdit( $tag, $reason, EDIT_MINOR );
  684. }
  685. if( strlen($talkTag) > 2 ) {
  686. $usertalk->doEdit( $talkTag, $reason, EDIT_MINOR );
  687. }
  688. }
  689. return $safeUsers;
  690. }
  691. }