From 953e0423c7bce3678314f58581a3ca0e62286647 Mon Sep 17 00:00:00 2001 From: Laurent Escalier Date: Thu, 22 Oct 2015 10:53:26 +0200 Subject: [PATCH] Merge Honeypot and HoneypotValidator, add the possibility to enable/disable the validation --- README.md | 13 ++++ src/Msurguy/Honeypot/Honeypot.php | 75 +++++++++++++++++++ .../Honeypot/HoneypotServiceProvider.php | 5 +- src/Msurguy/Honeypot/HoneypotValidator.php | 56 -------------- tests/HoneypotValidatorTest.php | 2 +- 5 files changed, 92 insertions(+), 59 deletions(-) delete mode 100644 src/Msurguy/Honeypot/HoneypotValidator.php diff --git a/README.md b/README.md index 8eff44d..40aca92 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,19 @@ Please note that "honeytime" takes a parameter specifying number of seconds it s That's it! Enjoy getting less spam in your inbox. If you need stronger spam protection, consider using [Akismet](https://github.com/kenmoini/akismet) or [reCaptcha](https://github.com/dontspamagain/recaptcha) +## Testing + +If you want to test the submission of a form using this package, you might want to disable Honeypot so that the validation passes. To do so, simply call the `disable()` method in your test: + + Honeypot::disable(); + + $this->visit('contact') + ->type('User', 'name') + ->type('user@email.com', 'email') + ->type('Hello World', 'message') + ->press('submit') + ->see('Your message has been sent!'); + ## Credits Based on work originally created by Ian Landsman: diff --git a/src/Msurguy/Honeypot/Honeypot.php b/src/Msurguy/Honeypot/Honeypot.php index 1b35e1f..9b60013 100644 --- a/src/Msurguy/Honeypot/Honeypot.php +++ b/src/Msurguy/Honeypot/Honeypot.php @@ -4,6 +4,24 @@ class Honeypot { + protected $disabled = false; + + /** + * Enable the Honeypot validation + */ + public function enable() + { + $this->disabled = false; + } + + /** + * Disable the Honeypot validation + */ + public function disable() + { + $this->disabled = true; + } + /** * Generate a new honeypot and return the form HTML * @param string $honey_name @@ -23,6 +41,44 @@ public function generate($honey_name, $honey_time) return $html; } + /** + * Validate honeypot is empty + * + * @param string $attribute + * @param mixed $value + * @param array $parameters + * @return boolean + */ + public function validateHoneypot($attribute, $value, $parameters) + { + if ($this->disabled) { + return true; + } + + return $value == ''; + } + + /** + * Validate honey time was within the time limit + * + * @param string $attribute + * @param mixed $value + * @param array $parameters + * @return boolean + */ + public function validateHoneytime($attribute, $value, $parameters) + { + if ($this->disabled) { + return true; + } + + // Get the decrypted time + $value = $this->decryptTime($value); + + // The current time should be greater than the time the form was built + the speed option + return ( is_numeric($value) && time() > ($value + $parameters[0]) ); + } + /** * Get encrypted time * @return string @@ -32,4 +88,23 @@ public function getEncryptedTime() return Crypt::encrypt(time()); } + /** + * Decrypt the given time + * + * @param mixed $time + * @return string|null + */ + public function decryptTime($time) + { + // Laravel will throw an uncaught exception if the value is empty + // We will try and catch it to make it easier on users. + try { + return Crypt::decrypt($time); + } + catch (\Illuminate\Encryption\DecryptException $exception) + { + return null; + } + } + } \ No newline at end of file diff --git a/src/Msurguy/Honeypot/HoneypotServiceProvider.php b/src/Msurguy/Honeypot/HoneypotServiceProvider.php index d7d703e..bd345a2 100644 --- a/src/Msurguy/Honeypot/HoneypotServiceProvider.php +++ b/src/Msurguy/Honeypot/HoneypotServiceProvider.php @@ -49,8 +49,9 @@ public function boot() $translator = $app['translator']; // Add honeypot and honeytime custom validation rules - $validator->extend('honeypot', 'Msurguy\Honeypot\HoneypotValidator@validateHoneypot', $translator->get('honeypot::validation.honeypot')); - $validator->extend('honeytime', 'Msurguy\Honeypot\HoneypotValidator@validateHoneytime', $translator->get('honeypot::validation.honeytime')); + $validator->extend('honeypot', 'honeypot@validateHoneypot', $translator->get('honeypot::validation.honeypot')); + $validator->extend('honeytime', 'honeypot@validateHoneytime', $translator->get('honeypot::validation.honeytime')); + }); } diff --git a/src/Msurguy/Honeypot/HoneypotValidator.php b/src/Msurguy/Honeypot/HoneypotValidator.php deleted file mode 100644 index c6582c1..0000000 --- a/src/Msurguy/Honeypot/HoneypotValidator.php +++ /dev/null @@ -1,56 +0,0 @@ -decryptTime($value); - - // The current time should be greater than the time the form was built + the speed option - return ( is_numeric($value) && time() > ($value + $parameters[0]) ); - } - - /** - * Decrypt the given time - * - * @param mixed $time - * @return string|null - */ - public function decryptTime($time) - { - // Laravel will throw an uncaught exception if the value is empty - // We will try and catch it to make it easier on users. - try { - return Crypt::decrypt($time); - } - catch (\Illuminate\Encryption\DecryptException $exception) - { - return null; - } - } - -} \ No newline at end of file diff --git a/tests/HoneypotValidatorTest.php b/tests/HoneypotValidatorTest.php index 584eb5f..5339f8d 100644 --- a/tests/HoneypotValidatorTest.php +++ b/tests/HoneypotValidatorTest.php @@ -8,7 +8,7 @@ class HoneypotValidatorTest extends \PHPUnit_Framework_TestCase { public function setUp() { - $this->validator = Mockery::mock('Msurguy\Honeypot\HoneypotValidator[decryptTime]'); + $this->validator = Mockery::mock('Msurguy\Honeypot\Honeypot[decryptTime]'); } /** @test */