Browse Source

* use new helpers, drop captcha table redefinition

master
Alex 'AdUser' Z 7 years ago
parent
commit
0008aacf02
  1. 1
      docs/cmtd.conf.sample
  2. 2
      lib/CMTD/Helpers.pm
  3. 24
      lib/CMTD/Main.pm

1
docs/cmtd.conf.sample

@ -1,7 +1,6 @@
{ {
db => ['dbi:SQLite:dbname=data/data.db'], db => ['dbi:SQLite:dbname=data/data.db'],
captcha => { captcha => {
table => 'captcha',
tries => 5, tries => 5,
limit => 500, limit => 500,
ft_file => '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf' ft_file => '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'

2
lib/CMTD/Helpers.pm

@ -36,7 +36,7 @@ sub register {
$app->helper(maintenance => sub { $app->helper(maintenance => sub {
my ($c) = @_; my ($c) = @_;
eval { eval {
my $table = $c->app->config->{captcha}->{table}; my $table = 'captcha';
my $tries = $c->app->config->{captcha}->{tries}; my $tries = $c->app->config->{captcha}->{tries};
my $limit = $c->app->config->{captcha}->{limit}; my $limit = $c->app->config->{captcha}->{limit};

24
lib/CMTD/Main.pm

@ -18,14 +18,9 @@ sub captcha {
my ($self) = @_; my ($self) = @_;
eval { eval {
my $table = $self->app->config->{captcha}->{table}; my ($cid, $data) = $self->app->get_captcha;
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;
$self->app->log->debug("showing captcha #$cid"); $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 { } or do {
chomp $@; chomp $@;
$self->app->log->error("error when showing captcha: $@"); $self->app->log->error("error when showing captcha: $@");
@ -92,18 +87,17 @@ sub c_add {
$self->app->client_reply(400, "no such site"); $self->app->client_reply(400, "no such site");
last; last;
} }
if ($cap{cid}) { if ($site->{captcha}) {
unless ($cap{code}) { unless ($cap{cid}) {
$self->app->client_reply(400, "missing captcha code"); $self->app->client_reply(400, "missing captcha id");
last; last;
} }
my $cap = $self->captcha_by_id($cap{cid}); unless ($cap{code}) {
unless ($cap and $cap eq 'HASH') { $self->app->client_reply(400, "missing captcha code");
$self->app->client_reply(400, "no captcha with this id");
last; last;
} }
unless ($cap->{code} eq $cap{code}) { if (my $error = $self->app->captcha_solve($cap{id}, $cap{code})) {
$self->app->client_reply(400, "captcha code mismatch"); $self->app->client_reply(400, $error);
last; last;
} }
} }

Loading…
Cancel
Save