anon_add.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace AU\AnonymousComments;
  3. elgg_make_sticky_form('comments/anon_add');
  4. // add in some extra htmlawed rules for non logged in commenters
  5. elgg_register_plugin_hook_handler('htmlawed', 'config', __NAMESPACE__ . '\\htmlawed_config');
  6. $anon_name = get_input('anon_name');
  7. $anon_email = get_input('anon_email');
  8. $entity_guid = (int) get_input('entity_guid', 0, false);
  9. $comment_guid = (int) get_input('comment_guid', 0, false);
  10. $comment_text = get_input('generic_comment');
  11. if (empty($comment_text)) {
  12. register_error(elgg_echo("generic_comment:blank"));
  13. forward(REFERER);
  14. }
  15. // check if name was entered, if not send them back
  16. if (empty($anon_name)) {
  17. register_error(elgg_echo("AU_anonymous_comments:name_blank"));
  18. forward(REFERER);
  19. }
  20. // check if email was entered, if not send them back
  21. if (empty($anon_email)) {
  22. register_error(elgg_echo("AU_anonymous_comments:email_blank"));
  23. forward(REFERER);
  24. }
  25. if (substr_count($comment_text, "http://") > 1 || substr_count($comment_text, "https://") > 1) {
  26. register_error(elgg_echo("AU_anonymous_comments:no_URLs_allowed"));
  27. forward(REFERER);
  28. }
  29. //simple check to ensure default text was overwritten
  30. if (substr_count($comment_text, elgg_echo("AU_anonymous_comments:longtextwarning")) > 0) {
  31. register_error(elgg_echo("AU_anonymous_comments:didntdelete"));
  32. forward(REFERER);
  33. }
  34. //use stopforumspam to limit attempts to mess with comments
  35. //$url = "http://api.stopforumspam.com/api?ip=" . get_ip() . "&email=" . $anon_email . "&f=json";
  36. // check stopforumspam
  37. //$curl = curl_init($url);
  38. //curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  39. //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  40. //curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  41. //$contents = curl_exec($curl);
  42. //$aInfo = curl_getinfo($curl);
  43. //if ($aInfo['http_code'] === 200) {
  44. // $data = json_decode($contents);
  45. // $ip_frequency = $data->ip->frequency;
  46. // if ($ip_frequency != 0) {
  47. // spammer
  48. // register_error(elgg_echo('AU_anonymous_comments:stopforumspam_fail'));
  49. // forward(REFERER);
  50. // }
  51. //}
  52. // Create a new comment on the target entity
  53. $entity = get_entity($entity_guid);
  54. if (!$entity) {
  55. register_error(elgg_echo("generic_comment:notfound"));
  56. forward(REFERER);
  57. }
  58. $user = get_anon_user();
  59. // custom context for write permissions
  60. elgg_push_context("AU_anonymous_comments_permissions");
  61. $comment_text .= "\n\n- " . $anon_name;
  62. $comment = new \ElggComment();
  63. $comment->description = $comment_text;
  64. $comment->owner_guid = $user->getGUID();
  65. $comment->container_guid = $entity->getGUID();
  66. $comment->access_id = $entity->access_id;
  67. $guid = $comment->save();
  68. if (!$guid) {
  69. register_error(elgg_echo("generic_comment:failure"));
  70. forward(REFERER);
  71. }
  72. if (!is_moderated($entity)) {
  73. $owner = $entity->getOwnerEntity();
  74. notify_user($owner->guid, $user->guid, elgg_echo('generic_comment:email:subject', array(), $owner->language), elgg_echo('generic_comment:email:body', array(
  75. $entity->title,
  76. $anon_name . " ({$anon_email})",
  77. $comment_text,
  78. $entity->getURL(),
  79. $user->name,
  80. $user->getURL()
  81. ), $owner->language), array(
  82. 'object' => $comment,
  83. 'action' => 'create',
  84. )
  85. );
  86. }
  87. else {
  88. $token = get_token($comment);
  89. $approveURL = elgg_normalize_url("auac/approve/{$comment->guid}/{$token}");
  90. $deleteURL = elgg_normalize_url("auac/delete/{$comment->guid}/{$token}");
  91. notify_user($owner->guid, $user->guid, elgg_echo('AU_anonymous_comments:email:subject', array(), $owner->language), elgg_echo('AU_anonymous_comments:email:body', array(
  92. $entity->title,
  93. $anon_name . " ({$anon_email}, IP:" . get_ip() . ")",
  94. $comment_text,
  95. $entity->getURL(),
  96. $approveURL,
  97. $deleteURL
  98. ), $owner->language), array(
  99. 'object' => $comment,
  100. 'action' => 'create',
  101. )
  102. );
  103. }
  104. // Add to river
  105. if (elgg_get_plugin_setting('add_to_river', PLUGIN_ID) == 'yes') {
  106. elgg_create_river_item(array(
  107. 'view' => 'river/object/comment/create',
  108. 'action_type' => 'comment',
  109. 'subject_guid' => $user->guid,
  110. 'object_guid' => $guid,
  111. 'target_guid' => $entity_guid,
  112. ));
  113. }
  114. if (is_moderated($entity)) {
  115. // disable the comment until approved
  116. $comment->disable();
  117. }
  118. elgg_pop_context();
  119. elgg_clear_sticky_form('comments/anon_add');
  120. system_message(elgg_echo('AU_anonymous_comments:comment_success'));
  121. forward(REFERER);