00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 if (!defined('MEDIAWIKI')) {
00026
00027 require_once ("ApiBase.php");
00028 }
00029
00036 class ApiBlock extends ApiBase {
00037
00041 public function __construct($main, $action) {
00042 parent :: __construct($main, $action);
00043 }
00044
00051 public function execute() {
00052 global $wgUser, $wgBlockAllowsUTEdit;
00053 $params = $this->extractRequestParams();
00054
00055 if($params['gettoken'])
00056 {
00057 $res['blocktoken'] = $wgUser->editToken();
00058 $this->getResult()->addValue(null, $this->getModuleName(), $res);
00059 return;
00060 }
00061
00062 if(is_null($params['user']))
00063 $this->dieUsageMsg(array('missingparam', 'user'));
00064 if(is_null($params['token']))
00065 $this->dieUsageMsg(array('missingparam', 'token'));
00066 if(!$wgUser->matchEditToken($params['token']))
00067 $this->dieUsageMsg(array('sessionfailure'));
00068 if(!$wgUser->isAllowed('block'))
00069 $this->dieUsageMsg(array('cantblock'));
00070 if($params['hidename'] && !$wgUser->isAllowed('hideuser'))
00071 $this->dieUsageMsg(array('canthide'));
00072 if($params['noemail'] && !$wgUser->isAllowed('blockemail'))
00073 $this->dieUsageMsg(array('cantblock-email'));
00074
00075 $form = new IPBlockForm('');
00076 $form->BlockAddress = $params['user'];
00077 $form->BlockReason = (is_null($params['reason']) ? '' : $params['reason']);
00078 $form->BlockReasonList = 'other';
00079 $form->BlockExpiry = ($params['expiry'] == 'never' ? 'infinite' : $params['expiry']);
00080 $form->BlockOther = '';
00081 $form->BlockAnonOnly = $params['anononly'];
00082 $form->BlockCreateAccount = $params['nocreate'];
00083 $form->BlockEnableAutoblock = $params['autoblock'];
00084 $form->BlockEmail = $params['noemail'];
00085 $form->BlockHideName = $params['hidename'];
00086 $form->BlockAllowUsertalk = $params['allowusertalk'] && $wgBlockAllowsUTEdit;
00087 $form->BlockReblock = $params['reblock'];
00088
00089 $userID = $expiry = null;
00090 $retval = $form->doBlock($userID, $expiry);
00091 if(count($retval))
00092
00093 $this->dieUsageMsg($retval);
00094
00095 $res['user'] = $params['user'];
00096 $res['userID'] = intval($userID);
00097 $res['expiry'] = ($expiry == Block::infinity() ? 'infinite' : wfTimestamp(TS_ISO_8601, $expiry));
00098 $res['reason'] = $params['reason'];
00099 if($params['anononly'])
00100 $res['anononly'] = '';
00101 if($params['nocreate'])
00102 $res['nocreate'] = '';
00103 if($params['autoblock'])
00104 $res['autoblock'] = '';
00105 if($params['noemail'])
00106 $res['noemail'] = '';
00107 if($params['hidename'])
00108 $res['hidename'] = '';
00109 if($params['allowusertalk'])
00110 $res['allowusertalk'] = '';
00111
00112 $this->getResult()->addValue(null, $this->getModuleName(), $res);
00113 }
00114
00115 public function mustBePosted() { return true; }
00116
00117 public function isWriteMode() {
00118 return true;
00119 }
00120
00121 public function getAllowedParams() {
00122 return array (
00123 'user' => null,
00124 'token' => null,
00125 'gettoken' => false,
00126 'expiry' => 'never',
00127 'reason' => null,
00128 'anononly' => false,
00129 'nocreate' => false,
00130 'autoblock' => false,
00131 'noemail' => false,
00132 'hidename' => false,
00133 'allowusertalk' => false,
00134 'reblock' => false,
00135 );
00136 }
00137
00138 public function getParamDescription() {
00139 return array (
00140 'user' => 'Username, IP address or IP range you want to block',
00141 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
00142 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
00143 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
00144 'reason' => 'Reason for block (optional)',
00145 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
00146 'nocreate' => 'Prevent account creation',
00147 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
00148 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
00149 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
00150 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
00151 'reblock' => 'If the user is already blocked, overwrite the existing block',
00152 );
00153 }
00154
00155 public function getDescription() {
00156 return array(
00157 'Block a user.'
00158 );
00159 }
00160
00161 protected function getExamples() {
00162 return array (
00163 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
00164 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
00165 );
00166 }
00167
00168 public function getVersion() {
00169 return __CLASS__ . ': $Id: ApiBlock.php 48091 2009-03-06 13:49:44Z catrope $';
00170 }
00171 }