functions.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace AU\AnonymousComments;
  3. /**
  4. * get our anonymous user
  5. *
  6. * @staticvar type $anon_user
  7. * @return \ElggUser
  8. */
  9. function get_anon_user() {
  10. static $anon_user;
  11. if ($anon_user) {
  12. return $anon_user;
  13. }
  14. $anon_guid = elgg_get_plugin_setting('anon_guid', PLUGIN_ID);
  15. $anon_user = get_user($anon_guid);
  16. if (!$anon_user) {
  17. $anon_user = set_anonymous_user();
  18. }
  19. return $anon_user;
  20. }
  21. /**
  22. *
  23. * @return string ip | null
  24. */
  25. function get_ip() {
  26. if (filter_var(getenv('HTTP_CLIENT_IP'), FILTER_VALIDATE_IP)) {
  27. $ip_address = getenv('HTTP_CLIENT_IP');
  28. } elseif (filter_var(getenv('HTTP_X_FORWARDED_FOR'), FILTER_VALIDATE_IP)) {
  29. $ip_address = getenv('HTTP_X_FORWARDED_FOR');
  30. } elseif (filter_var(getenv('HTTP_X_FORWARDED'), FILTER_VALIDATE_IP)) {
  31. $ip_address = getenv('HTTP_X_FORWARDED');
  32. } elseif (filter_var(getenv('HTTP_FORWARDED_FOR'), FILTER_VALIDATE_IP)) {
  33. $ip_address = getenv('HTTP_FORWARDED_FOR');
  34. } elseif (filter_var(getenv('HTTP_FORWARDED'), FILTER_VALIDATE_IP)) {
  35. $ip_address = getenv('HTTP_FORWARDED');
  36. } else {
  37. $ip_address = $_SERVER['REMOTE_ADDR'];
  38. }
  39. return $ip_address;
  40. }
  41. /*
  42. * This function will check to see if there is a user already created by the plugin (possibly from a
  43. * previous installation). If so, it will get the ID of that user and save it for the plugin to use.
  44. * If there is no user created already, it will create a fake user and save the ID for the plugin.
  45. * A piece of metadata ($user->AU_anonymous_comments = true) is set to differentiate just in case someone decided
  46. * to take our username.
  47. */
  48. function set_anonymous_user() {
  49. //first see if a user has been created previously
  50. $users = elgg_get_entities_from_metadata(array(
  51. 'types' => 'user',
  52. 'metadata_name' => 'AU_anonymous_comments',
  53. 'value' => true,
  54. ));
  55. if (!$users) {
  56. //no previous user - create a new one
  57. //find available username
  58. $i = 1;
  59. $username = "AU_anonymous_comments_user1";
  60. $basename = "AU_anonymous_comments_user";
  61. while (get_user_by_username($username)) {
  62. $i++;
  63. $username = $basename . $i;
  64. }
  65. //let's make a user
  66. $anon_user = new \ElggUser();
  67. $anon_user->username = $username;
  68. $anon_user->email = "AU_anonymous_comments_user" . $i . "@example.com";
  69. $anon_user->name = elgg_echo('AU_anonymous_comments:display_name');
  70. $anon_user->access_id = ACCESS_PUBLIC;
  71. $anon_user->salt = '';
  72. $anon_user->password = ''; // doesn't need to match, we don't want people logging in anyway
  73. $anon_user->owner_guid = 0; // Users aren't owned by anyone, even if they are admin created.
  74. $anon_user->container_guid = 0; // Users aren't contained by anyone, even if they are admin created.
  75. $anon_user->save();
  76. // set the plugin-identifiable metadata
  77. $anon_user->AU_anonymous_comments = true;
  78. } else {
  79. // we found our user through metadata
  80. $anon_user = $users[0];
  81. }
  82. elgg_set_plugin_setting('anon_guid', $anon_user->guid, PLUGIN_ID);
  83. return $anon_user;
  84. }
  85. /**
  86. * this function returns true if the entity is being moderated
  87. *
  88. * @param type $id
  89. * @return boolean
  90. */
  91. function is_moderated($entity) {
  92. if (!elgg_instanceof($entity)) {
  93. return false;
  94. }
  95. if ($entity->is_moderated) {
  96. return true;
  97. }
  98. // this check is necessary in case there's a way to set public content as unmoderated
  99. // then is_moderated can === 0
  100. if ($entity->access_id == ACCESS_PUBLIC && is_null($entity->is_moderated)) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * generate a hard-to-guess token for offline approval/rejection
  107. *
  108. * @param type $comment
  109. * @return type
  110. */
  111. function get_token($comment) {
  112. if ($comment->__anonymous_comment_token) {
  113. return $comment->__anonymous_comment_token;
  114. }
  115. // generate a token for this comment
  116. $token = sha1(uniqid('auac' . $comment->guid));
  117. $comment->__anonymous_comment_token = $token;
  118. return $token;
  119. }