]> git.mxchange.org Git - friendica.git/blob - src/Util/ACLFormatter.php
Merge pull request #7726 from tobiasd/20191010-uexport
[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 $ids 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 $ids = null)
20         {
21                 // In case there is no ID list, return empty array (=> no ACL set)
22                 if (!isset($ids)) {
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]+)>/', $ids, $matches, PREG_PATTERN_ORDER);
29
30                 return $matches[1];
31         }
32
33         /**
34          * Wrap ACL elements in angle brackets for storage
35          *
36          * @param string $item The item to sanitise
37          */
38         private function sanitize(string &$item) {
39                 // The item is an ACL int value
40                 if (intval($item)) {
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)) {
47                         unset($item);
48                 // The item is not supported, so remove it (cleanup)
49                 } else {
50                         $item = '';
51                 }
52         }
53
54         /**
55          * Convert an ACL array to a storable string
56          *
57          * Normally ACL permissions will be an array.
58          * We'll also allow a comma-separated string.
59          *
60          * @param string|array $permissions
61          *
62          * @return string
63          */
64         function toString($permissions) {
65                 $return = '';
66                 if (is_array($permissions)) {
67                         $item = $permissions;
68                 } else {
69                         $item = explode(',', $permissions);
70                 }
71
72                 if (is_array($item)) {
73                         array_walk($item, [$this, 'sanitize']);
74                         $return = implode('', $item);
75                 }
76                 return $return;
77         }
78 }