/public/plogger/plog-comment.php

https://github.com/eatenbyagrue/Rebuilding-Together · PHP · 156 lines · 118 code · 15 blank · 23 comment · 63 complexity · 3330d97747388a38cfdd730cbb459936 MD5 · raw file

  1. <?php
  2. /* Plogger comment script: writes comment information to the database and links it to the picture using the pictures ID */
  3. include_once(dirname(__FILE__).'/plog-load-config.php');
  4. // Remove plog-comment from the end, if present .. is there a better way to determine the full url?
  5. // Workaround for never-ending comment loop
  6. $is_comment = strpos($config['baseurl'], 'plog-comment.php');
  7. if ($is_comment !== false) {
  8. $config['baseurl'] = substr($config['baseurl'], 0, $is_comment);
  9. }
  10. // Loosely validate url string format without actually checking the link (cause that takes time)
  11. function is_valid_url($url) {
  12. if (preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)) {
  13. return 'http';
  14. } else if (preg_match('#^[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)) {
  15. return 'nohttp';
  16. } else {
  17. return 'badurl';
  18. }
  19. }
  20. function is_valid_email($email) {
  21. // Based on the is_email function from WordPress with some additional checks
  22. // Check that there is an @, a dot, no double dots, does not start with a dot, or have a dot next to the @ symbol
  23. if (strpos($email, '@') !== false && strpos($email, '.') !== false && strpos($email, '..') === false && $email[0] != '.' && $email[strrpos($email, '@')-1] != '.') {
  24. // check for the correct syntax
  25. if (preg_match("/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,}\$/i", $email)) {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. } else {
  31. return false;
  32. }
  33. }
  34. // Set up our error arrays
  35. $errors = array();
  36. $error_field = array();
  37. // Set up all the necessary variables
  38. $parent_id = intval($_POST['parent']);
  39. $author = $email = $url = $comment = '';
  40. $pic = get_picture_by_id($parent_id);
  41. // Check for a redirect, referrer, or default back to the generic Plogger URL
  42. if (isset($_POST['redirect'])) {
  43. $redirect = $_POST['redirect'];
  44. } else if (isset($_SERVER['HTTP_REFERRER']) && !empty($_SERVER['HTTP_REFERRER'])) {
  45. $redirect = $_SERVER['HTTP_REFERRER'];
  46. } else {
  47. $redirect = generate_url('picture', $parent_id);
  48. }
  49. if ($config['allow_comments'] && $pic['allow_comments']) {
  50. if (isset($_POST['plogger-token']) && isset($_SESSION['plogger-token']) && $_POST['plogger-token'] === $_SESSION['plogger-token']) {
  51. // Verify the author / name
  52. if (isset($_POST['author']) && $_POST['author'] != '') {
  53. $author = strip_tags(SmartStripSlashes($_POST['author']));
  54. } else {
  55. $author = '';
  56. $errors[] = plog_tr('Author name is missing.');
  57. $error_field[] = 'author';
  58. }
  59. // Verify the email
  60. if (isset($_POST['email']) && $_POST['email'] != '') {
  61. if (is_valid_email(strip_tags(SmartStripSlashes($_POST['email'])))) {
  62. $email = SmartStripSlashes($_POST['email']);
  63. } else {
  64. $email = '';
  65. $errors[] = plog_tr('The email address you entered does not appear to be valid.');
  66. $error_field[] = 'email';
  67. }
  68. } else {
  69. $email = '';
  70. $errors[] = plog_tr('You forgot to enter an email.');
  71. $error_field[] = 'email';
  72. }
  73. // Verify the website url if set
  74. if (isset($_POST['url']) && $_POST['url'] != '') {
  75. if (is_valid_url($_POST['url']) == 'http') {
  76. $url = $_POST['url'];
  77. } else if (is_valid_url($_POST['url']) == 'nohttp') {
  78. $url = 'http://'.$_POST['url'];
  79. } else {
  80. $url = '';
  81. $errors[] = plog_tr('The website URL you entered does not appear to be valid.');
  82. $error_field[] = 'url';
  83. }
  84. } else {
  85. $url = '';
  86. }
  87. // Verify the comment
  88. if (isset($_POST['comment']) && $_POST['comment'] != '') {
  89. // should we strip tags out for now and put limited allowability in later?
  90. $comment = strip_tags(SmartStripSlashes($_POST['comment']));
  91. } else {
  92. $comment = '';
  93. $errors[] = plog_tr('You forgot to enter a comment.');
  94. $error_field[] = 'comment';
  95. }
  96. // If the captcha is required, check it here
  97. if (isset($_SESSION['require_captcha']) && $_SESSION['require_captcha'] === true) {
  98. if (!isset($_POST['captcha']) || !isset($_SESSION['captcha']) || $_POST['captcha'] != $_SESSION['captcha']) {
  99. $errors[] = plog_tr('CAPTCHA check failed.');
  100. $error_field[] = 'captcha';
  101. }
  102. }
  103. if (empty($errors)) {
  104. $rv = add_comment($parent_id, $author, $email, $url, $comment);
  105. // We're done with this so empty it out to stop double posts
  106. unset($_POST);
  107. if (isset($rv['errors'])) {
  108. $errors = $rv['errors'];
  109. } else if ($config['comments_moderate']) {
  110. $_SESSION['comment_moderated'] = 1;
  111. }
  112. }
  113. unset($_SESSION['plogger-token']);
  114. } else {
  115. // Missing form token
  116. $errors = array(plog_tr('Spam token missing or does not match!'));
  117. }
  118. } else {
  119. // Comments are not on
  120. $errors = array(plog_tr('Comments are disabled. You are unable to add a comment!'));
  121. }
  122. if (!empty($errors)) {
  123. // Set the errors for form display
  124. $_SESSION['comment_post_error'] = $errors;
  125. // Set the session form variables so users don't have to re-enter their information
  126. $_SESSION['plogger-form'] = array(
  127. 'author' => $author,
  128. 'email' => $email,
  129. 'url' => $url,
  130. 'comment' => $comment
  131. );
  132. $_SESSION['plogger-form-error'] = $error_field;
  133. } else {
  134. // Clear out the session form variables if no errors
  135. unset($_SESSION['plogger-form']);
  136. unset($_SESSION['plogger-form-error']);
  137. }
  138. close_db();
  139. // Redirect back
  140. header('Location: '.$redirect);
  141. ?>