comment.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. $http_host = $_SERVER['HTTP_HOST'];
  3. include("../functions.php");
  4. $con = startdb('rw');
  5. //Get post values
  6. $post = mysqli_real_escape_string($con, $_POST["post"]);
  7. $user = mysqli_real_escape_string($con, $_POST["user"]);
  8. $text = mysqli_real_escape_string($con, $_POST["text"]);
  9. $lang = strtolower($_POST["lang"]);
  10. //Check if post exists and it allows comments
  11. $q_post = mysqli_query($con, "SELECT id FROM post WHERE id = $post AND comments = 1;");
  12. error_log("SELECT id FROM post WHERE id = $post AND comments = 1;");
  13. if (mysqli_num_rows($q_post) == 0){
  14. error_log("Tried to post a comment in a nonexistent post or a post that doesnt allow comments. POST ID: '$post'");
  15. http_response_code(405);
  16. exit(-1);
  17. }
  18. //Check language, set fallback.
  19. if ($lang != 'es' && $lang != 'en' && $lang != 'eu'){
  20. $lang = 'es';
  21. }
  22. //Chack for null fields
  23. if (strlen($user) <= 0 || strlen($text) <= 0){
  24. error_log("Tried to post a comment with null text or user on post. USER: '$user', TEXT: '$text'");
  25. http_response_code(405);
  26. exit(-1);
  27. }
  28. //Format newlines in text
  29. $text = str_replace(["\r\n", "\r", "\n"], "<br/>", $text);
  30. //Insert row
  31. mysqli_query($con, "INSERT INTO post_comment (post, text, username, lang) VALUES ($post, '$text', '$user', '$lang');");
  32. version();
  33. //Prepare the page to update the comment section
  34. //WARNING: If changes are done here, do the same in post.php
  35. $q_comment = mysqli_query($con, "SELECT id, post, DATE_FORMAT(dtime, '%Y-%m-%dT%T') AS isodate, dtime, user, username, lang, text FROM post_comment WHERE post = $post AND approved = 1 ORDER BY dtime;");
  36. error_log("SELECT id, post, DATE_FORMAT(dtime, '%Y-%m-%dT%T') AS isodate, dtime, user, username, lang, text FROM post_comment WHERE post = $post AND approved = 1 ORDER BY TIME;");
  37. while ($r_comment = mysqli_fetch_array($q_comment)){
  38. echo "<div itemprop='comment' itemscope itemtype='http://schema.org/UserComments' id='comment_$r_comment[id]' class='comment'>\n";
  39. //When official users are implementes, see if there is user or username
  40. echo "<span itemprop='creator' class='comment_user'>$r_comment[username]</span>\n";
  41. echo "<span class='comment_date date'><meta itemprop='commentTime' content='$r_comment[isodate]'/>" . formatDate($r_comment['dtime'], $lang) . "</span>\n";
  42. echo "<p itemprop='commentText' class='comment_text'>$r_comment[text]</p>";
  43. echo "<hr class='comment_line'/>\n";
  44. echo "</div>\n";
  45. }
  46. ?>