edit.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /* This script is used to edit a user own post or 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: save changes =======================================================
  16. if ($_SERVER['REQUEST_METHOD'] === 'POST')
  17. {
  18. // Edit a comment
  19. if (isset ($_POST['comment']))
  20. {
  21. $comment = $db->get_comment ($_POST['comment']);
  22. // Make sure user has the right to edit this comment
  23. if ($comment['userId'] != Session::get_userid ())
  24. {
  25. header ('Location: ./');
  26. exit ();
  27. }
  28. $new_comment_data =
  29. [
  30. 'text' => isset ($_POST['text']) ? trim ($_POST['text']) : ''
  31. ];
  32. $db->edit_comment (
  33. $new_comment_data['text'],
  34. $comment['hashId'],
  35. Session::get_userid ());
  36. header ('Location: ./post/' . $comment['postHashId'] . '#comment-' . $comment['hashId']);
  37. exit ();
  38. }
  39. // Edit a post
  40. if (isset ($_POST['post']))
  41. {
  42. $post = $db->get_post ($_POST['post']);
  43. // Make sure user has the right to edit this post
  44. if ($post['userId'] != Session::get_userid ())
  45. {
  46. header ('Location: ./');
  47. exit ();
  48. }
  49. // New title/link/text to update the post with
  50. $new_post_data =
  51. [
  52. 'title' => isset ($_POST['title']) ? trim ($_POST['title']) : '',
  53. 'link' => isset ($_POST['link']) ? trim ($_POST['link']) : '',
  54. 'text' => isset ($_POST['text']) ? trim ($_POST['text']) : ''
  55. ];
  56. // MUST have a title
  57. if (strlen ($new_post_data['title']) == 0)
  58. $new_post_data['title'] = $post['title'];
  59. // If no link given, keep an empty string
  60. if (strlen ($new_post_data['link']) > 0)
  61. {
  62. $link_components = parse_url ($new_post_data['link']);
  63. // Make sure there's a "scheme"
  64. if (!isset ($link_components['scheme']))
  65. $new_post_data['link'] = 'http://' . $new_post_data['link'];
  66. }
  67. $db->edit_post (
  68. $new_post_data['title'],
  69. $new_post_data['link'],
  70. $new_post_data['text'],
  71. $post['hashId'],
  72. Session::get_userid ());
  73. header ('Location: ./post/' . $post['hashId']);
  74. exit ();
  75. }
  76. header ('Location: ./');
  77. exit ();
  78. }
  79. // GET: show reply page =====================================================
  80. // Must have a comment id (to reply to)
  81. if (!isset ($_GET['post']) && !isset ($_GET['comment']))
  82. {
  83. header ('Location: ./');
  84. exit ();
  85. }
  86. // Is user editing a post or a comment?
  87. if (isset ($_GET['post']))
  88. $item = array(
  89. 'type' => 'post',
  90. 'data' => $db->get_post ($_GET['post']));
  91. else
  92. $item = array(
  93. 'type' => 'comment',
  94. 'data' => $db->get_comment ($_GET['comment']));
  95. // Make sure the user is the actual poster/commenter
  96. if ($item['data']['userId'] != Session::get_userid ())
  97. {
  98. header ('Location: ./');
  99. exit ();
  100. }
  101. // Render template
  102. switch ($item['type'])
  103. {
  104. case 'comment':
  105. $template = 'edit_comment.twig';
  106. break;
  107. case 'post':
  108. $template = 'edit_post.twig';
  109. break;
  110. }
  111. echo $twig->render (
  112. $template,
  113. array ('item' => $item));