Skip to content

Commit

Permalink
Merge pull request #46 from AlexStack/alex_dev
Browse files Browse the repository at this point in the history
Add inquiry_verify_str for basic spam check
  • Loading branch information
AlexStack authored Jan 22, 2020
2 parents 8807491 + 46d2e73 commit 5d38c18
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/Helpers/LaravelCmsPluginInquiry.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ public function submitForm(Request $request)
return json_encode($result);
}

// inquiry_verify_str for basic spam check
if (! isset($form_data['inquiry_verify_str']) || ! strpos($form_data['inquiry_verify_str'], '-')) {
$result['success'] = false;
$result['error_message'] = 'Verify inquiry_verify_str failed.';

return json_encode($result);
} else {
$verify_str_ary = explode('-', $form_data['inquiry_verify_str']);
if (3 != count($verify_str_ary) || $verify_str_ary[0] != $form_data['page_id'] || $verify_str_ary[1] < 5 || $verify_str_ary[2] < 4) {
$result['success'] = false;
$result['error_message'] = 'Verify inquiry_verify_str failed! Message too short?';

return json_encode($result);
}
}

$inquiry = new LaravelCmsInquiry();
foreach ($inquiry->fillable as $field) {
if (isset($form_data[$field])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/LaravelCmsPageAdminRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function create()
$data['helper'] = $this->helper;
$data['page_tab_blades'] = $this->extraPageTabs();

$this->extraPageTabs('create');
$data['plugins'] = $this->extraPageTabs('create');

return $data;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Repositories/LaravelCmsPageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,14 @@ public function search($slug, $search_type = 'content')

return $query;
})
->orderBy('sort_value', 'desc')
->orderBy('id', 'desc')
->paginate($this->helper->s('template.number_per_search') ?? 24);
} elseif ('tag' == $search_type) {
$data['search_results'] = LaravelCmsPage::when($keyword, function ($query, $keyword) {
return $query->where('tags', 'like', '%"'.trim($keyword).'"%');
})
->orderBy('sort_value', 'desc')
->orderBy('id', 'desc')
->paginate($this->helper->s('template.number_per_search') ?? 20);
}
Expand Down
39 changes: 32 additions & 7 deletions src/assets/frontend/js/bottom.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function adjustAllLinks() {
// location.href = $(this).attr('href');
// });

$("a").each(function () {
$("a").each(function() {
if (this.href.match("_blank")) {
$(this).attr("target", "_blank");
}
Expand All @@ -30,7 +30,33 @@ function submitInquiryForm() {
if (document.querySelector("#laravel-cms-inquiry-form") == null) {
return false;
}
$("#laravel-cms-inquiry-form").submit(function (event) {

// add inquiry_verify_str to prevent low level spam
var inputStartTime = null;
$('#laravel-cms-inquiry-form textarea[name="message"]')
.after(
'<input name="inquiry_verify_str" id="inquiry_verify_str" type="hidden" value="0-0-0" />'
)
.focus(function() {
if (inputStartTime == null) {
inputStartTime = new Date().getTime();
}
})
.keyup(function(event) {
var extra_verify_ary = $("#inquiry_verify_str")
.val()
.split("-");
var spent_time = parseInt(new Date().getTime() - inputStartTime);
$("#inquiry_verify_str").val(
$('#laravel-cms-inquiry-form input[name="page_id"]').val() +
"-" +
(parseInt(extra_verify_ary[1]) + 1) +
"-" +
parseInt((new Date().getTime() - inputStartTime) / 1000)
);
});

$("#laravel-cms-inquiry-form").submit(function(event) {
event.preventDefault();
if (
typeof grecaptcha != "undefined" &&
Expand All @@ -55,7 +81,7 @@ function submitInquiryForm() {
cache: false,
processData: false,
dataType: "json",
success: function (data) {
success: function(data) {
//console.log("Submission was successful.");
//console.log(data);
if (data.success) {
Expand All @@ -74,7 +100,7 @@ function submitInquiryForm() {
).remove();
}
},
error: function (data) {
error: function(data) {
$("#laravel-cms-inquiry-form .error_message").html(
"Error: " + data.responseJSON.message
);
Expand All @@ -88,15 +114,14 @@ function submitInquiryForm() {
//console.log("laravel-cms-inquiry-form : An error occurred.");
//console.log(data);
}
}).done(function (data) {
}).done(function(data) {
// console.log("laravel-cms-inquiry-form submitted");
//console.log(data);
});
});
}


$(function () {
$(function() {
submitInquiryForm();

adjustAllLinks();
Expand Down
5 changes: 4 additions & 1 deletion src/resources/views/backend/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@
@else
{{$item->title}}
@endif
</a></li>
</a>
<a class="text-secondary float-right ml-2 " href="{{$helper->url($item)}}" target="_blank"><i
class="fas fa-external-link-square-alt small"></i></a>
</li>
@endforeach
</ul>
</div>
Expand Down
9 changes: 5 additions & 4 deletions src/tests/Feature/LaravelCmsInquiryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public function test_submitForm()
$inquiry->save();

$form_data = [
'first_name' => 'Name'.rand(0, 999),
'email' => 'email'.rand(0, 999).'@example.com',
'message' => 'Message '.rand(0, 999),
'page_id' => 2,
'first_name' => 'Name'.rand(0, 999),
'email' => 'email'.rand(0, 999).'@example.com',
'message' => 'Message '.rand(0, 999),
'page_id' => 2,
'inquiry_verify_str' => '2-8-9',
];
$request = new \Illuminate\Http\Request($form_data);

Expand Down

0 comments on commit 5d38c18

Please sign in to comment.