ApiBlock.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\Block\DatabaseBlock;
  23. /**
  24. * API module that facilitates the blocking of users. Requires API write mode
  25. * to be enabled.
  26. *
  27. * @ingroup API
  28. */
  29. class ApiBlock extends ApiBase {
  30. use ApiBlockInfoTrait;
  31. /**
  32. * Blocks the user specified in the parameters for the given expiry, with the
  33. * given reason, and with all other settings provided in the params. If the block
  34. * succeeds, produces a result containing the details of the block and notice
  35. * of success. If it fails, the result will specify the nature of the error.
  36. */
  37. public function execute() {
  38. $this->checkUserRightsAny( 'block' );
  39. $user = $this->getUser();
  40. $params = $this->extractRequestParams();
  41. $this->requireOnlyOneParameter( $params, 'user', 'userid' );
  42. # T17810: blocked admins should have limited access here
  43. $block = $user->getBlock();
  44. if ( $block ) {
  45. $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
  46. if ( $status !== true ) {
  47. $this->dieWithError(
  48. $status,
  49. null,
  50. [ 'blockinfo' => $this->getBlockDetails( $block ) ]
  51. );
  52. }
  53. }
  54. $editingRestriction = 'sitewide';
  55. $pageRestrictions = '';
  56. $namespaceRestrictions = '';
  57. if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
  58. if ( $params['partial'] ) {
  59. $editingRestriction = 'partial';
  60. }
  61. $pageRestrictions = implode( "\n", (array)$params['pagerestrictions'] );
  62. $namespaceRestrictions = implode( "\n", (array)$params['namespacerestrictions'] );
  63. }
  64. if ( $params['userid'] !== null ) {
  65. $username = User::whoIs( $params['userid'] );
  66. if ( $username === false ) {
  67. $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
  68. } else {
  69. $params['user'] = $username;
  70. }
  71. } else {
  72. list( $target, $type ) = SpecialBlock::getTargetAndType( $params['user'] );
  73. // T40633 - if the target is a user (not an IP address), but it
  74. // doesn't exist or is unusable, error.
  75. if ( $type === DatabaseBlock::TYPE_USER &&
  76. ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $params['user'] ) )
  77. ) {
  78. $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
  79. }
  80. }
  81. if ( $params['tags'] ) {
  82. $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
  83. if ( !$ableToTag->isOK() ) {
  84. $this->dieStatus( $ableToTag );
  85. }
  86. }
  87. if ( $params['hidename'] &&
  88. !$this->getPermissionManager()->userHasRight( $user, 'hideuser' ) ) {
  89. $this->dieWithError( 'apierror-canthide' );
  90. }
  91. if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
  92. $this->dieWithError( 'apierror-cantblock-email' );
  93. }
  94. $data = [
  95. 'PreviousTarget' => $params['user'],
  96. 'Target' => $params['user'],
  97. 'Reason' => [
  98. $params['reason'],
  99. 'other',
  100. $params['reason']
  101. ],
  102. 'Expiry' => $params['expiry'],
  103. 'HardBlock' => !$params['anononly'],
  104. 'CreateAccount' => $params['nocreate'],
  105. 'AutoBlock' => $params['autoblock'],
  106. 'DisableEmail' => $params['noemail'],
  107. 'HideUser' => $params['hidename'],
  108. 'DisableUTEdit' => !$params['allowusertalk'],
  109. 'Reblock' => $params['reblock'],
  110. 'Watch' => $params['watchuser'],
  111. 'Confirm' => true,
  112. 'Tags' => $params['tags'],
  113. 'EditingRestriction' => $editingRestriction,
  114. 'PageRestrictions' => $pageRestrictions,
  115. 'NamespaceRestrictions' => $namespaceRestrictions,
  116. ];
  117. $status = SpecialBlock::validateTarget( $params['user'], $user );
  118. if ( !$status->isOK() ) {
  119. $this->dieStatus( $status );
  120. }
  121. $retval = SpecialBlock::processForm( $data, $this->getContext() );
  122. if ( $retval !== true ) {
  123. $this->dieStatus( $this->errorArrayToStatus( $retval ) );
  124. }
  125. $res = [];
  126. $res['user'] = $params['user'];
  127. list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
  128. $res['userID'] = $target instanceof User ? $target->getId() : 0;
  129. $block = DatabaseBlock::newFromTarget( $target, null, true );
  130. if ( $block instanceof DatabaseBlock ) {
  131. $res['expiry'] = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
  132. $res['id'] = $block->getId();
  133. } else {
  134. # should be unreachable
  135. $res['expiry'] = ''; // @codeCoverageIgnore
  136. $res['id'] = ''; // @codeCoverageIgnore
  137. }
  138. $res['reason'] = $params['reason'];
  139. $res['anononly'] = $params['anononly'];
  140. $res['nocreate'] = $params['nocreate'];
  141. $res['autoblock'] = $params['autoblock'];
  142. $res['noemail'] = $params['noemail'];
  143. $res['hidename'] = $params['hidename'];
  144. $res['allowusertalk'] = $params['allowusertalk'];
  145. $res['watchuser'] = $params['watchuser'];
  146. if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
  147. $res['partial'] = $params['partial'];
  148. $res['pagerestrictions'] = $params['pagerestrictions'];
  149. $res['namespacerestrictions'] = $params['namespacerestrictions'];
  150. }
  151. $this->getResult()->addValue( null, $this->getModuleName(), $res );
  152. }
  153. public function mustBePosted() {
  154. return true;
  155. }
  156. public function isWriteMode() {
  157. return true;
  158. }
  159. public function getAllowedParams() {
  160. $params = [
  161. 'user' => [
  162. ApiBase::PARAM_TYPE => 'user',
  163. ],
  164. 'userid' => [
  165. ApiBase::PARAM_TYPE => 'integer',
  166. ],
  167. 'expiry' => 'never',
  168. 'reason' => '',
  169. 'anononly' => false,
  170. 'nocreate' => false,
  171. 'autoblock' => false,
  172. 'noemail' => false,
  173. 'hidename' => false,
  174. 'allowusertalk' => false,
  175. 'reblock' => false,
  176. 'watchuser' => false,
  177. 'tags' => [
  178. ApiBase::PARAM_TYPE => 'tags',
  179. ApiBase::PARAM_ISMULTI => true,
  180. ],
  181. ];
  182. if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
  183. $params['partial'] = false;
  184. $params['pagerestrictions'] = [
  185. ApiBase::PARAM_ISMULTI => true,
  186. ApiBase::PARAM_ISMULTI_LIMIT1 => 10,
  187. ApiBase::PARAM_ISMULTI_LIMIT2 => 10,
  188. ];
  189. $params['namespacerestrictions'] = [
  190. ApiBase::PARAM_ISMULTI => true,
  191. ApiBase::PARAM_TYPE => 'namespace',
  192. ];
  193. }
  194. return $params;
  195. }
  196. public function needsToken() {
  197. return 'csrf';
  198. }
  199. protected function getExamplesMessages() {
  200. // phpcs:disable Generic.Files.LineLength
  201. return [
  202. 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
  203. => 'apihelp-block-example-ip-simple',
  204. 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
  205. => 'apihelp-block-example-user-complex',
  206. ];
  207. // phpcs:enable
  208. }
  209. public function getHelpUrls() {
  210. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
  211. }
  212. }