]> git.mxchange.org Git - friendica.git/blob - src/Model/PermissionSet.php
Postupdate is now working again (#5512)
[friendica.git] / src / Model / PermissionSet.php
1 <?php
2 /**
3  * @file src/Model/PermissionSet.php
4  */
5 namespace Friendica\Model;
6
7 use Friendica\BaseObject;
8 use Friendica\Database\DBA;
9
10 require_once 'include/dba.php';
11
12 /**
13  * @brief functions for interacting with the permission set of an object (item, photo, event, ...)
14  */
15 class PermissionSet extends BaseObject
16 {
17         /**
18          * Fetch the id of a given permission set. Generate a new one when needed
19          *
20          * @param array $postarray The array from an item, picture or event post
21          * @return id
22          */
23         public static function fetchIDForPost(&$postarray)
24         {
25                 if (is_null($postarray['allow_cid']) || is_null($postarray['allow_gid'])
26                         || is_null($postarray['deny_cid']) || is_null($postarray['deny_gid'])) {
27                         return null;
28                 }
29
30                 $condition = ['uid' => $postarray['uid'],
31                         'allow_cid' => self::sortPermissions(defaults($postarray, 'allow_cid', '')),
32                         'allow_gid' => self::sortPermissions(defaults($postarray, 'allow_gid', '')),
33                         'deny_cid' => self::sortPermissions(defaults($postarray, 'deny_cid', '')),
34                         'deny_gid' => self::sortPermissions(defaults($postarray, 'deny_gid', ''))];
35
36                 $set = DBA::selectFirst('permissionset', ['id'], $condition);
37
38                 if (!DBA::isResult($set)) {
39                         DBA::insert('permissionset', $condition, true);
40
41                         $set = DBA::selectFirst('permissionset', ['id'], $condition);
42                 }
43
44                 $postarray['allow_cid'] = null;
45                 $postarray['allow_gid'] = null;
46                 $postarray['deny_cid'] = null;
47                 $postarray['deny_gid'] = null;
48
49                 return $set['id'];
50         }
51
52         private static function sortPermissions($permissionlist)
53         {
54                 $cleaned_list = trim($permissionlist, '<>');
55
56                 if (empty($cleaned_list)) {
57                         return $permissionlist;
58                 }
59
60                 $elements = explode('><', $cleaned_list);
61
62                 if (count($elements) <= 1) {
63                         return $permissionlist;
64                 }
65
66                 asort($elements);
67
68                 return '<' . implode('><', $elements) . '>';
69         }
70
71         /**
72          * @brief Returns a permission set for a given contact
73          *
74          * @param integer $uid        User id whom the items belong
75          * @param integer $contact_id Contact id of the visitor
76          * @param array   $groups     Possibly previously fetched group ids for that contact
77          *
78          * @return array of permission set ids.
79          */
80
81         static public function get($uid, $contact_id, $groups = null)
82         {
83                 if (empty($groups) && DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
84                         $groups = Group::getIdsByContactId($contact_id);
85                 }
86
87                 if (empty($groups) || !is_array($groups)) {
88                         return [];
89                 }
90                 $group_str = '<<>>'; // should be impossible to match
91
92                 foreach ($groups as $g) {
93                         $group_str .= '|<' . intval($g) . '>';
94                 }
95
96                 $contact_str = '<' . $contact_id . '>';
97
98                 $condition = ["`uid` = ? AND (`allow_cid` = '' OR`allow_cid` REGEXP ?)
99                         AND (`deny_cid` = '' OR NOT `deny_cid` REGEXP ?)
100                         AND (`allow_gid` = '' OR `allow_gid` REGEXP ?)
101                         AND (`deny_gid` = '' OR NOT `deny_gid` REGEXP ?)",
102                         $uid, $contact_str, $contact_str, $group_str, $group_str];
103
104                 $ret = DBA::select('permissionset', ['id'], $condition);
105                 $set = [];
106                 while ($permission = DBA::fetch($ret)) {
107                         $set[] = $permission['id'];
108                 }
109                 DBA::close($ret);
110                 logger('Blubb: '.$uid.' - '.$contact_id.': '.implode(', ', $set));
111
112                 return $set;
113         }
114 }