00001 <?php
00002
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 $res['blocktoken'] = $wgUser->editToken();
00057 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00058 return;
00059 }
00060
00061 if ( is_null( $params['user'] ) ) {
00062 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
00063 }
00064 if ( !$wgUser->isAllowed( 'block' ) ) {
00065 $this->dieUsageMsg( array( 'cantblock' ) );
00066 }
00067 if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) ) {
00068 $this->dieUsageMsg( array( 'canthide' ) );
00069 }
00070 if ( $params['noemail'] && !IPBlockForm::canBlockEmail( $wgUser ) ) {
00071 $this->dieUsageMsg( array( 'cantblock-email' ) );
00072 }
00073
00074 $form = new IPBlockForm( '' );
00075 $form->BlockAddress = $params['user'];
00076 $form->BlockReason = ( is_null( $params['reason'] ) ? '' : $params['reason'] );
00077 $form->BlockReasonList = 'other';
00078 $form->BlockExpiry = ( $params['expiry'] == 'never' ? 'infinite' : $params['expiry'] );
00079 $form->BlockOther = '';
00080 $form->BlockAnonOnly = $params['anononly'];
00081 $form->BlockCreateAccount = $params['nocreate'];
00082 $form->BlockEnableAutoblock = $params['autoblock'];
00083 $form->BlockEmail = $params['noemail'];
00084 $form->BlockHideName = $params['hidename'];
00085 $form->BlockAllowUsertalk = $params['allowusertalk'] && $wgBlockAllowsUTEdit;
00086 $form->BlockReblock = $params['reblock'];
00087
00088 $userID = $expiry = null;
00089 $retval = $form->doBlock( $userID, $expiry );
00090 if ( count( $retval ) ) {
00091
00092 $this->dieUsageMsg( $retval );
00093 }
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 }
00102 if ( $params['nocreate'] ) {
00103 $res['nocreate'] = '';
00104 }
00105 if ( $params['autoblock'] ) {
00106 $res['autoblock'] = '';
00107 }
00108 if ( $params['noemail'] ) {
00109 $res['noemail'] = '';
00110 }
00111 if ( $params['hidename'] ) {
00112 $res['hidename'] = '';
00113 }
00114 if ( $params['allowusertalk'] ) {
00115 $res['allowusertalk'] = '';
00116 }
00117
00118 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00119 }
00120
00121 public function mustBePosted() {
00122 return true;
00123 }
00124
00125 public function isWriteMode() {
00126 return true;
00127 }
00128
00129 public function getAllowedParams() {
00130 return array(
00131 'user' => null,
00132 'token' => null,
00133 'gettoken' => false,
00134 'expiry' => 'never',
00135 'reason' => null,
00136 'anononly' => false,
00137 'nocreate' => false,
00138 'autoblock' => false,
00139 'noemail' => false,
00140 'hidename' => false,
00141 'allowusertalk' => false,
00142 'reblock' => false,
00143 );
00144 }
00145
00146 public function getParamDescription() {
00147 return array(
00148 'user' => 'Username, IP address or IP range you want to block',
00149 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
00150 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
00151 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
00152 'reason' => 'Reason for block (optional)',
00153 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
00154 'nocreate' => 'Prevent account creation',
00155 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
00156 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
00157 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
00158 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
00159 'reblock' => 'If the user is already blocked, overwrite the existing block',
00160 );
00161 }
00162
00163 public function getDescription() {
00164 return array(
00165 'Block a user.'
00166 );
00167 }
00168
00169 public function getPossibleErrors() {
00170 return array_merge( parent::getPossibleErrors(), array(
00171 array( 'missingparam', 'user' ),
00172 array( 'cantblock' ),
00173 array( 'canthide' ),
00174 array( 'cantblock-email' ),
00175 ) );
00176 }
00177
00178 public function needsToken() {
00179 return true;
00180 }
00181
00182 public function getTokenSalt() {
00183 return '';
00184 }
00185
00186 protected function getExamples() {
00187 return array(
00188 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
00189 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
00190 );
00191 }
00192
00193 public function getVersion() {
00194 return __CLASS__ . ': $Id: ApiBlock.php 74217 2010-10-03 15:53:07Z reedy $';
00195 }
00196 }