c3aab88190261fca4a03ca92aeef2b913c207a70
[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 $this->checkUserRightsAny( 'block' );
43
44 $user = $this->getUser();
45 $params = $this->extractRequestParams();
46
47 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
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->dieWithError(
54 $status,
55 null,
56 [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
57 );
58 }
59 }
60
61 if ( $params['userid'] !== null ) {
62 $username = User::whoIs( $params['userid'] );
63
64 if ( $username === false ) {
65 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
66 } else {
67 $params['user'] = $username;
68 }
69 } else {
70 $target = User::newFromName( $params['user'] );
71
72 // Bug 38633 - if the target is a user (not an IP address), but it
73 // doesn't exist or is unusable, error.
74 if ( $target instanceof User &&
75 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
76 ) {
77 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
78 }
79 }
80
81 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
82 $this->dieWithError( 'apierror-canthide' );
83 }
84 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
85 $this->dieWithError( 'apierror-cantblock-email' );
86 }
87
88 $data = [
89 'PreviousTarget' => $params['user'],
90 'Target' => $params['user'],
91 'Reason' => [
92 $params['reason'],
93 'other',
94 $params['reason']
95 ],
96 'Expiry' => $params['expiry'],
97 'HardBlock' => !$params['anononly'],
98 'CreateAccount' => $params['nocreate'],
99 'AutoBlock' => $params['autoblock'],
100 'DisableEmail' => $params['noemail'],
101 'HideUser' => $params['hidename'],
102 'DisableUTEdit' => !$params['allowusertalk'],
103 'Reblock' => $params['reblock'],
104 'Watch' => $params['watchuser'],
105 'Confirm' => true,
106 ];
107
108 $retval = SpecialBlock::processForm( $data, $this->getContext() );
109 if ( $retval !== true ) {
110 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
111 }
112
113 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
114 $res['user'] = $params['user'];
115 $res['userID'] = $target instanceof User ? $target->getId() : 0;
116
117 $block = Block::newFromTarget( $target, null, true );
118 if ( $block instanceof Block ) {
119 $res['expiry'] = ApiResult::formatExpiry( $block->mExpiry, 'infinite' );
120 $res['id'] = $block->getId();
121 } else {
122 # should be unreachable
123 $res['expiry'] = '';
124 $res['id'] = '';
125 }
126
127 $res['reason'] = $params['reason'];
128 $res['anononly'] = $params['anononly'];
129 $res['nocreate'] = $params['nocreate'];
130 $res['autoblock'] = $params['autoblock'];
131 $res['noemail'] = $params['noemail'];
132 $res['hidename'] = $params['hidename'];
133 $res['allowusertalk'] = $params['allowusertalk'];
134 $res['watchuser'] = $params['watchuser'];
135
136 $this->getResult()->addValue( null, $this->getModuleName(), $res );
137 }
138
139 public function mustBePosted() {
140 return true;
141 }
142
143 public function isWriteMode() {
144 return true;
145 }
146
147 public function getAllowedParams() {
148 return [
149 'user' => [
150 ApiBase::PARAM_TYPE => 'user',
151 ],
152 'userid' => [
153 ApiBase::PARAM_TYPE => 'integer',
154 ],
155 'expiry' => 'never',
156 'reason' => '',
157 'anononly' => false,
158 'nocreate' => false,
159 'autoblock' => false,
160 'noemail' => false,
161 'hidename' => false,
162 'allowusertalk' => false,
163 'reblock' => false,
164 'watchuser' => false,
165 ];
166 }
167
168 public function needsToken() {
169 return 'csrf';
170 }
171
172 protected function getExamplesMessages() {
173 // @codingStandardsIgnoreStart Generic.Files.LineLength
174 return [
175 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
176 => 'apihelp-block-example-ip-simple',
177 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
178 => 'apihelp-block-example-user-complex',
179 ];
180 // @codingStandardsIgnoreEnd
181 }
182
183 public function getHelpUrls() {
184 return 'https://www.mediawiki.org/wiki/API:Block';
185 }
186 }