Add checkDependencies.php
[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 use ApiBlockInfoTrait;
32
33 /**
34 * Blocks the user specified in the parameters for the given expiry, with the
35 * given reason, and with all other settings provided in the params. If the block
36 * succeeds, produces a result containing the details of the block and notice
37 * of success. If it fails, the result will specify the nature of the error.
38 */
39 public function execute() {
40 $this->checkUserRightsAny( 'block' );
41
42 $user = $this->getUser();
43 $params = $this->extractRequestParams();
44
45 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
46
47 # T17810: blocked admins should have limited access here
48 $block = $user->getBlock();
49 if ( $block ) {
50 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
51 if ( $status !== true ) {
52 $this->dieWithError(
53 $status,
54 null,
55 [ 'blockinfo' => $this->getBlockDetails( $block ) ]
56 );
57 }
58 }
59
60 $editingRestriction = 'sitewide';
61 $pageRestrictions = '';
62 $namespaceRestrictions = '';
63 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
64 if ( $params['partial'] ) {
65 $editingRestriction = 'partial';
66 }
67
68 $pageRestrictions = implode( "\n", (array)$params['pagerestrictions'] );
69 $namespaceRestrictions = implode( "\n", (array)$params['namespacerestrictions'] );
70 }
71
72 if ( $params['userid'] !== null ) {
73 $username = User::whoIs( $params['userid'] );
74
75 if ( $username === false ) {
76 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
77 } else {
78 $params['user'] = $username;
79 }
80 } else {
81 list( $target, $type ) = SpecialBlock::getTargetAndType( $params['user'] );
82
83 // T40633 - if the target is a user (not an IP address), but it
84 // doesn't exist or is unusable, error.
85 if ( $type === Block::TYPE_USER &&
86 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $params['user'] ) )
87 ) {
88 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
89 }
90 }
91
92 if ( $params['tags'] ) {
93 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
94 if ( !$ableToTag->isOK() ) {
95 $this->dieStatus( $ableToTag );
96 }
97 }
98
99 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
100 $this->dieWithError( 'apierror-canthide' );
101 }
102 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
103 $this->dieWithError( 'apierror-cantblock-email' );
104 }
105
106 $data = [
107 'PreviousTarget' => $params['user'],
108 'Target' => $params['user'],
109 'Reason' => [
110 $params['reason'],
111 'other',
112 $params['reason']
113 ],
114 'Expiry' => $params['expiry'],
115 'HardBlock' => !$params['anononly'],
116 'CreateAccount' => $params['nocreate'],
117 'AutoBlock' => $params['autoblock'],
118 'DisableEmail' => $params['noemail'],
119 'HideUser' => $params['hidename'],
120 'DisableUTEdit' => !$params['allowusertalk'],
121 'Reblock' => $params['reblock'],
122 'Watch' => $params['watchuser'],
123 'Confirm' => true,
124 'Tags' => $params['tags'],
125 'EditingRestriction' => $editingRestriction,
126 'PageRestrictions' => $pageRestrictions,
127 'NamespaceRestrictions' => $namespaceRestrictions,
128 ];
129
130 $retval = SpecialBlock::processForm( $data, $this->getContext() );
131 if ( $retval !== true ) {
132 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
133 }
134
135 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
136 $res['user'] = $params['user'];
137 $res['userID'] = $target instanceof User ? $target->getId() : 0;
138
139 $block = Block::newFromTarget( $target, null, true );
140 if ( $block instanceof Block ) {
141 $res['expiry'] = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
142 $res['id'] = $block->getId();
143 } else {
144 # should be unreachable
145 $res['expiry'] = ''; // @codeCoverageIgnore
146 $res['id'] = ''; // @codeCoverageIgnore
147 }
148
149 $res['reason'] = $params['reason'];
150 $res['anononly'] = $params['anononly'];
151 $res['nocreate'] = $params['nocreate'];
152 $res['autoblock'] = $params['autoblock'];
153 $res['noemail'] = $params['noemail'];
154 $res['hidename'] = $params['hidename'];
155 $res['allowusertalk'] = $params['allowusertalk'];
156 $res['watchuser'] = $params['watchuser'];
157
158 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
159 $res['partial'] = $params['partial'];
160 $res['pagerestrictions'] = $params['pagerestrictions'];
161 $res['namespacerestrictions'] = $params['namespacerestrictions'];
162 }
163
164 $this->getResult()->addValue( null, $this->getModuleName(), $res );
165 }
166
167 public function mustBePosted() {
168 return true;
169 }
170
171 public function isWriteMode() {
172 return true;
173 }
174
175 public function getAllowedParams() {
176 $params = [
177 'user' => [
178 ApiBase::PARAM_TYPE => 'user',
179 ],
180 'userid' => [
181 ApiBase::PARAM_TYPE => 'integer',
182 ],
183 'expiry' => 'never',
184 'reason' => '',
185 'anononly' => false,
186 'nocreate' => false,
187 'autoblock' => false,
188 'noemail' => false,
189 'hidename' => false,
190 'allowusertalk' => false,
191 'reblock' => false,
192 'watchuser' => false,
193 'tags' => [
194 ApiBase::PARAM_TYPE => 'tags',
195 ApiBase::PARAM_ISMULTI => true,
196 ],
197 ];
198
199 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
200 $params['partial'] = false;
201 $params['pagerestrictions'] = [
202 ApiBase::PARAM_ISMULTI => true,
203 ApiBase::PARAM_ISMULTI_LIMIT1 => 10,
204 ApiBase::PARAM_ISMULTI_LIMIT2 => 10,
205 ];
206 $params['namespacerestrictions'] = [
207 ApiBase::PARAM_ISMULTI => true,
208 ApiBase::PARAM_TYPE => 'namespace',
209 ];
210 }
211
212 return $params;
213 }
214
215 public function needsToken() {
216 return 'csrf';
217 }
218
219 protected function getExamplesMessages() {
220 // phpcs:disable Generic.Files.LineLength
221 return [
222 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
223 => 'apihelp-block-example-ip-simple',
224 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
225 => 'apihelp-block-example-user-complex',
226 ];
227 // phpcs:enable
228 }
229
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
232 }
233 }