ApiBlock.php 5.7 KB

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