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 if (is_null($postarray['allow_cid']) || is_null($postarray['allow_gid'])
26 || is_null($postarray['deny_cid']) || is_null($postarray['deny_gid'])) {
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', ''))];
36 $set = DBA::selectFirst('permissionset', ['id'], $condition);
38 if (!DBA::isResult($set)) {
39 DBA::insert('permissionset', $condition, true);
41 $set = DBA::selectFirst('permissionset', ['id'], $condition);
45 $postarray['allow_cid'] = null;
46 $postarray['allow_gid'] = null;
47 $postarray['deny_cid'] = null;
48 $postarray['deny_gid'] = null;
53 private static function sortPermissions($permissionlist)
55 $cleaned_list = trim($permissionlist, '<>');
57 if (empty($cleaned_list)) {
58 return $permissionlist;
61 $elements = explode('><', $cleaned_list);
63 if (count($elements) <= 1) {
64 return $permissionlist;
69 return '<' . implode('><', $elements) . '>';
73 * @brief Returns a permission set for a given contact
75 * @param integer $uid User id whom the items belong
76 * @param integer $contact_id Contact id of the visitor
77 * @param array $groups Possibly previously fetched group ids for that contact
79 * @return array of permission set ids.
82 static public function get($uid, $contact_id, $groups = null)
84 if (empty($groups) && DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
85 $groups = Group::getIdsByContactId($contact_id);
88 if (empty($groups) || !is_array($groups)) {
91 $group_str = '<<>>'; // should be impossible to match
93 foreach ($groups as $g) {
94 $group_str .= '|<' . intval($g) . '>';
97 $contact_str = '<' . $contact_id . '>';
99 $condition = ["`uid` = ? AND (`allow_cid` = '' OR`allow_cid` REGEXP ?)
100 AND (`deny_cid` = '' OR NOT `deny_cid` REGEXP ?)
101 AND (`allow_gid` = '' OR `allow_gid` REGEXP ?)
102 AND (`deny_gid` = '' OR NOT `deny_gid` REGEXP ?)",
103 $uid, $contact_str, $contact_str, $group_str, $group_str];
105 $ret = DBA::select('permissionset', ['id'], $condition);
107 while ($permission = DBA::fetch($ret)) {
108 $set[] = $permission['id'];
111 logger('Blubb: '.$uid.' - '.$contact_id.': '.implode(', ', $set));