post.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. require_once 'session.php';
  3. require_once 'config.php';
  4. require_once 'database.php';
  5. require_once 'date.php';
  6. require_once 'twig.php';
  7. $db = new Database ();
  8. $db->connect ();
  9. // POST new comment submission
  10. if ($_SERVER['REQUEST_METHOD'] === 'POST')
  11. {
  12. // Must be logged in
  13. if (!Session::is_valid ())
  14. {
  15. header ('Location: ./');
  16. exit ();
  17. }
  18. // Make sure we have a valid comment
  19. if (!isset ($_POST['new_comment']) || is_null($_POST['new_comment']))
  20. {
  21. header ('Location: ./');
  22. exit ();
  23. }
  24. // Clear input data
  25. $comment = trim ($_POST['new_comment']);
  26. // Empty text... do nothing
  27. if (strlen ($comment) == 0)
  28. {
  29. // Retrieve the post
  30. $post = $db->get_post ($_GET['hash_id']);
  31. if (is_null ($post) || empty ($post))
  32. exit ();
  33. header ('Location: ./' . $post['hashId']);
  34. exit();
  35. }
  36. // Everything seems OK, add the new comment
  37. $post_hash_id = $_GET['hash_id'];
  38. $comment_hash_id = $db->new_comment ($comment, $post_hash_id, Session::get_userid());
  39. /* Send email notification for the new comment
  40. * $post_op is the post's original poster
  41. */
  42. /*
  43. if (Config::$SEND_EMAILS)
  44. {
  45. $post = $db->get_post ($post_hash_id);
  46. $post_op = $db->get_post_op ($post_hash_id);
  47. if ($post_op['email_notifications'])
  48. mail ($post_op['email'],
  49. 'freepost: new comment to one of your posts',
  50. $twig->render ('email/new_comment.twig', array (
  51. 'commenter' => Session::get_username (),
  52. 'post' => $post['title']
  53. )),
  54. 'From: freepost <noreply@freepo.st>' . "\r\n" . 'Reply-To: freepost <noreply@freepo.st>');
  55. }
  56. */
  57. header ('Location: ./' . $post_hash_id . '#comment-' . $comment_hash_id);
  58. exit ();
  59. }
  60. // GET display default page
  61. // Retrieve the post
  62. $post = $db->get_post ($_GET['hash_id']);
  63. // Wrong hash_id
  64. if (is_null ($post) || empty ($post))
  65. {
  66. echo '404';
  67. exit ();
  68. }
  69. // Retrieve if user has voted this post
  70. $votes_post = $db->get_posts_votes ($post['id'], Session::get_userid ());
  71. // Retrieve comments for this post
  72. $comments = $db->get_post_comments ($post['id']);
  73. // Retrieve a list of user votes for the comments
  74. $IDs = array();
  75. foreach ($comments as $parent)
  76. foreach ($parent as $child)
  77. $IDs[] = $child['id'];
  78. $votes_comment = $db->get_comments_votes (implode (',', $IDs), Session::get_userid ());
  79. // Render template
  80. echo $twig->render (
  81. 'post.twig',
  82. array(
  83. 'title' => $post['title'],
  84. 'post' => $post,
  85. 'comments' => $comments,
  86. 'votes' => array (
  87. 'post' => $votes_post,
  88. 'comment' => $votes_comment)));