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($postarray['allow_cid'] ?? ''),
26 'allow_gid' => self::sortPermissions($postarray['allow_gid'] ?? ''),
27 'deny_cid' => self::sortPermissions($postarray['deny_cid'] ?? ''),
28 'deny_gid' => self::sortPermissions($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
71 * @return array of permission set ids.
74 static public function get($uid, $contact_id)
76 if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
77 $groups = Group::getIdsByContactId($contact_id);
80 if (empty($groups) || !is_array($groups)) {
84 $group_str = '<<>>'; // should be impossible to match
86 foreach ($groups as $g) {
87 $group_str .= '|<' . intval($g) . '>';
90 $contact_str = '<' . $contact_id . '>';
92 $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
93 AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
94 $uid, $contact_str, $group_str, $contact_str, $group_str];
96 $ret = DBA::select('permissionset', ['id'], $condition);
98 while ($permission = DBA::fetch($ret)) {
99 $set[] = $permission['id'];