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