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 $ids 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 $ids = null)
21 // In case there is no ID list, return empty array (=> no ACL set)
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]+)>/', $ids, $matches, PREG_PATTERN_ORDER);
34 * Wrap ACL elements in angle brackets for storage
36 * @param string $item The item to sanitise
38 private function sanitize(string &$item) {
39 // The item is an ACL int value
41 $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
42 // The item is a allowed ACL character
43 } elseif (in_array($item, [Group::FOLLOWERS, Group::MUTUALS])) {
44 $item = '<' . $item . '>';
45 // The item is already a ACL string
46 } elseif (preg_match('/<\d+?>/', $item)) {
48 // The item is not supported, so remove it (cleanup)
55 * Convert an ACL array to a storable string
57 * Normally ACL permissions will be an array.
58 * We'll also allow a comma-separated string.
60 * @param string|array $permissions
64 function toString($permissions) {
66 if (is_array($permissions)) {
69 $item = explode(',', $permissions);
72 if (is_array($item)) {
73 array_walk($item, [$this, 'sanitize']);
74 $return = implode('', $item);