PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/comment/post.php

https://github.com/allysha/Loggix
PHP | 186 lines | 145 code | 21 blank | 20 comment | 26 complexity | 90503fa2348a8d27ce1cd14982b58504 MD5 | raw file
  1. <?php
  2. /**
  3. * Post Comment Controller
  4. *
  5. * @package Loggix_Module_Comment
  6. * @since 5.5.15
  7. * @version 9.3.17
  8. */
  9. /**
  10. * Include Module class
  11. */
  12. $pathToIndex = '../..';
  13. require_once $pathToIndex . '/lib/Loggix/Module/Comment.php';
  14. $app = new Loggix_Module_Comment;
  15. $config = $app->getConfigArray();
  16. $app->insertTagSafe();
  17. if ((isset($_POST['title'],
  18. $_POST['comment'],
  19. $_POST['user_name'],
  20. $_POST['user_pass'],
  21. $_POST['refer_id'],
  22. $_POST['parent_key'])) &&
  23. ($_POST['title'] != '') &&
  24. ($_POST['comment'] != '') &&
  25. ($_POST['user_name'] != '') &&
  26. ($_POST['user_pass'] != '') &&
  27. ($_POST['refer_id'] != '') &&
  28. ($_POST['parent_key'] != '')
  29. ) {
  30. // Check if posting comment to the entry is allowed.
  31. $checkSql = 'SELECT '
  32. . 'allow_comments '
  33. . 'FROM '
  34. . LOG_TABLE . ' '
  35. . 'WHERE '
  36. . "id = '" . $_POST['refer_id'] . "'";
  37. $checkRes = $app->db->query($checkSql);
  38. $checkRes = $checkRes->fetchColumn();
  39. $receiveComment = ($checkRes == '1') ? 'allowed' : 'not_allowed';
  40. /**
  41. * Comment User Cookie
  42. */
  43. if (isset($_POST['loggix_comment_cookie'])) {
  44. if (isset($_POST['user_name'])) {
  45. $item['user_cookie']['user_name'] = $_POST['user_name'];
  46. setcookie('loggix_comment_user', $item['user_cookie']['user_name'], time()+86400*365, '/');
  47. }
  48. if (isset($_POST['user_email'])) {
  49. $item['user_cookie']['user_email'] = $_POST['user_email'];
  50. setcookie('loggix_comment_email', $item['user_cookie']['user_email'], time()+86400*365, '/');
  51. }
  52. if (isset($_POST['user_uri'])) {
  53. $item['user_cookie']['user_uri'] = $_POST['user_uri'];
  54. setcookie('loggix_comment_uri', $item['user_cookie']['user_uri'], time()+86400*365, '/');
  55. }
  56. }
  57. $userName = $_POST['user_name'];
  58. $userPass = sha1($_POST['user_pass']);
  59. $referId = intval($_POST['refer_id']);
  60. $parentKey = intval($_POST['parent_key']);
  61. $title = $_POST['title'];
  62. $comment = $_POST['comment'];
  63. // Deny comment with the same content
  64. $checkSql = 'SELECT '
  65. . 'COUNT(id) '
  66. . 'FROM '
  67. . COMMENT_TABLE . ' '
  68. . 'WHERE '
  69. . 'comment = :comment';
  70. $stmt = $app->db->prepare($checkSql);
  71. $stmt->execute(array(':comment' => $comment));
  72. $checkRow = $stmt->fetchColumn();
  73. if ($checkRow > 1) {
  74. header('Location: ' . $pathToIndex . '/index.php?id=' . $referId . '#comments');
  75. exit;
  76. }
  77. // Kill check sql connection
  78. unset($checkRes);
  79. // Deny by Referer
  80. if ((!isset($_SERVER['HTTP_REFERER'])) &&
  81. (!stristr($_SERVER['HTTP_REFERER'], 'comment/post.php'))
  82. ) {
  83. header('Location: ' . $pathToIndex . '/index.php?id=' . $referId . '#comments');
  84. exit;
  85. }
  86. // Plugin Filter before receiving comment
  87. $app->plugin->doAction('before-receive-comment', $referId);
  88. // Spam Blocking
  89. if ((preg_match('/.*<\/?(?: ' . $config['block_tags'] . ')/i', $_POST['comment'])) ||
  90. (preg_match('/.*(' . $config['block_keywords'] . ')/i', $_POST['comment'])) ||
  91. (($config['block_ascii_only_text'] == 'yes') &&
  92. (!preg_match('/.*[\x80-\xff]/', $_POST['comment']))) ||
  93. (preg_match('/.*<\/?(?:' . $config['block_tags'] . ')/i', $_POST['title'])) ||
  94. ($receiveComment == 'not_allowed')
  95. ) {
  96. header('Location: ' . $pathToIndex . '/index.php?id=' . $referId . '#comments');
  97. } else {
  98. if ($title == '') { $title = 'Re:'; }
  99. // Get user's remote host info
  100. $remoteHost = (!isset($_SERVER['REMOTE_HOST']))
  101. ? @gethostbyaddr($_SERVER['REMOTE_ADDR'])
  102. : $_SERVER['REMOTE_HOST'];
  103. $userUri = (isset($_POST['user_uri'])) ? $_POST['user_uri'] : '';
  104. $app->db->beginTransaction();
  105. $fdate = gmdate('Y-m-d H:i:s', time() + ($config['tz'] * 3600));
  106. $cmod = gmdate('Y-m-d H:i:s', time() + ($config['tz'] * 3600));
  107. $sql = 'INSERT INTO '
  108. . COMMENT_TABLE . ' '
  109. . '('
  110. . '`parent_key`, '
  111. . '`title`, '
  112. . '`comment`, '
  113. . '`user_name`, '
  114. . '`user_pass`, '
  115. . '`user_uri`, '
  116. . '`date`, '
  117. . '`mod`, '
  118. . '`user_ip`, '
  119. . '`refer_id`'
  120. . ') '
  121. . 'VALUES'
  122. . '('
  123. . ':parent_key, '
  124. . ':title, '
  125. . ':comment, '
  126. . ':user_name, '
  127. . ':user_pass, '
  128. . ':user_uri, '
  129. . ':date, '
  130. . ':mod, '
  131. . ':user_ip, '
  132. . ':refer_id'
  133. . ')';
  134. $sql = $app->setDelimitedIdentifier($sql);
  135. $stmt = $app->db->prepare($sql);
  136. $res = $stmt->execute(
  137. array(
  138. ':parent_key' => $parentKey,
  139. ':title' => $title,
  140. ':comment' => $comment,
  141. ':user_name' => $userName,
  142. ':user_pass' => $userPass,
  143. ':user_uri' => $userUri,
  144. ':date' => $fdate,
  145. ':mod' => $cmod,
  146. ':user_ip' => $remoteHost,
  147. ':refer_id' => $referId
  148. )
  149. );
  150. $app->db->commit();
  151. header('Location: ' . $pathToIndex . '/index.php?id=' . $referId . '#comments');
  152. }
  153. } else {
  154. $sessionState = $app->getSessionState();
  155. $additionalTitle = 'Not Allowed';
  156. $content = "<h2>Request Not Allowed</h2>\n";
  157. $item = array(
  158. 'title' => $app->setTitle($additionalTitle),
  159. 'contents' => $content,
  160. 'result' => '',
  161. 'pager' => ''
  162. );
  163. $app->display($item, $sessionState);
  164. exit;
  165. }