comment.php 3.1 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. $post = mysqli_real_escape_string($con, $_POST["post"]);
  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 post exists and it allows comments
  12. $q_post = mysqli_query($con, "SELECT id FROM post WHERE id = $post AND comments = 1;");
  13. error_log("SELECT id FROM post WHERE id = $post AND comments = 1;");
  14. if (mysqli_num_rows($q_post) == 0){
  15. error_log("Tried to post a comment in a nonexistent post or a post that doesnt allow comments. POST ID: '$post'");
  16. http_response_code(405);
  17. exit(-1);
  18. }
  19. //Check language, set fallback.
  20. if ($lang != 'es' && $lang != 'en' && $lang != 'eu'){
  21. $lang = 'es';
  22. }
  23. //Chack for null fields
  24. if (strlen($user) <= 0 || strlen($text) <= 0){
  25. error_log("Tried to post a comment with null text or user on post. USER: '$user', TEXT: '$text'");
  26. http_response_code(405);
  27. exit(-1);
  28. }
  29. if ($from == 'web'){
  30. //Format newlines in text
  31. $text = str_replace(["\r\n", "\r", "\n"], "<br/>", $text);
  32. //Get visit
  33. $ip = getUserIP();
  34. $visit = '';
  35. $q = mysqli_query($con, "SELECT id FROM stat_visit WHERE ip = '$ip';");
  36. if (mysqli_num_rows($q) > 0){
  37. $r = mysqli_fetch_array($q);
  38. $visit = $r['id'];
  39. }
  40. //Insert row
  41. mysqli_query($con, "INSERT INTO post_comment (post, text, username, lang, visit) VALUES ($post, '$text', '$user', '$lang', '$visit');");
  42. version();
  43. }
  44. elseif ($from == 'app'){
  45. $code = mysqli_real_escape_string($con, $_POST["code"]);
  46. mysqli_query($con, "INSERT INTO post_comment (post, text, username, lang, app) VALUES ($post, '$text', '$user', '$lang', '$code');");
  47. version();
  48. }
  49. //Prepare the page to update the comment section
  50. //WARNING: If changes are done here, do the same in post.php
  51. $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;");
  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. ?>