Merge "Normalise inline comment markers to "//" in WikiPage.php"
[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 /**
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 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 /**
40 * Blocks the user specified in the parameters for the given expiry, with the
41 * given reason, and with all other settings provided in the params. If the block
42 * succeeds, produces a result containing the details of the block and notice
43 * of success. If it fails, the result will specify the nature of the error.
44 */
45 public function execute() {
46 $user = $this->getUser();
47 $params = $this->extractRequestParams();
48
49 if ( $params['gettoken'] ) {
50 $res['blocktoken'] = $user->getEditToken();
51 $this->getResult()->addValue( null, $this->getModuleName(), $res );
52 return;
53 }
54
55 if ( !$user->isAllowed( 'block' ) ) {
56 $this->dieUsageMsg( 'cantblock' );
57 }
58
59 # bug 15810: blocked admins should have limited access here
60 if ( $user->isBlocked() ) {
61 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
62 if ( $status !== true ) {
63 $this->dieUsageMsg( array( $status ) );
64 }
65 }
66
67 $target = User::newFromName( $params['user'] );
68 // Bug 38633 - if the target is a user (not an IP address), but it doesn't exist or is unusable, error.
69 if ( $target instanceof User && ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) ) ) {
70 $this->dieUsageMsg( array( 'nosuchuser', $params['user'] ) );
71 }
72
73 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
74 $this->dieUsageMsg( 'canthide' );
75 }
76 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
77 $this->dieUsageMsg( 'cantblock-email' );
78 }
79
80 $data = array(
81 'PreviousTarget' => $params['user'],
82 'Target' => $params['user'],
83 'Reason' => array(
84 $params['reason'],
85 'other',
86 $params['reason']
87 ),
88 'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
89 'HardBlock' => !$params['anononly'],
90 'CreateAccount' => $params['nocreate'],
91 'AutoBlock' => $params['autoblock'],
92 'DisableEmail' => $params['noemail'],
93 'HideUser' => $params['hidename'],
94 'DisableUTEdit' => !$params['allowusertalk'],
95 'Reblock' => $params['reblock'],
96 'Watch' => $params['watchuser'],
97 'Confirm' => true,
98 );
99
100 $retval = SpecialBlock::processForm( $data, $this->getContext() );
101 if ( $retval !== true ) {
102 // We don't care about multiple errors, just report one of them
103 $this->dieUsageMsg( $retval );
104 }
105
106 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
107 $res['user'] = $params['user'];
108 $res['userID'] = $target instanceof User ? $target->getId() : 0;
109
110 $block = Block::newFromTarget( $target );
111 if( $block instanceof Block ){
112 $res['expiry'] = $block->mExpiry == $this->getDB()->getInfinity()
113 ? 'infinite'
114 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
115 $res['id'] = $block->getId();
116 } else {
117 # should be unreachable
118 $res['expiry'] = '';
119 $res['id'] = '';
120 }
121
122 $res['reason'] = $params['reason'];
123 if ( $params['anononly'] ) {
124 $res['anononly'] = '';
125 }
126 if ( $params['nocreate'] ) {
127 $res['nocreate'] = '';
128 }
129 if ( $params['autoblock'] ) {
130 $res['autoblock'] = '';
131 }
132 if ( $params['noemail'] ) {
133 $res['noemail'] = '';
134 }
135 if ( $params['hidename'] ) {
136 $res['hidename'] = '';
137 }
138 if ( $params['allowusertalk'] ) {
139 $res['allowusertalk'] = '';
140 }
141 if ( $params['watchuser'] ) {
142 $res['watchuser'] = '';
143 }
144
145 $this->getResult()->addValue( null, $this->getModuleName(), $res );
146 }
147
148 public function mustBePosted() {
149 return true;
150 }
151
152 public function isWriteMode() {
153 return true;
154 }
155
156 public function getAllowedParams() {
157 return array(
158 'user' => array(
159 ApiBase::PARAM_TYPE => 'string',
160 ApiBase::PARAM_REQUIRED => true
161 ),
162 'token' => null,
163 'gettoken' => array(
164 ApiBase::PARAM_DFLT => false,
165 ApiBase::PARAM_DEPRECATED => true,
166 ),
167 'expiry' => 'never',
168 'reason' => '',
169 'anononly' => false,
170 'nocreate' => false,
171 'autoblock' => false,
172 'noemail' => false,
173 'hidename' => false,
174 'allowusertalk' => false,
175 'reblock' => false,
176 'watchuser' => false,
177 );
178 }
179
180 public function getParamDescription() {
181 return array(
182 'user' => 'Username, IP address or IP range you want to block',
183 'token' => 'A block token previously obtained through prop=info',
184 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
185 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
186 'reason' => 'Reason for block',
187 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
188 'nocreate' => 'Prevent account creation',
189 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
190 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
191 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
192 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
193 'reblock' => 'If the user is already blocked, overwrite the existing block',
194 'watchuser' => 'Watch the user/IP\'s user and talk pages',
195 );
196 }
197
198 public function getResultProperties() {
199 return array(
200 '' => array(
201 'blocktoken' => array(
202 ApiBase::PROP_TYPE => 'string',
203 ApiBase::PROP_NULLABLE => true
204 ),
205 'user' => array(
206 ApiBase::PROP_TYPE => 'string',
207 ApiBase::PROP_NULLABLE => true
208 ),
209 'userID' => array(
210 ApiBase::PROP_TYPE => 'integer',
211 ApiBase::PROP_NULLABLE => true
212 ),
213 'expiry' => array(
214 ApiBase::PROP_TYPE => 'string',
215 ApiBase::PROP_NULLABLE => true
216 ),
217 'id' => array(
218 ApiBase::PROP_TYPE => 'integer',
219 ApiBase::PROP_NULLABLE => true
220 ),
221 'reason' => array(
222 ApiBase::PROP_TYPE => 'string',
223 ApiBase::PROP_NULLABLE => true
224 ),
225 'anononly' => 'boolean',
226 'nocreate' => 'boolean',
227 'autoblock' => 'boolean',
228 'noemail' => 'boolean',
229 'hidename' => 'boolean',
230 'allowusertalk' => 'boolean',
231 'watchuser' => 'boolean'
232 )
233 );
234 }
235
236 public function getDescription() {
237 return 'Block a user';
238 }
239
240 public function getPossibleErrors() {
241 return array_merge( parent::getPossibleErrors(), array(
242 array( 'cantblock' ),
243 array( 'canthide' ),
244 array( 'cantblock-email' ),
245 array( 'ipbblocked' ),
246 array( 'ipbnounblockself' ),
247 ) );
248 }
249
250 public function needsToken() {
251 return true;
252 }
253
254 public function getTokenSalt() {
255 return '';
256 }
257
258 public function getExamples() {
259 return array(
260 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
261 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
262 );
263 }
264
265 public function getHelpUrls() {
266 return 'https://www.mediawiki.org/wiki/API:Block';
267 }
268
269 public function getVersion() {
270 return __CLASS__ . ': $Id$';
271 }
272 }