(bug 25137) Don't open a DB connection just to (wrongly) use $dbr->timestamp()
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Sep 4, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * API module that facilitates the blocking of users. Requires API write mode
34 * to be enabled.
35 *
36 * @ingroup API
37 */
38 class ApiBlock extends ApiBase {
39
40 /**
41 * Std ctor.
42 */
43 public function __construct( $main, $action ) {
44 parent::__construct( $main, $action );
45 }
46
47 /**
48 * Blocks the user specified in the parameters for the given expiry, with the
49 * given reason, and with all other settings provided in the params. If the block
50 * succeeds, produces a result containing the details of the block and notice
51 * of success. If it fails, the result will specify the nature of the error.
52 */
53 public function execute() {
54 global $wgUser, $wgBlockAllowsUTEdit;
55 $params = $this->extractRequestParams();
56
57 if ( $params['gettoken'] ) {
58 $res['blocktoken'] = $wgUser->editToken();
59 $this->getResult()->addValue( null, $this->getModuleName(), $res );
60 return;
61 }
62
63 if ( !$wgUser->isAllowed( 'block' ) ) {
64 $this->dieUsageMsg( array( 'cantblock' ) );
65 }
66 # bug 15810: blocked admins should have limited access here
67 if ( $wgUser->isBlocked() ) {
68 $status = IPBlockForm::checkUnblockSelf( $params['user'] );
69 if ( $status !== true ) {
70 $this->dieUsageMsg( array( $status ) );
71 }
72 }
73 if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) ) {
74 $this->dieUsageMsg( array( 'canthide' ) );
75 }
76 if ( $params['noemail'] && !IPBlockForm::canBlockEmail( $wgUser ) ) {
77 $this->dieUsageMsg( array( 'cantblock-email' ) );
78 }
79
80 $form = new IPBlockForm( '' );
81 $form->BlockAddress = $params['user'];
82 $form->BlockReason = ( is_null( $params['reason'] ) ? '' : $params['reason'] );
83 $form->BlockReasonList = 'other';
84 $form->BlockExpiry = ( $params['expiry'] == 'never' ? 'infinite' : $params['expiry'] );
85 $form->BlockOther = '';
86 $form->BlockAnonOnly = $params['anononly'];
87 $form->BlockCreateAccount = $params['nocreate'];
88 $form->BlockEnableAutoblock = $params['autoblock'];
89 $form->BlockEmail = $params['noemail'];
90 $form->BlockHideName = $params['hidename'];
91 $form->BlockAllowUsertalk = $params['allowusertalk'] && $wgBlockAllowsUTEdit;
92 $form->BlockReblock = $params['reblock'];
93
94 $userID = $expiry = null;
95 $retval = $form->doBlock( $userID, $expiry );
96 if ( count( $retval ) ) {
97 // We don't care about multiple errors, just report one of them
98 $this->dieUsageMsg( $retval );
99 }
100
101 $res['user'] = $params['user'];
102 $res['userID'] = intval( $userID );
103 $res['expiry'] = ( $expiry == Block::infinity() ? 'infinite' : wfTimestamp( TS_ISO_8601, $expiry ) );
104 $res['reason'] = $params['reason'];
105 if ( $params['anononly'] ) {
106 $res['anononly'] = '';
107 }
108 if ( $params['nocreate'] ) {
109 $res['nocreate'] = '';
110 }
111 if ( $params['autoblock'] ) {
112 $res['autoblock'] = '';
113 }
114 if ( $params['noemail'] ) {
115 $res['noemail'] = '';
116 }
117 if ( $params['hidename'] ) {
118 $res['hidename'] = '';
119 }
120 if ( $params['allowusertalk'] ) {
121 $res['allowusertalk'] = '';
122 }
123
124 $this->getResult()->addValue( null, $this->getModuleName(), $res );
125 }
126
127 public function mustBePosted() {
128 return true;
129 }
130
131 public function isWriteMode() {
132 return true;
133 }
134
135 public function getAllowedParams() {
136 return array(
137 'user' => array(
138 ApiBase::PARAM_TYPE => 'string',
139 ApiBase::PARAM_REQUIRED => true
140 ),
141 'token' => null,
142 'gettoken' => false,
143 'expiry' => 'never',
144 'reason' => null,
145 'anononly' => false,
146 'nocreate' => false,
147 'autoblock' => false,
148 'noemail' => false,
149 'hidename' => false,
150 'allowusertalk' => false,
151 'reblock' => false,
152 );
153 }
154
155 public function getParamDescription() {
156 return array(
157 'user' => 'Username, IP address or IP range you want to block',
158 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
159 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
160 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
161 'reason' => 'Reason for block (optional)',
162 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
163 'nocreate' => 'Prevent account creation',
164 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
165 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
166 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
167 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
168 'reblock' => 'If the user is already blocked, overwrite the existing block',
169 );
170 }
171
172 public function getDescription() {
173 return 'Block a user';
174 }
175
176 public function getPossibleErrors() {
177 return array_merge( parent::getPossibleErrors(), array(
178 array( 'cantblock' ),
179 array( 'canthide' ),
180 array( 'cantblock-email' ),
181 array( 'ipbblocked' ),
182 array( 'ipbnounblockself' ),
183 ) );
184 }
185
186 public function getTokenSalt() {
187 return '';
188 }
189
190 protected function getExamples() {
191 return array(
192 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
193 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
194 );
195 }
196
197 public function getVersion() {
198 return __CLASS__ . ': $Id$';
199 }
200 }