.
*
*/
namespace Friendica\Module;
use Friendica\Core\Hook;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\APContact;
use Friendica\Model\Group;
use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Model\User;
use Friendica\Network\HTTPException;
use Friendica\Protocol\ActivityPub;
/**
* Outputs the permission tooltip HTML content for the provided item, photo or event id.
*/
class PermissionTooltip extends \Friendica\BaseModule
{
protected function rawContent(array $request = [])
{
$type = $this->parameters['type'];
$referenceId = $this->parameters['id'];
$expectedTypes = ['item', 'photo', 'event'];
if (!in_array($type, $expectedTypes)) {
throw new HTTPException\BadRequestException(DI::l10n()->t('Wrong type "%s", expected one of: %s', $type, implode(', ', $expectedTypes)));
}
$condition = ['id' => $referenceId];
if ($type == 'item') {
$fields = ['uid', 'psid', 'private', 'uri-id'];
$model = Post::selectFirst($fields, $condition);
} else {
$fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
$model = DBA::selectFirst($type, $fields, $condition);
$model['allow_cid'] = DI::aclFormatter()->expand($model['allow_cid']);
$model['allow_gid'] = DI::aclFormatter()->expand($model['allow_gid']);
$model['deny_cid'] = DI::aclFormatter()->expand($model['deny_cid']);
$model['deny_gid'] = DI::aclFormatter()->expand($model['deny_gid']);
}
if (!DBA::isResult($model)) {
throw new HttpException\NotFoundException(DI::l10n()->t('Model not found'));
}
if (isset($model['psid'])) {
$permissionSet = DI::permissionSet()->selectOneById($model['psid'], $model['uid']);
$model['allow_cid'] = $permissionSet->allow_cid;
$model['allow_gid'] = $permissionSet->allow_gid;
$model['deny_cid'] = $permissionSet->deny_cid;
$model['deny_gid'] = $permissionSet->deny_gid;
}
// Kept for backwards compatiblity
Hook::callAll('lockview_content', $model);
if ($type == 'item') {
$receivers = $this->fetchReceivers($model['uri-id']);
} else {
$receivers = '';
}
if ($model['uid'] != local_user() ||
empty($model['allow_cid'])
&& empty($model['allow_gid'])
&& empty($model['deny_cid'])
&& empty($model['deny_gid'])
&& empty($receivers))
{
echo DI::l10n()->t('Remote privacy information not available.');
exit;
}
$allowed_users = $model['allow_cid'];
$allowed_groups = $model['allow_gid'];
$deny_users = $model['deny_cid'];
$deny_groups = $model['deny_gid'];
$o = DI::l10n()->t('Visible to:') . '
';
$l = [];
if (count($allowed_groups)) {
$key = array_search(Group::FOLLOWERS, $allowed_groups);
if ($key !== false) {
$l[] = '' . DI::l10n()->t('Followers') . '';
unset($allowed_groups[$key]);
}
$key = array_search(Group::MUTUALS, $allowed_groups);
if ($key !== false) {
$l[] = '' . DI::l10n()->t('Mutuals') . '';
unset($allowed_groups[$key]);
}
foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
$l[] = '' . $group['name'] . '';
}
}
foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $allowed_users]) as $contact) {
$l[] = $contact['name'];
}
if (count($deny_groups)) {
$key = array_search(Group::FOLLOWERS, $deny_groups);
if ($key !== false) {
$l[] = '' . DI::l10n()->t('Followers') . '';
unset($deny_groups[$key]);
}
$key = array_search(Group::MUTUALS, $deny_groups);
if ($key !== false) {
$l[] = '' . DI::l10n()->t('Mutuals') . '';
unset($deny_groups[$key]);
}
foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_groups]) as $group) {
$l[] = '' . $group['name'] . '';
}
}
foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $deny_users]) as $contact) {
$l[] = '' . $contact['name'] . '';
}
if (!empty($l)) {
echo $o . implode(', ', $l);
} else {
echo $o . $receivers;
}
exit();
}
/**
* Fetch a list of receivers
*
* @param int $uriId
* @return string
*/
private function fetchReceivers(int $uriId):string
{
$own_url = '';
$uid = local_user();
if ($uid) {
$owner = User::getOwnerDataById($uid);
if (!empty($owner['url'])) {
$own_url = $owner['url'];
}
}
$receivers = [];
foreach (Tag::getByURIId($uriId, [Tag::TO, Tag::CC, Tag::BCC]) as $receiver) {
// We only display BCC when it contains the current user
if (($receiver['type'] == Tag::BCC) && ($receiver['url'] != $own_url)) {
continue;
}
if ($receiver['url'] == ActivityPub::PUBLIC_COLLECTION) {
$receivers[$receiver['type']][] = DI::l10n()->t('Public');
} else {
$apcontact = DBA::selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]);
if (!empty($apcontact['name'])) {
$receivers[$receiver['type']][] = DI::l10n()->t('Followers (%s)', $apcontact['name']);
} elseif ($apcontact = APContact::getByURL($receiver['url'], false)) {
$receivers[$receiver['type']][] = $apcontact['name'];
} else {
$receivers[$receiver['type']][] = $receiver['name'];
}
}
}
$output = '';
foreach ($receivers as $type => $receiver) {
$max = DI::config()->get('system', 'max_receivers');
$total = count($receiver);
if ($total > $max) {
$receiver = array_slice($receiver, 0, $max);
$receiver[] = DI::l10n()->t('%d more', $total - $max);
}
switch ($type) {
case Tag::TO:
$output .= DI::l10n()->t('To: %s
', implode(', ', $receiver));
break;
case Tag::CC:
$output .= DI::l10n()->t('CC: %s
', implode(', ', $receiver));
break;
case Tag::BCC:
$output .= DI::l10n()->t('BCC: %s
', implode(', ', $receiver));
break;
}
}
return $output;
}
}