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