2018-07-19 09:52:05 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Model/PermissionSet.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Model;
|
|
|
|
|
|
|
|
use Friendica\BaseObject;
|
2018-07-20 08:19:26 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-07-19 09:52:05 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief functions for interacting with the permission set of an object (item, photo, event, ...)
|
|
|
|
*/
|
|
|
|
class PermissionSet extends BaseObject
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Fetch the id of a given permission set. Generate a new one when needed
|
|
|
|
*
|
|
|
|
* @param array $postarray The array from an item, picture or event post
|
2019-01-06 16:06:53 -05:00
|
|
|
* @return int id
|
|
|
|
* @throws \Exception
|
2018-07-19 09:52:05 -04:00
|
|
|
*/
|
2018-07-25 19:14:55 -04:00
|
|
|
public static function fetchIDForPost(&$postarray)
|
2018-07-19 09:52:05 -04:00
|
|
|
{
|
|
|
|
$condition = ['uid' => $postarray['uid'],
|
2018-07-19 18:40:39 -04:00
|
|
|
'allow_cid' => self::sortPermissions(defaults($postarray, 'allow_cid', '')),
|
|
|
|
'allow_gid' => self::sortPermissions(defaults($postarray, 'allow_gid', '')),
|
|
|
|
'deny_cid' => self::sortPermissions(defaults($postarray, 'deny_cid', '')),
|
|
|
|
'deny_gid' => self::sortPermissions(defaults($postarray, 'deny_gid', ''))];
|
2018-07-19 09:52:05 -04:00
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
$set = DBA::selectFirst('permissionset', ['id'], $condition);
|
2018-07-19 09:52:05 -04:00
|
|
|
|
2018-07-21 08:46:04 -04:00
|
|
|
if (!DBA::isResult($set)) {
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::insert('permissionset', $condition, true);
|
2018-07-19 09:52:05 -04:00
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
$set = DBA::selectFirst('permissionset', ['id'], $condition);
|
2018-07-19 09:52:05 -04:00
|
|
|
}
|
2018-07-25 19:14:55 -04:00
|
|
|
|
|
|
|
$postarray['allow_cid'] = null;
|
|
|
|
$postarray['allow_gid'] = null;
|
|
|
|
$postarray['deny_cid'] = null;
|
|
|
|
$postarray['deny_gid'] = null;
|
|
|
|
|
2018-07-19 09:52:05 -04:00
|
|
|
return $set['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function sortPermissions($permissionlist)
|
|
|
|
{
|
|
|
|
$cleaned_list = trim($permissionlist, '<>');
|
|
|
|
|
|
|
|
if (empty($cleaned_list)) {
|
|
|
|
return $permissionlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
$elements = explode('><', $cleaned_list);
|
|
|
|
|
|
|
|
if (count($elements) <= 1) {
|
|
|
|
return $permissionlist;
|
|
|
|
}
|
|
|
|
|
|
|
|
asort($elements);
|
|
|
|
|
|
|
|
return '<' . implode('><', $elements) . '>';
|
|
|
|
}
|
2018-07-25 19:14:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a permission set for a given contact
|
|
|
|
*
|
|
|
|
* @param integer $uid User id whom the items belong
|
|
|
|
* @param integer $contact_id Contact id of the visitor
|
|
|
|
*
|
|
|
|
* @return array of permission set ids.
|
2019-01-06 16:06:53 -05:00
|
|
|
* @throws \Exception
|
2018-07-25 19:14:55 -04:00
|
|
|
*/
|
2019-09-28 01:37:24 -04:00
|
|
|
static public function get($uid, $contact_id)
|
2018-07-25 19:14:55 -04:00
|
|
|
{
|
2019-09-28 01:37:24 -04:00
|
|
|
if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
|
2018-07-25 19:14:55 -04:00
|
|
|
$groups = Group::getIdsByContactId($contact_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($groups) || !is_array($groups)) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-09-28 01:37:24 -04:00
|
|
|
|
2018-07-25 19:14:55 -04:00
|
|
|
$group_str = '<<>>'; // should be impossible to match
|
|
|
|
|
|
|
|
foreach ($groups as $g) {
|
|
|
|
$group_str .= '|<' . intval($g) . '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$contact_str = '<' . $contact_id . '>';
|
|
|
|
|
2019-09-28 16:42:33 -04:00
|
|
|
$condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
|
|
|
|
AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
|
|
|
|
$uid, $contact_str, $group_str, $contact_str, $group_str];
|
2018-07-25 19:14:55 -04:00
|
|
|
|
|
|
|
$ret = DBA::select('permissionset', ['id'], $condition);
|
|
|
|
$set = [];
|
|
|
|
while ($permission = DBA::fetch($ret)) {
|
|
|
|
$set[] = $permission['id'];
|
|
|
|
}
|
|
|
|
DBA::close($ret);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
2018-07-19 09:52:05 -04:00
|
|
|
}
|