comment.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. $http_host = $_SERVER["HTTP_HOST"];
  3. include("../functions.php");
  4. $con = startdb("rw");
  5. $proto = getProtocol();
  6. $server = "$proto$http_host";
  7. //Get post values
  8. $from =mysqli_real_escape_string($con, $_POST["from"]);
  9. $post = mysqli_real_escape_string($con, $_POST["post"]);
  10. $user = mysqli_real_escape_string($con, $_POST["user"]);
  11. $text = mysqli_real_escape_string($con, $_POST["text"]);
  12. $lang = strtolower($_POST["lang"]);
  13. //Check if post exists and it allows comments
  14. $q_post = mysqli_query($con, "SELECT id FROM post WHERE id = $post AND comments = 1;");
  15. error_log("SELECT id FROM post WHERE id = $post AND comments = 1;");
  16. if (mysqli_num_rows($q_post) == 0){
  17. error_log("Tried to post a comment in a nonexistent post or a post that doesnt allow comments. POST ID: '$post'");
  18. http_response_code(405);
  19. exit(-1);
  20. }
  21. //Check language, set fallback.
  22. if ($lang != 'es' && $lang != 'en' && $lang != 'eu'){
  23. $lang = 'es';
  24. }
  25. //Chack for null fields
  26. if (strlen($user) <= 0 || strlen($text) <= 0){
  27. error_log("Tried to post a comment with null text or user on post. USER: '$user', TEXT: '$text'");
  28. http_response_code(405);
  29. exit(-1);
  30. }
  31. if ($from == 'web'){
  32. //Format newlines in text
  33. $text = str_replace(["\r\n", "\r", "\n"], "<br/>", $text);
  34. //Get visit
  35. $ip = getUserIP();
  36. $visit = '';
  37. $q = mysqli_query($con, "SELECT id FROM stat_visit WHERE ip = '$ip';");
  38. if (mysqli_num_rows($q) > 0){
  39. $r = mysqli_fetch_array($q);
  40. $visit = $r["id"];
  41. }
  42. //Insert row
  43. mysqli_query($con, "INSERT INTO post_comment (post, text, username, lang, visit) VALUES ($post, '$text', '$user', '$lang', '$visit');");
  44. }
  45. elseif ($from == 'app'){
  46. $code = mysqli_real_escape_string($con, $_POST["code"]);
  47. mysqli_query($con, "INSERT INTO post_comment (post, text, username, lang, app) VALUES ($post, '$text', '$user', '$lang', '$code');");
  48. }
  49. //Prepare the page to update the comment section
  50. $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;");
  51. while ($r_comment = mysqli_fetch_array($q_comment)){
  52. ?>
  53. <div itemprop='comment' itemscope itemtype='http://schema.org/UserComments' id='comment_<?=$r_comment["id"]?>' class='comment'>
  54. <span itemprop='creator' class='comment_user'><?=$r_comment["username"]?></span>
  55. <span class='comment_date date'><meta itemprop='commentTime' content='<?=$r_comment["isodate"]?>'/><?=formatDate(<?=$r_comment["dtime"], $lang)?></span>
  56. <p itemprop='commentText' class='comment_text'><?=$r_comment["text"]?></p>
  57. <hr class='comment_line'/>
  58. </div>
  59. <?php
  60. }
  61. ?>