**Changed**
- report categories methods related now static
- `SilentApiHandler` class renamed `QuietApiHandler` **break change**
- new static method `ApiResponse::createErrorResponse`
This commit is contained in:
kristuff 2021-01-29 20:30:12 +01:00
commit 77c2857eb8
8 changed files with 63 additions and 56 deletions

View file

@ -32,7 +32,7 @@ Deploy with composer:
```json ```json
... ...
"require": { "require": {
"kristuff/abuseipdb": ">=0.9.9-stable" "kristuff/abuseipdb": ">=0.9.10-stable"
}, },
``` ```

View file

@ -2,6 +2,7 @@
"name": "kristuff/abuseipdb", "name": "kristuff/abuseipdb",
"description": "A PHP wrapper for AbuseIPDB API v2", "description": "A PHP wrapper for AbuseIPDB API v2",
"type": "library", "type": "library",
"keywords": ["abuseIPDB", "API"],
"license": "MIT", "license": "MIT",
"authors": [ "authors": [
{ {

View file

@ -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.9 * @version 0.9.10
* @copyright 2020-2021 Kristuff * @copyright 2020-2021 Kristuff
*/ */
@ -47,9 +47,10 @@ abstract class ApiBase
* shorname, id (string), long name * shorname, id (string), long name
* last paramter is false when the category cant' be used alone * last paramter is false when the category cant' be used alone
* *
* @static
* @var array * @var array
*/ */
protected $aipdbApiCategories = [ protected static $aipdbApiCategories = [
// Altering DNS records resulting in improper redirection. // Altering DNS records resulting in improper redirection.
['dns-c' , '1', 'DNS Compromise', true], ['dns-c' , '1', 'DNS Compromise', true],
@ -134,25 +135,27 @@ abstract class ApiBase
* Get the list of report categories * Get the list of report categories
* *
* @access public * @access public
* @static
* *
* @return array * @return array
*/ */
public function getCategories() public static function getCategories(): array
{ {
return $this->aipdbApiCategories; return self::$aipdbApiCategories;
} }
/** /**
* Get the category id corresponding to given name * Get the category id corresponding to given name
* *
* @access public * @access public
* @param string $categoryName The report categoriy name * @static
* @param string $categoryName The report category name
* *
* @return string|bool The category id in string format if found, otherwise false * @return string|bool The category id in string format if found, otherwise false
*/ */
public function getCategoryIdbyName(string $categoryName) public static function getCategoryIdbyName(string $categoryName)
{ {
foreach ($this->aipdbApiCategories as $cat){ foreach (self::$aipdbApiCategories as $cat){
if ($cat[0] === $categoryName) { if ($cat[0] === $categoryName) {
return $cat; return $cat;
} }
@ -166,13 +169,14 @@ abstract class ApiBase
* Get the category name corresponding to given id * Get the category name corresponding to given id
* *
* @access public * @access public
* @static
* @param string $categoryId The report category id * @param string $categoryId The report category id
* *
* @return string|bool The category name if found, otherwise false * @return string|bool The category name if found, otherwise false
*/ */
public function getCategoryNameById(string $categoryId) public static function getCategoryNameById(string $categoryId)
{ {
foreach ($this->aipdbApiCategories as $cat){ foreach (self::$aipdbApiCategories as $cat){
if ($cat[1] === $categoryId) { if ($cat[1] === $categoryId) {
return $cat; return $cat;
} }
@ -186,15 +190,16 @@ abstract class ApiBase
* Get the index of category corresponding to given value * Get the index of category corresponding to given value
* *
* @access protected * @access protected
* @static
* @param string $value The report category id or name * @param string $value The report category id or name
* @param string $index The index in value array * @param string $index The index in value array
* *
* @return int|bool The category index if found, otherwise false * @return int|bool The category index if found, otherwise false
*/ */
protected function getCategoryIndex(string $value, int $index) protected static function getCategoryIndex(string $value, int $index)
{ {
$i = 0; $i = 0;
foreach ($this->aipdbApiCategories as $cat){ foreach (self::$aipdbApiCategories as $cat){
if ($cat[$index] === $value) { if ($cat[$index] === $value) {
return $i; return $i;
} }
@ -229,7 +234,7 @@ abstract class ApiBase
foreach ($cats as $cat) { foreach ($cats as $cat) {
// get index on our array of categories // get index on our array of categories
$catIndex = is_numeric($cat) ? $this->getCategoryIndex($cat, 1) : $this->getCategoryIndex($cat, 0); $catIndex = is_numeric($cat) ? self::getCategoryIndex($cat, 1) : self::getCategoryIndex($cat, 0);
// check if found // check if found
if ($catIndex === false ){ if ($catIndex === false ){
@ -237,13 +242,13 @@ abstract class ApiBase
} }
// get Id // get Id
$catId = $this->aipdbApiCategories[$catIndex][1]; $catId = self::$aipdbApiCategories[$catIndex][1];
// need another ? // need another ?
if ($needAnother !== false){ if ($needAnother !== false){
// is a standalone cat ? // is a standalone cat ?
if ($this->aipdbApiCategories[$catIndex][3] === false) { if (self::$aipdbApiCategories[$catIndex][3] === false) {
$needAnother = true; $needAnother = true;
} else { } else {

View file

@ -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.9 * @version 0.9.10
* @copyright 2020-2021 Kristuff * @copyright 2020-2021 Kristuff
*/ */
@ -33,9 +33,9 @@ class ApiHandler extends ApiBase
use CurlTrait; use CurlTrait;
/** /**
* @var string $version * @var string
*/ */
const VERSION = 'v0.9.9'; const VERSION = 'v0.9.10';
/** /**
* The ips to remove from report messages * The ips to remove from report messages
@ -314,7 +314,7 @@ class ApiHandler extends ApiBase
* @param bool $plainText True to get the response in plaintext list. Default is false * @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. * @param int $confidenceMinimum The abuse confidence score minimum (subscribers feature). Default is 100.
* The confidence minimum must be between 25 and 100. * The confidence minimum must be between 25 and 100.
* This parameter is subscriber feature (not honored otherwise). * This parameter is a subscriber feature (not honored otherwise).
* *
* @return ApiResponse * @return ApiResponse
* @throws \RuntimeException * @throws \RuntimeException

View file

@ -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.9 * @version 0.9.10
* @copyright 2020-2021 Kristuff * @copyright 2020-2021 Kristuff
*/ */
@ -50,7 +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); $this->decodedResponse = !empty($plaintext) ? json_decode($plaintext, false) : null;
} }
/** /**
@ -112,4 +112,27 @@ class ApiResponse
{ {
return ($this->decodedResponse && $this->decodedResponse->errors) ? $this->decodedResponse->errors : []; return ($this->decodedResponse && $this->decodedResponse->errors) ? $this->decodedResponse->errors : [];
} }
/**
* Get an internal error message in an ApiResponse object
*
* @access public
* @static
* @param string $message The error message
*
* @return ApiResponse
*/
public static function createErrorResponse(string $message): ApiResponse
{
$response = [
"errors" => [
[
"title" => "Internal Error",
"detail" => $message
]
]
];
return new ApiResponse(json_encode($response));
}
} }

View file

@ -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.9 * @version 0.9.10
* @copyright 2020-2021 Kristuff * @copyright 2020-2021 Kristuff
*/ */

View file

@ -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.9 * @version 0.9.10
* @copyright 2020-2021 Kristuff * @copyright 2020-2021 Kristuff
*/ */

View file

@ -14,43 +14,21 @@
* 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.9 * @version 0.9.10
* @copyright 2020-2021 Kristuff * @copyright 2020-2021 Kristuff
*/ */
namespace Kristuff\AbuseIPDB; namespace Kristuff\AbuseIPDB;
/** /**
* Class SilentApiHandler * Class QuietApiHandler
* *
* Overwrite ApiHandler with Exception handling * Overwrite ApiHandler with Exception handling
* Instead of Exception, all method return an ApiResponse that may * Instead of Exception, all methods return an ApiResponse that may
* contains errors from the AbuseIPDB API, or internal errors * contains errors from the AbuseIPDB API, or internal errors
*/ */
class SilentApiHandler extends ApiHandler class QuietApiHandler 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 * Performs a 'report' api request, with Exception handling
* *
@ -66,7 +44,7 @@ class SilentApiHandler extends ApiHandler
try { try {
return parent::report($ip,$categories,$message); return parent::report($ip,$categories,$message);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->getErrorResponse($e->getMessage()); return ApiResponse::createErrorResponse($e->getMessage());
} }
} }
@ -83,7 +61,7 @@ class SilentApiHandler extends ApiHandler
try { try {
return parent::bulkReport($filePath); return parent::bulkReport($filePath);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->getErrorResponse($e->getMessage()); return ApiResponse::createErrorResponse($e->getMessage());
} }
} }
@ -100,7 +78,7 @@ class SilentApiHandler extends ApiHandler
try { try {
return parent::clearAddress($ip); return parent::clearAddress($ip);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->getErrorResponse($e->getMessage()); return ApiResponse::createErrorResponse($e->getMessage());
} }
} }
@ -119,7 +97,7 @@ class SilentApiHandler extends ApiHandler
try { try {
return parent::check($ip, $maxAgeInDays, $verbose); return parent::check($ip, $maxAgeInDays, $verbose);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->getErrorResponse($e->getMessage()); return ApiResponse::createErrorResponse($e->getMessage());
} }
} }
@ -137,7 +115,7 @@ class SilentApiHandler extends ApiHandler
try { try {
return parent::checkBlock($network, $maxAgeInDays); return parent::checkBlock($network, $maxAgeInDays);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->getErrorResponse($e->getMessage()); return ApiResponse::createErrorResponse($e->getMessage());
} }
} }
@ -149,7 +127,7 @@ class SilentApiHandler extends ApiHandler
* @param bool $plainText True to get the response in plaintext list. Default is false * @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. * @param int $confidenceMinimum The abuse confidence score minimum (subscribers feature). Default is 100.
* The confidence minimum must be between 25 and 100. * The confidence minimum must be between 25 and 100.
* This parameter is subscriber feature (not honored otherwise). * This parameter is a subscriber feature (not honored otherwise).
* *
* @return ApiResponse * @return ApiResponse
*/ */
@ -158,7 +136,7 @@ class SilentApiHandler extends ApiHandler
try { try {
return parent::blacklist($limit, $plainText, $confidenceMinimum); return parent::blacklist($limit, $plainText, $confidenceMinimum);
} catch (\Exception $e) { } catch (\Exception $e) {
return $this->getErrorResponse($e->getMessage()); return ApiResponse::createErrorResponse($e->getMessage());
} }
} }
} }