reply.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /* This script is used to reply to a user comment */
  3. require_once 'session.php';
  4. require_once 'database.php';
  5. require_once 'date.php';
  6. require_once 'twig.php';
  7. $db = new Database();
  8. $db->connect();
  9. // Must be logged in
  10. if (!Session::is_valid())
  11. {
  12. header ('Location: ./');
  13. exit ();
  14. }
  15. // POST ====================================================================
  16. // Submit the new comment
  17. if ($_SERVER['REQUEST_METHOD'] === 'POST')
  18. {
  19. if (!isset ($_POST['text']))
  20. {
  21. header ('Location: ./');
  22. exit ();
  23. }
  24. $parent_comment = $db->get_comment ($_POST['parent_comment']);
  25. $text = trim ($_POST['text']);
  26. // Empty comment. Redirect to parent comment
  27. if (strlen ($text) == 0)
  28. {
  29. header ('Location: ./post/' . $parent_comment['postHashId'] . '#comment-' . $parent_comment['hashId']);
  30. exit ();
  31. }
  32. // We have a text, add the reply and redirect to the new reply
  33. $hash_id = $db->new_reply (
  34. $text,
  35. $parent_comment['hashId'],
  36. Session::get_userid ());
  37. // Can't post?! What happened?!
  38. if (is_null ($hash_id))
  39. header ('Location: ./');
  40. else
  41. header ('Location: ./post/' . $hash_id['post'] . '#comment-' . $hash_id['comment']);
  42. exit ();
  43. }
  44. // GET =====================================================================
  45. // Must have a comment id (to reply to)
  46. if (!isset ($_GET['comment']))
  47. {
  48. header ('Location: ./');
  49. exit ();
  50. }
  51. $comment = $db->get_comment ($_GET['comment']);
  52. // Render template
  53. echo $twig->render (
  54. 'reply.twig',
  55. array ('comment' => $comment));