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