3 * @file src/Model/PermissionSet.php
5 namespace Friendica\Model;
7 use Friendica\BaseObject;
8 use Friendica\Database\DBA;
10 require_once 'include/dba.php';
13 * @brief functions for interacting with the permission set of an object (item, photo, event, ...)
15 class PermissionSet extends BaseObject
18 * Fetch the id of a given permission set. Generate a new one when needed
20 * @param array $postarray The array from an item, picture or event post
23 public static function fetchIDForPost(&$postarray)
25 $condition = ['uid' => $postarray['uid'],
26 'allow_cid' => self::sortPermissions(defaults($postarray, 'allow_cid', '')),
27 'allow_gid' => self::sortPermissions(defaults($postarray, 'allow_gid', '')),
28 'deny_cid' => self::sortPermissions(defaults($postarray, 'deny_cid', '')),
29 'deny_gid' => self::sortPermissions(defaults($postarray, 'deny_gid', ''))];
31 $set = DBA::selectFirst('permissionset', ['id'], $condition);
33 if (!DBA::isResult($set)) {
34 DBA::insert('permissionset', $condition, true);
36 $set = DBA::selectFirst('permissionset', ['id'], $condition);
39 $postarray['allow_cid'] = null;
40 $postarray['allow_gid'] = null;
41 $postarray['deny_cid'] = null;
42 $postarray['deny_gid'] = null;
47 private static function sortPermissions($permissionlist)
49 $cleaned_list = trim($permissionlist, '<>');
51 if (empty($cleaned_list)) {
52 return $permissionlist;
55 $elements = explode('><', $cleaned_list);
57 if (count($elements) <= 1) {
58 return $permissionlist;
63 return '<' . implode('><', $elements) . '>';
67 * @brief Returns a permission set for a given contact
69 * @param integer $uid User id whom the items belong
70 * @param integer $contact_id Contact id of the visitor
71 * @param array $groups Possibly previously fetched group ids for that contact
73 * @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'];