From b3da6874e11d886914ef6f8cd0faadb781ef45e3 Mon Sep 17 00:00:00 2001 From: swag Date: Sat, 18 Dec 2021 23:03:53 -0500 Subject: [PATCH] Implement CAPTCHA --- guestbook-ng.pl | 11 ++++++++++- lib/GuestbookNg/Model/Message.pm | 8 +++++++- templates/sign.html.ep | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/guestbook-ng.pl b/guestbook-ng.pl index 279e659..9b16f26 100755 --- a/guestbook-ng.pl +++ b/guestbook-ng.pl @@ -34,6 +34,14 @@ helper message => sub { }; # Routes +under sub ($c) { + # Opt out of Google FLoC + # https://paramdeo.com/blog/opting-your-website-out-of-googles-floc-network + $c->res->headers->header('Permissions-Policy', 'interest-cohort=()'); + + 1; +}; + get '/' => sub ($c) { my $posts = $c->message->get_posts(); my $last_page = $c->message->get_last_page(@$posts); @@ -53,8 +61,9 @@ any '/sign' => sub ($c) { if ($c->req->method() eq 'POST') { my $name = $c->param('name'); my $message = $c->param('message'); + my $answer = $c->param('answer'); - $c->message->send_post($name, $message); + $c->message->send_post($name, $message) if $answer; $c->redirect_to('index'); } else { diff --git a/lib/GuestbookNg/Model/Message.pm b/lib/GuestbookNg/Model/Message.pm index b664ab9..e0d9f27 100644 --- a/lib/GuestbookNg/Model/Message.pm +++ b/lib/GuestbookNg/Model/Message.pm @@ -36,7 +36,13 @@ sub max_posts($self, $value = undef) { } sub get_last_page($self, @posts) { - sprintf('%d', scalar(@posts) / $self->{'max_posts'}) + 1 + # Add a page if we have "remainder" posts + if (scalar(@posts) % $self->{'max_posts'}) { + sprintf('%d', scalar(@posts) / $self->{'max_posts'}) + 1 + } + else { + sprintf('%d', scalar(@posts) / $self->{'max_posts'}) + } } 1; diff --git a/templates/sign.html.ep b/templates/sign.html.ep index e6152c2..fadbb17 100644 --- a/templates/sign.html.ep +++ b/templates/sign.html.ep @@ -11,6 +11,27 @@ Message: <%= text_area 'message', cols => 40, rows => 6 %> + + Swagg CAPTCHA: + + <%= radio_button answer => 0 %> + <%= label_for answer => "I don\'t want to post" %> + + + +   + + <%= radio_button answer => "false" %> + <%= label_for answer => "I\'m ready to post" %> + + + +   + + <%= radio_button answer => undef %> + <%= label_for answer => 'This is spam, do not post' %> + +   <%= submit_button 'Send it' %>