Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
16 / 20 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ApiTorBlock | |
80.00% |
16 / 20 |
|
66.67% |
2 / 3 |
4.13 | |
0.00% |
0 / 1 |
| execute | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| getAllowedParams | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getExamplesMessages | |
0.00% |
0 / 4 |
|
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 | |
| 20 | namespace MediaWiki\Extension\TorBlock; |
| 21 | |
| 22 | use MediaWiki\Api\ApiBase; |
| 23 | use MediaWiki\Api\ApiResult; |
| 24 | use Wikimedia\IPUtils; |
| 25 | use 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 | */ |
| 33 | class 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 | /** @inheritDoc */ |
| 63 | protected function getExamplesMessages() { |
| 64 | return [ |
| 65 | 'action=torblock&ip=192.0.2.18' |
| 66 | => 'apihelp-torblock-example-1', |
| 67 | ]; |
| 68 | } |
| 69 | } |