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\Config;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Renderer;
13 use Friendica\Database\DBA;
15 use Friendica\Util\Strings;
17 function remote_permissions_install() {
18 Hook::register('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
19 Hook::register('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
20 Hook::register('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
23 function remote_permissions_uninstall() {
24 Hook::unregister('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
25 Hook::unregister('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
26 Hook::unregister('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
29 function remote_permissions_settings(&$a,&$o) {
34 $global = Config::get("remote_perms", "global");
38 /* Add our stylesheet to the page so we can make our settings look nice */
40 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
42 /* Get the current state of our config variable */
44 $remote_perms = DI::pConfig()->get(local_user(),'remote_perms','show');
46 /* Add some HTML to the existing form */
48 // $t = file_get_contents("addon/remote_permissions/settings.tpl" );
49 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/remote_permissions/" );
50 $o .= Renderer::replaceMacros($t, [
51 '$remote_perms_title' => DI::l10n()->t('Remote Permissions Settings'),
52 '$remote_perms_label' => DI::l10n()->t('Allow recipients of your private posts to see the other recipients of the posts'),
53 '$checked' => (($remote_perms == 1) ? 'checked="checked"' : ''),
54 '$submit' => DI::l10n()->t('Save Settings')
59 function remote_permissions_settings_post($a,$post) {
60 if(! local_user() || empty($_POST['remote-perms-submit']))
63 DI::pConfig()->set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
64 info(DI::l10n()->t('Remote Permissions settings updated.') . EOL);
67 function remote_permissions_content($a, $item_copy) {
69 if($item_copy['uid'] != local_user())
72 if(Config::get('remote_perms','global') == 0) {
73 // Admin has set Individual choice. We need to find
74 // the original poster. First, get the contact's info
75 $r = q("SELECT nick, url FROM contact WHERE id = %d LIMIT 1",
76 intval($item_copy['contact-id'])
81 // Find out if the contact lives here
82 $baseurl = DI::baseUrl()->get();
83 $baseurl = substr($baseurl, strpos($baseurl, '://') + 3);
84 if(strpos($r[0]['url'], $baseurl) === false)
87 // The contact lives here. Get his/her user info
88 $nick = $r[0]['nick'];
89 $r = q("SELECT uid FROM user WHERE nickname = '%s' LIMIT 1",
95 if(DI::pConfig()->get($r[0]['uid'],'remote_perms','show') == 0)
99 if(($item_copy['private'] == 1) && (! strlen($item_copy['allow_cid'])) && (! strlen($item_copy['allow_gid']))
100 && (! strlen($item_copy['deny_cid'])) && (! strlen($item_copy['deny_gid']))) {
104 // Check for the original post here -- that's the only way
105 // to definitely get all of the recipients
107 if($item_copy['uri'] === $item_copy['parent-uri']) {
108 // Lockview for a top-level post
109 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s' AND type = 'wall' LIMIT 1",
110 DBA::escape($item_copy['uri'])
114 // Lockview for a comment
115 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s'
116 AND parent = ( SELECT id FROM item WHERE uri = '%s' AND type = 'wall' ) LIMIT 1",
117 DBA::escape($item_copy['uri']),
118 DBA::escape($item_copy['parent-uri'])
125 $aclFormatter = DI::aclFormatter();
127 $allowed_users = $aclFormatter->expand($item['allow_cid']);
128 $allowed_groups = $aclFormatter->expand($item['allow_gid']);
129 $deny_users = $aclFormatter->expand($item['deny_cid']);
130 $deny_groups = $aclFormatter->expand($item['deny_gid']);
132 $o = DI::l10n()->t('Visible to:') . '<br />';
136 if(count($allowed_groups)) {
137 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
138 DBA::escape(implode(', ', $allowed_groups))
141 $allow[] = $rr['contact-id'];
143 $allow = array_unique($allow + $allowed_users);
145 if(count($deny_groups)) {
146 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
147 DBA::escape(implode(', ', $deny_groups))
150 $deny[] = $rr['contact-id'];
152 $deny = $deny + $deny_users;
156 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
157 DBA::escape(implode(', ', array_diff($allow, $deny)))
160 $allow_names[] = $rr['name'];
164 // We don't have the original post. Let's try for the next best thing:
165 // checking who else has the post on our own server. Note that comments
166 // that were sent to Diaspora and were relayed to others on our server
167 // will have different URIs than the original. We can match the GUID for
169 $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
170 DBA::escape($item_copy['uri']),
171 DBA::escape($item_copy['guid'])
178 $allow[] = $rr['uid'];
180 $r = q("SELECT username FROM user WHERE uid IN ( %s )",
181 DBA::escape(implode(', ', $allow))
186 $o = DI::l10n()->t('Visible to') . ' (' . DI::l10n()->t('may only be a partial list') . '):<br />';
189 $allow_names[] = $rr['username'];
192 // Sort the names alphabetically, case-insensitive
193 natcasesort($allow_names);
194 echo $o . implode(', ', $allow_names);
201 function remote_permissions_addon_admin(&$a, &$o){
202 $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/remote_permissions/" );
203 $o = Renderer::replaceMacros($t, [
204 '$submit' => DI::l10n()->t('Save Settings'),
205 '$global' => ['remotepermschoice', DI::l10n()->t('Global'), 1, DI::l10n()->t('The posts of every user on this server show the post recipients'), Config::get('remote_perms', 'global') == 1],
206 '$individual' => ['remotepermschoice', DI::l10n()->t('Individual'), 2, DI::l10n()->t('Each user chooses whether his/her posts show the post recipients'), Config::get('remote_perms', 'global') == 0]
210 function remote_permissions_addon_admin_post(&$a){
211 $choice = (!empty($_POST['remotepermschoice']) ? Strings::escapeTags(trim($_POST['remotepermschoice'])) : '');
212 Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
213 info(DI::l10n()->t('Settings updated.'). EOL);