Browse Source

* CMTD::Helpers->captcha_solve

master
Alex 'AdUser' Z 7 years ago
parent
commit
262cabe3c7
  1. 23
      lib/CMTD/Helpers.pm

23
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) = @_;

Loading…
Cancel
Save