Advertisement
Guest User

Untitled

a guest
Aug 15th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. use WebService::CaptchasDotNet;
  2. $WebService::CaptchasDotNet::DEBUG = 1;
  3.  
  4.  
  5. my $o = WebService::CaptchasDotNet->new(secret => 'g4VE1IEwYCGjCM7M14Mwy8GOILJUuGJH4wt9DP5H',
  6. username => 'drd_drd',
  7. expire => 1800);
  8.  
  9. get '/post/:slug' => sub {
  10.  
  11. my $slug = route_parameters->{'slug'};
  12. my $post = resultset('Post')->find({ slug => $slug });
  13. my $settings = resultset('Setting')->first;
  14. my @tags = resultset('View::PublishedTags')->all();
  15. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  16. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  17. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  18.  
  19. #new_captcha_code();
  20. #generate_captcha();
  21. #recaptcha => recaptcha_display();
  22.  
  23. # Grab the approved comments for this post and the corresponding reply comments
  24. my @comments;
  25. @comments = resultset('Comment')->search({ post_id => $post->id, status => 'approved', reply_to => undef }) if ( $post );
  26. foreach my $comment (@comments) {
  27. my @comment_replies = resultset('Comment')->search({ reply_to => $comment->id, status => 'approved' }, {order_by => { -asc => "comment_date" }});
  28. foreach my $reply (@comment_replies) {
  29. my $el;
  30. map { $el->{$_} = $reply->$_ } ('avatar', 'fullname', 'comment_date', 'content', );
  31. $el->{uid}->{username} = $reply->uid->username if $reply->uid;
  32. push(@{$comment->{comment_replies}}, $el);
  33.  
  34. #my $re = { recaptcha => recaptcha_display() };
  35. #push(@{$comment->{comment_replies}}, $re);
  36. #template 'post',{recaptcha => recaptcha_display(),};
  37. }
  38. }
  39.  
  40.  
  41. my $random = $o->random;
  42. my $url = $o->url($random);
  43. template 'post',
  44. {
  45. post => $post,
  46. recent => \@recent,
  47. popular => \@popular,
  48. categories => \@categories,
  49. comments => \@comments,
  50. setting => $settings,
  51. tags => \@tags,
  52. #recaptcha => recaptcha_display(),
  53. url => $url,
  54. random => $random,
  55.  
  56. };
  57.  
  58. #warn recaptcha => recaptcha_display();
  59. };
  60.  
  61. =head
  62.  
  63. Add a comment method
  64.  
  65. =cut
  66.  
  67. post '/comment/add' => sub {
  68.  
  69. my $parameters = body_parameters;
  70. my $fullname = $parameters->{'fullname'};
  71. my $post_id = $parameters->{'id'};
  72. #my $secret = $parameters->{recaptcha => recaptcha_display()};
  73. my @comments = resultset('Comment')->search({ post_id => $post_id, status => 'approved', reply_to => undef });
  74. my $post = resultset('Post')->find( $post_id );
  75. my @categories = resultset('Category')->all();
  76. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  77. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  78. my $user = session('user');
  79.  
  80. $parameters->{'reply_to'} = $1 if ($parameters->{'in_reply_to'} =~ /(\d+)/);
  81. if ($parameters->{'reply_to'}) {
  82. my $comm = resultset('Comment')->find({ id => $parameters->{'reply_to'} });
  83. if ($comm) {
  84. $parameters->{'reply_to_content'} = $comm->content;
  85. $parameters->{'reply_to_user'} = $comm->fullname;
  86.  
  87. }
  88. }
  89.  
  90. my $template_params = {
  91. post => $post,
  92. categories => \@categories,
  93. popular => \@popular,
  94. recent => \@recent,
  95. warning => 'The secret code is incorrect',
  96. recaptcha => recaptcha_display(),
  97. };
  98.  
  99. #warn "the display is |$template_params->{'recaptcha'}|";
  100. #my $user_input = param->{'random'};
  101. #my $ok = $o->verify($user_input, $random);
  102.  
  103.  
  104. my $random = param('random');
  105. warn "the random is |$random|";
  106. my $password = param('password');
  107. warn "the password is |$password |";
  108. my $ok = $o->verify($password, $random);
  109. warn "The ok is |$ok|";
  110. #my $response = param('g-recaptcha-response');
  111. #my $result = recaptcha_verify($response);
  112. #warn $result->{error_codes}->[0];
  113. #$result->{success}
  114.  
  115. if ( $ok ) {
  116. # The user entered the correct secret code
  117. eval {
  118.  
  119. # If the person who leaves the comment is either the author or the admin the comment is automaticaly approved
  120.  
  121. my $comment = resultset('Comment')->can_create( $parameters, $user );
  122.  
  123. # Notify the author that a new comment was submited
  124. my $author = $post->user;
  125.  
  126. Email::Template->send( config->{email_templates} . 'new_comment.tt',
  127. {
  128. From => config->{default_email_sender},
  129. To => $author->email,
  130. Subject => ($parameters->{'reply_to'} ? 'A comment reply was submitted to your post' : 'A new comment was submitted to your post'),
  131.  
  132. tt_vars => {
  133. fullname => $fullname,
  134. title => $post->title,
  135. comment => $parameters->{'comment'},
  136. signature => config->{email_signature},
  137. post_url => config->{app_url} . '/post/' . $post->slug,
  138. app_url => config->{app_url},
  139. reply_to_content => $parameters->{'reply_to_content'} || '',
  140. reply_to_user => $parameters->{'reply_to_user'} || '',
  141.  
  142. },
  143. }) or error "Could not send the email";
  144. };
  145. error $@ if ( $@ );
  146.  
  147. # Grab the approved comments for this post
  148. @comments = resultset('Comment')->search({ post_id => $post->id, status => 'approved', reply_to => undef }) if ( $post );
  149.  
  150. delete $template_params->{warning};
  151. delete $template_params->{in_reply_to};
  152.  
  153. if (($post->user_id && $user && $post->user_id == $user->{id}) or ($user && $user->{is_admin})) {
  154. $template_params->{success} = 'Your comment has been submited. Thank you!';
  155. } else {
  156. $template_params->{success} = 'Your comment has been submited and it will be displayed as soon as the author accepts it. Thank you!';
  157. }
  158. }
  159. #else {
  160. # The secret code inncorrect
  161. # Repopulate the fields with the data
  162. #$template_params->{fields} = $parameters;
  163.  
  164. #$template_params->{recaptcha} = recaptcha_display();
  165.  
  166. #my $response = param('g-recaptcha-response');
  167. #my $result = recaptcha_verify($response);
  168.  
  169. #template 'comment_form',{recaptcha => recaptcha_display()};
  170.  
  171. #$template_params->{recaptcha} = recaptcha_display();
  172. #}
  173.  
  174. foreach my $comment (@comments) {
  175. my @comment_replies = resultset('Comment')->search({ reply_to => $comment->id, status => 'approved' }, {order_by => { -asc => "comment_date" }});
  176. foreach my $reply (@comment_replies) {
  177. my $el;
  178. map { $el->{$_} = $reply->$_ } ('avatar', 'fullname', 'comment_date', 'content');
  179. $el->{uid}->{username} = $reply->uid->username if $reply->uid;
  180. push(@{$comment->{comment_replies}}, $el);
  181. }
  182. }
  183. $template_params->{comments} = \@comments;
  184. #$template_params->{recaptcha} = recaptcha_display();
  185.  
  186. #new_captcha_code();
  187. #generate_captcha();
  188. #recaptcha => recaptcha_display();
  189.  
  190.  
  191. template 'post', $template_params;
  192.  
  193.  
  194. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement