comment.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. $photo = mysqli_real_escape_string($con, $_POST["photo"]);
  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 photo exists and it allows comments
  14. $q_photo = mysqli_query($con, "SELECT id FROM photo WHERE id = $photo;"); //Not checking if comments are alowed
  15. if (mysqli_num_rows($q_photo) == 0){
  16. error_log("Tried to post a comment in a nonexistent photo or a photo that doesnt allow comments. POST ID: '$id'");
  17. http_response_code(405);
  18. exit(-1);
  19. }
  20. //Check language, set fallback.
  21. if ($lang != 'es' && $lang != 'en' && $lang != 'eu'){
  22. $lang = 'es';
  23. }
  24. //Chack for null fields
  25. if (strlen($user) <= 0 || strlen($text) <= 0){
  26. error_log("Tried to post a comment with null text or user on photo. USER: '$user', TEXT: '$text'");
  27. http_response_code(405);
  28. exit(-1);
  29. }
  30. if ($from == 'web'){
  31. //Format newlines in text
  32. $text = str_replace(["\r\n", "\r", "\n"], "<br/>", $text);
  33. //Get visit
  34. $ip = getUserIP();
  35. $visit = '';
  36. $q = mysqli_query($con, "SELECT id FROM stat_visit WHERE ip = '$ip';");
  37. if (mysqli_num_rows($q) > 0){
  38. $r = mysqli_fetch_array($q);
  39. $visit = $r['id'];
  40. }
  41. //Insert row
  42. mysqli_query($con, "INSERT INTO photo_comment (photo, text, username, lang, visit) VALUES ($photo, '$text', '$user', '$lang', '$visit');");
  43. version();
  44. }
  45. elseif ($from == 'app'){
  46. $code = mysqli_real_escape_string($con, $_POST["code"]);
  47. mysqli_query($con, "INSERT INTO photo_comment (photo, text, username, lang, app) VALUES ($post, '$text', '$user', '$lang', '$code');");
  48. version();
  49. }
  50. //Prepare the page to update the comment section
  51. $q_comment = mysqli_query($con, "SELECT id, photo, DATE_FORMAT(dtime, '%Y-%m-%dT%T') AS isodate, dtime, user, username, lang, text FROM photo_comment WHERE photo = $photo AND approved = 1 ORDER BY dtime;");
  52. error_log("SELECT id, photo, DATE_FORMAT(dtime, '%Y-%m-%dT%T') AS isodate, dtime, user, username, lang, text FROM photo_comment WHERE photo = $photo AND approved = 1 ORDER BY TIME;");
  53. while ($r_comment = mysqli_fetch_array($q_comment)){
  54. ?>
  55. <div itemprop='comment' itemscope itemtype='http://schema.org/UserComments' id='comment_<?=$r_comment["id"]?>' class='comment'>
  56. <span itemprop='creator' class='comment_user'><?=$r_comment["username"]?></span>
  57. <span class='comment_date date'>
  58. <meta itemprop='commentTime' content='<?=$r_comment["isodate"]?>'/>
  59. <?=formatDate($r_comment["dtime"], $lang)?>
  60. </span>
  61. <p itemprop='commentText' class='comment_text'><?$r_comment["text"]?></p>
  62. <hr class='comment_line'/>
  63. </div>
  64. <?php
  65. }
  66. ?>