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\Core\L10n;
13 use Friendica\Model\PermissionSet;
14 use Friendica\Core\System;
17 * Secures that User is allow to do requests
19 class Security extends BaseObject
21 public static function canWriteToUserWall($owner)
25 if (!local_user() && !remote_user()) {
34 if (local_user() && ($owner == 0)) {
39 // use remembered decision and avoid a DB lookup for each and every display item
40 // DO NOT use this function if there are going to be multiple owners
41 // We have a contact-id for an authenticated remote user, this block determines if the contact
42 // belongs to this page owner, and has the necessary permissions to post content
44 if ($verified === 2) {
46 } elseif ($verified === 1) {
51 if (!empty($_SESSION['remote'])) {
52 foreach ($_SESSION['remote'] as $visitor) {
53 if ($visitor['uid'] == $owner) {
54 $cid = $visitor['cid'];
64 $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` INNER JOIN `user` on `user`.`uid` = `contact`.`uid`
65 WHERE `contact`.`uid` = %d AND `contact`.`id` = %d AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
66 AND `user`.`blockwall` = 0 AND `readonly` = 0 AND ( `contact`.`rel` IN ( %d , %d ) OR `user`.`page-flags` = %d ) LIMIT 1",
69 intval(Contact::SHARING),
70 intval(Contact::FRIEND),
71 intval(Contact::PAGE_COMMUNITY)
74 if (DBA::isResult($r)) {
86 /// @TODO $groups should be array
87 public static function getPermissionsSQLByUserId($owner_id, $remote_verified = false, $groups = null)
89 $local_user = local_user();
90 $remote_user = remote_user();
93 * Construct permissions
95 * default permissions - anonymous user
97 $sql = " AND allow_cid = ''
104 * Profile owner - everything is visible
106 if ($local_user && $local_user == $owner_id) {
109 * Authenticated visitor. Unless pre-verified,
110 * check that the contact belongs to this $owner_id
111 * and load the groups the visitor belongs to.
112 * If pre-verified, the caller is expected to have already
113 * done this and passed the groups into this function.
115 } elseif ($remote_user) {
117 * Authenticated visitor. Unless pre-verified,
118 * check that the contact belongs to this $owner_id
119 * and load the groups the visitor belongs to.
120 * If pre-verified, the caller is expected to have already
121 * done this and passed the groups into this function.
124 if (!$remote_verified) {
125 if (DBA::exists('contact', ['id' => $remote_user, 'uid' => $owner_id, 'blocked' => false])) {
126 $remote_verified = true;
127 $groups = Group::getIdsByContactId($remote_user);
131 if ($remote_verified) {
132 $gs = '<<>>'; // should be impossible to match
134 if (is_array($groups)) {
135 foreach ($groups as $g) {
136 $gs .= '|<' . intval($g) . '>';
141 " AND ( NOT (deny_cid REGEXP '<%d>' OR deny_gid REGEXP '%s')
142 AND ( allow_cid REGEXP '<%d>' OR allow_gid REGEXP '%s' OR ( allow_cid = '' AND allow_gid = '') )
145 intval($remote_user),
147 intval($remote_user),