]> git.mxchange.org Git - friendica.git/blob - src/Model/PermissionSet.php
de943c977c3841ae5292c5af2ea3c57b776bf174
[friendica.git] / src / Model / PermissionSet.php
1 <?php
2 /**
3  * @file src/Model/PermissionSet.php
4  */
5
6 namespace Friendica\Model;
7
8 use Friendica\Core\L10n;
9 use Friendica\Database\DBA;
10 use Friendica\DI;
11 use Friendica\Network\HTTPException;
12
13 /**
14  * functions for interacting with the permission set of an object (item, photo, event, ...)
15  */
16 class PermissionSet
17 {
18         /**
19          * Fetch the id of a given permission set. Generate a new one when needed
20          *
21          * @param int         $uid
22          * @param string|null $allow_cid Allowed contact IDs    - empty = everyone
23          * @param string|null $allow_gid Allowed group IDs      - empty = everyone
24          * @param string|null $deny_cid  Disallowed contact IDs - empty = no one
25          * @param string|null $deny_gid  Disallowed group IDs   - empty = no one
26          * @return int id
27          * @throws HTTPException\InternalServerErrorException
28          */
29         public static function getIdFromACL(
30                 int $uid,
31                 string $allow_cid = null,
32                 string $allow_gid = null,
33                 string $deny_cid = null,
34                 string $deny_gid = null
35         ) {
36                 $ACLFormatter = DI::aclFormatter();
37
38                 $allow_cid = $ACLFormatter->sanitize($allow_cid);
39                 $allow_gid = $ACLFormatter->sanitize($allow_gid);
40                 $deny_cid = $ACLFormatter->sanitize($deny_cid);
41                 $deny_gid = $ACLFormatter->sanitize($deny_gid);
42
43                 // Public permission
44                 if (!$allow_cid && !$allow_gid && !$deny_cid && !$deny_gid) {
45                         return 0;
46                 }
47
48                 $condition = [
49                         'uid' => $uid,
50                         'allow_cid' => $allow_cid,
51                         'allow_gid' => $allow_gid,
52                         'deny_cid'  => $deny_cid,
53                         'deny_gid'  => $deny_gid
54                 ];
55                 $permissionset = DBA::selectFirst('permissionset', ['id'], $condition);
56
57                 if (DBA::isResult($permissionset)) {
58                         $psid = $permissionset['id'];
59                 } else {
60                         if (DBA::insert('permissionset', $condition, true)) {
61                                 $psid = DBA::lastInsertId();
62                         } else {
63                                 throw new HTTPException\InternalServerErrorException(L10n::t('Unable to create a new permission set.'));
64                         }
65                 }
66
67                 return $psid;
68         }
69
70         /**
71          * Returns a permission set for a given contact
72          *
73          * @param integer $uid        User id whom the items belong
74          * @param integer $contact_id Contact id of the visitor
75          *
76          * @return array of permission set ids.
77          * @throws \Exception
78          */
79         static public function get($uid, $contact_id)
80         {
81                 if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
82                         $groups = Group::getIdsByContactId($contact_id);
83                 }
84
85                 if (empty($groups) || !is_array($groups)) {
86                         return [];
87                 }
88
89                 $group_str = '<<>>'; // should be impossible to match
90
91                 foreach ($groups as $g) {
92                         $group_str .= '|<' . intval($g) . '>';
93                 }
94
95                 $contact_str = '<' . $contact_id . '>';
96
97                 $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
98                         AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
99                         $uid, $contact_str, $group_str, $contact_str, $group_str];
100
101                 $ret = DBA::select('permissionset', ['id'], $condition);
102                 $set = [];
103                 while ($permission = DBA::fetch($ret)) {
104                         $set[] = $permission['id'];
105                 }
106                 DBA::close($ret);
107
108                 return $set;
109         }
110 }