comment.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. $http_host = $_SERVER['HTTP_HOST'];
  3. include("../functions.php");
  4. $con = startdb('rw');
  5. //Get post values
  6. $photo = mysqli_real_escape_string($con, $_POST["photo"]);
  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 photo exists and it allows comments
  11. $q_photo = mysqli_query($con, "SELECT id FROM photo WHERE id = $photo;"); //Not checking if comments are alowed
  12. if (mysqli_num_rows($q_photo) == 0){
  13. error_log("Tried to post a comment in a nonexistent photo or a photo that doesnt allow comments. POST ID: '$id'");
  14. http_response_code(405);
  15. exit(-1);
  16. }
  17. //Check language, set fallback.
  18. if ($lang != 'es' && $lang != 'en' && $lang != 'eu'){
  19. $lang = 'es';
  20. }
  21. //Chack for null fields
  22. if (strlen($user) <= 0 || strlen($text) <= 0){
  23. error_log("Tried to post a comment with null text or user on photo. USER: '$user', TEXT: '$text'");
  24. http_response_code(405);
  25. exit(-1);
  26. }
  27. //Format newlines in text
  28. $text = str_replace(["\r\n", "\r", "\n"], "<br/>", $text);
  29. //Get visit
  30. $ip = getUserIP();
  31. $visit = '';
  32. $q = mysqli_query($con, "SELECT id FROM stat_visit WHERE ip = '$ip';");
  33. if (mysqli_num_rows($q) > 0){
  34. $r = mysqli_fetch_array($q);
  35. $visit = $r['id'];
  36. }
  37. //Insert row
  38. mysqli_query($con, "INSERT INTO photo_comment (photo, text, username, lang, visit) VALUES ($photo, '$text', '$user', '$lang', '$visit');");
  39. version();
  40. //Prepare the page to update the comment section
  41. //WARNING: If changes are done here, do the same in album.php
  42. $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;");
  43. 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;");
  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. ?>