Blame hashar for this giant commit; he teased me for making so many smaller ones...
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * API module that facilitates the blocking of users. Requires API write mode
34 * to be enabled.
35 *
36 * @ingroup API
37 */
38 class ApiBlock extends ApiBase {
39
40 public function __construct( $main, $action ) {
41 parent::__construct( $main, $action );
42 }
43
44 /**
45 * Blocks the user specified in the parameters for the given expiry, with the
46 * given reason, and with all other settings provided in the params. If the block
47 * succeeds, produces a result containing the details of the block and notice
48 * of success. If it fails, the result will specify the nature of the error.
49 */
50 public function execute() {
51 global $wgUser, $wgBlockAllowsUTEdit;
52 $params = $this->extractRequestParams();
53
54 if ( $params['gettoken'] ) {
55 $res['blocktoken'] = $wgUser->editToken( '', $this->getMain()->getRequest() );
56 $this->getResult()->addValue( null, $this->getModuleName(), $res );
57 return;
58 }
59
60 if ( !$wgUser->isAllowed( 'block' ) ) {
61 $this->dieUsageMsg( array( 'cantblock' ) );
62 }
63 # bug 15810: blocked admins should have limited access here
64 if ( $wgUser->isBlocked() ) {
65 $status = SpecialBlock::checkUnblockSelf( $params['user'] );
66 if ( $status !== true ) {
67 $this->dieUsageMsg( array( $status ) );
68 }
69 }
70 if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) ) {
71 $this->dieUsageMsg( array( 'canthide' ) );
72 }
73 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $wgUser ) ) {
74 $this->dieUsageMsg( array( 'cantblock-email' ) );
75 }
76
77 $data = array(
78 'Target' => $params['user'],
79 'Reason' => array(
80 is_null( $params['reason'] ) ? '' : $params['reason'],
81 'other',
82 is_null( $params['reason'] ) ? '' : $params['reason']
83 ),
84 'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
85 'HardBlock' => !$params['anononly'],
86 'CreateAccount' => $params['nocreate'],
87 'AutoBlock' => $params['autoblock'],
88 'DisableEmail' => $params['noemail'],
89 'HideUser' => $params['hidename'],
90 'DisableUTEdit' => $params['allowusertalk'],
91 'AlreadyBlocked' => $params['reblock'],
92 'Watch' => $params['watchuser'],
93 );
94
95 $retval = SpecialBlock::processForm( $data );
96 if ( $retval !== true ) {
97 // We don't care about multiple errors, just report one of them
98 $this->dieUsageMsg( $retval );
99 }
100
101 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
102 $res['user'] = $params['user'];
103 $res['userID'] = $target instanceof User ? $target->getId() : 0;
104
105 $block = Block::newFromTarget( $target );
106 if( $block instanceof Block ){
107 $res['expiry'] = $block->mExpiry == wfGetDB( DB_SLAVE )->getInfinity()
108 ? 'infinite'
109 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
110 } else {
111 # should be unreachable
112 $res['expiry'] = '';
113 }
114
115 $res['reason'] = $params['reason'];
116 if ( $params['anononly'] ) {
117 $res['anononly'] = '';
118 }
119 if ( $params['nocreate'] ) {
120 $res['nocreate'] = '';
121 }
122 if ( $params['autoblock'] ) {
123 $res['autoblock'] = '';
124 }
125 if ( $params['noemail'] ) {
126 $res['noemail'] = '';
127 }
128 if ( $params['hidename'] ) {
129 $res['hidename'] = '';
130 }
131 if ( $params['allowusertalk'] ) {
132 $res['allowusertalk'] = '';
133 }
134 if ( $params['watchuser'] ) {
135 $res['watchuser'] = '';
136 }
137
138 $this->getResult()->addValue( null, $this->getModuleName(), $res );
139 }
140
141 public function mustBePosted() {
142 return true;
143 }
144
145 public function isWriteMode() {
146 return true;
147 }
148
149 public function getAllowedParams() {
150 return array(
151 'user' => array(
152 ApiBase::PARAM_TYPE => 'string',
153 ApiBase::PARAM_REQUIRED => true
154 ),
155 'token' => null,
156 'gettoken' => false,
157 'expiry' => 'never',
158 'reason' => null,
159 'anononly' => false,
160 'nocreate' => false,
161 'autoblock' => false,
162 'noemail' => false,
163 'hidename' => false,
164 'allowusertalk' => false,
165 'reblock' => false,
166 'watchuser' => false,
167 );
168 }
169
170 public function getParamDescription() {
171 return array(
172 'user' => 'Username, IP address or IP range you want to block',
173 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
174 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
175 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
176 'reason' => 'Reason for block (optional)',
177 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
178 'nocreate' => 'Prevent account creation',
179 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
180 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
181 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
182 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
183 'reblock' => 'If the user is already blocked, overwrite the existing block',
184 'watchuser' => 'Watch the user/IP\'s user and talk pages',
185 );
186 }
187
188 public function getDescription() {
189 return 'Block a user';
190 }
191
192 public function getPossibleErrors() {
193 return array_merge( parent::getPossibleErrors(), array(
194 array( 'cantblock' ),
195 array( 'canthide' ),
196 array( 'cantblock-email' ),
197 array( 'ipbblocked' ),
198 array( 'ipbnounblockself' ),
199 ) );
200 }
201
202 public function needsToken() {
203 return true;
204 }
205
206 public function getTokenSalt() {
207 return '';
208 }
209
210 protected function getExamples() {
211 return array(
212 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
213 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
214 );
215 }
216
217 public function getVersion() {
218 return __CLASS__ . ': $Id$';
219 }
220 }