]> git.mxchange.org Git - friendica.git/blob - src/Util/Security.php
Daemon: Added check for empty data
[friendica.git] / src / Util / Security.php
1 <?php
2 /**
3  * @file /src/Util/Security.php
4  */
5
6 namespace Friendica\Util;
7
8 use Friendica\Database\DBA;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Group;
11 use Friendica\Model\User;
12 use Friendica\Core\Session;
13
14 /**
15  * Secures that User is allow to do requests
16  */
17 class Security
18 {
19         public static function canWriteToUserWall($owner)
20         {
21                 static $verified = 0;
22
23                 if (!Session::isAuthenticated()) {
24                         return false;
25                 }
26
27                 $uid = local_user();
28                 if ($uid == $owner) {
29                         return true;
30                 }
31
32                 if (local_user() && ($owner == 0)) {
33                         return true;
34                 }
35
36                 if (!empty(Session::getRemoteContactID($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
41
42                         if ($verified === 2) {
43                                 return true;
44                         } elseif ($verified === 1) {
45                                 return false;
46                         } else {
47                                 $cid = Session::getRemoteContactID($owner);
48                                 if (!$cid) {
49                                         return false;
50                                 }
51
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",
55                                         intval($owner),
56                                         intval($cid),
57                                         intval(Contact::SHARING),
58                                         intval(Contact::FRIEND),
59                                         intval(User::PAGE_FLAGS_COMMUNITY)
60                                 );
61
62                                 if (DBA::isResult($r)) {
63                                         $verified = 2;
64                                         return true;
65                                 } else {
66                                         $verified = 1;
67                                 }
68                         }
69                 }
70
71                 return false;
72         }
73
74         public static function getPermissionsSQLByUserId($owner_id)
75         {
76                 $local_user = local_user();
77                 $remote_contact = Session::getRemoteContactID($owner_id);
78
79                 /*
80                  * Construct permissions
81                  *
82                  * default permissions - anonymous user
83                  */
84                 $sql = " AND allow_cid = ''
85                          AND allow_gid = ''
86                          AND deny_cid  = ''
87                          AND deny_gid  = '' ";
88
89                 /*
90                  * Profile owner - everything is visible
91                  */
92                 if ($local_user && $local_user == $owner_id) {
93                         $sql = '';
94                 /*
95                  * Authenticated visitor. Load the groups the visitor belongs to.
96                  */
97                 } elseif ($remote_contact) {
98                         $gs = '<<>>'; // should be impossible to match
99
100                         $groups = Group::getIdsByContactId($remote_contact);
101
102                         if (is_array($groups)) {
103                                 foreach ($groups as $g) {
104                                         $gs .= '|<' . intval($g) . '>';
105                                 }
106                         }
107
108                         $sql = sprintf(
109                                 " AND (NOT (deny_cid REGEXP '<%d>' OR deny_gid REGEXP '%s')
110                                   AND (allow_cid REGEXP '<%d>' OR allow_gid REGEXP '%s' OR (allow_cid = '' AND allow_gid = ''))) ",
111                                 intval($remote_contact),
112                                 DBA::escape($gs),
113                                 intval($remote_contact),
114                                 DBA::escape($gs)
115                         );
116                 }
117                 return $sql;
118         }
119 }