Deprecate ORMTable::getFieldPrefix
[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
59 // doesn't exist or is unusable, error.
60 if ( $target instanceof User &&
61 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
62 ) {
63 $this->dieUsageMsg( array( 'nosuchuser', $params['user'] ) );
64 }
65
66 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
67 $this->dieUsageMsg( 'canthide' );
68 }
69 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
70 $this->dieUsageMsg( 'cantblock-email' );
71 }
72
73 $data = array(
74 'PreviousTarget' => $params['user'],
75 'Target' => $params['user'],
76 'Reason' => array(
77 $params['reason'],
78 'other',
79 $params['reason']
80 ),
81 'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
82 'HardBlock' => !$params['anononly'],
83 'CreateAccount' => $params['nocreate'],
84 'AutoBlock' => $params['autoblock'],
85 'DisableEmail' => $params['noemail'],
86 'HideUser' => $params['hidename'],
87 'DisableUTEdit' => !$params['allowusertalk'],
88 'Reblock' => $params['reblock'],
89 'Watch' => $params['watchuser'],
90 'Confirm' => true,
91 );
92
93 $retval = SpecialBlock::processForm( $data, $this->getContext() );
94 if ( $retval !== true ) {
95 // We don't care about multiple errors, just report one of them
96 $this->dieUsageMsg( $retval );
97 }
98
99 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
100 $res['user'] = $params['user'];
101 $res['userID'] = $target instanceof User ? $target->getId() : 0;
102
103 $block = Block::newFromTarget( $target );
104 if ( $block instanceof Block ) {
105 $res['expiry'] = $block->mExpiry == $this->getDB()->getInfinity()
106 ? 'infinite'
107 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
108 $res['id'] = $block->getId();
109 } else {
110 # should be unreachable
111 $res['expiry'] = '';
112 $res['id'] = '';
113 }
114
115 $res['reason'] = $params['reason'];
116 if ( $params['anononly'] ) {
117 $res['anononly'] = '';
118 }
119 if ( $params['nocreate'] ) {
120 $res['nocreate'] = '';
121 }
122 if ( $params['autoblock'] ) {
123 $res['autoblock'] = '';
124 }
125 if ( $params['noemail'] ) {
126 $res['noemail'] = '';
127 }
128 if ( $params['hidename'] ) {
129 $res['hidename'] = '';
130 }
131 if ( $params['allowusertalk'] ) {
132 $res['allowusertalk'] = '';
133 }
134 if ( $params['watchuser'] ) {
135 $res['watchuser'] = '';
136 }
137
138 $this->getResult()->addValue( null, $this->getModuleName(), $res );
139 }
140
141 public function mustBePosted() {
142 return true;
143 }
144
145 public function isWriteMode() {
146 return true;
147 }
148
149 public function getAllowedParams() {
150 return array(
151 'user' => array(
152 ApiBase::PARAM_TYPE => 'string',
153 ApiBase::PARAM_REQUIRED => true
154 ),
155 'token' => null,
156 'expiry' => 'never',
157 'reason' => '',
158 'anononly' => false,
159 'nocreate' => false,
160 'autoblock' => false,
161 'noemail' => false,
162 'hidename' => false,
163 'allowusertalk' => false,
164 'reblock' => false,
165 'watchuser' => false,
166 );
167 }
168
169 public function getParamDescription() {
170 return array(
171 'user' => 'Username, IP address or IP range you want to block',
172 'token' => 'A block token previously obtained through prop=info',
173 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. ' .
174 'If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
175 'reason' => 'Reason for block',
176 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
177 'nocreate' => 'Prevent account creation',
178 'autoblock' => 'Automatically block the last used IP address, and ' .
179 'any subsequent IP addresses they try to login from',
180 'noemail'
181 => 'Prevent user from sending email through the wiki. (Requires the "blockemail" right.)',
182 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
183 'allowusertalk'
184 => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
185 'reblock' => 'If the user is already blocked, overwrite the existing block',
186 'watchuser' => 'Watch the user/IP\'s user and talk pages',
187 );
188 }
189
190 public function getDescription() {
191 return 'Block a user.';
192 }
193
194 public function needsToken() {
195 return true;
196 }
197
198 public function getTokenSalt() {
199 return '';
200 }
201
202 public function getExamples() {
203 return array(
204 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
205 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
206 );
207 }
208
209 public function getHelpUrls() {
210 return 'https://www.mediawiki.org/wiki/API:Block';
211 }
212 }