Revert r28215: incorrectly moved files
[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 * @addtogroup API
32 */
33 class ApiBlock extends ApiBase {
34
35 public function __construct($main, $action) {
36 parent :: __construct($main, $action);
37 }
38
39 public function execute() {
40 global $wgUser;
41 $this->getMain()->requestWriteMode();
42 $params = $this->extractRequestParams();
43
44 if($params['gettoken'])
45 {
46 $res['blocktoken'] = $wgUser->editToken();
47 $this->getResult()->addValue(null, $this->getModuleName(), $res);
48 return;
49 }
50
51 if(is_null($params['user']))
52 $this->dieUsage('The user parameter must be set', 'nouser');
53 if(is_null($params['token']))
54 $this->dieUsage('The token parameter must be set', 'notoken');
55 if(!$wgUser->matchEditToken($params['token']))
56 $this->dieUsage('Invalid token', 'badtoken');
57 if(!$wgUser->isAllowed('block'))
58 $this->dieUsage('You don\'t have permission to block users', 'permissiondenied');
59 if($params['hidename'] && !$wgUser->isAllowed('hideuser'))
60 $this->dieUsage('You don\'t have permission to hide user names from the block log', 'nohide');
61 if(wfReadOnly())
62 $this->dieUsage('The wiki is in read-only mode', 'readonly');
63
64 $form = new IPBlockForm('');
65 $form->BlockAddress = $params['user'];
66 $form->BlockReason = $params['reason'];
67 $form->BlockReasonList = 'other';
68 $form->BlockExpiry = ($params['expiry'] == 'never' ? 'infinite' : $params['expiry']);
69 $form->BlockOther = '';
70 $form->BlockAnonOnly = $params['anononly'];
71 $form->BlockCreateAccount = $params['nocreate'];
72 $form->BlockEnableAutoBlock = $params['autoblock'];
73 $form->BlockEmail = $params['noemail'];
74 $form->BlockHideName = $params['hidename'];
75
76 $dbw = wfGetDb(DB_MASTER);
77 $dbw->begin();
78 $retval = $form->doBlock($userID, $expiry);
79 switch($retval)
80 {
81 case IPBlockForm::BLOCK_SUCCESS:
82 break; // We'll deal with that later
83 case IPBlockForm::BLOCK_RANGE_INVALID:
84 $this->dieUsage("Invalid IP range ``{$params['user']}''", 'invalidrange');
85 case IPBlockForm::BLOCK_RANGE_DISABLED:
86 $this->dieUsage('Blocking IP ranges has been disabled', 'rangedisabled');
87 case IPBlockForm::BLOCK_NONEXISTENT_USER:
88 $this->dieUsage("User ``{$params['user']}'' doesn't exist", 'nosuchuser');
89 case IPBlockForm::BLOCK_IP_INVALID:
90 $this->dieUsage("Invaild IP address ``{$params['user']}''", 'invalidip');
91 case IPBlockForm::BLOCK_EXPIRY_INVALID:
92 $this->dieUsage("Invalid expiry time ``{$params['expiry']}''", 'invalidexpiry');
93 case IPBlockForm::BLOCK_ALREADY_BLOCKED:
94 $this->dieUsage("User ``{$params['user']}'' is already blocked", 'alreadyblocked');
95 default:
96 $this->dieDebug(__METHOD__, "IPBlockForm::doBlock() returned an unknown error ($retval)");
97 }
98 $dbw->commit();
99
100 $res['user'] = $params['user'];
101 $res['userID'] = $userID;
102 $res['expiry'] = ($expiry == Block::infinity() ? 'infinite' : $expiry);
103 $res['reason'] = $params['reason'];
104 if($params['anononly'])
105 $res['anononly'] = '';
106 if($params['nocreate'])
107 $res['nocreate'] = '';
108 if($params['autoblock'])
109 $res['autoblock'] = '';
110 if($params['noemail'])
111 $res['noemail'] = '';
112 if($params['hidename'])
113 $res['hidename'] = '';
114
115 $this->getResult()->addValue(null, $this->getModuleName(), $res);
116 }
117
118 protected function getAllowedParams() {
119 return array (
120 'user' => null,
121 'token' => null,
122 'gettoken' => false,
123 'expiry' => 'never',
124 'reason' => null,
125 'anononly' => false,
126 'nocreate' => false,
127 'autoblock' => false,
128 'noemail' => false,
129 'hidename' => false,
130 );
131 }
132
133 protected function getParamDescription() {
134 return array (
135 'user' => 'Username, IP address or IP range you want to block',
136 'token' => 'A block token previously obtained through the gettoken parameter',
137 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
138 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
139 'reason' => 'Reason for block (optional)',
140 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
141 'nocreate' => 'Prevent account creation',
142 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
143 'noemail' => 'Prevent user from sending e-mail through the wiki',
144 'hidename' => 'Hide the username from the block log.'
145 );
146 }
147
148 protected function getDescription() {
149 return array(
150 'Block a user.'
151 );
152 }
153
154 protected function getExamples() {
155 return array (
156 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
157 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
158 );
159 }
160
161 public function getVersion() {
162 return __CLASS__ . ': $Id$';
163 }
164 }