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