Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiTorBlock
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20namespace MediaWiki\Extension\TorBlock;
21
22use ApiBase;
23use ApiResult;
24use Wikimedia\IPUtils;
25use Wikimedia\ParamValidator\ParamValidator;
26
27/**
28 * API module to check if an IP is blocked by Tor
29 *
30 * @ingroup API
31 * @ingroup Extensions
32 */
33class ApiTorBlock extends ApiBase {
34
35    public function execute() {
36        $params = $this->extractRequestParams();
37
38        if ( IPUtils::isIPAddress( $params['ip'] ) ) {
39            $result = [
40                ApiResult::META_BC_BOOLS => [ 'result' ],
41                'result' => TorExitNodes::isExitNode( $params['ip'] ),
42            ];
43
44            $this->getResult()->addValue( null, $this->getModuleName(), $result );
45        } else {
46            $this->dieWithError(
47                [ 'apierror-torblock-badip', wfEscapeWikiText( $params['ip'] ) ],
48                'badvalue'
49            );
50        }
51    }
52
53    /** @inheritDoc */
54    public function getAllowedParams() {
55        return [
56            'ip' => [
57                ParamValidator::PARAM_REQUIRED => true,
58            ],
59        ];
60    }
61
62    /**
63     * @see ApiBase::getExamplesMessages()
64     * @return array
65     */
66    protected function getExamplesMessages() {
67        return [
68            'action=torblock&ip=192.0.2.18'
69                => 'apihelp-torblock-example-1',
70        ];
71    }
72}