3 * @file src/Model/PermissionSet.php
5 namespace Friendica\Model;
7 use Friendica\BaseObject;
8 use Friendica\Database\DBA;
11 * @brief functions for interacting with the permission set of an object (item, photo, event, ...)
13 class PermissionSet extends BaseObject
16 * Fetch the id of a given permission set. Generate a new one when needed
18 * @param array $postarray The array from an item, picture or event post
22 public static function fetchIDForPost(&$postarray)
24 $condition = ['uid' => $postarray['uid'],
25 'allow_cid' => self::sortPermissions(defaults($postarray, 'allow_cid', '')),
26 'allow_gid' => self::sortPermissions(defaults($postarray, 'allow_gid', '')),
27 'deny_cid' => self::sortPermissions(defaults($postarray, 'deny_cid', '')),
28 'deny_gid' => self::sortPermissions(defaults($postarray, 'deny_gid', ''))];
30 $set = DBA::selectFirst('permissionset', ['id'], $condition);
32 if (!DBA::isResult($set)) {
33 DBA::insert('permissionset', $condition, true);
35 $set = DBA::selectFirst('permissionset', ['id'], $condition);
38 $postarray['allow_cid'] = null;
39 $postarray['allow_gid'] = null;
40 $postarray['deny_cid'] = null;
41 $postarray['deny_gid'] = null;
46 private static function sortPermissions($permissionlist)
48 $cleaned_list = trim($permissionlist, '<>');
50 if (empty($cleaned_list)) {
51 return $permissionlist;
54 $elements = explode('><', $cleaned_list);
56 if (count($elements) <= 1) {
57 return $permissionlist;
62 return '<' . implode('><', $elements) . '>';
66 * @brief Returns a permission set for a given contact
68 * @param integer $uid User id whom the items belong
69 * @param integer $contact_id Contact id of the visitor
70 * @param array $groups Possibly previously fetched group ids for that contact
72 * @return array of permission set ids.
76 static public function get($uid, $contact_id, $groups = null)
78 if (empty($groups) && DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
79 $groups = Group::getIdsByContactId($contact_id);
82 if (empty($groups) || !is_array($groups)) {
85 $group_str = '<<>>'; // should be impossible to match
87 foreach ($groups as $g) {
88 $group_str .= '|<' . intval($g) . '>';
91 $contact_str = '<' . $contact_id . '>';
93 $condition = ["`uid` = ? AND (`allow_cid` = '' OR`allow_cid` REGEXP ?)
94 AND (`deny_cid` = '' OR NOT `deny_cid` REGEXP ?)
95 AND (`allow_gid` = '' OR `allow_gid` REGEXP ?)
96 AND (`deny_gid` = '' OR NOT `deny_gid` REGEXP ?)",
97 $uid, $contact_str, $contact_str, $group_str, $group_str];
99 $ret = DBA::select('permissionset', ['id'], $condition);
101 while ($permission = DBA::fetch($ret)) {
102 $set[] = $permission['id'];