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