start.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace AU\RiverPrivacy;
  3. const PLUGIN_ID = 'river_privacy';
  4. elgg_register_event_handler('init', 'system', __NAMESPACE__ . '\\init');
  5. /**
  6. * Plugin Init
  7. */
  8. function init() {
  9. // set the river item to private if it's not an object
  10. elgg_register_plugin_hook_handler('creating', 'river', __NAMESPACE__ . '\\creating_river_hook');
  11. // filter river views if necessary
  12. elgg_register_plugin_hook_handler('view_vars', 'page/components/list', __NAMESPACE__ . '\\filter_list_vars');
  13. // add access check back into the river queries
  14. elgg_register_plugin_hook_handler('get_sql', 'access', __NAMESPACE__ . '\\river_access_query');
  15. }
  16. /**
  17. * hook called before river creation
  18. * return associative array of parameters to create the river entry
  19. *
  20. * @param type $hook
  21. * @param type $type
  22. * @param string $returnvalue
  23. * @param type $params
  24. * @return string
  25. */
  26. function creating_river_hook($hook, $type, $returnvalue, $params) {
  27. if ($returnvalue['type'] != 'object') {
  28. $returnvalue['access_id'] = ACCESS_PRIVATE;
  29. }
  30. return $returnvalue;
  31. }
  32. /**
  33. * filter the items sent to a list view
  34. *
  35. * @param type $hook
  36. * @param type $type
  37. * @param type $return
  38. * @param type $params
  39. * @return type
  40. */
  41. function filter_list_vars($hook, $type, $return, $params) {
  42. $filter_river = elgg_get_plugin_setting('hide_old_items', PLUGIN_ID);
  43. if ($filter_river == 'no') {
  44. // no need to filter
  45. return $return;
  46. }
  47. if ($return['items'] && is_array($return['items'])) {
  48. foreach ($return['items'] as $key => $item) {
  49. if (!($item instanceof \ElggRiverItem)) {
  50. continue;
  51. }
  52. if ($item->type == 'object') {
  53. continue;
  54. }
  55. if ($item->subject_guid == elgg_get_logged_in_user_guid()) {
  56. continue;
  57. }
  58. if (elgg_is_admin_logged_in()) {
  59. continue;
  60. }
  61. if (elgg_get_ignore_access()) {
  62. continue;
  63. }
  64. unset($return['items'][$key]);
  65. }
  66. }
  67. return $return;
  68. }
  69. /**
  70. * Add a custom access clause for river queries
  71. *
  72. * @param type $hook
  73. * @param type $type
  74. * @param array $return
  75. * @param type $params
  76. * @return type
  77. */
  78. function river_access_query($hook, $type, $return, $params) {
  79. // anything else we can use to isolate river queries?
  80. // currently 'oe' is only used in core by river queries
  81. // but it's not really a great way to judge...
  82. if ($params['table_alias'] != 'oe') {
  83. return $return;
  84. }
  85. if ($params['ignore_access']) {
  86. return $return;
  87. }
  88. if (elgg_is_admin_logged_in()) {
  89. return $return;
  90. }
  91. $guid = (int) elgg_get_logged_in_user_guid();
  92. $return['ands'][] = "((rv.type != 'object' AND rv.subject_guid = {$guid}) OR rv.access_id != 0)";
  93. return $return;
  94. }