From 8f528d94bd305525e62aa9178fe4fd38b84e720d Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Wed, 23 Aug 2017 12:17:55 +1000 Subject: [PATCH] * CMTD::Helpers : add 'get_captcha' and 'captcha_solve' --- lib/CMTD/Helpers.pm | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/CMTD/Helpers.pm b/lib/CMTD/Helpers.pm index fc42432..70a2c59 100644 --- a/lib/CMTD/Helpers.pm +++ b/lib/CMTD/Helpers.pm @@ -83,6 +83,30 @@ sub register { my ($c, $cid) = @_; return $self->app->db->select('captcha', '*', {cid => $cid})->hash; }); + $app->helper(captcha_solve => sub { + my ($c, $id, $input) = @_; + my $cap = $self->app->db->select('captcha', '*', {cid => $id})->hash; + return "no captcha with this id" + unless $cap and ref($cap) eq 'HASH'; + if ($cap->{code} ne $input) { + if ($cap->{tries} + 1 >= $app->config->{tries}) { + $app->db->delete('captcha', {id => $cap->{id}}); + } else { + $app->db->update('captcha', \'tries = tries + 1', {id => $cap->{id}}); + } + return "captcha code mismatch"; + } + }); + $app->helper(get_captcha => sub { + my ($c) = @_; + my $table = 'captcha'; + my @sort = qw(shown tries); + $app->db->begin; + my ($cid, $data) = $app->db->select($table, ['id', 'data'], undef, \@sort)->list; + $app->db->update($table, {shown => time()}, {id => $cid}); + $app->db->commit; + return ($cid, $data); + }); $app->helper(add_page => sub { my ($c, $sid, $page) = @_; $self->app->db->insert('pages', {