From 0008aacf023655b35fa6f7878d2909098e4950ad Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Wed, 23 Aug 2017 12:18:54 +1000 Subject: [PATCH] * use new helpers, drop captcha table redefinition --- docs/cmtd.conf.sample | 1 - lib/CMTD/Helpers.pm | 2 +- lib/CMTD/Main.pm | 24 +++++++++--------------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/docs/cmtd.conf.sample b/docs/cmtd.conf.sample index b64f810..9782429 100644 --- a/docs/cmtd.conf.sample +++ b/docs/cmtd.conf.sample @@ -1,7 +1,6 @@ { db => ['dbi:SQLite:dbname=data/data.db'], captcha => { - table => 'captcha', tries => 5, limit => 500, ft_file => '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf' diff --git a/lib/CMTD/Helpers.pm b/lib/CMTD/Helpers.pm index 70a2c59..2aa3991 100644 --- a/lib/CMTD/Helpers.pm +++ b/lib/CMTD/Helpers.pm @@ -36,7 +36,7 @@ sub register { $app->helper(maintenance => sub { my ($c) = @_; eval { - my $table = $c->app->config->{captcha}->{table}; + my $table = 'captcha'; my $tries = $c->app->config->{captcha}->{tries}; my $limit = $c->app->config->{captcha}->{limit}; diff --git a/lib/CMTD/Main.pm b/lib/CMTD/Main.pm index 4a29c58..b50d113 100644 --- a/lib/CMTD/Main.pm +++ b/lib/CMTD/Main.pm @@ -18,14 +18,9 @@ sub captcha { my ($self) = @_; eval { - my $table = $self->app->config->{captcha}->{table}; - my @sort = qw(shown tries); - $self->app->db->begin; - my ($cid, $data) = $self->app->db->select($table, ['id', 'data'], undef, \@sort)->list; - $self->app->db->update($table, {shown => time()}, {id => $cid}); - $self->app->db->commit; + my ($cid, $data) = $self->app->get_captcha; $self->app->log->debug("showing captcha #$cid"); - $self->render(json => {id => $cid, data => $data}); 1; + $self->render(json => {cid => $cid, data => $data}); 1; } or do { chomp $@; $self->app->log->error("error when showing captcha: $@"); @@ -92,18 +87,17 @@ sub c_add { $self->app->client_reply(400, "no such site"); last; } - if ($cap{cid}) { - unless ($cap{code}) { - $self->app->client_reply(400, "missing captcha code"); + if ($site->{captcha}) { + unless ($cap{cid}) { + $self->app->client_reply(400, "missing captcha id"); last; } - my $cap = $self->captcha_by_id($cap{cid}); - unless ($cap and $cap eq 'HASH') { - $self->app->client_reply(400, "no captcha with this id"); + unless ($cap{code}) { + $self->app->client_reply(400, "missing captcha code"); last; } - unless ($cap->{code} eq $cap{code}) { - $self->app->client_reply(400, "captcha code mismatch"); + if (my $error = $self->app->captcha_solve($cap{id}, $cap{code})) { + $self->app->client_reply(400, $error); last; } }