Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.66 KB | None | 0 0
  1. package PearlBee;
  2. # ABSTRACT: PerlBee Blog platform
  3.  
  4. use Dancer2 0.163000;
  5. use Dancer2::Plugin::DBIC;
  6.  
  7. # Other used modules
  8. use DateTime;
  9.  
  10. # Included controllers
  11.  
  12. # Common controllers
  13. use PearlBee::Authentication;
  14. use PearlBee::Authorization;
  15. use PearlBee::Dashboard;
  16. use PearlBee::REST;
  17.  
  18. # Admin controllers
  19. use PearlBee::Admin;
  20.  
  21. # Author controllers
  22. use PearlBee::Author::Post;
  23. use PearlBee::Author::Comment;
  24.  
  25. use PearlBee::Helpers::Util qw(generate_crypted_filename map_posts create_password);
  26. use PearlBee::Helpers::Pagination qw(get_total_pages get_previous_next_link);
  27. #use PearlBee::Helpers::Captcha;
  28. use Dancer2::Plugin::reCAPTCHA;
  29. use Data::Dumper;
  30.  
  31. our $VERSION = '0.1';
  32.  
  33. =head
  34.  
  35. Prepare the blog path
  36.  
  37. =cut
  38.  
  39. my $env_url = $ENV{MYAPP_DB_DSN};
  40. my $env_user = $ENV{MYAPP_DB_USERNAME};
  41. my $env_password = $ENV{MYAPP_DB_PASSWORD};
  42. my $schema = PearlBee::Model::Schema->connect("$env_url;user=$env_user;password=$env_password");
  43.  
  44. hook 'before' => sub {
  45. session app_url => config->{app_url} unless ( session('app_url') );
  46. my $app_url = session('app_url');
  47. #warn "the app url is : ";
  48.  
  49. session blog_name => resultset('Setting')->first->blog_name unless ( session('blog_name') );
  50. session multiuser => resultset('Setting')->first->multiuser;
  51. if ( request->dispatch_path =~ /^(.*)\.html$/ ) { forward $1; }
  52. };
  53.  
  54. =head
  55.  
  56. Home page
  57.  
  58. =cut
  59.  
  60. get '/' => sub {
  61. my $nr_of_rows = config->{posts_on_page} || 5; # Number of posts per page
  62. my @posts = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => $nr_of_rows });
  63. my $nr_of_posts = resultset('Post')->search({ status => 'published' })->count;
  64. my @tags = resultset('View::PublishedTags')->all();
  65. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  66. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  67. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  68.  
  69. # extract demo posts info
  70. my @mapped_posts = map_posts(@posts);
  71.  
  72. my $total_pages = get_total_pages($nr_of_posts, $nr_of_rows);
  73. my ($previous_link, $next_link) = get_previous_next_link(1, $total_pages);
  74.  
  75. template 'index',
  76. {
  77. posts => \@mapped_posts,
  78. recent => \@recent,
  79. popular => \@popular,
  80. tags => \@tags,
  81. categories => \@categories,
  82. page => 1,
  83. total_pages => $total_pages,
  84. previous_link => $previous_link,
  85. next_link => $next_link
  86. };
  87. };
  88.  
  89. =head
  90.  
  91. Home page
  92.  
  93. =cut
  94.  
  95. get '/page/:page' => sub {
  96.  
  97. my $nr_of_rows = config->{posts_on_page} || 5; # Number of posts per page
  98. my $page = route_parameters->{'page'};
  99. my @posts = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => $nr_of_rows, page => $page });
  100. my $nr_of_posts = resultset('Post')->search({ status => 'published' })->count;
  101. my @tags = resultset('View::PublishedTags')->all();
  102. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  103. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  104. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  105.  
  106. # extract demo posts info
  107. my @mapped_posts = map_posts(@posts);
  108.  
  109. # Calculate the next and previous page link
  110. my $total_pages = get_total_pages($nr_of_posts, $nr_of_rows);
  111. my ($previous_link, $next_link) = get_previous_next_link($page, $total_pages);
  112.  
  113. template 'index',
  114. {
  115. posts => \@mapped_posts,
  116. recent => \@recent,
  117. popular => \@popular,
  118. tags => \@tags,
  119. categories => \@categories,
  120. page => $page,
  121. total_pages => $total_pages,
  122. previous_link => $previous_link,
  123. next_link => $next_link
  124. };
  125. };
  126.  
  127.  
  128. =head
  129.  
  130. View post method
  131.  
  132. =cut
  133.  
  134. get '/post/:slug' => sub {
  135.  
  136. my $slug = route_parameters->{'slug'};
  137. my $post = resultset('Post')->find({ slug => $slug });
  138. my $settings = resultset('Setting')->first;
  139. my @tags = resultset('View::PublishedTags')->all();
  140. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  141. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  142. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  143.  
  144. #my $response = param('g-recaptcha-response');
  145. #my $result = recaptcha_verify($response);
  146.  
  147.  
  148. # Grab the approved comments for this post and the corresponding reply comments
  149. my @comments;
  150. @comments = resultset('Comment')->search({ post_id => $post->id, status => 'approved', reply_to => undef }) if ( $post );
  151. foreach my $comment (@comments) {
  152. my @comment_replies = resultset('Comment')->search({ reply_to => $comment->id, status => 'approved' }, {order_by => { -asc => "comment_date" }});
  153. foreach my $reply (@comment_replies) {
  154. my $el;
  155. map { $el->{$_} = $reply->$_ } ('avatar', 'fullname', 'comment_date', 'content');
  156. $el->{uid}->{username} = $reply->uid->username if $reply->uid;
  157. push(@{$comment->{comment_replies}}, $el);
  158. }
  159. }
  160.  
  161. template 'post',
  162. {
  163. post => $post,
  164. recent => \@recent,
  165. popular => \@popular,
  166. categories => \@categories,
  167. comments => \@comments,
  168. setting => $settings,
  169. tags => \@tags,
  170. recaptcha => recaptcha_display()
  171. };
  172. };
  173.  
  174. =head
  175.  
  176. Add a comment method
  177.  
  178. =cut
  179.  
  180. post '/comment/add' => sub {
  181.  
  182.  
  183.  
  184. my $parameters = body_parameters;
  185. my $fullname = $parameters->{'fullname'};
  186. my $post_id = $parameters->{'id'};
  187. my $secret = $parameters->{'secret'};
  188. my @comments = resultset('Comment')->search({ post_id => $post_id, status => 'approved', reply_to => undef });
  189. my $post = resultset('Post')->find( $post_id );
  190. my @categories = resultset('Category')->all();
  191. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  192. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  193. my $user = session('user');
  194. #warn "The secret is";
  195. #warn Dumper($secret);
  196. #warn "The params are |$parameters| ";
  197. #warn Dumper($parameters);
  198. $parameters->{'reply_to'} = $1 if ($parameters->{'in_reply_to'} =~ /(\d+)/);
  199. if ($parameters->{'reply_to'}) {
  200. my $comm = resultset('Comment')->find({ id => $parameters->{'reply_to'} });
  201. if ($comm) {
  202. $parameters->{'reply_to_content'} = $comm->content;
  203. $parameters->{'reply_to_user'} = $comm->fullname;
  204. }
  205. }
  206.  
  207. my $template_params = {
  208. post => $post,
  209. categories => \@categories,
  210. popular => \@popular,
  211. recent => \@recent,
  212. warning => 'The secret code is incorrect',
  213. recaptcha => recaptcha_display()
  214.  
  215. };
  216.  
  217.  
  218. my $response = param('g-recaptcha-response');
  219. warn "The response is |$response |\n";
  220. my $result = recaptcha_verify($response);
  221. warn "The response in english is:\n ";
  222. warn Dumper($result);
  223. warn $result;
  224. my $err;
  225.  
  226. #my $result = recaptcha_verify($response); #recaptcha_verify($secret);
  227. #warn "The secret is";
  228. #warn Dumper($result );
  229.  
  230. if ( $result->{success} ) {
  231. # The user entered the correct secret code
  232. eval {
  233.  
  234. # If the person who leaves the comment is either the author or the admin the comment is automaticaly approved
  235.  
  236. my $comment = resultset('Comment')->can_create( $parameters, $user );
  237.  
  238. # Notify the author that a new comment was submited
  239. my $author = $post->user;
  240.  
  241. Email::Template->send( config->{email_templates} . 'new_comment.tt',
  242. {
  243. From => config->{default_email_sender},
  244. To => $author->email,
  245. Subject => ($parameters->{'reply_to'} ? 'A comment reply was submitted to your post' : 'A new comment was submitted to your post'),
  246.  
  247. tt_vars => {
  248. fullname => $fullname,
  249. title => $post->title,
  250. comment => $parameters->{'comment'},
  251. signature => config->{email_signature},
  252. post_url => config->{app_url} . '/post/' . $post->slug,
  253. app_url => config->{app_url},
  254. reply_to_content => $parameters->{'reply_to_content'} || '',
  255. reply_to_user => $parameters->{'reply_to_user'} || '',
  256. },
  257. }) or error "Could not send the email";
  258. };
  259. error $@ if ( $@ );
  260.  
  261. # Grab the approved comments for this post
  262. @comments = resultset('Comment')->search({ post_id => $post->id, status => 'approved', reply_to => undef }) if ( $post );
  263.  
  264. delete $template_params->{warning};
  265. delete $template_params->{in_reply_to};
  266. warn $post->user_id;
  267. warn $user;
  268. warn $post->user_id;
  269. warn $user->{id};
  270.  
  271. if (($post->user_id && $user && $post->user_id == $user->{id}) or ($user && $user->{is_admin})) {
  272. $template_params->{success} = 'Your comment has been submited. Thank you!';
  273. } else {
  274. $template_params->{success} = 'Your comment has been submited and it will be displayed as soon as the author accepts it. Thank you!';
  275. }
  276. }
  277. else {
  278. # The secret code inncorrect
  279. $err = "Invalid secret code.";
  280.  
  281. }
  282. #$template_params->{fields} = $parameters;
  283. #$template_params->{success} = 'Are you a robot ?';
  284. #return template 'post'{$template_params};
  285.  
  286. #}
  287.  
  288.  
  289. #if ($err) {
  290. #$template_params->{warning} = $err if $err;
  291.  
  292. ##new_captcha_code();
  293. #my $response = param('g-recaptcha-response');
  294. #my $result = recaptcha_verify($response);
  295.  
  296. #template 'comment_form',{
  297. #$template_params,
  298. #recaptcha => recaptcha_display(),
  299.  
  300.  
  301. #};
  302. #}
  303.  
  304.  
  305. foreach my $comment (@comments) {
  306. my @comment_replies = resultset('Comment')->search({ reply_to => $comment->id, status => 'approved' }, {order_by => { -asc => "comment_date" }});
  307. foreach my $reply (@comment_replies) {
  308. my $el;
  309. map { $el->{$_} = $reply->$_ } ('avatar', 'fullname', 'comment_date', 'content');
  310. $el->{uid}->{username} = $reply->uid->username if $reply->uid;
  311. push(@{$comment->{comment_replies}}, $el);
  312. }
  313. }
  314. $template_params->{comments} = \@comments;
  315.  
  316.  
  317.  
  318. template 'post',{
  319. $template_params,
  320. recaptcha => recaptcha_display(),
  321. };
  322.  
  323. };
  324.  
  325. =head
  326.  
  327. List all posts by selected category
  328.  
  329. =cut
  330.  
  331. get '/posts/category/:slug' => sub {
  332.  
  333. my $nr_of_rows = config->{posts_on_page} || 5; # Number of posts per page
  334. my $slug = route_parameters->{'slug'};
  335. my @posts = resultset('Post')->search({ 'category.slug' => $slug, 'status' => 'published' }, { join => { 'post_categories' => 'category' }, order_by => { -desc => "created_date" }, rows => $nr_of_rows });
  336. my $nr_of_posts = resultset('Post')->search({ 'category.slug' => $slug, 'status' => 'published' }, { join => { 'post_categories' => 'category' } })->count;
  337. my @tags = resultset('View::PublishedTags')->all();
  338. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  339. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  340. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  341.  
  342. # extract demo posts info
  343. my @mapped_posts = map_posts(@posts);
  344.  
  345. # Calculate the next and previous page link
  346. my $total_pages = get_total_pages($nr_of_posts, $nr_of_rows);
  347. my ($previous_link, $next_link) = get_previous_next_link(1, $total_pages, '/posts/category/' . $slug);
  348.  
  349. # Extract all posts with the wanted category
  350. template 'index',
  351. {
  352. posts => \@mapped_posts,
  353. recent => \@recent,
  354. popular => \@popular,
  355. tags => \@tags,
  356. page => 1,
  357. categories => \@categories,
  358. total_pages => $total_pages,
  359. next_link => $next_link,
  360. previous_link => $previous_link,
  361. posts_for_category => $slug
  362. };
  363. };
  364.  
  365. =head
  366.  
  367. List all posts by selected category
  368.  
  369. =cut
  370.  
  371. get '/posts/category/:slug/page/:page' => sub {
  372.  
  373. my $nr_of_rows = config->{posts_on_page} || 5; # Number of posts per page
  374. my $page = route_parameters->{'page'};
  375. my $slug = route_parameters->{'slug'};
  376. my @posts = resultset('Post')->search({ 'category.slug' => $slug, 'status' => 'published' }, { join => { 'post_categories' => 'category' }, order_by => { -desc => "created_date" }, rows => $nr_of_rows, page => $page });
  377. my $nr_of_posts = resultset('Post')->search({ 'category.slug' => $slug, 'status' => 'published' }, { join => { 'post_categories' => 'category' } })->count;
  378. my @tags = resultset('View::PublishedTags')->all();
  379. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  380. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  381. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  382.  
  383. # extract demo posts info
  384. my @mapped_posts = map_posts(@posts);
  385.  
  386. # Calculate the next and previous page link
  387. my $total_pages = get_total_pages($nr_of_posts, $nr_of_rows);
  388. my ($previous_link, $next_link) = get_previous_next_link($page, $total_pages, '/posts/category/' . $slug);
  389.  
  390. template 'index',
  391. {
  392. posts => \@mapped_posts,
  393. recent => \@recent,
  394. popular => \@popular,
  395. tags => \@tags,
  396. categories => \@categories,
  397. page => $page,
  398. total_pages => $total_pages,
  399. next_link => $next_link,
  400. previous_link => $previous_link,
  401. posts_for_category => $slug
  402. };
  403. };
  404.  
  405. =head
  406.  
  407. List all posts by selected author
  408.  
  409. =cut
  410.  
  411. get '/posts/user/:username' => sub {
  412.  
  413. my $nr_of_rows = config->{posts_on_page} || 5; # Number of posts per page
  414. my $username = route_parameters->{'username'};
  415. my $user = resultset('MyUser')->find({username => $username});
  416. unless ($user) {
  417. # we did not identify the user
  418. }
  419. my @posts = resultset('Post')->search({ 'user_id' => $user->id, 'status' => 'published' }, { order_by => { -desc => "created_date" }, rows => $nr_of_rows });
  420. my $nr_of_posts = resultset('Post')->search({ 'user_id' => $user->id, 'status' => 'published' })->count;
  421. my @tags = resultset('View::PublishedTags')->all();
  422. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  423. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  424. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  425.  
  426. # extract demo posts info
  427. my @mapped_posts = map_posts(@posts);
  428.  
  429. # Calculate the next and previous page link
  430. my $total_pages = get_total_pages($nr_of_posts, $nr_of_rows);
  431. my ($previous_link, $next_link) = get_previous_next_link(1, $total_pages, '/posts/user/' . $username);
  432.  
  433. # Extract all posts with the wanted category
  434. template 'index',
  435. {
  436. posts => \@mapped_posts,
  437. recent => \@recent,
  438. popular => \@popular,
  439. tags => \@tags,
  440. page => 1,
  441. categories => \@categories,
  442. total_pages => $total_pages,
  443. next_link => $next_link,
  444. previous_link => $previous_link,
  445. posts_for_user => $username,
  446. };
  447. };
  448.  
  449. =head
  450.  
  451. List all posts by selected category
  452.  
  453. =cut
  454.  
  455. get '/posts/user/:username/page/:page' => sub {
  456.  
  457. my $nr_of_rows = config->{posts_on_page} || 5; # Number of posts per page
  458. my $username = route_parameters->{'username'};
  459. my $user = resultset('User')->find({username => $username});
  460. unless ($user) {
  461. # we did not identify the user
  462. }
  463. my $page = route_parameters->{'page'};
  464. my @posts = resultset('Post')->search({ 'user_id' => $user->id, 'status' => 'published' }, { order_by => { -desc => "created_date" }, rows => $nr_of_rows, page => $page });
  465. my $nr_of_posts = resultset('Post')->search({ 'user_id' => $user->id, 'status' => 'published' })->count;
  466. my @tags = resultset('View::PublishedTags')->all();
  467. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  468. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  469. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  470.  
  471. # extract demo posts info
  472. my @mapped_posts = map_posts(@posts);
  473.  
  474. # Calculate the next and previous page link
  475. my $total_pages = get_total_pages($nr_of_posts, $nr_of_rows);
  476. my ($previous_link, $next_link) = get_previous_next_link($page, $total_pages, '/posts/user/' . $username);
  477.  
  478. template 'index',
  479. {
  480. posts => \@mapped_posts,
  481. recent => \@recent,
  482. popular => \@popular,
  483. tags => \@tags,
  484. categories => \@categories,
  485. page => $page,
  486. total_pages => $total_pages,
  487. next_link => $next_link,
  488. previous_link => $previous_link,
  489. posts_for_user => $username,
  490. };
  491. };
  492.  
  493. =head
  494.  
  495. List all posts by selected tag
  496.  
  497. =cut
  498.  
  499. get '/posts/tag/:slug' => sub {
  500.  
  501. my $nr_of_rows = config->{posts_on_page} || 5; # Number of posts per page
  502. my $slug = route_parameters->{'slug'};
  503. my @posts = resultset('Post')->search({ 'tag.slug' => $slug, 'status' => 'published' }, { join => { 'post_tags' => 'tag' }, order_by => { -desc => "created_date" }, rows => $nr_of_rows });
  504. my $nr_of_posts = resultset('Post')->search({ 'tag.slug' => $slug, 'status' => 'published' }, { join => { 'post_tags' => 'tag' } })->count;
  505. my @tags = resultset('View::PublishedTags')->all();
  506. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  507. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  508. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  509.  
  510. # extract demo posts info
  511. my @mapped_posts = map_posts(@posts);
  512.  
  513. # Calculate the next and previous page link
  514. my $total_pages = get_total_pages($nr_of_posts, $nr_of_rows);
  515. my ($previous_link, $next_link) = get_previous_next_link(1, $total_pages, '/posts/tag/' . $slug);
  516.  
  517. template 'index',
  518. {
  519. posts => \@mapped_posts,
  520. recent => \@recent,
  521. popular => \@popular,
  522. tags => \@tags,
  523. page => 1,
  524. categories => \@categories,
  525. total_pages => $total_pages,
  526. next_link => $next_link,
  527. previous_link => $previous_link,
  528. posts_for_tag => $slug
  529. };
  530. };
  531.  
  532. =head
  533.  
  534. List all posts by selected tag
  535.  
  536. =cut
  537.  
  538. get '/posts/tag/:slug/page/:page' => sub {
  539.  
  540. my $nr_of_rows = config->{posts_on_page} || 5; # Number of posts per page
  541. my $page = route_parameters->{'page'};
  542. my $slug = route_parameters->{'slug'};
  543. my $tag = resultset('Tag')->find({ slug => $slug });
  544. my @posts = resultset('Post')->search({ 'tag.slug' => $slug, 'status' => 'published' }, { join => { 'post_tags' => 'tag' }, order_by => { -desc => "created_date" }, rows => $nr_of_rows });
  545. my $nr_of_posts = resultset('Post')->search({ 'tag.slug' => $slug, 'status' => 'published' }, { join => { 'post_tags' => 'tag' } })->count;
  546. my @tags = resultset('View::PublishedTags')->all();
  547. my @categories = resultset('View::PublishedCategories')->search({ name => { '!=' => 'Uncategorized'} });
  548. my @recent = resultset('Post')->search({ status => 'published' },{ order_by => { -desc => "created_date" }, rows => 3 });
  549. my @popular = resultset('View::PopularPosts')->search({}, { rows => 3 });
  550.  
  551. # extract demo posts info
  552. my @mapped_posts = map_posts(@posts);
  553.  
  554. # Calculate the next and previous page link
  555. my $total_pages = get_total_pages($nr_of_posts, $nr_of_rows);
  556. my ($previous_link, $next_link) = get_previous_next_link($page, $total_pages, '/posts/tag/' . $slug);
  557.  
  558. template 'index',
  559. {
  560. posts => \@mapped_posts,
  561. recent => \@recent,
  562. popular => \@popular,
  563. tags => \@tags,
  564. page => $page,
  565. categories => \@categories,
  566. total_pages => $total_pages,
  567. next_link => $next_link,
  568. previous_link => $previous_link,
  569. posts_for_tag => $slug
  570. };
  571. };
  572.  
  573. get '/sign-up' => sub {
  574.  
  575. #new_captcha_code();
  576.  
  577. template 'signup',{recaptcha => recaptcha_display()};
  578. };
  579.  
  580. post '/sign-up' => sub {
  581. my $response = param('g-recaptcha-response');
  582. my $result = recaptcha_verify($response);
  583. warn "the recaptcha_verify is |$result |";
  584. my $params = body_parameters;
  585.  
  586. my $err;
  587.  
  588. my $template_params = {
  589. username => $params->{'username'},
  590. email => $params->{'email'},
  591. first_name => $params->{'first_name'},
  592. last_name => $params->{'last_name'},
  593. recaptcha => recaptcha_display(),
  594. };
  595.  
  596. #my $response = params->{'g-recaptcha-response'};
  597.  
  598.  
  599. if ( $result->{success} ) {
  600. # The user entered the correct secrete code
  601. eval {
  602.  
  603. my $u = resultset('MyUser')->search( { email => $params->{'email'} } )->first;
  604. if ($u) {
  605. $err = "An user with this email address already exists.";
  606. } else {
  607. $u = resultset('MyUser')->search( { username => $params->{'username'} } )->first;
  608. if ($u) {
  609. $err = "The provided username is already in use.";
  610. } else {
  611.  
  612. # Create the user
  613. if ( $params->{'username'} ) {
  614.  
  615. # Set the proper timezone
  616. my $dt = DateTime->now;
  617. my $settings = resultset('Setting')->first;
  618. $dt->set_time_zone( $settings->timezone );
  619.  
  620. my ($password, $pass_hash, $salt) = create_password();
  621.  
  622. resultset('MyUser')->create({
  623. username => $params->{username},
  624. password => $pass_hash,
  625. salt => $salt,
  626. email => $params->{'email'},
  627. first_name => $params->{'first_name'},
  628. last_name => $params->{'last_name'},
  629. register_date => join (' ', $dt->ymd, $dt->hms),
  630. role => 'author',
  631. status => 'pending'
  632. });
  633.  
  634. # Notify the author that a new comment was submited
  635. my $first_admin = resultset('MyUser')->search( {role => 'admin', status => 'activated' } )->first;
  636.  
  637. Email::Template->send( config->{email_templates} . 'new_user.tt',
  638. {
  639. From => config->{default_email_sender},
  640. To => $first_admin->email,
  641. Subject => 'A new user applied as an author to the blog',
  642.  
  643. tt_vars => {
  644. first_name => $params->{'first_name'},
  645. last_name => $params->{'last_name'},
  646. username => $params->{'username'},
  647. email => $params->{'email'},
  648. signature => config->{email_signature},
  649. blog_name => session('blog_name'),
  650. app_url => session('app_url'),
  651. }
  652. }) or error "Could not send the email";
  653.  
  654. } else {
  655. $err = 'Please provide a username.';
  656. }
  657. }
  658. }
  659. };
  660. error $@ if ( $@ );
  661. }
  662. else {
  663. # The secret code inncorrect
  664. # Repopulate the fields with the data
  665. $err = "Invalid secret code.";
  666. }
  667.  
  668. if ($err) {
  669. $template_params->{warning} = $err if $err;
  670.  
  671. #new_captcha_code();
  672. my $response = param('g-recaptcha-response');
  673. my $result = recaptcha_verify($response);
  674.  
  675. template 'signup', $template_params;
  676. } else {
  677. template 'notify', {
  678. success => 'The user was created and it is waiting for admin approval.'};
  679. }
  680. };
  681.  
  682. sub new_captcha_code {
  683.  
  684. my $code = PearlBee::Helpers::Captcha::generate();
  685.  
  686. session secret => $code;
  687. session secrets => [] unless session('secrets'); # this is a hack because Google Chrome triggers GET 2 times, and it messes up the valid captcha code
  688. push(session('secrets'), $code);
  689.  
  690. return $code;
  691. }
  692.  
  693. sub check_captcha_code {
  694. my $code = shift;
  695.  
  696. my $ok = 0;
  697. my $sess = session();
  698.  
  699. if ($sess->{data}->{secrets}) {
  700. foreach my $secret (@{$sess->{data}->{secrets}}) {
  701. my $result= $PearlBee::Helpers::Captcha::captcha->check_code($code, $secret);
  702. if ( $result == 1 ) {
  703. $ok = 1;
  704. session secrets => [];
  705. last;
  706. }
  707. }
  708. }
  709.  
  710. return $ok;
  711. }
  712.  
  713. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement