]> git.mxchange.org Git - friendica.git/blob - src/Util/Security.php
Merge pull request #8271 from MrPetovan/bug/8229-frio-mobile-back-to-top
[friendica.git] / src / Util / Security.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
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;
29
30 /**
31  * Secures that User is allow to do requests
32  */
33 class Security
34 {
35         public static function canWriteToUserWall($owner)
36         {
37                 static $verified = 0;
38
39                 if (!Session::isAuthenticated()) {
40                         return false;
41                 }
42
43                 $uid = local_user();
44                 if ($uid == $owner) {
45                         return true;
46                 }
47
48                 if (local_user() && ($owner == 0)) {
49                         return true;
50                 }
51
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
57
58                         if ($verified === 2) {
59                                 return true;
60                         } elseif ($verified === 1) {
61                                 return false;
62                         } else {
63                                 $cid = Session::getRemoteContactID($owner);
64                                 if (!$cid) {
65                                         return false;
66                                 }
67
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",
71                                         intval($owner),
72                                         intval($cid),
73                                         intval(Contact::SHARING),
74                                         intval(Contact::FRIEND),
75                                         intval(User::PAGE_FLAGS_COMMUNITY)
76                                 );
77
78                                 if (DBA::isResult($r)) {
79                                         $verified = 2;
80                                         return true;
81                                 } else {
82                                         $verified = 1;
83                                 }
84                         }
85                 }
86
87                 return false;
88         }
89
90         public static function getPermissionsSQLByUserId($owner_id)
91         {
92                 $local_user = local_user();
93                 $remote_contact = Session::getRemoteContactID($owner_id);
94
95                 /*
96                  * Construct permissions
97                  *
98                  * default permissions - anonymous user
99                  */
100                 $sql = " AND allow_cid = ''
101                          AND allow_gid = ''
102                          AND deny_cid  = ''
103                          AND deny_gid  = '' ";
104
105                 /*
106                  * Profile owner - everything is visible
107                  */
108                 if ($local_user && $local_user == $owner_id) {
109                         $sql = '';
110                 /*
111                  * Authenticated visitor. Load the groups the visitor belongs to.
112                  */
113                 } elseif ($remote_contact) {
114                         $gs = '<<>>'; // should be impossible to match
115
116                         $groups = Group::getIdsByContactId($remote_contact);
117
118                         if (is_array($groups)) {
119                                 foreach ($groups as $g) {
120                                         $gs .= '|<' . intval($g) . '>';
121                                 }
122                         }
123
124                         $sql = sprintf(
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),
128                                 DBA::escape($gs),
129                                 intval($remote_contact),
130                                 DBA::escape($gs)
131                         );
132                 }
133                 return $sql;
134         }
135 }