From c76fdf936d043fb1d5855b79c5a4da47f2400b2b Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Wed, 17 Sep 2014 12:50:54 -0400 Subject: [PATCH] Preparing for a formal release Converted tabs to spaces, did some cleanup and updated the composer file. --- LICENSE | 23 ++- README.md | 36 ++-- composer.json | 35 ++-- src/ProjectHoneyPot.php | 231 +++++++++++----------- tests/ProjectHoneyPotTest.php | 363 +++++++++++++++++----------------- 5 files changed, 351 insertions(+), 337 deletions(-) diff --git a/LICENSE b/LICENSE index 92a58f0..be2bff2 100644 --- a/LICENSE +++ b/LICENSE @@ -2,20 +2,19 @@ The MIT License (MIT) Copyright (c) 2012-2014 Josh Sherman -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: +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. +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 index 13202a4..deef3ce 100644 --- a/README.md +++ b/README.md @@ -5,20 +5,16 @@ [![Downloads](http://img.shields.io/packagist/dm/joshtronic/php-projecthoneypot.svg?style=flat)][packagist] [![Gittip](http://img.shields.io/gittip/joshtronic.svg?style=flat)][gittip] -[travis]: http://travis-ci.org/joshtronic/php-projecthoneypot -[coveralls]: https://coveralls.io/r/joshtronic/php-projecthoneypot -[packagist]: https://packagist.org/packages/joshtronic/php-projecthoneypot -[gittip]: https://www.gittip.com/joshtronic/ - PHP Wrapper for Project Honey Pot. Compatible with PHP 5.3+ and HHVM. ## Installation -The preferred installation method is via `composer`. First add the following to your `composer.json` +The preferred installation method is via `composer`. First add the following +to your `composer.json` ```json "require": { - "joshtronic/php-projecthoneypot": "dev-master" + "joshtronic/php-projecthoneypot": "dev-master" } ``` @@ -42,11 +38,14 @@ print_r($results); ### Results -Queries will return an array of information about the IP address. The array will contain `last_activity`, `threat_score` and an array of `categories`. +Queries will return an array of information about the IP address. The array +will contain `last_activity`, `threat_score` and an array of `categories`. ### Simulating Results -The folks at Project Honey Pot were kind enough to include a way to simulate results from their http:BL API. You can do so by performing queries against the following IP addresses. +The folks at Project Honey Pot were kind enough to include a way to simulate +results from their http:BL API. You can do so by performing queries against +the following IP addresses. #### Visitor Types @@ -75,10 +74,21 @@ The folks at Project Honey Pot were kind enough to include a way to simulate res ## Contributing -Suggestions and bug reports are always welcome, but karma points are earned for pull requests. +Suggestions and bug reports are always welcome, but karma points are earned +for pull requests. -Unit tests are required for all contributions. You can run the test suite from the `tests` directory simply by running `phpunit .` +Unit tests are required for all contributions. You can run the test suite from +the `tests` directory simply by running `phpunit .` -I also urge you to install a honey pot and donate some MX records to Project Honey Pot. They are doing great work, they provide http:BL as a free service and your donations help make their service ever better! +I also urge you to install a honey pot and donate some MX records to Project +Honey Pot. They are doing great work, they provide http:BL as a free service +and your donations help make their service ever better! -If you’re not already a member, please sign up today with [my referral code](http://www.projecthoneypot.org?rf=123193). +If you’re not already a member, please sign up today with +[my referral code][referral]. + +[coveralls]: https://coveralls.io/r/joshtronic/php-projecthoneypot +[gittip]: https://www.gittip.com/joshtronic/ +[packagist]: https://packagist.org/packages/joshtronic/php-projecthoneypot +[referral]: http://www.projecthoneypot.org?rf=123193 +[travis]: http://travis-ci.org/joshtronic/php-projecthoneypot diff --git a/composer.json b/composer.json index ef1fc0f..e7d9e51 100644 --- a/composer.json +++ b/composer.json @@ -1,16 +1,23 @@ { - "name": "joshtronic/php-projecthoneypot", - "description": "PHP Wrapper for Project Honey Pot", - "license": "MIT", - "authors": [{ - "name": "Josh Sherman", - "email": "josh@gravityblvd.com", - "homepage": "https://github.com/joshtronic/php-projecthoneypot" - }], - "autoload": { - "classmap": ["src/"] - }, - "require-dev": { - "satooshi/php-coveralls": "dev-master" - } + "name": "joshtronic/php-projecthoneypot", + "description": "PHP Wrapper for Project Honey Pot", + "version": "1.0.0", + "type": "library", + "keywords": ["project", "honey", "pot", "api", "spam", "spammer"], + "homepage": "https://github.com/joshtronic/php-projecthoneypot", + "license": "MIT", + "authors": [{ + "name": "Josh Sherman", + "email": "josh@gravityblvd.com", + "homepage": "http://joshtronic.com" + }], + "require-dev": { + "php": ">=5.3.0" + }, + "require-dev": { + "satooshi/php-coveralls": "dev-master" + }, + "autoload": { + "psr-4": {"joshtronic\\": "src/"} + } } diff --git a/src/ProjectHoneyPot.php b/src/ProjectHoneyPot.php index fc4618e..23cfb1b 100644 --- a/src/ProjectHoneyPot.php +++ b/src/ProjectHoneyPot.php @@ -19,138 +19,137 @@ namespace joshtronic; class ProjectHoneyPot { - /** - * API Key - * - * @access private - * @var string - */ - private $api_key = ''; + /** + * API Key + * + * @access private + * @var string + */ + private $api_key = ''; - /** - * Constructor - * - * Adds the specified API key to the object. - * - * @access public - * @param string $api_key PHP API Key (12 characters) - */ - public function __construct($api_key) - { - if (preg_match('/^[a-z]{12}$/', $api_key)) - { - $this->api_key = $api_key; - } - else - { - throw new \Exception('You must specify a valid API key.'); - } - } + /** + * Constructor + * + * Adds the specified API key to the object. + * + * @access public + * @param string $api_key PHP API Key (12 characters) + */ + public function __construct($api_key) + { + if (preg_match('/^[a-z]{12}$/', $api_key)) + { + $this->api_key = $api_key; + } + else + { + throw new \Exception('You must specify a valid API key.'); + } + } - /** - * Query - * - * Performs a DNS lookup to obtain information about the IP address. - * - * @access public - * @param string $ip_address IPv4 address to check - * @return array results from query - */ - public function query($ip_address) - { - // Validates the IP format - if (filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) - { - // Flips the script, err, IP address - $octets = explode('.', $ip_address); - krsort($octets); - $reversed_ip = implode('.', $octets); + /** + * Query + * + * Performs a DNS lookup to obtain information about the IP address. + * + * @access public + * @param string $ip_address IPv4 address to check + * @return array results from query + */ + public function query($ip_address) + { + // Validates the IP format + if (filter_var($ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE)) + { + // Flips the script, err, IP address + $octets = explode('.', $ip_address); + krsort($octets); + $reversed_ip = implode('.', $octets); - // Performs the query - $results = $this->dns_get_record($reversed_ip); + // Performs the query + $results = $this->dns_get_record($reversed_ip); - // Processes the results - if (isset($results[0]['ip'])) - { - $results = explode('.', $results[0]['ip']); + // Processes the results + if (isset($results[0]['ip'])) + { + $results = explode('.', $results[0]['ip']); - if ($results[0] == 127) - { - $results = array( - 'last_activity' => $results[1], - 'threat_score' => $results[2], - 'categories' => $results[3], - ); + if ($results[0] == 127) + { + $results = array( + 'last_activity' => $results[1], + 'threat_score' => $results[2], + 'categories' => $results[3], + ); - // Creates an array of categories - switch ($results['categories']) - { - case 0: - $categories = array('Search Engine'); - break; + // Creates an array of categories + switch ($results['categories']) + { + case 0: + $categories = array('Search Engine'); + break; - case 1: - $categories = array('Suspicious'); - break; + case 1: + $categories = array('Suspicious'); + break; - case 2: - $categories = array('Harvester'); - break; + case 2: + $categories = array('Harvester'); + break; - case 3: - $categories = array('Suspicious', 'Harvester'); - break; + case 3: + $categories = array('Suspicious', 'Harvester'); + break; - case 4: - $categories = array('Comment Spammer'); - break; + case 4: + $categories = array('Comment Spammer'); + break; - case 5: - $categories = array('Suspicious', 'Comment Spammer'); - break; + case 5: + $categories = array('Suspicious', 'Comment Spammer'); + break; - case 6: - $categories = array('Harvester', 'Comment Spammer'); - break; + case 6: + $categories = array('Harvester', 'Comment Spammer'); + break; - case 7: - $categories = array('Suspicious', 'Harvester', 'Comment Spammer'); - break; + case 7: + $categories = array('Suspicious', 'Harvester', 'Comment Spammer'); + break; - default: - $categories = array('Reserved for Future Use'); - break; - } - - $results['categories'] = $categories; + default: + $categories = array('Reserved for Future Use'); + break; + } - return $results; - } - } - } - else - { - return array('error' => 'Invalid IP address.'); - } + $results['categories'] = $categories; - return false; - } + return $results; + } + } + } + else + { + return array('error' => 'Invalid IP address.'); + } - /** - * DNS Get Record - * - * Wrapper method for dns_get_record() to allow fo easy mocking of the - * results in our tests. Takes an already reversed IP address and does a - * DNS lookup for A records against the http:BL API. - * - * @access public - * @param string $reversed_ip reversed IPv4 address to check - * @return array results from the DNS lookup - */ - public function dns_get_record($reversed_ip) - { - return dns_get_record($this->api_key . '.' . $reversed_ip . '.dnsbl.httpbl.org', DNS_A); - } + return false; + } + + /** + * DNS Get Record + * + * Wrapper method for dns_get_record() to allow fo easy mocking of the + * results in our tests. Takes an already reversed IP address and does a + * DNS lookup for A records against the http:BL API. + * + * @access public + * @param string $reversed_ip reversed IPv4 address to check + * @return array results from the DNS lookup + */ + public function dns_get_record($reversed_ip) + { + return dns_get_record($this->api_key . '.' . $reversed_ip . '.dnsbl.httpbl.org', DNS_A); + } } -?> diff --git a/tests/ProjectHoneyPotTest.php b/tests/ProjectHoneyPotTest.php index e5c4943..38128db 100644 --- a/tests/ProjectHoneyPotTest.php +++ b/tests/ProjectHoneyPotTest.php @@ -4,233 +4,232 @@ require_once '../src/ProjectHoneyPot.php'; class ProjectHoneyPotTest extends PHPUnit_Framework_TestCase { - /** - * @expectedException Exception - * @expectedExceptionMessage You must specify a valid API key. - */ - public function testInvalidKey() - { - new joshtronic\ProjectHoneyPot('foo'); - } + /** + * @expectedException Exception + * @expectedExceptionMessage You must specify a valid API key. + */ + public function testInvalidKey() + { + new joshtronic\ProjectHoneyPot('foo'); + } - public function testInvalidIP() - { - $object = new joshtronic\ProjectHoneyPot('foobarfoobar'); + public function testInvalidIP() + { + $object = new joshtronic\ProjectHoneyPot('foobarfoobar'); - $this->assertEquals( - array('error' => 'Invalid IP address.'), - $object->query('foo') - ); - } + $this->assertEquals( + array('error' => 'Invalid IP address.'), + $object->query('foo') + ); + } - public function testMissingResults() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testMissingResults() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue('foo')); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue('foo')); - $this->assertFalse($mock->query('1.2.3.4')); - } + $this->assertFalse($mock->query('1.2.3.4')); + } - public function testCategory0() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategory0() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.0')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.0')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals(array('Search Engine'), $results['categories']); - } + $this->assertEquals(array('Search Engine'), $results['categories']); + } - public function testCategory1() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategory1() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.1')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.1')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals(array('Suspicious'), $results['categories']); - } + $this->assertEquals(array('Suspicious'), $results['categories']); + } - public function testCategory2() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategory2() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.2')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.2')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals(array('Harvester'), $results['categories']); - } + $this->assertEquals(array('Harvester'), $results['categories']); + } - public function testCategory3() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategory3() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.3')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.3')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals( - array('Suspicious', 'Harvester'), - $results['categories'] - ); - } + $this->assertEquals( + array('Suspicious', 'Harvester'), + $results['categories'] + ); + } - public function testCategory4() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategory4() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.4')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.4')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals( - array('Comment Spammer'), - $results['categories'] - ); - } + $this->assertEquals( + array('Comment Spammer'), + $results['categories'] + ); + } - public function testCategory5() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategory5() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.5')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.5')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals( - array('Suspicious', 'Comment Spammer'), - $results['categories'] - ); - } + $this->assertEquals( + array('Suspicious', 'Comment Spammer'), + $results['categories'] + ); + } - public function testCategory6() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategory6() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.6')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.6')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals( - array('Harvester', 'Comment Spammer'), - $results['categories'] - ); - } + $this->assertEquals( + array('Harvester', 'Comment Spammer'), + $results['categories'] + ); + } - public function testCategory7() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategory7() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.7')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.7')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals( - array('Suspicious', 'Harvester', 'Comment Spammer'), - $results['categories'] - ); - } + $this->assertEquals( + array('Suspicious', 'Harvester', 'Comment Spammer'), + $results['categories'] + ); + } - public function testCategoryDefault() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testCategoryDefault() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '127.0.0.255')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '127.0.0.255')))); - $results = $mock->query('1.2.3.4'); + $results = $mock->query('1.2.3.4'); - $this->assertEquals( - array('Reserved for Future Use'), - $results['categories'] - ); - } + $this->assertEquals( + array('Reserved for Future Use'), + $results['categories'] + ); + } - public function testWithout127() - { - $mock = $this->getMock( - 'joshtronic\ProjectHoneyPot', - array('dns_get_record'), - array('foobarfoobar') - ); + public function testWithout127() + { + $mock = $this->getMock( + 'joshtronic\ProjectHoneyPot', + array('dns_get_record'), + array('foobarfoobar') + ); - $mock->expects($this->once()) - ->method('dns_get_record') - ->will($this->returnValue(array(array('ip' => '1.0.0.0')))); + $mock->expects($this->once()) + ->method('dns_get_record') + ->will($this->returnValue(array(array('ip' => '1.0.0.0')))); - $this->assertFalse($mock->query('1.2.3.4')); - } + $this->assertFalse($mock->query('1.2.3.4')); + } - // Doesn't serve much purpose aside from helping achieve 100% coverage - public function testDnsGetRecord() - { - $object = new joshtronic\ProjectHoneyPot('foobarfoobar'); + // Doesn't serve much purpose aside from helping achieve 100% coverage + public function testDnsGetRecord() + { + $object = new joshtronic\ProjectHoneyPot('foobarfoobar'); - $object->dns_get_record('1.2.3.4'); - } + $object->dns_get_record('1.2.3.4'); + } } -?>