c7e51286e5386edb1fb21270aff7916bbf77fe0a
[friendica-addons.git] / remote_permissions / remote_permissions.php
1 <?php
2 /**
3  * Name: Remote Permissions
4  * Description: Allow the recipients of private posts to see who else can see the post by clicking the lock icon
5  * Version: 1.0
6  * Author: Zach <https://f.shmuz.in/profile/techcity>
7  *
8  */
9
10 use Friendica\Core\Config;
11 use Friendica\Core\PConfig;
12
13 function remote_permissions_install() {
14         register_hook('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
15         register_hook('plugin_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
16         register_hook('plugin_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
17 }
18
19 function remote_permissions_uninstall() {
20         unregister_hook('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
21         unregister_hook('plugin_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
22         unregister_hook('plugin_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
23 }
24
25 function remote_permissions_settings(&$a,&$o) {
26
27         if(! local_user())
28                 return;
29
30         $global = Config::get("remote_perms", "global");
31         if($global == 1)
32                 return;
33
34         /* Add our stylesheet to the page so we can make our settings look nice */
35
36         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
37
38         /* Get the current state of our config variable */
39
40         $remote_perms = PConfig::get(local_user(),'remote_perms','show');
41
42         /* Add some HTML to the existing form */
43
44 //      $t = file_get_contents("addon/remote_permissions/settings.tpl" );
45         $t = get_markup_template("settings.tpl", "addon/remote_permissions/" );
46         $o .= replace_macros($t, [
47                 '$remote_perms_title' => t('Remote Permissions Settings'),
48                 '$remote_perms_label' => t('Allow recipients of your private posts to see the other recipients of the posts'),
49                 '$checked' => (($remote_perms == 1) ? 'checked="checked"' : ''),
50                 '$submit' => t('Save Settings')
51         ]);
52
53 }
54
55 function remote_permissions_settings_post($a,$post) {
56         if(! local_user() || (! x($_POST,'remote-perms-submit')))
57                 return;
58
59         PConfig::set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
60         info( t('Remote Permissions settings updated.') . EOL);
61 }
62
63 function remote_permissions_content($a, $item_copy) {
64
65         if($item_copy['uid'] != local_user())
66                 return;
67
68         if(Config::get('remote_perms','global') == 0) {
69                 // Admin has set Individual choice. We need to find
70                 // the original poster. First, get the contact's info
71                 $r = q("SELECT nick, url FROM contact WHERE id = %d LIMIT 1",
72                        intval($item_copy['contact-id'])
73                 );
74                 if(! $r)
75                         return;
76
77                 // Find out if the contact lives here
78                 $baseurl = $a->get_baseurl();
79                 $baseurl = substr($baseurl, strpos($baseurl, '://') + 3);
80                 if(strpos($r[0]['url'], $baseurl) === false)
81                         return;
82
83                 // The contact lives here. Get his/her user info
84                 $nick = $r[0]['nick'];
85                 $r = q("SELECT uid FROM user WHERE nickname = '%s' LIMIT 1",
86                        dbesc($nick)
87                 );
88                 if(! $r)
89                         return;
90
91                 if(PConfig::get($r[0]['uid'],'remote_perms','show') == 0)
92                         return;
93         }
94
95         if(($item_copy['private'] == 1) && (! strlen($item_copy['allow_cid'])) && (! strlen($item_copy['allow_gid']))
96                 && (! strlen($item_copy['deny_cid'])) && (! strlen($item_copy['deny_gid']))) {
97
98                 $allow_names = [];
99
100                 // Check for the original post here -- that's the only way
101                 // to definitely get all of the recipients
102
103                 if($item_copy['uri'] === $item_copy['parent-uri']) {
104                         // Lockview for a top-level post
105                         $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s' AND type = 'wall' LIMIT 1",
106                                    dbesc($item_copy['uri'])
107                         );
108                 }
109                 else {
110                         // Lockview for a comment
111                         $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s'
112                                 AND parent = ( SELECT id FROM item WHERE uri = '%s' AND type = 'wall' ) LIMIT 1",
113                                    dbesc($item_copy['uri']),
114                                    dbesc($item_copy['parent-uri'])
115                         );
116                 }
117                 if($r) {
118
119                         $item = $r[0];
120
121                         $allowed_users = expand_acl($item['allow_cid']);
122                         $allowed_groups = expand_acl($item['allow_gid']);
123                         $deny_users = expand_acl($item['deny_cid']);
124                         $deny_groups = expand_acl($item['deny_gid']);
125
126                         $o = t('Visible to:') . '<br />';
127                         $allow = [];
128                         $deny = [];
129
130                         if(count($allowed_groups)) {
131                                 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
132                                         dbesc(implode(', ', $allowed_groups))
133                                 );
134                                 foreach($r as $rr)
135                                         $allow[] = $rr['contact-id'];
136                         }
137                         $allow = array_unique($allow + $allowed_users);
138
139                         if(count($deny_groups)) {
140                                 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
141                                         dbesc(implode(', ', $deny_groups))
142                                 );
143                                 foreach($r as $rr)
144                                         $deny[] = $rr['contact-id'];
145                         }
146                         $deny = $deny + $deny_users;
147
148                         if($allow)
149                         {
150                                 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
151                                            dbesc(implode(', ', array_diff($allow, $deny)))
152                                 );
153                                 foreach($r as $rr)
154                                         $allow_names[] = $rr['name'];
155                         }
156                 }
157                 else {
158                         // We don't have the original post. Let's try for the next best thing:
159                         // checking who else has the post on our own server. Note that comments
160                         // that were sent to Diaspora and were relayed to others on our server
161                         // will have different URIs than the original. We can match the GUID for
162                         // those
163                         $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
164                                    dbesc($item_copy['uri']),
165                                dbesc($item_copy['guid'])
166                         );
167                         if(! $r)
168                                 return;
169
170                         $allow = [];
171                         foreach($r as $rr)
172                                 $allow[] = $rr['uid'];
173
174                         $r = q("SELECT username FROM user WHERE uid IN ( %s )",
175                                 dbesc(implode(', ', $allow))
176                         );
177                         if(! $r)
178                                 return;
179
180                         $o = t('Visible to') . ' (' . t('may only be a partial list') . '):<br />';
181
182                         foreach($r as $rr)
183                                 $allow_names[] = $rr['username'];
184                 }
185
186                 // Sort the names alphabetically, case-insensitive
187                 natcasesort($allow_names);
188                 echo $o . implode(', ', $allow_names);
189                 killme();
190         }
191
192         return;
193 }
194
195 function remote_permissions_plugin_admin(&$a, &$o){
196         $t = get_markup_template( "admin.tpl", "addon/remote_permissions/" );
197         $o = replace_macros($t, [
198                 '$submit' => t('Save Settings'),
199                 '$global' => ['remotepermschoice', t('Global'), 1, t('The posts of every user on this server show the post recipients'),  Config::get('remote_perms', 'global') == 1],
200                 '$individual' => ['remotepermschoice', t('Individual'), 2, t('Each user chooses whether his/her posts show the post recipients'),  Config::get('remote_perms', 'global') == 0]
201         ]);
202 }
203
204 function remote_permissions_plugin_admin_post(&$a){
205         $choice =       ((x($_POST,'remotepermschoice'))                ? notags(trim($_POST['remotepermschoice']))     : '');
206         Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
207         info( t('Settings updated.'). EOL );
208 }
209