From 262cabe3c71fcf7f3b321d68815976fb1426a54b Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Thu, 14 Sep 2017 17:23:02 +1000 Subject: [PATCH] * CMTD::Helpers->captcha_solve --- lib/CMTD/Helpers.pm | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/CMTD/Helpers.pm b/lib/CMTD/Helpers.pm index 5f36afd..659ad3c 100644 --- a/lib/CMTD/Helpers.pm +++ b/lib/CMTD/Helpers.pm @@ -86,17 +86,22 @@ sub register { }); $app->helper(captcha_solve => sub { my ($c, $id, $input) = @_; - my $cap = $c->app->db->select('captcha', '*', {cid => $id})->hash; + my ($table, $filter) = ('captcha', {id => $id}); + my $maxtry = $app->config->{captcha}->{tries}; + my $cap = $c->app->db->select($table, '*', $filter)->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"; + unless $cap and ref($cap) eq 'HASH' and $cap->{id}; + if ($cap->{code} eq $input) { + # captcha solved + $app->db->delete($table, $filter); + return; } + if ($cap->{tries} + 1 >= $maxtry) { + $app->db->delete($table, $filter); # num tries exceeded + } else { + $app->db->update($table, {tries => $cap->{tries} + 1}, $filter); + } + return "captcha code mismatch"; }); $app->helper(get_captcha => sub { my ($c) = @_;