3 * @file /src/Util/Security.php
6 namespace Friendica\Util;
8 use Friendica\BaseObject;
9 use Friendica\Database\DBA;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Group;
12 use Friendica\Model\User;
15 * Secures that User is allow to do requests
17 class Security extends BaseObject
19 public static function canWriteToUserWall($owner)
23 if (!local_user() && !remote_user()) {
32 if (local_user() && ($owner == 0)) {
36 if (remote_user($owner)) {
37 // use remembered decision and avoid a DB lookup for each and every display item
38 // DO NOT use this function if there are going to be multiple owners
39 // We have a contact-id for an authenticated remote user, this block determines if the contact
40 // belongs to this page owner, and has the necessary permissions to post content
42 if ($verified === 2) {
44 } elseif ($verified === 1) {
47 $cid = remote_user($owner);
52 $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` INNER JOIN `user` on `user`.`uid` = `contact`.`uid`
53 WHERE `contact`.`uid` = %d AND `contact`.`id` = %d AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
54 AND `user`.`blockwall` = 0 AND `readonly` = 0 AND ( `contact`.`rel` IN ( %d , %d ) OR `user`.`page-flags` = %d ) LIMIT 1",
57 intval(Contact::SHARING),
58 intval(Contact::FRIEND),
59 intval(User::PAGE_FLAGS_COMMUNITY)
62 if (DBA::isResult($r)) {
74 /// @TODO $groups should be array
75 public static function getPermissionsSQLByUserId($owner_id, $remote_verified = false, $groups = null)
77 $local_user = local_user();
78 $remote_user = remote_user();
81 * Construct permissions
83 * default permissions - anonymous user
85 $sql = " AND allow_cid = ''
92 * Profile owner - everything is visible
94 if ($local_user && $local_user == $owner_id) {
97 * Authenticated visitor. Unless pre-verified,
98 * check that the contact belongs to this $owner_id
99 * and load the groups the visitor belongs to.
100 * If pre-verified, the caller is expected to have already
101 * done this and passed the groups into this function.
103 } elseif ($remote_user) {
105 * Authenticated visitor. Unless pre-verified,
106 * check that the contact belongs to this $owner_id
107 * and load the groups the visitor belongs to.
108 * If pre-verified, the caller is expected to have already
109 * done this and passed the groups into this function.
112 if (!$remote_verified) {
115 foreach (\Friendica\Core\Session::get('remote', []) as $visitor) {
116 if ($visitor['uid'] == $owner_id) {
117 $cid = $visitor['cid'];
122 if ($cid && DBA::exists('contact', ['id' => $cid, 'uid' => $owner_id, 'blocked' => false])) {
123 $remote_verified = true;
124 $groups = Group::getIdsByContactId($cid);
128 if ($remote_verified) {
129 $gs = '<<>>'; // should be impossible to match
131 if (is_array($groups)) {
132 foreach ($groups as $g) {
133 $gs .= '|<' . intval($g) . '>';
138 " AND ( NOT (deny_cid REGEXP '<%d>' OR deny_gid REGEXP '%s')
139 AND ( allow_cid REGEXP '<%d>' OR allow_gid REGEXP '%s' OR ( allow_cid = '' AND allow_gid = '') )