3 namespace Friendica\Util;
5 use Friendica\Model\Group;
8 * Util class for ACL formatting
10 final class ACLFormatter
13 * Turn user/group ACLs stored as angle bracketed text into arrays
15 * @param string|null $acl_string A angle-bracketed list of IDs
17 * @return array The array based on the IDs (empty in case there is no list)
19 public function expand(string $acl_string = null)
21 // In case there is no ID list, return empty array (=> no ACL set)
22 if (!isset($acl_string)) {
26 // turn string array of angle-bracketed elements into numeric array
27 // e.g. "<1><2><3>" => array(1,2,3);
28 preg_match_all('/<(' . Group::FOLLOWERS . '|'. Group::MUTUALS . '|[0-9]+)>/', $acl_string, $matches, PREG_PATTERN_ORDER);
34 * Takes an arbitrary ACL string and sanitizes it for storage
36 * @param string|null $acl_string
39 public function sanitize(string $acl_string = null)
41 if (empty($acl_string)) {
45 $cleaned_list = trim($acl_string, '<>');
47 if (empty($cleaned_list)) {
51 $elements = explode('><', $cleaned_list);
55 array_walk($elements, [$this, 'sanitizeItem']);
57 return implode('', $elements);
61 * Wrap ACL elements in angle brackets for storage
63 * @param string $item The item to sanitise
65 private function sanitizeItem(string &$item) {
66 // The item is an ACL int value
68 $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
69 // The item is a allowed ACL character
70 } elseif (in_array($item, [Group::FOLLOWERS, Group::MUTUALS])) {
71 $item = '<' . $item . '>';
72 // The item is already a ACL string
73 } elseif (preg_match('/<\d+?>/', $item)) {
75 // The item is not supported, so remove it (cleanup)
82 * Convert an ACL array to a storable string
84 * Normally ACL permissions will be an array.
85 * We'll also allow a comma-separated string.
87 * @param string|array $permissions
91 function toString($permissions) {
93 if (is_array($permissions)) {
96 $item = explode(',', $permissions);
99 if (is_array($item)) {
100 array_walk($item, [$this, 'sanitizeItem']);
101 $return = implode('', $item);