2021-12-04 05:11:37 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
|
|
|
|
# Dec 2021
|
|
|
|
# Daniel Bowling <swaggboi@slackware.uk>
|
|
|
|
|
|
|
|
use Mojolicious::Lite -signatures;
|
2021-12-04 23:34:35 +00:00
|
|
|
use Mojo::Pg;
|
2022-01-01 03:32:39 +00:00
|
|
|
use List::Util qw{shuffle};
|
2022-01-02 07:01:45 +00:00
|
|
|
use Regexp::Common qw{URI};
|
2022-01-12 00:51:45 +00:00
|
|
|
use Number::Format qw{format_number};
|
2022-01-10 17:53:10 +00:00
|
|
|
#use Data::Dumper; # Uncomment for debugging
|
2022-01-01 03:32:39 +00:00
|
|
|
|
|
|
|
# Load the model
|
2021-12-04 07:26:25 +00:00
|
|
|
use lib 'lib';
|
2021-12-05 07:52:31 +00:00
|
|
|
use GuestbookNg::Model::Message;
|
2021-12-04 07:26:25 +00:00
|
|
|
|
2021-12-04 23:34:35 +00:00
|
|
|
# Plugins
|
|
|
|
plugin 'Config';
|
2021-12-12 02:02:54 +00:00
|
|
|
plugin 'TagHelpers::Pagination';
|
2021-12-23 04:18:35 +00:00
|
|
|
plugin AssetPack => {pipes => [qw{Css JavaScript Combine}]};
|
2021-12-04 23:34:35 +00:00
|
|
|
|
|
|
|
# Helpers
|
|
|
|
helper pg => sub {
|
2022-01-10 19:18:49 +00:00
|
|
|
my $env = app->mode() eq 'development' ? 'dev_env' : 'prod_env';
|
|
|
|
state $pg = Mojo::Pg->new(app->config->{$env}{'pg_string'});
|
2021-12-04 23:34:35 +00:00
|
|
|
};
|
|
|
|
|
2021-12-05 04:06:05 +00:00
|
|
|
helper message => sub {
|
2021-12-05 07:52:31 +00:00
|
|
|
state $message = GuestbookNg::Model::Message->new(pg => shift->pg)
|
2021-12-05 04:06:05 +00:00
|
|
|
};
|
|
|
|
|
2021-12-05 09:22:55 +00:00
|
|
|
# Routes
|
2021-12-19 04:03:53 +00:00
|
|
|
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=()');
|
|
|
|
|
2022-02-10 00:33:47 +00:00
|
|
|
$c->session(expiration => 86400);
|
2022-01-02 04:37:41 +00:00
|
|
|
|
2022-01-09 03:01:51 +00:00
|
|
|
$c->stash(status => 403) if $c->flash('error');
|
2022-01-09 02:21:22 +00:00
|
|
|
|
2022-01-12 01:15:53 +00:00
|
|
|
$c->stash(post_count => format_number $c->message->get_post_count);
|
|
|
|
|
2021-12-19 04:03:53 +00:00
|
|
|
1;
|
|
|
|
};
|
|
|
|
|
2021-12-04 05:11:37 +00:00
|
|
|
get '/' => sub ($c) {
|
2021-12-19 22:20:58 +00:00
|
|
|
my $this_page = $c->param('page') || 1;
|
2021-12-23 03:23:10 +00:00
|
|
|
my $last_page = $c->message->get_last_page();
|
|
|
|
my $view_posts = $c->message->get_posts($this_page);
|
2022-01-12 20:18:34 +00:00
|
|
|
|
|
|
|
$c->stash(
|
|
|
|
view_posts => $view_posts,
|
|
|
|
this_page => $this_page,
|
|
|
|
last_page => $last_page
|
|
|
|
);
|
|
|
|
|
|
|
|
$c->render();
|
|
|
|
} => 'index';
|
|
|
|
|
|
|
|
get '/spam' => sub ($c) {
|
|
|
|
my $this_page = $c->param('page') || 1;
|
|
|
|
my $last_page = $c->message->get_last_page('spam');
|
|
|
|
my $view_posts = $c->message->get_spam($this_page);
|
2021-12-05 07:52:31 +00:00
|
|
|
|
2021-12-12 02:02:54 +00:00
|
|
|
$c->stash(
|
2021-12-23 03:23:10 +00:00
|
|
|
view_posts => $view_posts,
|
2021-12-12 02:02:54 +00:00
|
|
|
this_page => $this_page,
|
2022-01-12 01:15:53 +00:00
|
|
|
last_page => $last_page
|
2021-12-12 02:02:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$c->render();
|
2021-12-04 07:26:25 +00:00
|
|
|
} => 'index';
|
|
|
|
|
2021-12-19 05:43:18 +00:00
|
|
|
any [qw{GET POST}], '/sign' => sub ($c) {
|
2022-01-09 02:21:22 +00:00
|
|
|
my $v = $c->validation();
|
|
|
|
|
|
|
|
if ($c->req->method eq 'POST' && $v->has_data) {
|
2022-01-01 04:05:34 +00:00
|
|
|
my $name = $c->param('name') || 'Anonymous';
|
2022-01-01 04:45:21 +00:00
|
|
|
my $url = $c->param('url');
|
2021-12-05 07:52:31 +00:00
|
|
|
my $message = $c->param('message');
|
2022-01-02 22:53:43 +00:00
|
|
|
my $spam =
|
|
|
|
!$c->param('answer') ? 1 :
|
|
|
|
$message =~ /$RE{URI}{HTTP}{-scheme => qr<https?>}/ ? 1 :
|
|
|
|
0;
|
2022-01-02 07:01:45 +00:00
|
|
|
|
2022-01-09 02:21:22 +00:00
|
|
|
$v->required('name' )->size(1, 63);
|
|
|
|
$v->required('message')->size(2, 2000);
|
|
|
|
$v->optional('url', 'not_empty')->size(1, 255)
|
|
|
|
->like(qr/$RE{URI}{HTTP}{-scheme => qr<https?>}/);
|
|
|
|
|
|
|
|
unless ($v->has_error) {
|
2022-01-02 06:31:33 +00:00
|
|
|
$c->message->create_post($name, $message, $url, $spam);
|
2022-01-02 22:53:43 +00:00
|
|
|
|
|
|
|
$c->flash(error => 'This message was flagged as spam') if $spam;
|
2022-01-10 18:44:30 +00:00
|
|
|
|
|
|
|
return $c->redirect_to('index');
|
2022-01-02 04:37:41 +00:00
|
|
|
}
|
2021-12-05 07:52:31 +00:00
|
|
|
}
|
2022-01-09 02:21:22 +00:00
|
|
|
|
|
|
|
# Try to randomize things for the CAPTCHA challenge. The
|
|
|
|
# string 'false' actually evaluates to true so this is an
|
|
|
|
# attempt to confuse a (hypothetical) bot that would try to
|
|
|
|
# select what it thinks is the right answer
|
|
|
|
my @answers = shuffle(0, 'false', undef);
|
|
|
|
my $right_answer_label = 'I\'m ready to sign (choose this one)';
|
|
|
|
my @wrong_answer_labels = shuffle(
|
|
|
|
'I don\'t want to sign (wrong answer)',
|
|
|
|
'This is spam/I\'m a bot, do not sign'
|
|
|
|
);
|
|
|
|
|
|
|
|
$c->stash(
|
|
|
|
answers => \@answers,
|
|
|
|
right_answer_label => $right_answer_label,
|
|
|
|
wrong_answer_labels => \@wrong_answer_labels
|
|
|
|
);
|
|
|
|
|
|
|
|
$c->render();
|
2021-12-05 07:52:31 +00:00
|
|
|
};
|
|
|
|
|
2021-12-04 23:34:35 +00:00
|
|
|
# Send it
|
2021-12-05 08:32:22 +00:00
|
|
|
app->secrets(app->config->{'secrets'}) || die $@;
|
2021-12-12 06:50:07 +00:00
|
|
|
|
|
|
|
app->message->max_posts(app->config->{'max_posts'})
|
|
|
|
if app->config->{'max_posts'};
|
|
|
|
|
2022-01-05 03:05:51 +00:00
|
|
|
app->pg->migrations->from_dir('migrations')->migrate(7);
|
2021-12-12 06:50:07 +00:00
|
|
|
|
2021-12-23 04:18:35 +00:00
|
|
|
app->asset->process('swagg.css', 'css/swagg.css');
|
|
|
|
|
2021-12-04 05:11:37 +00:00
|
|
|
app->start();
|