comment.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. $http_host = $_SERVER['HTTP_HOST'];
  3. include("../functions.php");
  4. $con = startdb('rw');
  5. //Get post values
  6. $from =mysqli_real_escape_string($con, $_POST["from"]);
  7. $photo = mysqli_real_escape_string($con, $_POST["photo"]);
  8. $user = mysqli_real_escape_string($con, $_POST["user"]);
  9. $text = mysqli_real_escape_string($con, $_POST["text"]);
  10. $lang = strtolower($_POST["lang"]);
  11. //Check if photo exists and it allows comments
  12. $q_photo = mysqli_query($con, "SELECT id FROM photo WHERE id = $photo;"); //Not checking if comments are alowed
  13. if (mysqli_num_rows($q_photo) == 0){
  14. error_log("Tried to post a comment in a nonexistent photo or a photo that doesnt allow comments. POST ID: '$id'");
  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 photo. USER: '$user', TEXT: '$text'");
  25. http_response_code(405);
  26. exit(-1);
  27. }
  28. if ($from == 'web'){
  29. //Format newlines in text
  30. $text = str_replace(["\r\n", "\r", "\n"], "<br/>", $text);
  31. //Get visit
  32. $ip = getUserIP();
  33. $visit = '';
  34. $q = mysqli_query($con, "SELECT id FROM stat_visit WHERE ip = '$ip';");
  35. if (mysqli_num_rows($q) > 0){
  36. $r = mysqli_fetch_array($q);
  37. $visit = $r['id'];
  38. }
  39. //Insert row
  40. mysqli_query($con, "INSERT INTO photo_comment (photo, text, username, lang, visit) VALUES ($photo, '$text', '$user', '$lang', '$visit');");
  41. version();
  42. }
  43. elseif ($from == 'app'){
  44. $code = mysqli_real_escape_string($con, $_POST["code"]);
  45. mysqli_query($con, "INSERT INTO photo_comment (photo, text, username, lang, app) VALUES ($post, '$text', '$user', '$lang', '$code');");
  46. version();
  47. }
  48. //Prepare the page to update the comment section
  49. //WARNING: If changes are done here, do the same in album.php
  50. $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;");
  51. 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;");
  52. while ($r_comment = mysqli_fetch_array($q_comment)){
  53. echo "<div itemprop='comment' itemscope itemtype='http://schema.org/UserComments' id='comment_$r_comment[id]' class='comment'>\n";
  54. //When official users are implementes, see if there is user or username
  55. echo "<span itemprop='creator' class='comment_user'>$r_comment[username]</span>\n";
  56. echo "<span class='comment_date date'><meta itemprop='commentTime' content='$r_comment[isodate]'/>" . formatDate($r_comment['dtime'], $lang) . "</span>\n";
  57. echo "<p itemprop='commentText' class='comment_text'>$r_comment[text]</p>";
  58. echo "<hr class='comment_line'/>\n";
  59. echo "</div>\n";
  60. }
  61. ?>