From d5b67f43116c7e0ca742674f68f7f9d34ec0e864 Mon Sep 17 00:00:00 2001 From: fungku Date: Sun, 2 Nov 2014 09:02:46 -0800 Subject: [PATCH 1/2] Add validation for reply and thread completion time --- app/Lio/Forum/Replies/ReplyForm.php | 15 +++++++++++++++ app/Lio/Forum/Threads/ThreadForm.php | 12 ++++++++++++ app/views/forum/replies/_create.blade.php | 4 ++++ app/views/forum/threads/create.blade.php | 4 ++++ 4 files changed, 35 insertions(+) diff --git a/app/Lio/Forum/Replies/ReplyForm.php b/app/Lio/Forum/Replies/ReplyForm.php index ca907b75c..2e6914c9d 100644 --- a/app/Lio/Forum/Replies/ReplyForm.php +++ b/app/Lio/Forum/Replies/ReplyForm.php @@ -7,4 +7,19 @@ class ReplyForm extends FormModel protected $validationRules = [ 'body' => 'required', ]; + + protected function beforeValidation() + { + $time = Input::get('_time'); + + // Conditional validation rule: + // - compare processing time to form-creation time + // - if the difference is less than 5 seconds + // - we apply a rule where the max length of the _time input is 0 + // - and then validation will fail because it is not 0 + Validator::sometimes('_time', 'max:0', function() use ($time) + { + return (strtotime("now") - $time) < 5; + }); + } } \ No newline at end of file diff --git a/app/Lio/Forum/Threads/ThreadForm.php b/app/Lio/Forum/Threads/ThreadForm.php index da940578d..285321bb6 100644 --- a/app/Lio/Forum/Threads/ThreadForm.php +++ b/app/Lio/Forum/Threads/ThreadForm.php @@ -27,5 +27,17 @@ protected function beforeValidation() return true; }); + + $time = Input::get('_time'); + + // Conditional validation rule: + // - compare processing time to form-creation time + // - if the difference is less than 5 seconds + // - we apply a rule where the max length of the _time input is 0 + // - and validation will always fail because it is not 0 + Validator::sometimes('_time', 'max:0', function() use ($time) + { + return (strtotime("now") - $time) < 5; + }); } } diff --git a/app/views/forum/replies/_create.blade.php b/app/views/forum/replies/_create.blade.php index 2973f64c2..1f55d594f 100644 --- a/app/views/forum/replies/_create.blade.php +++ b/app/views/forum/replies/_create.blade.php @@ -1,6 +1,10 @@
{{ Form::open(['data-persist' => 'garlic', 'data-expires' => '300']) }} + + {{-- Time field used to check for spam bots --}} + {{ Form::hidden('_time', time()) }} +
{{ Form::textarea("body", null, ['class' => '_tab_indent _reply_form']) }} diff --git a/app/views/forum/threads/create.blade.php b/app/views/forum/threads/create.blade.php index ac1643027..3fd1ad9ea 100644 --- a/app/views/forum/threads/create.blade.php +++ b/app/views/forum/threads/create.blade.php @@ -9,6 +9,10 @@

Create Thread

{{ Form::open(['data-persist' => 'garlic', 'data-expires' => '600']) }} + + {{-- Time field used to check for spam bots --}} + {{ Form::hidden('_time', time()) }} +
{{ Form::label('subject', 'Subject', ['class' => 'field-title']) }} From d1ec1e2291e185878bfc00dbbaafd6d8ccb2ee05 Mon Sep 17 00:00:00 2001 From: fungku Date: Sun, 2 Nov 2014 09:09:52 -0800 Subject: [PATCH 2/2] Change reply timer from 5 to 2 seconds --- app/Lio/Forum/Replies/ReplyForm.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Lio/Forum/Replies/ReplyForm.php b/app/Lio/Forum/Replies/ReplyForm.php index 2e6914c9d..f89ae90d4 100644 --- a/app/Lio/Forum/Replies/ReplyForm.php +++ b/app/Lio/Forum/Replies/ReplyForm.php @@ -14,12 +14,12 @@ protected function beforeValidation() // Conditional validation rule: // - compare processing time to form-creation time - // - if the difference is less than 5 seconds + // - if the difference is less than 2 seconds // - we apply a rule where the max length of the _time input is 0 // - and then validation will fail because it is not 0 Validator::sometimes('_time', 'max:0', function() use ($time) { - return (strtotime("now") - $time) < 5; + return (strtotime("now") - $time) < 2; }); } } \ No newline at end of file