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']));
63 info(DI::l10n()->t('Remote Permissions settings updated.') . EOL);
66 function remote_permissions_content($a, $item_copy) {
68 if($item_copy['uid'] != local_user())
71 if(DI::config()->get('remote_perms','global') == 0) {
72 // Admin has set Individual choice. We need to find
73 // the original poster. First, get the contact's info
74 $r = q("SELECT nick, url FROM contact WHERE id = %d LIMIT 1",
75 intval($item_copy['contact-id'])
80 // Find out if the contact lives here
81 $baseurl = DI::baseUrl()->get();
82 $baseurl = substr($baseurl, strpos($baseurl, '://') + 3);
83 if(strpos($r[0]['url'], $baseurl) === false)
86 // The contact lives here. Get his/her user info
87 $nick = $r[0]['nick'];
88 $r = q("SELECT uid FROM user WHERE nickname = '%s' LIMIT 1",
94 if(DI::pConfig()->get($r[0]['uid'],'remote_perms','show') == 0)
98 if(($item_copy['private'] == 1) && (! strlen($item_copy['allow_cid'])) && (! strlen($item_copy['allow_gid']))
99 && (! strlen($item_copy['deny_cid'])) && (! strlen($item_copy['deny_gid']))) {
103 // Check for the original post here -- that's the only way
104 // to definitely get all of the recipients
106 if($item_copy['uri'] === $item_copy['parent-uri']) {
107 // Lockview for a top-level post
108 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s' AND type = 'wall' LIMIT 1",
109 DBA::escape($item_copy['uri'])
113 // Lockview for a comment
114 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s'
115 AND parent = ( SELECT id FROM item WHERE uri = '%s' AND type = 'wall' ) LIMIT 1",
116 DBA::escape($item_copy['uri']),
117 DBA::escape($item_copy['parent-uri'])
124 $aclFormatter = DI::aclFormatter();
126 $allowed_users = $aclFormatter->expand($item['allow_cid']);
127 $allowed_groups = $aclFormatter->expand($item['allow_gid']);
128 $deny_users = $aclFormatter->expand($item['deny_cid']);
129 $deny_groups = $aclFormatter->expand($item['deny_gid']);
131 $o = DI::l10n()->t('Visible to:') . '<br />';
135 if(count($allowed_groups)) {
136 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
137 DBA::escape(implode(', ', $allowed_groups))
140 $allow[] = $rr['contact-id'];
142 $allow = array_unique($allow + $allowed_users);
144 if(count($deny_groups)) {
145 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
146 DBA::escape(implode(', ', $deny_groups))
149 $deny[] = $rr['contact-id'];
151 $deny = $deny + $deny_users;
155 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
156 DBA::escape(implode(', ', array_diff($allow, $deny)))
159 $allow_names[] = $rr['name'];
163 // We don't have the original post. Let's try for the next best thing:
164 // checking who else has the post on our own server. Note that comments
165 // that were sent to Diaspora and were relayed to others on our server
166 // will have different URIs than the original. We can match the GUID for
168 $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
169 DBA::escape($item_copy['uri']),
170 DBA::escape($item_copy['guid'])
177 $allow[] = $rr['uid'];
179 $r = q("SELECT username FROM user WHERE uid IN ( %s )",
180 DBA::escape(implode(', ', $allow))
185 $o = DI::l10n()->t('Visible to') . ' (' . DI::l10n()->t('may only be a partial list') . '):<br />';
188 $allow_names[] = $rr['username'];
191 // Sort the names alphabetically, case-insensitive
192 natcasesort($allow_names);
193 echo $o . implode(', ', $allow_names);
200 function remote_permissions_addon_admin(&$a, &$o){
201 $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/remote_permissions/" );
202 $o = Renderer::replaceMacros($t, [
203 '$submit' => DI::l10n()->t('Save Settings'),
204 '$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],
205 '$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]
209 function remote_permissions_addon_admin_post(&$a){
210 $choice = (!empty($_POST['remotepermschoice']) ? Strings::escapeTags(trim($_POST['remotepermschoice'])) : '');
211 DI::config()->set('remote_perms','global',($choice == 1 ? 1 : 0));
212 info(DI::l10n()->t('Settings updated.'). EOL);