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
6 * Author: Zach <https://f.shmuz.in/profile/techcity>
10 use Friendica\Core\Hook;
11 use Friendica\Core\Renderer;
12 use Friendica\Database\DBA;
14 use Friendica\Util\Strings;
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');
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');
28 function remote_permissions_settings(&$a,&$o) {
33 $global = DI::config()->get("remote_perms", "global");
37 /* Add our stylesheet to the page so we can make our settings look nice */
39 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
41 /* Get the current state of our config variable */
43 $remote_perms = DI::pConfig()->get(local_user(),'remote_perms','show');
45 /* Add some HTML to the existing form */
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')
58 function remote_permissions_settings_post($a,$post) {
59 if(! local_user() || empty($_POST['remote-perms-submit']))
62 DI::pConfig()->set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
65 function remote_permissions_content($a, $item_copy) {
67 if($item_copy['uid'] != local_user())
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'])
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)
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",
93 if(DI::pConfig()->get($r[0]['uid'],'remote_perms','show') == 0)
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']))) {
102 // Check for the original post here -- that's the only way
103 // to definitely get all of the recipients
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'])
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'])
123 $aclFormatter = DI::aclFormatter();
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']);
130 $o = DI::l10n()->t('Visible to:') . '<br />';
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))
139 $allow[] = $rr['contact-id'];
141 $allow = array_unique($allow + $allowed_users);
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))
148 $deny[] = $rr['contact-id'];
150 $deny = $deny + $deny_users;
154 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
155 DBA::escape(implode(', ', array_diff($allow, $deny)))
158 $allow_names[] = $rr['name'];
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
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'])
176 $allow[] = $rr['uid'];
178 $r = q("SELECT username FROM user WHERE uid IN ( %s )",
179 DBA::escape(implode(', ', $allow))
184 $o = DI::l10n()->t('Visible to') . ' (' . DI::l10n()->t('may only be a partial list') . '):<br />';
187 $allow_names[] = $rr['username'];
190 // Sort the names alphabetically, case-insensitive
191 natcasesort($allow_names);
192 echo $o . implode(', ', $allow_names);
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]
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));