comment.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. //Get visit
  31. $ip = getUserIP();
  32. $visit = '';
  33. $q = mysqli_query($con, "SELECT id FROM stat_visit WHERE ip = '$ip';");
  34. if (mysqli_num_rows($q) > 0){
  35. $r = mysqli_fetch_array($q);
  36. $visit = $r['id'];
  37. }
  38. //Insert row
  39. mysqli_query($con, "INSERT INTO post_comment (post, text, username, lang, visit) VALUES ($post, '$text', '$user', '$lang', '$visit');");
  40. version();
  41. //Prepare the page to update the comment section
  42. //WARNING: If changes are done here, do the same in post.php
  43. $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;");
  44. while ($r_comment = mysqli_fetch_array($q_comment)){
  45. echo "<div itemprop='comment' itemscope itemtype='http://schema.org/UserComments' id='comment_$r_comment[id]' class='comment'>\n";
  46. //When official users are implementes, see if there is user or username
  47. echo "<span itemprop='creator' class='comment_user'>$r_comment[username]</span>\n";
  48. echo "<span class='comment_date date'><meta itemprop='commentTime' content='$r_comment[isodate]'/>" . formatDate($r_comment['dtime'], $lang) . "</span>\n";
  49. echo "<p itemprop='commentText' class='comment_text'>$r_comment[text]</p>";
  50. echo "<hr class='comment_line'/>\n";
  51. echo "</div>\n";
  52. }
  53. ?>