From 85de369a503897ab026e5cc889cdb2a9a4a49565 Mon Sep 17 00:00:00 2001 From: Thomas Deinhamer Date: Thu, 25 Jun 2015 23:13:35 +0200 Subject: [PATCH] Add initial files --- .editorconfig | 16 + .gitattributes | 1 + .gitignore | 1 + CHANGELOG.md | 5 + LICENSE.md | 21 + README.md | 124 +++++ composer.json | 24 + composer.lock | 970 ++++++++++++++++++++++++++++++++++++++++ src/Blacklist.php | 413 +++++++++++++++++ tests/BlacklistTest.php | 103 +++++ 10 files changed, 1678 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 src/Blacklist.php create mode 100644 tests/BlacklistTest.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..66eda49 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +charset = utf-8 +indent_style = tab +end_of_line = lf +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +indent_style = space +indent_size = 2 +trim_trailing_whitespace = false + +[composer.json] +indent_style = space diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2447160 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Change Log + +## [0.1.0] - 2015-06-25 + +Initial release. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..bce460b --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Thomas "Thasmo" Deinhamer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..aad551e --- /dev/null +++ b/README.md @@ -0,0 +1,124 @@ +# Project Honeypot Http:BL Library + +A simple PHP library for querying the [Project Honeypot Http:BL API](http://www.projecthoneypot.org/httpbl_api.php). + +## Usage + +### Create a new instance +```php +use Thasmo\ProjectHoneypot\Blacklist; +$client = new Blacklist('127.0.0.1', 'api-key'); +``` + +### Create multiple instances +```php +use Thasmo\ProjectHoneypot\Blacklist; + +# Set default API key. +Blacklist::setDefaultKey('api-key'); + +# Use the default API key. +$clientOne = new Blacklist('127.0.0.1'); + +# Use a specific API key. +$clientTwo = new Blacklist('127.0.0.2', 'other-api-key'); + +# Use the default API key, again. +$clientThree = new Blacklist('127.0.0.3'); +``` + +### Check for various types of clients +```php +# Client is a search engine. +$client->isSearchEngine(); + +# Client is suspicious. +$client->isSuspicious(); + +# Client is a harvester. +$client->isHarvester() + +# Client is a spammer. +$client->isSpammer(); + +# Client is blacklisted. +# Which means it is suspicious, a harvester or a spammer but not a search engine. +$client->isListed(); +``` + +### Get last activity +```php +# Get the last activity for the client in days. +$lastActivity = $client->getActivity(); +``` + +### Get threat score +```php +# Get the threat score of the client. +$threatScore = $client->getThreat(); +``` + +### Check last activity +```php +# Check if the client was active in the last 10 days. +$isActive = $client->isActive(10); +``` + +### Check threat score +```php +# Check if the threat score is within the limit of 100. +$isThreat = $client->isThreat(100); +``` + +### Get the name for a search engine +```php +# Get the name of the search engine. +if($client->isSearchEngine()) { + $name = $client->getName(); +} +``` + +### Get the API result +```php +# Return an array holding the result from the API call +$result = $client->getResult(); +``` + +### Change the address +```php +use Thasmo\ProjectHoneypot\Blacklist; + +# Create an instance +$client = new Blacklist('127.0.0.1', 'api-key'); + +# Get the result +$result1 = $client->getResult(); + +# Set a new address which resets the object +$client->setAddress('127.0.0.2'); + +# Get the new result +$result2 = $client->getResult(); +``` + +### Query the API +```php +use Thasmo\ProjectHoneypot\Blacklist; + +# Create an instance +$client = new Blacklist('127.0.0.1', 'api-key'); + +# Query the API immediately +$client->query(); + +# Use other methods +if($client->isSearchEngine()) { + $name = $client->getName(); +} +``` + +## Implementation Details + +* Requests to the API are delayed until you first call a method like `isSearchEngine` etc. or `query` explicitly. +* API responses for the same IP address on the same instance will be cached, the API will be queried only once. +* When changing the IP address via `setAddress` the cache is cleared and the API will be queried again. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6e986d1 --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "thasmo/honeypot-blacklist", + "description": "", + "type": "library", + "keywords": ["honeypot", "blacklist", "spam", "api", "http"], + "license": "MIT", + "authors": [ + { + "name": "Thomas Deinhamer", + "email": "thasmo@gmail.com" + } + ], + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "4.7.*" + }, + "autoload": { + "psr-4": { + "Thasmo\\ProjectHoneypot\\": "src/" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..998f4cd --- /dev/null +++ b/composer.lock @@ -0,0 +1,970 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "fcb3a4567ca7b6ef97f2015a1002508e", + "packages": [], + "packages-dev": [ + { + "name": "doctrine/instantiator", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/doctrine/instantiator.git", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/instantiator/zipball/8e884e78f9f0eb1329e445619e04456e64d8051d", + "reference": "8e884e78f9f0eb1329e445619e04456e64d8051d", + "shasum": "" + }, + "require": { + "php": ">=5.3,<8.0-DEV" + }, + "require-dev": { + "athletic/athletic": "~0.1.8", + "ext-pdo": "*", + "ext-phar": "*", + "phpunit/phpunit": "~4.0", + "squizlabs/php_codesniffer": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Instantiator\\": "src/Doctrine/Instantiator/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "http://ocramius.github.com/" + } + ], + "description": "A small, lightweight utility to instantiate objects in PHP without invoking their constructors", + "homepage": "https://github.com/doctrine/instantiator", + "keywords": [ + "constructor", + "instantiate" + ], + "time": "2015-06-14 21:17:01" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "2.0.4", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/d68dbdc53dc358a816f00b300704702b2eaff7b8", + "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "suggest": { + "dflydev/markdown": "~1.0", + "erusev/parsedown": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "phpDocumentor": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "mike.vanriel@naenius.com" + } + ], + "time": "2015-02-03 12:10:50" + }, + { + "name": "phpspec/prophecy", + "version": "v1.4.1", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "3132b1f44c7bf2ec4c7eb2d3cb78fdeca760d373" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/3132b1f44c7bf2ec4c7eb2d3cb78fdeca760d373", + "reference": "3132b1f44c7bf2ec4c7eb2d3cb78fdeca760d373", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "^1.0.2", + "phpdocumentor/reflection-docblock": "~2.0", + "sebastian/comparator": "~1.1" + }, + "require-dev": { + "phpspec/phpspec": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "https://github.com/phpspec/prophecy", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2015-04-27 22:15:08" + }, + { + "name": "phpunit/php-code-coverage", + "version": "2.1.6", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-code-coverage.git", + "reference": "631e365cf26bb2c078683e8d9bcf8bc631ac4d44" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/631e365cf26bb2c078683e8d9bcf8bc631ac4d44", + "reference": "631e365cf26bb2c078683e8d9bcf8bc631ac4d44", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "phpunit/php-file-iterator": "~1.3", + "phpunit/php-text-template": "~1.2", + "phpunit/php-token-stream": "~1.3", + "sebastian/environment": "~1.0", + "sebastian/version": "~1.0" + }, + "require-dev": { + "ext-xdebug": ">=2.1.4", + "phpunit/phpunit": "~4" + }, + "suggest": { + "ext-dom": "*", + "ext-xdebug": ">=2.2.1", + "ext-xmlwriter": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Library that provides collection, processing, and rendering functionality for PHP code coverage information.", + "homepage": "https://github.com/sebastianbergmann/php-code-coverage", + "keywords": [ + "coverage", + "testing", + "xunit" + ], + "time": "2015-06-19 07:11:55" + }, + { + "name": "phpunit/php-file-iterator", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-file-iterator.git", + "reference": "a923bb15680d0089e2316f7a4af8f437046e96bb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/a923bb15680d0089e2316f7a4af8f437046e96bb", + "reference": "a923bb15680d0089e2316f7a4af8f437046e96bb", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "FilterIterator implementation that filters files based on a list of suffixes.", + "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", + "keywords": [ + "filesystem", + "iterator" + ], + "time": "2015-04-02 05:19:05" + }, + { + "name": "phpunit/php-text-template", + "version": "1.2.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-text-template.git", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-text-template/zipball/31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "reference": "31f8b717e51d9a2afca6c9f046f5d69fc27c8686", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Simple template engine.", + "homepage": "https://github.com/sebastianbergmann/php-text-template/", + "keywords": [ + "template" + ], + "time": "2015-06-21 13:50:34" + }, + { + "name": "phpunit/php-timer", + "version": "1.0.6", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-timer.git", + "reference": "83fe1bdc5d47658b727595c14da140da92b3d66d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/83fe1bdc5d47658b727595c14da140da92b3d66d", + "reference": "83fe1bdc5d47658b727595c14da140da92b3d66d", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for timing", + "homepage": "https://github.com/sebastianbergmann/php-timer/", + "keywords": [ + "timer" + ], + "time": "2015-06-13 07:35:30" + }, + { + "name": "phpunit/php-token-stream", + "version": "1.4.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-token-stream.git", + "reference": "7a9b0969488c3c54fd62b4d504b3ec758fd005d9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/7a9b0969488c3c54fd62b4d504b3ec758fd005d9", + "reference": "7a9b0969488c3c54fd62b4d504b3ec758fd005d9", + "shasum": "" + }, + "require": { + "ext-tokenizer": "*", + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Wrapper around PHP's tokenizer extension.", + "homepage": "https://github.com/sebastianbergmann/php-token-stream/", + "keywords": [ + "tokenizer" + ], + "time": "2015-06-19 03:43:16" + }, + { + "name": "phpunit/phpunit", + "version": "4.7.5", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit.git", + "reference": "f6701ef3faea759acd1910a7751d8d102a7fd5bc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f6701ef3faea759acd1910a7751d8d102a7fd5bc", + "reference": "f6701ef3faea759acd1910a7751d8d102a7fd5bc", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "ext-json": "*", + "ext-pcre": "*", + "ext-reflection": "*", + "ext-spl": "*", + "php": ">=5.3.3", + "phpspec/prophecy": "~1.3,>=1.3.1", + "phpunit/php-code-coverage": "~2.1", + "phpunit/php-file-iterator": "~1.4", + "phpunit/php-text-template": "~1.2", + "phpunit/php-timer": ">=1.0.6", + "phpunit/phpunit-mock-objects": "~2.3", + "sebastian/comparator": "~1.1", + "sebastian/diff": "~1.2", + "sebastian/environment": "~1.2", + "sebastian/exporter": "~1.2", + "sebastian/global-state": "~1.0", + "sebastian/version": "~1.0", + "symfony/yaml": "~2.1|~3.0" + }, + "suggest": { + "phpunit/php-invoker": "~1.1" + }, + "bin": [ + "phpunit" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.7.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "The PHP Unit Testing framework.", + "homepage": "https://phpunit.de/", + "keywords": [ + "phpunit", + "testing", + "xunit" + ], + "time": "2015-06-21 07:23:36" + }, + { + "name": "phpunit/phpunit-mock-objects", + "version": "2.3.4", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", + "reference": "92408bb1968a81b3217a6fdf6c1a198da83caa35" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/92408bb1968a81b3217a6fdf6c1a198da83caa35", + "reference": "92408bb1968a81b3217a6fdf6c1a198da83caa35", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "~1.0,>=1.0.2", + "php": ">=5.3.3", + "phpunit/php-text-template": "~1.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "suggest": { + "ext-soap": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Mock Object library for PHPUnit", + "homepage": "https://github.com/sebastianbergmann/phpunit-mock-objects/", + "keywords": [ + "mock", + "xunit" + ], + "time": "2015-06-11 15:55:48" + }, + { + "name": "sebastian/comparator", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/comparator.git", + "reference": "1dd8869519a225f7f2b9eb663e225298fade819e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1dd8869519a225f7f2b9eb663e225298fade819e", + "reference": "1dd8869519a225f7f2b9eb663e225298fade819e", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/diff": "~1.2", + "sebastian/exporter": "~1.2" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides the functionality to compare PHP values for equality", + "homepage": "http://www.github.com/sebastianbergmann/comparator", + "keywords": [ + "comparator", + "compare", + "equality" + ], + "time": "2015-01-29 16:28:08" + }, + { + "name": "sebastian/diff", + "version": "1.3.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/diff.git", + "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/863df9687835c62aa423a22412d26fa2ebde3fd3", + "reference": "863df9687835c62aa423a22412d26fa2ebde3fd3", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Kore Nordmann", + "email": "mail@kore-nordmann.de" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Diff implementation", + "homepage": "http://www.github.com/sebastianbergmann/diff", + "keywords": [ + "diff" + ], + "time": "2015-02-22 15:13:53" + }, + { + "name": "sebastian/environment", + "version": "1.2.2", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/environment.git", + "reference": "5a8c7d31914337b69923db26c4221b81ff5a196e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/5a8c7d31914337b69923db26c4221b81ff5a196e", + "reference": "5a8c7d31914337b69923db26c4221b81ff5a196e", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Provides functionality to handle HHVM/PHP environments", + "homepage": "http://www.github.com/sebastianbergmann/environment", + "keywords": [ + "Xdebug", + "environment", + "hhvm" + ], + "time": "2015-01-01 10:01:08" + }, + { + "name": "sebastian/exporter", + "version": "1.2.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/exporter.git", + "reference": "84839970d05254c73cde183a721c7af13aede943" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/84839970d05254c73cde183a721c7af13aede943", + "reference": "84839970d05254c73cde183a721c7af13aede943", + "shasum": "" + }, + "require": { + "php": ">=5.3.3", + "sebastian/recursion-context": "~1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Volker Dusch", + "email": "github@wallbash.com" + }, + { + "name": "Bernhard Schussek", + "email": "bschussek@2bepublished.at" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides the functionality to export PHP variables for visualization", + "homepage": "http://www.github.com/sebastianbergmann/exporter", + "keywords": [ + "export", + "exporter" + ], + "time": "2015-01-27 07:23:06" + }, + { + "name": "sebastian/global-state", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "c7428acdb62ece0a45e6306f1ae85e1c05b09c01" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/c7428acdb62ece0a45e6306f1ae85e1c05b09c01", + "reference": "c7428acdb62ece0a45e6306f1ae85e1c05b09c01", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2014-10-06 09:23:50" + }, + { + "name": "sebastian/recursion-context", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "3989662bbb30a29d20d9faa04a846af79b276252" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/3989662bbb30a29d20d9faa04a846af79b276252", + "reference": "3989662bbb30a29d20d9faa04a846af79b276252", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2015-01-24 09:48:32" + }, + { + "name": "sebastian/version", + "version": "1.0.6", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/version.git", + "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", + "reference": "58b3a85e7999757d6ad81c787a1fbf5ff6c628c6", + "shasum": "" + }, + "type": "library", + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de", + "role": "lead" + } + ], + "description": "Library that helps with managing the version number of Git-hosted PHP projects", + "homepage": "https://github.com/sebastianbergmann/version", + "time": "2015-06-21 13:59:46" + }, + { + "name": "symfony/yaml", + "version": "v2.7.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/Yaml.git", + "reference": "9808e75c609a14f6db02f70fccf4ca4aab53c160" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/9808e75c609a14f6db02f70fccf4ca4aab53c160", + "reference": "9808e75c609a14f6db02f70fccf4ca4aab53c160", + "shasum": "" + }, + "require": { + "php": ">=5.3.9" + }, + "require-dev": { + "symfony/phpunit-bridge": "~2.7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2015-06-10 15:30:22" + } + ], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.5.0" + }, + "platform-dev": [] +} diff --git a/src/Blacklist.php b/src/Blacklist.php new file mode 100644 index 0000000..bc67289 --- /dev/null +++ b/src/Blacklist.php @@ -0,0 +1,413 @@ + 'AltaVista', + 2 => 'Ask', + 3 => 'Baidu', + 4 => 'Excite', + 5 => 'Google', + 6 => 'Looksmart', + 7 => 'Lycos', + 8 => 'MSN', + 9 => 'Yahoo', + 10 => 'Cuil', + 11 => 'InfoSeek', + ]; + + /** + * Create a new instance. + * @param string $address + * @param string $key + */ + public function __construct($address, $key = NULL) { + $this->setAddress($address); + + if(!$key) { + $key = static::getDefaultKey(); + } + + $this->setKey($key); + } + + /** + * Validate an API key. + * @param string $key + * @return bool + */ + public static function validateKey($key) { + if(preg_match('/^[a-z]{12}$/', $key)) { + return TRUE; + } + + return FALSE; + } + + /** + * Validate an IPv4 address. + * @param string $address + * @return bool + */ + public static function validateAddress($address) { + if(filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) { + return TRUE; + } + + return FALSE; + } + + /** + * Get the default API key. + * @return string|NULL + */ + public static function getDefaultKey() { + return static::$defaultKey; + } + + /** + * Set the default API key. + * @param $key + */ + public static function setDefaultKey($key) { + if(!static::validateKey($key)) { + throw new InvalidArgumentException('Default API key is invalid.'); + } + + static::$defaultKey = $key; + } + + /** + * Reset the default API key. + */ + public static function unsetDefaultKey() { + static::$defaultKey = NULL; + } + + /** + * Get the API key. + * @return string|NULL + */ + public function getKey() { + return $this->key; + } + + /** + * Set the API key. + * @param string $key + */ + public function setKey($key) { + if(!static::validateKey($key)) { + throw new InvalidArgumentException('API key is invalid.'); + } + + $this->key = $key; + } + + /** + * Get the IP address. + * @return string|NULL + */ + public function getAddress() { + return $this->address; + } + + /** + * Set the IP address. + * @param string $address + */ + public function setAddress($address) { + if(!static::validateAddress($address)) { + throw new InvalidArgumentException('IP address is invalid.'); + } + + if($address != $this->getAddress()) { + $this->reset(); + } + + $this->address = $address; + } + + /** + * Get the transformed API result. + * @param string $key + * @return array|int|NULL + */ + public function getResult($key = NULL) { + $this->query(); + + if(!is_null($key)) { + return array_key_exists($key, $this->result) + ? $this->result[$key] + : NULL; + } + + return $this->result; + } + + /** + * Indicate whether the IP address refers to a search engine or not. + * @param int $type + * @return bool + */ + public function isSearchEngine($type = NULL) { + $this->query(); + + if($this->getType() != static::TYPE_SEARCH_ENGINE) { + return FALSE; + } + + if(!is_null($type) && $type != $this->getThreat()) { + return FALSE; + } + + return TRUE; + } + + /** + * Indicate whether the API address is known for suspicious behavior or not. + * @return bool + */ + public function isListed() { + $this->query(); + return (bool) ($this->getType() > static::TYPE_SEARCH_ENGINE); + } + + /** + * Indicate whether the IP address is suspicious or not. + * @return bool + */ + public function isSuspicious() { + $this->query(); + return (bool) ($this->getType() & static::TYPE_SUSPICIOUS); + } + + /** + * Indicate whether the IP address refers to a harvester or not. + * @return bool + */ + public function isHarvester() { + $this->query(); + return (bool) ($this->getType() & static::TYPE_HARVESTER); + } + + /** + * Indicate whether the IP address refers to a spammer or not. + * @return bool + */ + public function isSpammer() { + $this->query(); + return (bool) ($this->getType() & static::TYPE_SPAMMER); + } + + /** + * Get the IP's last activity in days. + * @return int|NULL + */ + public function getActivity() { + $this->query(); + return $this->getResult('activity'); + } + + /** + * Get the IP's threat score. + * @return int + */ + public function getThreat() { + $this->query(); + return $this->getResult('threat'); + } + + /** + * Get the IP's type. + * @return int + */ + public function getType() { + $this->query(); + return $this->getResult('type'); + } + + /** + * Check if the IP was active within the given number of days. + * @param int $days + * @return bool + */ + public function isActive($days) { + $this->query(); + return $days >= $this->getActivity(); + } + + /** + * Check if the IP's threat score is within in the given score. + * @param int $threat + * @return bool + */ + public function isThreat($threat) { + $this->query(); + return $threat <= $this->getThreat(); + } + + /** + * Get the IP's search engine name if applicable. + * @return bool|FALSE + */ + public function getName() { + $this->query(); + + if(!$this->isSearchEngine()) { + return FALSE; + } + + $identifier = $this->getThreat(); + + if(in_array($identifier, [static::CLIENT_UNKNOWN, static::CLIENT_OTHER])) { + return FALSE; + } + + return $this->names[$identifier]; + } + + /** + * Query the API for data and format the response. + * @param bool $force + * @return array|FALSE + */ + public function query($force = FALSE) { + if($this->queried && !$force) { + return $this->getResult(); + } + + # format address + $host = $this->buildHost(); + + # lookup address + $result = $this->lookup($host); + + # set flag + $this->queried = TRUE; + + if($result == $host) { + return FALSE; + } + + return $this->result = $this->formatResult($result); + } + + /** + * Get host information via API call. + * @param string $host + * @return string + */ + protected function lookup($host) { + return gethostbyname($host); + } + + /** + * Build the host for the API call. + * @return string + */ + protected function buildHost() { + + # retrieve key + $key = $this->getKey(); + + # prepare address + $address = $this->getAddress(); + $address = explode('.', $address); + krsort($address); + $address = implode('.', $address); + + # build hostname + return implode('.', [ + $key, + $address, + static::API_HOST + ]); + } + + /** + * Format the raw API result. + * @param string $result + * @return array + */ + protected function formatResult($result) { + + # split and ... + $result = explode('.', $result); + + # ... structure result + return [ + 'activity' => (int) $result[1], + 'threat' => (int) $result[2], + 'type' => (int) $result[3], + ]; + } + + /** + * Reset the state of the object. + */ + protected function reset() { + $this->result = NULL; + $this->queried = FALSE; + } +} diff --git a/tests/BlacklistTest.php b/tests/BlacklistTest.php new file mode 100644 index 0000000..0c87b9a --- /dev/null +++ b/tests/BlacklistTest.php @@ -0,0 +1,103 @@ +invalidAddress(); + $key = $this->validKey(); + + new Blacklist($address, $key); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testInvalidKey() { + $address = $this->validAddress(); + $key = $this->invalidKey(); + + new Blacklist($address, $key); + } + + public function testValidateKey() { + $validKey = $this->validKey(); + $invalidKey = $this->invalidKey(); + + $this->assertTrue(Blacklist::validateKey($validKey)); + $this->assertFalse(Blacklist::validateKey($invalidKey)); + $this->assertFalse(Blacklist::validateKey(NULL)); + } + + public function testValidateAddress() { + $validAddress = $this->validAddress(); + $invalidAddress = $this->invalidAddress(); + + $this->assertTrue(Blacklist::validateAddress($validAddress)); + $this->assertFalse(Blacklist::validateAddress($invalidAddress)); + $this->assertFalse(Blacklist::validateAddress(NULL)); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testDefaultKey() { + $validKey = $this->validKey(); + $invalidKey = $this->invalidKey(); + + # set/get valid key + Blacklist::setDefaultKey($validKey); + $this->assertEquals($validKey, Blacklist::getDefaultKey()); + + # unset/get empty key + Blacklist::unsetDefaultKey(); + $this->assertEmpty(Blacklist::getDefaultKey()); + + # set invalid key + Blacklist::setDefaultKey($invalidKey); + } + + public function testQuery() { + $dummy = $this->dummyBlacklistforQuery('127.1.2.3'); + + $this->assertEquals([ + 'activity' => 1, + 'threat' => 2, + 'type' => 3 + ], $dummy->query()); + } + + private function validKey() { + return 'abcdefghijkl'; + } + + private function invalidKey() { + return '123'; + } + + private function validAddress() { + return '127.0.0.1'; + } + + private function invalidAddress() { + return '192.168.0.1'; + } + + private function dummyBlacklistforQuery($result) { + $dummy = $this->getMockBuilder('Thasmo\ProjectHoneypot\Blacklist') + ->disableOriginalConstructor() + ->setMethods(['lookup']) + ->getMock(); + + $dummy->method('lookup') + ->will($this->returnValue($result)); + + return $dummy; + } +}