]> git.mxchange.org Git - friendica.git/blob - src/Util/ACLFormatter.php
Merge pull request #11519 from MrPetovan/task/11511-console-domain-move
[friendica.git] / src / Util / ACLFormatter.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 use Friendica\Model\Group;
25
26 /**
27  * Util class for ACL formatting
28  */
29 final class ACLFormatter
30 {
31         /**
32          * Turn user/group ACLs stored as angle bracketed text into arrays
33          *
34          * @param string|null $acl_string A angle-bracketed list of IDs
35          *
36          * @return array The array based on the IDs (empty in case there is no list)
37          */
38         public function expand(string $acl_string = null)
39         {
40                 // In case there is no ID list, return empty array (=> no ACL set)
41                 if (empty($acl_string)) {
42                         return [];
43                 }
44
45                 // turn string array of angle-bracketed elements into numeric array
46                 // e.g. "<1><2><3>" => array(1,2,3);
47                 preg_match_all('/<(' . Group::FOLLOWERS . '|'. Group::MUTUALS . '|[0-9]+)>/', $acl_string, $matches, PREG_PATTERN_ORDER);
48
49                 return $matches[1];
50         }
51
52         /**
53          * Takes an arbitrary ACL string and sanitizes it for storage
54          *
55          * @param string|null $acl_string
56          * @return string
57          */
58         public function sanitize(string $acl_string = null)
59         {
60                 if (empty($acl_string)) {
61                         return '';
62                 }
63
64                 $cleaned_list = trim($acl_string, '<>');
65
66                 if (empty($cleaned_list)) {
67                         return '';
68                 }
69
70                 $elements = explode('><', $cleaned_list);
71
72                 sort($elements);
73
74                 array_walk($elements, [$this, 'sanitizeItem']);
75
76                 return implode('', $elements);
77         }
78
79         /**
80          * Wrap ACL elements in angle brackets for storage
81          *
82          * @param string $item The item to sanitise
83          */
84         private function sanitizeItem(string &$item) {
85                 // The item is an ACL int value
86                 if (intval($item)) {
87                         $item = '<' . intval($item) . '>';
88                 // The item is a allowed ACL character
89                 } elseif (in_array($item, [Group::FOLLOWERS, Group::MUTUALS])) {
90                         $item = '<' . $item . '>';
91                 // The item is already a ACL string
92                 } elseif (preg_match('/<\d+?>/', $item)) {
93                         unset($item);
94                 // The item is not supported, so remove it (cleanup)
95                 } else {
96                         $item = '';
97                 }
98         }
99
100         /**
101          * Convert an ACL array to a storable string
102          *
103          * Normally ACL permissions will be an array.
104          * We'll also allow a comma-separated string.
105          *
106          * @param string|array $permissions
107          *
108          * @return string
109          */
110         function toString($permissions) {
111                 $return = '';
112                 if (is_array($permissions)) {
113                         $item = $permissions;
114                 } else {
115                         $item = explode(',', $permissions);
116                 }
117
118                 if (is_array($item)) {
119                         array_walk($item, [$this, 'sanitizeItem']);
120                         $return = implode('', $item);
121                 }
122                 return $return;
123         }
124 }