]> git.mxchange.org Git - friendica.git/blob - include/security.php
cleanup
[friendica.git] / include / security.php
1 <?php
2
3 function can_write_wall(&$a,$owner) {
4
5         static $verified = 0;
6
7         if((! (local_user())) && (! (remote_user())))
8                 return false;
9
10         $uid = local_user();
11
12         if(($uid) && ($uid == $owner)) {
13                 return true;
14         }
15
16         if(remote_user()) {
17
18                 // user remembered decision and avoid a DB lookup for each and every display item
19                 // DO NOT use this function if there are going to be multiple owners
20
21                 if($verified === 2)
22                         return true;
23                 elseif($verified === 1)
24                         return false;
25                 else {
26                         $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` LEFT JOIN `user` on `user`.`uid` = `contact`.`uid` 
27                                 WHERE `contact`.`uid` = %d AND `contact`.`id` = %d AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 
28                                 AND `user`.`blockwall` = 0 AND `readonly` = 0  AND ( `contact`.`rel` IN ( %d , %d ) OR `user`.`page-flags` = %d ) LIMIT 1",
29                                 intval($owner),
30                                 intval(remote_user()),
31                                 intval(CONTACT_IS_FOLLOWER),
32                                 intval(CONTACT_IS_FRIEND),
33                                 intval(PAGE_COMMUNITY)
34                         );
35                         if(count($r)) {
36                                 $verified = 2;
37                                 return true;
38                         }
39                         else {
40                                 $verified = 1;
41                         }
42                 }
43         }
44
45         return false;
46 }
47
48
49 function permissions_sql($owner_id,$remote_verified = false,$groups = null) {
50
51         $local_user = local_user();
52         $remote_user = remote_user();
53
54         /**
55          * Construct permissions
56          *
57          * default permissions - anonymous user
58          */
59
60         $sql = " AND allow_cid = '' 
61                          AND allow_gid = '' 
62                          AND deny_cid  = '' 
63                          AND deny_gid  = '' 
64         ";
65
66         /**
67          * Profile owner - everything is visible
68          */
69
70         if(($local_user) && ($local_user == $owner_id)) {
71                 $sql = ''; 
72         }
73
74         /**
75          * Authenticated visitor. Unless pre-verified, 
76          * check that the contact belongs to this $owner_id
77          * and load the groups the visitor belongs to.
78          * If pre-verified, the caller is expected to have already
79          * done this and passed the groups into this function.
80          */
81
82         elseif($remote_user) {
83
84                 if(! $remote_verified) {
85                         $r = q("SELECT id FROM contact WHERE id = %d AND uid = %d AND blocked = 0 LIMIT 1",
86                                 intval($remote_user),
87                                 intval($owner_id)
88                         );
89                         if(count($r)) {
90                                 $remote_verified = true;
91                                 $groups = init_groups_visitor($remote_user);
92                         }
93                 }
94                 if($remote_verified) {
95                 
96                         $gs = '<<>>'; // should be impossible to match
97
98                         if(is_array($groups) && count($groups)) {
99                                 foreach($groups as $g)
100                                         $gs .= '|<' . intval($g) . '>';
101                         } 
102
103                         $sql = sprintf(
104                                 " AND ( allow_cid = '' OR allow_cid REGEXP '<%d>' ) 
105                                   AND ( deny_cid  = '' OR  NOT deny_cid REGEXP '<%d>' ) 
106                                   AND ( allow_gid = '' OR allow_gid REGEXP '%s' )
107                                   AND ( deny_gid  = '' OR NOT deny_gid REGEXP '%s') 
108                                 ",
109                                 intval($remote_user),
110                                 intval($remote_user),
111                                 dbesc($gs),
112                                 dbesc($gs)
113                         );
114                 }
115         }
116         return $sql;
117 }