SECURITY: API: Respect $wgBlockCIDRLimit in action=block
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * API module that facilitates the blocking of users. Requires API write mode
25 * to be enabled.
26 *
27 * @ingroup API
28 */
29 class ApiBlock extends ApiBase {
30
31 /**
32 * Blocks the user specified in the parameters for the given expiry, with the
33 * given reason, and with all other settings provided in the params. If the block
34 * succeeds, produces a result containing the details of the block and notice
35 * of success. If it fails, the result will specify the nature of the error.
36 */
37 public function execute() {
38 $this->checkUserRightsAny( 'block' );
39
40 $user = $this->getUser();
41 $params = $this->extractRequestParams();
42
43 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
44
45 # T17810: blocked admins should have limited access here
46 if ( $user->isBlocked() ) {
47 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
48 if ( $status !== true ) {
49 $this->dieWithError(
50 $status,
51 null,
52 [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
53 );
54 }
55 }
56
57 if ( $params['userid'] !== null ) {
58 $username = User::whoIs( $params['userid'] );
59
60 if ( $username === false ) {
61 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
62 } else {
63 $params['user'] = $username;
64 }
65 } else {
66 list( $target, $type ) = SpecialBlock::getTargetAndType( $params['user'] );
67
68 // T40633 - if the target is a user (not an IP address), but it
69 // doesn't exist or is unusable, error.
70 if ( $type === Block::TYPE_USER &&
71 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $params['user'] ) )
72 ) {
73 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
74 }
75 }
76
77 if ( $params['tags'] ) {
78 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
79 if ( !$ableToTag->isOK() ) {
80 $this->dieStatus( $ableToTag );
81 }
82 }
83
84 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
85 $this->dieWithError( 'apierror-canthide' );
86 }
87 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
88 $this->dieWithError( 'apierror-cantblock-email' );
89 }
90
91 $data = [
92 'PreviousTarget' => $params['user'],
93 'Target' => $params['user'],
94 'Reason' => [
95 $params['reason'],
96 'other',
97 $params['reason']
98 ],
99 'Expiry' => $params['expiry'],
100 'HardBlock' => !$params['anononly'],
101 'CreateAccount' => $params['nocreate'],
102 'AutoBlock' => $params['autoblock'],
103 'DisableEmail' => $params['noemail'],
104 'HideUser' => $params['hidename'],
105 'DisableUTEdit' => !$params['allowusertalk'],
106 'Reblock' => $params['reblock'],
107 'Watch' => $params['watchuser'],
108 'Confirm' => true,
109 'Tags' => $params['tags'],
110 ];
111
112 $status = SpecialBlock::validateTarget( $params['user'], $user );
113 if ( !$status->isOK() ) {
114 $this->dieStatus( $status );
115 }
116
117 $retval = SpecialBlock::processForm( $data, $this->getContext() );
118 if ( $retval !== true ) {
119 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
120 }
121
122 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
123 $res['user'] = $params['user'];
124 $res['userID'] = $target instanceof User ? $target->getId() : 0;
125
126 $block = Block::newFromTarget( $target, null, true );
127 if ( $block instanceof Block ) {
128 $res['expiry'] = ApiResult::formatExpiry( $block->mExpiry, 'infinite' );
129 $res['id'] = $block->getId();
130 } else {
131 # should be unreachable
132 $res['expiry'] = ''; // @codeCoverageIgnore
133 $res['id'] = ''; // @codeCoverageIgnore
134 }
135
136 $res['reason'] = $params['reason'];
137 $res['anononly'] = $params['anononly'];
138 $res['nocreate'] = $params['nocreate'];
139 $res['autoblock'] = $params['autoblock'];
140 $res['noemail'] = $params['noemail'];
141 $res['hidename'] = $params['hidename'];
142 $res['allowusertalk'] = $params['allowusertalk'];
143 $res['watchuser'] = $params['watchuser'];
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 [
158 'user' => [
159 ApiBase::PARAM_TYPE => 'user',
160 ],
161 'userid' => [
162 ApiBase::PARAM_TYPE => 'integer',
163 ],
164 'expiry' => 'never',
165 'reason' => '',
166 'anononly' => false,
167 'nocreate' => false,
168 'autoblock' => false,
169 'noemail' => false,
170 'hidename' => false,
171 'allowusertalk' => false,
172 'reblock' => false,
173 'watchuser' => false,
174 'tags' => [
175 ApiBase::PARAM_TYPE => 'tags',
176 ApiBase::PARAM_ISMULTI => true,
177 ],
178 ];
179 }
180
181 public function needsToken() {
182 return 'csrf';
183 }
184
185 protected function getExamplesMessages() {
186 // phpcs:disable Generic.Files.LineLength
187 return [
188 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
189 => 'apihelp-block-example-ip-simple',
190 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
191 => 'apihelp-block-example-user-complex',
192 ];
193 // phpcs:enable
194 }
195
196 public function getHelpUrls() {
197 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
198 }
199 }