]> git.mxchange.org Git - friendica.git/blob - src/Util/ACLFormatter.php
Rename PermissionSet::fetchIDForPost to PermissionSet::getIdFromACL
[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|null $acl_string A angle-bracketed list of IDs
16          *
17          * @return array The array based on the IDs (empty in case there is no list)
18          */
19         public function expand(string $acl_string = null)
20         {
21                 // In case there is no ID list, return empty array (=> no ACL set)
22                 if (!isset($acl_string)) {
23                         return [];
24                 }
25
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);
29
30                 return $matches[1];
31         }
32
33         /**
34          * Takes an arbitrary ACL string and sanitizes it for storage
35          *
36          * @param string|null $acl_string
37          * @return string
38          */
39         public function sanitize(string $acl_string = null)
40         {
41                 if (empty($acl_string)) {
42                         return '';
43                 }
44
45                 $cleaned_list = trim($acl_string, '<>');
46
47                 if (empty($cleaned_list)) {
48                         return '';
49                 }
50
51                 $elements = explode('><', $cleaned_list);
52
53                 sort($elements);
54
55                 array_walk($elements, [$this, 'sanitizeItem']);
56
57                 return implode('', $elements);
58         }
59
60         /**
61          * Wrap ACL elements in angle brackets for storage
62          *
63          * @param string $item The item to sanitise
64          */
65         private function sanitizeItem(string &$item) {
66                 // The item is an ACL int value
67                 if (intval($item)) {
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)) {
74                         unset($item);
75                 // The item is not supported, so remove it (cleanup)
76                 } else {
77                         $item = '';
78                 }
79         }
80
81         /**
82          * Convert an ACL array to a storable string
83          *
84          * Normally ACL permissions will be an array.
85          * We'll also allow a comma-separated string.
86          *
87          * @param string|array $permissions
88          *
89          * @return string
90          */
91         function toString($permissions) {
92                 $return = '';
93                 if (is_array($permissions)) {
94                         $item = $permissions;
95                 } else {
96                         $item = explode(',', $permissions);
97                 }
98
99                 if (is_array($item)) {
100                         array_walk($item, [$this, 'sanitizeItem']);
101                         $return = implode('', $item);
102                 }
103                 return $return;
104         }
105 }