3 * @file src/Model/PermissionSet.php
5 namespace Friendica\Model;
7 use Friendica\Database\DBA;
10 * @brief functions for interacting with the permission set of an object (item, photo, event, ...)
15 * Fetch the id of a given permission set. Generate a new one when needed
17 * @param array $postarray The array from an item, picture or event post
21 public static function fetchIDForPost(&$postarray)
23 $condition = ['uid' => $postarray['uid'],
24 'allow_cid' => self::sortPermissions($postarray['allow_cid'] ?? ''),
25 'allow_gid' => self::sortPermissions($postarray['allow_gid'] ?? ''),
26 'deny_cid' => self::sortPermissions($postarray['deny_cid'] ?? ''),
27 'deny_gid' => self::sortPermissions($postarray['deny_gid'] ?? '')];
29 $set = DBA::selectFirst('permissionset', ['id'], $condition);
31 if (!DBA::isResult($set)) {
32 DBA::insert('permissionset', $condition, true);
34 $set = DBA::selectFirst('permissionset', ['id'], $condition);
37 $postarray['allow_cid'] = null;
38 $postarray['allow_gid'] = null;
39 $postarray['deny_cid'] = null;
40 $postarray['deny_gid'] = null;
45 private static function sortPermissions($permissionlist)
47 $cleaned_list = trim($permissionlist, '<>');
49 if (empty($cleaned_list)) {
50 return $permissionlist;
53 $elements = explode('><', $cleaned_list);
55 if (count($elements) <= 1) {
56 return $permissionlist;
61 return '<' . implode('><', $elements) . '>';
65 * @brief Returns a permission set for a given contact
67 * @param integer $uid User id whom the items belong
68 * @param integer $contact_id Contact id of the visitor
70 * @return array of permission set ids.
73 static public function get($uid, $contact_id)
75 if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
76 $groups = Group::getIdsByContactId($contact_id);
79 if (empty($groups) || !is_array($groups)) {
83 $group_str = '<<>>'; // should be impossible to match
85 foreach ($groups as $g) {
86 $group_str .= '|<' . intval($g) . '>';
89 $contact_str = '<' . $contact_id . '>';
91 $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
92 AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
93 $uid, $contact_str, $group_str, $contact_str, $group_str];
95 $ret = DBA::select('permissionset', ['id'], $condition);
97 while ($permission = DBA::fetch($ret)) {
98 $set[] = $permission['id'];