Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.8 KiB

  1. <?php
  2. namespace Friendica\Util;
  3. use Friendica\Model\Group;
  4. /**
  5. * Util class for ACL formatting
  6. */
  7. final class ACLFormatter
  8. {
  9. /**
  10. * Turn user/group ACLs stored as angle bracketed text into arrays
  11. *
  12. * @param string|null $ids A angle-bracketed list of IDs
  13. *
  14. * @return array The array based on the IDs (empty in case there is no list)
  15. */
  16. public function expand(string $ids = null)
  17. {
  18. // In case there is no ID list, return empty array (=> no ACL set)
  19. if (!isset($ids)) {
  20. return [];
  21. }
  22. // turn string array of angle-bracketed elements into numeric array
  23. // e.g. "<1><2><3>" => array(1,2,3);
  24. preg_match_all('/<(' . Group::FOLLOWERS . '|'. Group::MUTUALS . '|[0-9]+)>/', $ids, $matches, PREG_PATTERN_ORDER);
  25. return $matches[1];
  26. }
  27. /**
  28. * Wrap ACL elements in angle brackets for storage
  29. *
  30. * @param string $item The item to sanitise
  31. */
  32. private function sanitize(string &$item) {
  33. // The item is an ACL int value
  34. if (intval($item)) {
  35. $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
  36. // The item is a allowed ACL character
  37. } elseif (in_array($item, [Group::FOLLOWERS, Group::MUTUALS])) {
  38. $item = '<' . $item . '>';
  39. // The item is already a ACL string
  40. } elseif (preg_match('/<\d+?>/', $item)) {
  41. unset($item);
  42. // The item is not supported, so remove it (cleanup)
  43. } else {
  44. $item = '';
  45. }
  46. }
  47. /**
  48. * Convert an ACL array to a storable string
  49. *
  50. * Normally ACL permissions will be an array.
  51. * We'll also allow a comma-separated string.
  52. *
  53. * @param string|array $permissions
  54. *
  55. * @return string
  56. */
  57. function toString($permissions) {
  58. $return = '';
  59. if (is_array($permissions)) {
  60. $item = $permissions;
  61. } else {
  62. $item = explode(',', $permissions);
  63. }
  64. if (is_array($item)) {
  65. array_walk($item, [$this, 'sanitize']);
  66. $return = implode('', $item);
  67. }
  68. return $return;
  69. }
  70. }