Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use WebService::CaptchasDotNet;
- $WebService::CaptchasDotNet::DEBUG = 1;
- my $o = WebService::CaptchasDotNet->new(secret => 'g4VE1IEwYCGjCM7M14Mwy8GOILJUuGJH4wt9DP5H',
- username => 'drd_drd',
- expire => 1800);
- get '/post/:slug' => sub {
- my $slug = route_parameters->{'slug'};
- my $post = resultset('Post')->find({ slug => $slug });
- my $settings = resultset('Setting')->first;
- my @tags = resultset('View::PublishedTags')->all();
- my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
- my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
- my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
- #new_captcha_code();
- #generate_captcha();
- #recaptcha => recaptcha_display();
- # Grab the approved comments for this post and the corresponding reply comments
- my @comments;
- @comments = resultset('Comment')->search({ post_id => $post->id, status => 'approved', reply_to => undef }) if ( $post );
- foreach my $comment (@comments) {
- my @comment_replies = resultset('Comment')->search({ reply_to => $comment->id, status => 'approved' }, {order_by => { -asc => "comment_date" }});
- foreach my $reply (@comment_replies) {
- my $el;
- map { $el->{$_} = $reply->$_ } ('avatar', 'fullname', 'comment_date', 'content', );
- $el->{uid}->{username} = $reply->uid->username if $reply->uid;
- push(@{$comment->{comment_replies}}, $el);
- #my $re = { recaptcha => recaptcha_display() };
- #push(@{$comment->{comment_replies}}, $re);
- #template 'post',{recaptcha => recaptcha_display(),};
- }
- }
- my $random = $o->random;
- my $url = $o->url($random);
- template 'post',
- {
- post => $post,
- recent => \@recent,
- popular => \@popular,
- categories => \@categories,
- comments => \@comments,
- setting => $settings,
- tags => \@tags,
- #recaptcha => recaptcha_display(),
- url => $url,
- random => $random,
- };
- #warn recaptcha => recaptcha_display();
- };
- =head
- Add a comment method
- =cut
- post '/comment/add' => sub {
- my $parameters = body_parameters;
- my $fullname = $parameters->{'fullname'};
- my $post_id = $parameters->{'id'};
- #my $secret = $parameters->{recaptcha => recaptcha_display()};
- my @comments = resultset('Comment')->search({ post_id => $post_id, status => 'approved', reply_to => undef });
- my $post = resultset('Post')->find( $post_id );
- my @categories = resultset('Category')->all();
- my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
- my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
- my $user = session('user');
- $parameters->{'reply_to'} = $1 if ($parameters->{'in_reply_to'} =~ /(\d+)/);
- if ($parameters->{'reply_to'}) {
- my $comm = resultset('Comment')->find({ id => $parameters->{'reply_to'} });
- if ($comm) {
- $parameters->{'reply_to_content'} = $comm->content;
- $parameters->{'reply_to_user'} = $comm->fullname;
- }
- }
- my $template_params = {
- post => $post,
- categories => \@categories,
- popular => \@popular,
- recent => \@recent,
- warning => 'The secret code is incorrect',
- recaptcha => recaptcha_display(),
- };
- #warn "the display is |$template_params->{'recaptcha'}|";
- #my $user_input = param->{'random'};
- #my $ok = $o->verify($user_input, $random);
- my $random = param('random');
- warn "the random is |$random|";
- my $password = param('password');
- warn "the password is |$password |";
- my $ok = $o->verify($password, $random);
- warn "The ok is |$ok|";
- #my $response = param('g-recaptcha-response');
- #my $result = recaptcha_verify($response);
- #warn $result->{error_codes}->[0];
- #$result->{success}
- if ( $ok ) {
- # The user entered the correct secret code
- eval {
- # If the person who leaves the comment is either the author or the admin the comment is automaticaly approved
- my $comment = resultset('Comment')->can_create( $parameters, $user );
- # Notify the author that a new comment was submited
- my $author = $post->user;
- Email::Template->send( config->{email_templates} . 'new_comment.tt',
- {
- From => config->{default_email_sender},
- To => $author->email,
- Subject => ($parameters->{'reply_to'} ? 'A comment reply was submitted to your post' : 'A new comment was submitted to your post'),
- tt_vars => {
- fullname => $fullname,
- title => $post->title,
- comment => $parameters->{'comment'},
- signature => config->{email_signature},
- post_url => config->{app_url} . '/post/' . $post->slug,
- app_url => config->{app_url},
- reply_to_content => $parameters->{'reply_to_content'} || '',
- reply_to_user => $parameters->{'reply_to_user'} || '',
- },
- }) or error "Could not send the email";
- };
- error $@ if ( $@ );
- # Grab the approved comments for this post
- @comments = resultset('Comment')->search({ post_id => $post->id, status => 'approved', reply_to => undef }) if ( $post );
- delete $template_params->{warning};
- delete $template_params->{in_reply_to};
- if (($post->user_id && $user && $post->user_id == $user->{id}) or ($user && $user->{is_admin})) {
- $template_params->{success} = 'Your comment has been submited. Thank you!';
- } else {
- $template_params->{success} = 'Your comment has been submited and it will be displayed as soon as the author accepts it. Thank you!';
- }
- }
- #else {
- # The secret code inncorrect
- # Repopulate the fields with the data
- #$template_params->{fields} = $parameters;
- #$template_params->{recaptcha} = recaptcha_display();
- #my $response = param('g-recaptcha-response');
- #my $result = recaptcha_verify($response);
- #template 'comment_form',{recaptcha => recaptcha_display()};
- #$template_params->{recaptcha} = recaptcha_display();
- #}
- foreach my $comment (@comments) {
- my @comment_replies = resultset('Comment')->search({ reply_to => $comment->id, status => 'approved' }, {order_by => { -asc => "comment_date" }});
- foreach my $reply (@comment_replies) {
- my $el;
- map { $el->{$_} = $reply->$_ } ('avatar', 'fullname', 'comment_date', 'content');
- $el->{uid}->{username} = $reply->uid->username if $reply->uid;
- push(@{$comment->{comment_replies}}, $el);
- }
- }
- $template_params->{comments} = \@comments;
- #$template_params->{recaptcha} = recaptcha_display();
- #new_captcha_code();
- #generate_captcha();
- #recaptcha => recaptcha_display();
- template 'post', $template_params;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement