ApiBlock.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * Created on Sep 4, 2007
  4. * API for MediaWiki 1.8+
  5. *
  6. * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. */
  23. if (!defined('MEDIAWIKI')) {
  24. // Eclipse helper - will be ignored in production
  25. require_once ("ApiBase.php");
  26. }
  27. /**
  28. * API module that facilitates the blocking of users. Requires API write mode
  29. * to be enabled.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiBlock extends ApiBase {
  34. /**
  35. * Std ctor.
  36. */
  37. public function __construct($main, $action) {
  38. parent :: __construct($main, $action);
  39. }
  40. /**
  41. * Blocks the user specified in the parameters for the given expiry, with the
  42. * given reason, and with all other settings provided in the params. If the block
  43. * succeeds, produces a result containing the details of the block and notice
  44. * of success. If it fails, the result will specify the nature of the error.
  45. */
  46. public function execute() {
  47. global $wgUser, $wgBlockAllowsUTEdit;
  48. $params = $this->extractRequestParams();
  49. if($params['gettoken'])
  50. {
  51. $res['blocktoken'] = $wgUser->editToken();
  52. $this->getResult()->addValue(null, $this->getModuleName(), $res);
  53. return;
  54. }
  55. if(is_null($params['user']))
  56. $this->dieUsageMsg(array('missingparam', 'user'));
  57. if(is_null($params['token']))
  58. $this->dieUsageMsg(array('missingparam', 'token'));
  59. if(!$wgUser->matchEditToken($params['token']))
  60. $this->dieUsageMsg(array('sessionfailure'));
  61. if(!$wgUser->isAllowed('block'))
  62. $this->dieUsageMsg(array('cantblock'));
  63. if($params['hidename'] && !$wgUser->isAllowed('hideuser'))
  64. $this->dieUsageMsg(array('canthide'));
  65. if($params['noemail'] && !$wgUser->isAllowed('blockemail'))
  66. $this->dieUsageMsg(array('cantblock-email'));
  67. $form = new IPBlockForm('');
  68. $form->BlockAddress = $params['user'];
  69. $form->BlockReason = (is_null($params['reason']) ? '' : $params['reason']);
  70. $form->BlockReasonList = 'other';
  71. $form->BlockExpiry = ($params['expiry'] == 'never' ? 'infinite' : $params['expiry']);
  72. $form->BlockOther = '';
  73. $form->BlockAnonOnly = $params['anononly'];
  74. $form->BlockCreateAccount = $params['nocreate'];
  75. $form->BlockEnableAutoblock = $params['autoblock'];
  76. $form->BlockEmail = $params['noemail'];
  77. $form->BlockHideName = $params['hidename'];
  78. $form->BlockAllowUsertalk = $params['allowusertalk'] && $wgBlockAllowsUTEdit;
  79. $form->BlockReblock = $params['reblock'];
  80. $userID = $expiry = null;
  81. $retval = $form->doBlock($userID, $expiry);
  82. if(count($retval))
  83. // We don't care about multiple errors, just report one of them
  84. $this->dieUsageMsg($retval);
  85. $res['user'] = $params['user'];
  86. $res['userID'] = intval($userID);
  87. $res['expiry'] = ($expiry == Block::infinity() ? 'infinite' : wfTimestamp(TS_ISO_8601, $expiry));
  88. $res['reason'] = $params['reason'];
  89. if($params['anononly'])
  90. $res['anononly'] = '';
  91. if($params['nocreate'])
  92. $res['nocreate'] = '';
  93. if($params['autoblock'])
  94. $res['autoblock'] = '';
  95. if($params['noemail'])
  96. $res['noemail'] = '';
  97. if($params['hidename'])
  98. $res['hidename'] = '';
  99. if($params['allowusertalk'])
  100. $res['allowusertalk'] = '';
  101. $this->getResult()->addValue(null, $this->getModuleName(), $res);
  102. }
  103. public function mustBePosted() { return true; }
  104. public function isWriteMode() {
  105. return true;
  106. }
  107. public function getAllowedParams() {
  108. return array (
  109. 'user' => null,
  110. 'token' => null,
  111. 'gettoken' => false,
  112. 'expiry' => 'never',
  113. 'reason' => null,
  114. 'anononly' => false,
  115. 'nocreate' => false,
  116. 'autoblock' => false,
  117. 'noemail' => false,
  118. 'hidename' => false,
  119. 'allowusertalk' => false,
  120. 'reblock' => false,
  121. );
  122. }
  123. public function getParamDescription() {
  124. return array (
  125. 'user' => 'Username, IP address or IP range you want to block',
  126. 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
  127. 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
  128. 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
  129. 'reason' => 'Reason for block (optional)',
  130. 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
  131. 'nocreate' => 'Prevent account creation',
  132. 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
  133. 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
  134. 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
  135. 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
  136. 'reblock' => 'If the user is already blocked, overwrite the existing block',
  137. );
  138. }
  139. public function getDescription() {
  140. return array(
  141. 'Block a user.'
  142. );
  143. }
  144. protected function getExamples() {
  145. return array (
  146. 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
  147. 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
  148. );
  149. }
  150. public function getVersion() {
  151. return __CLASS__ . ': $Id: ApiBlock.php 48091 2009-03-06 13:49:44Z catrope $';
  152. }
  153. }