|
|
|
package CMTD::Main;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use utf8;
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
|
|
|
|
use Mojo::Util qw(trim);
|
|
|
|
|
|
|
|
sub index {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
$self->render(text => "Go away!\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
$self->app->log->debug("showing captcha #$cid");
|
|
|
|
$self->render(json => {id => $cid, data => $data}); 1;
|
|
|
|
} or do {
|
|
|
|
chomp $@;
|
|
|
|
$self->app->log->error("error when showing captcha: $@");
|
|
|
|
$self->app->client_reply(500, "internal error");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub c_list {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $ref = $self->referrer;
|
|
|
|
unless ($ref and ref($ref) eq 'HASH') {
|
|
|
|
$self->app->client_reply(400, "can't detect referred page");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my $site = $self->app->site_by_name($ref->{site});
|
|
|
|
if ($site and ref($site) eq 'HASH') {
|
|
|
|
my @cm = ();
|
|
|
|
if (my $pid = $self->app->pid_by_hash($site->{id}, $ref->{hash})) {
|
|
|
|
my @tm = $self->app->comments_by_pid($pid);
|
|
|
|
# TODO: processing
|
|
|
|
@cm = @tm;
|
|
|
|
}
|
|
|
|
$self->render(json => \@cm);
|
|
|
|
} else {
|
|
|
|
$self->app->client_reply(400, "no such site");
|
|
|
|
} 1;
|
|
|
|
} or do {
|
|
|
|
chomp $@;
|
|
|
|
my $msg = sprintf "Error when listing comments for %s/%s: %s",
|
|
|
|
$ref->{site}, $ref->{pid}, $@;
|
|
|
|
$self->app->log->error($msg);
|
|
|
|
$self->app->client_reply(500, "internal error");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub c_add {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
|
|
|
my $ref = $self->referrer;
|
|
|
|
unless ($ref and ref($ref) eq 'HASH') {
|
|
|
|
$self->app->client_reply(400, "can't detect referred page");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
eval {
|
|
|
|
my (%msg, %cap);
|
|
|
|
$msg{addr} = $self->tx->remote_address;
|
|
|
|
foreach my $param (qw(name email text reply)) {
|
|
|
|
$msg{$param} = trim($self->req->param($param) || '');
|
|
|
|
}
|
|
|
|
foreach my $param (qw(cid code)) {
|
|
|
|
$cap{$param} = trim($self->req->param($param) || '');
|
|
|
|
}
|
|
|
|
do {{
|
|
|
|
unless ($msg{text}) {
|
|
|
|
$self->app->client_reply(400, "empty message");
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
my $site = $self->app->site_by_name($ref->{site});
|
|
|
|
unless ($site and ref($site) eq 'HASH') {
|
|
|
|
$self->app->client_reply(400, "no such site");
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
if ($cap{cid}) {
|
|
|
|
unless ($cap{code}) {
|
|
|
|
$self->app->client_reply(400, "missing captcha code");
|
|
|
|
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");
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
unless ($cap->{code} eq $cap{code}) {
|
|
|
|
$self->app->client_reply(400, "captcha code mismatch");
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
my $pid = $self->app->pid_by_hash($site->{id}, $ref->{hash});
|
|
|
|
unless ($pid) {
|
|
|
|
$self->app->add_page($site->{id}, $ref);
|
|
|
|
$pid = $self->app->pid_by_hash($site->{id}, $ref->{hash});
|
|
|
|
}
|
|
|
|
$self->add_comment($pid, \%msg);
|
|
|
|
}} while (0); 1;
|
|
|
|
} or do {
|
|
|
|
chomp $@;
|
|
|
|
my $msg = sprintf "Error when listing comments for %s/%s: %s",
|
|
|
|
$ref->{site}, $ref->{pid}, $@;
|
|
|
|
$self->app->log->error($msg);
|
|
|
|
$self->app->client_reply(500, "internal error");
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|