mirror of
https://github.com/kristuff/abuseipdb
synced 2025-08-20 13:33:14 -07:00
v0.9.9
**Added** - better exception handling with new class `SilentApiHandler`: to not raise any exception during API request but instead return an `ApiResponse` with errors - version constant - `ApiReponse` has new `errors()` and `hasError()` methods
This commit is contained in:
parent
fa42dafc36
commit
3c5c7155c2
7 changed files with 210 additions and 9 deletions
|
@ -32,7 +32,7 @@ Deploy with composer:
|
||||||
```json
|
```json
|
||||||
...
|
...
|
||||||
"require": {
|
"require": {
|
||||||
"kristuff/abuseipdb": ">=0.9.8-stable"
|
"kristuff/abuseipdb": ">=0.9.9-stable"
|
||||||
},
|
},
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*
|
*
|
||||||
* @version 0.9.8
|
* @version 0.9.9
|
||||||
* @copyright 2020-2021 Kristuff
|
* @copyright 2020-2021 Kristuff
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*
|
*
|
||||||
* @version 0.9.8
|
* @version 0.9.9
|
||||||
* @copyright 2020-2021 Kristuff
|
* @copyright 2020-2021 Kristuff
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -32,6 +32,11 @@ class ApiHandler extends ApiBase
|
||||||
*/
|
*/
|
||||||
use CurlTrait;
|
use CurlTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string $version
|
||||||
|
*/
|
||||||
|
const VERSION = 'v0.9.9';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ips to remove from report messages
|
* The ips to remove from report messages
|
||||||
* Generally you will add to this list yours ipv4 and ipv6, hostname, domain names
|
* Generally you will add to this list yours ipv4 and ipv6, hostname, domain names
|
||||||
|
@ -215,7 +220,7 @@ class ApiHandler extends ApiBase
|
||||||
{
|
{
|
||||||
// max age must be less or equal to 365
|
// max age must be less or equal to 365
|
||||||
if ( $maxAgeInDays > 365 || $maxAgeInDays < 1 ){
|
if ( $maxAgeInDays > 365 || $maxAgeInDays < 1 ){
|
||||||
throw new \InvalidArgumentException('maxAgeInDays must be between 1 and 365 (' . $maxAgeInDays . ' was given)');
|
throw new \InvalidArgumentException('maxAgeInDays must be between 1 and 365.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// ip must be set
|
// ip must be set
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*
|
*
|
||||||
* @version 0.9.8
|
* @version 0.9.9
|
||||||
* @copyright 2020-2021 Kristuff
|
* @copyright 2020-2021 Kristuff
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -33,6 +33,13 @@ class ApiResponse
|
||||||
*/
|
*/
|
||||||
protected $curlResponse;
|
protected $curlResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @access protected
|
||||||
|
* @var object
|
||||||
|
*/
|
||||||
|
protected $decodedResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
*
|
*
|
||||||
|
@ -43,6 +50,7 @@ class ApiResponse
|
||||||
public function __construct(?string $plaintext = null)
|
public function __construct(?string $plaintext = null)
|
||||||
{
|
{
|
||||||
$this->curlResponse = $plaintext;
|
$this->curlResponse = $plaintext;
|
||||||
|
$this->decodedResponse = json_decode($plaintext, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,7 +74,7 @@ class ApiResponse
|
||||||
*/
|
*/
|
||||||
public function getObject(): ?object
|
public function getObject(): ?object
|
||||||
{
|
{
|
||||||
return json_decode($this->curlResponse, false);
|
return $this->decodedResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,4 +88,28 @@ class ApiResponse
|
||||||
{
|
{
|
||||||
return $this->curlResponse;
|
return $this->curlResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get whether the response contains error(s)
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasError(): bool
|
||||||
|
{
|
||||||
|
return count($this->errors()) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an array of errors (object) contained is response
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function errors(): array
|
||||||
|
{
|
||||||
|
return ($this->decodedResponse && $this->decodedResponse->errors) ? $this->decodedResponse->errors : [];
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -14,7 +14,7 @@
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*
|
*
|
||||||
* @version 0.9.8
|
* @version 0.9.9
|
||||||
* @copyright 2020-2021 Kristuff
|
* @copyright 2020-2021 Kristuff
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* For the full copyright and license information, please view the LICENSE
|
* For the full copyright and license information, please view the LICENSE
|
||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*
|
*
|
||||||
* @version 0.9.8
|
* @version 0.9.9
|
||||||
* @copyright 2020-2021 Kristuff
|
* @copyright 2020-2021 Kristuff
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
164
lib/SilentApiHandler.php
Normal file
164
lib/SilentApiHandler.php
Normal file
|
@ -0,0 +1,164 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* _ _ ___ ____ ____ ____
|
||||||
|
* / \ | |__ _ _ ___ ___|_ _| _ \| _ \| __ )
|
||||||
|
* / _ \ | '_ \| | | / __|/ _ \| || |_) | | | | _ \
|
||||||
|
* / ___ \| |_) | |_| \__ \ __/| || __/| |_| | |_) |
|
||||||
|
* /_/ \_\_.__/ \__,_|___/\___|___|_| |____/|____/
|
||||||
|
*
|
||||||
|
* This file is part of Kristuff\AbsuseIPDB.
|
||||||
|
*
|
||||||
|
* (c) Kristuff <contact@kristuff.fr>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*
|
||||||
|
* @version 0.9.9
|
||||||
|
* @copyright 2020-2021 Kristuff
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Kristuff\AbuseIPDB;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class SilentApiHandler
|
||||||
|
*
|
||||||
|
* Overwrite ApiHandler with Exception handling
|
||||||
|
* Instead of Exception, all method return an ApiResponse that may
|
||||||
|
* contains errors from the AbuseIPDB API, or internal errors
|
||||||
|
*/
|
||||||
|
class SilentApiHandler extends ApiHandler
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get an internal error message in an ApiResponse object
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $message The error message
|
||||||
|
*
|
||||||
|
* @return ApiResponse
|
||||||
|
*/
|
||||||
|
public function getErrorResponse(string $message): ApiResponse
|
||||||
|
{
|
||||||
|
$response = [
|
||||||
|
"errors" => [
|
||||||
|
[
|
||||||
|
"title" => "Internal Error",
|
||||||
|
"detail" => $message
|
||||||
|
]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
return new ApiResponse(json_encode($response));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a 'report' api request, with Exception handling
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $ip The ip to report
|
||||||
|
* @param string $categories The report category(es)
|
||||||
|
* @param string $message The report message
|
||||||
|
*
|
||||||
|
* @return ApiResponse
|
||||||
|
*/
|
||||||
|
public function report(string $ip, string $categories, string $message): ApiResponse
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return parent::report($ip,$categories,$message);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->getErrorResponse($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a 'bulk-report' api request, with Exception handling
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $filePath The CSV file path. Could be an absolute or relative path.
|
||||||
|
*
|
||||||
|
* @return ApiResponse
|
||||||
|
*/
|
||||||
|
public function bulkReport(string $filePath): ApiResponse
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return parent::bulkReport($filePath);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->getErrorResponse($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform a 'clear-address' api request, with Exception handling
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $ip The IP to clear reports
|
||||||
|
*
|
||||||
|
* @return ApiResponse
|
||||||
|
*/
|
||||||
|
public function clearAddress(string $ip): ApiResponse
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return parent::clearAddress($ip);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->getErrorResponse($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform a 'check' api request, with Exception handling
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $ip The ip to check
|
||||||
|
* @param int $maxAgeInDays Max age in days. Default is 30.
|
||||||
|
* @param bool $verbose True to get the full response (last reports and countryName). Default is false
|
||||||
|
*
|
||||||
|
* @return ApiResponse
|
||||||
|
*/
|
||||||
|
public function check(string $ip, int $maxAgeInDays = 30, bool $verbose = false): ApiResponse
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return parent::check($ip, $maxAgeInDays, $verbose);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->getErrorResponse($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform a 'check-block' api request, with Exception handling
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param string $network The network to check
|
||||||
|
* @param int $maxAgeInDays The Max age in days, must
|
||||||
|
*
|
||||||
|
* @return ApiResponse
|
||||||
|
*/
|
||||||
|
public function checkBlock(string $network, int $maxAgeInDays = 30): ApiResponse
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return parent::checkBlock($network, $maxAgeInDays);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->getErrorResponse($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform a 'blacklist' api request, with Exception handling
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param int $limit The blacklist limit. Default is 10000 (the api default limit)
|
||||||
|
* @param bool $plainText True to get the response in plaintext list. Default is false
|
||||||
|
* @param int $confidenceMinimum The abuse confidence score minimum (subscribers feature). Default is 100.
|
||||||
|
* The confidence minimum must be between 25 and 100.
|
||||||
|
* This parameter is subscriber feature (not honored otherwise).
|
||||||
|
*
|
||||||
|
* @return ApiResponse
|
||||||
|
*/
|
||||||
|
public function blacklist(int $limit = 10000, bool $plainText = false, int $confidenceMinimum = 100): ApiResponse
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return parent::blacklist($limit, $plainText, $confidenceMinimum);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->getErrorResponse($e->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue