And while I'm at it: removed unused global declarations of $wgFeedClasses
[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 (C) 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 {
57 $res['blocktoken'] = $wgUser->editToken();
58 $this->getResult()->addValue( null, $this->getModuleName(), $res );
59 return;
60 }
61
62 if ( is_null( $params['user'] ) )
63 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
64 if ( is_null( $params['token'] ) )
65 $this->dieUsageMsg( array( 'missingparam', 'token' ) );
66 if ( !$wgUser->matchEditToken( $params['token'] ) )
67 $this->dieUsageMsg( array( 'sessionfailure' ) );
68 if ( !$wgUser->isAllowed( 'block' ) )
69 $this->dieUsageMsg( array( 'cantblock' ) );
70 if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) )
71 $this->dieUsageMsg( array( 'canthide' ) );
72 if ( $params['noemail'] && !IPBlockForm::canBlockEmail( $wgUser ) )
73 $this->dieUsageMsg( array( 'cantblock-email' ) );
74
75 $form = new IPBlockForm( '' );
76 $form->BlockAddress = $params['user'];
77 $form->BlockReason = ( is_null( $params['reason'] ) ? '' : $params['reason'] );
78 $form->BlockReasonList = 'other';
79 $form->BlockExpiry = ( $params['expiry'] == 'never' ? 'infinite' : $params['expiry'] );
80 $form->BlockOther = '';
81 $form->BlockAnonOnly = $params['anononly'];
82 $form->BlockCreateAccount = $params['nocreate'];
83 $form->BlockEnableAutoblock = $params['autoblock'];
84 $form->BlockEmail = $params['noemail'];
85 $form->BlockHideName = $params['hidename'];
86 $form->BlockAllowUsertalk = $params['allowusertalk'] && $wgBlockAllowsUTEdit;
87 $form->BlockReblock = $params['reblock'];
88
89 $userID = $expiry = null;
90 $retval = $form->doBlock( $userID, $expiry );
91 if ( count( $retval ) )
92 // We don't care about multiple errors, just report one of them
93 $this->dieUsageMsg( $retval );
94
95 $res['user'] = $params['user'];
96 $res['userID'] = intval( $userID );
97 $res['expiry'] = ( $expiry == Block::infinity() ? 'infinite' : wfTimestamp( TS_ISO_8601, $expiry ) );
98 $res['reason'] = $params['reason'];
99 if ( $params['anononly'] )
100 $res['anononly'] = '';
101 if ( $params['nocreate'] )
102 $res['nocreate'] = '';
103 if ( $params['autoblock'] )
104 $res['autoblock'] = '';
105 if ( $params['noemail'] )
106 $res['noemail'] = '';
107 if ( $params['hidename'] )
108 $res['hidename'] = '';
109 if ( $params['allowusertalk'] )
110 $res['allowusertalk'] = '';
111
112 $this->getResult()->addValue( null, $this->getModuleName(), $res );
113 }
114
115 public function mustBePosted() { return true; }
116
117 public function isWriteMode() {
118 return true;
119 }
120
121 public function getAllowedParams() {
122 return array (
123 'user' => null,
124 'token' => null,
125 'gettoken' => false,
126 'expiry' => 'never',
127 'reason' => null,
128 'anononly' => false,
129 'nocreate' => false,
130 'autoblock' => false,
131 'noemail' => false,
132 'hidename' => false,
133 'allowusertalk' => false,
134 'reblock' => false,
135 );
136 }
137
138 public function getParamDescription() {
139 return array (
140 'user' => 'Username, IP address or IP range you want to block',
141 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
142 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
143 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
144 'reason' => 'Reason for block (optional)',
145 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
146 'nocreate' => 'Prevent account creation',
147 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
148 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
149 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
150 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
151 'reblock' => 'If the user is already blocked, overwrite the existing block',
152 );
153 }
154
155 public function getDescription() {
156 return array(
157 'Block a user.'
158 );
159 }
160
161 protected function getExamples() {
162 return array (
163 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
164 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
165 );
166 }
167
168 public function getVersion() {
169 return __CLASS__ . ': $Id$';
170 }
171 }