3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Util;
24 use Friendica\Database\DBA;
25 use Friendica\Model\Contact;
26 use Friendica\Model\Group;
27 use Friendica\Model\User;
28 use Friendica\Core\Session;
31 * Secures that User is allow to do requests
35 public static function canWriteToUserWall($owner)
39 if (!Session::isAuthenticated()) {
48 if (local_user() && ($owner == 0)) {
52 if (!empty(Session::getRemoteContactID($owner))) {
53 // use remembered decision and avoid a DB lookup for each and every display item
54 // DO NOT use this function if there are going to be multiple owners
55 // We have a contact-id for an authenticated remote user, this block determines if the contact
56 // belongs to this page owner, and has the necessary permissions to post content
58 if ($verified === 2) {
60 } elseif ($verified === 1) {
63 $cid = Session::getRemoteContactID($owner);
68 $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` INNER JOIN `user` on `user`.`uid` = `contact`.`uid`
69 WHERE `contact`.`uid` = %d AND `contact`.`id` = %d AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
70 AND `user`.`blockwall` = 0 AND `readonly` = 0 AND (`contact`.`rel` IN (%d , %d) OR `user`.`page-flags` = %d) LIMIT 1",
73 intval(Contact::SHARING),
74 intval(Contact::FRIEND),
75 intval(User::PAGE_FLAGS_COMMUNITY)
78 if (DBA::isResult($r)) {
90 public static function getPermissionsSQLByUserId($owner_id)
92 $local_user = local_user();
93 $remote_contact = Session::getRemoteContactID($owner_id);
96 * Construct permissions
98 * default permissions - anonymous user
100 $sql = " AND allow_cid = ''
106 * Profile owner - everything is visible
108 if ($local_user && $local_user == $owner_id) {
111 * Authenticated visitor. Load the groups the visitor belongs to.
113 } elseif ($remote_contact) {
114 $gs = '<<>>'; // should be impossible to match
116 $groups = Group::getIdsByContactId($remote_contact);
118 if (is_array($groups)) {
119 foreach ($groups as $g) {
120 $gs .= '|<' . intval($g) . '>';
125 " AND (NOT (deny_cid REGEXP '<%d>' OR deny_gid REGEXP '%s')
126 AND (allow_cid REGEXP '<%d>' OR allow_gid REGEXP '%s' OR (allow_cid = '' AND allow_gid = ''))) ",
127 intval($remote_contact),
129 intval($remote_contact),