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\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\Renderer;
15 use Friendica\Database\DBA;
16 use Friendica\Util\Strings;
18 function remote_permissions_install() {
19 Addon::registerHook('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
20 Addon::registerHook('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
21 Addon::registerHook('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
24 function remote_permissions_uninstall() {
25 Addon::unregisterHook('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
26 Addon::unregisterHook('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
27 Addon::unregisterHook('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
30 function remote_permissions_settings(&$a,&$o) {
35 $global = Config::get("remote_perms", "global");
39 /* Add our stylesheet to the page so we can make our settings look nice */
41 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
43 /* Get the current state of our config variable */
45 $remote_perms = PConfig::get(local_user(),'remote_perms','show');
47 /* Add some HTML to the existing form */
49 // $t = file_get_contents("addon/remote_permissions/settings.tpl" );
50 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/remote_permissions/" );
51 $o .= Renderer::replaceMacros($t, [
52 '$remote_perms_title' => L10n::t('Remote Permissions Settings'),
53 '$remote_perms_label' => L10n::t('Allow recipients of your private posts to see the other recipients of the posts'),
54 '$checked' => (($remote_perms == 1) ? 'checked="checked"' : ''),
55 '$submit' => L10n::t('Save Settings')
60 function remote_permissions_settings_post($a,$post) {
61 if(! local_user() || empty($_POST['remote-perms-submit']))
64 PConfig::set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
65 info(L10n::t('Remote Permissions settings updated.') . EOL);
68 function remote_permissions_content($a, $item_copy) {
70 if($item_copy['uid'] != local_user())
73 if(Config::get('remote_perms','global') == 0) {
74 // Admin has set Individual choice. We need to find
75 // the original poster. First, get the contact's info
76 $r = q("SELECT nick, url FROM contact WHERE id = %d LIMIT 1",
77 intval($item_copy['contact-id'])
82 // Find out if the contact lives here
83 $baseurl = $a->getBaseURL();
84 $baseurl = substr($baseurl, strpos($baseurl, '://') + 3);
85 if(strpos($r[0]['url'], $baseurl) === false)
88 // The contact lives here. Get his/her user info
89 $nick = $r[0]['nick'];
90 $r = q("SELECT uid FROM user WHERE nickname = '%s' LIMIT 1",
96 if(PConfig::get($r[0]['uid'],'remote_perms','show') == 0)
100 if(($item_copy['private'] == 1) && (! strlen($item_copy['allow_cid'])) && (! strlen($item_copy['allow_gid']))
101 && (! strlen($item_copy['deny_cid'])) && (! strlen($item_copy['deny_gid']))) {
105 // Check for the original post here -- that's the only way
106 // to definitely get all of the recipients
108 if($item_copy['uri'] === $item_copy['parent-uri']) {
109 // Lockview for a top-level post
110 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s' AND type = 'wall' LIMIT 1",
111 DBA::escape($item_copy['uri'])
115 // Lockview for a comment
116 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s'
117 AND parent = ( SELECT id FROM item WHERE uri = '%s' AND type = 'wall' ) LIMIT 1",
118 DBA::escape($item_copy['uri']),
119 DBA::escape($item_copy['parent-uri'])
126 $allowed_users = expand_acl($item['allow_cid']);
127 $allowed_groups = expand_acl($item['allow_gid']);
128 $deny_users = expand_acl($item['deny_cid']);
129 $deny_groups = expand_acl($item['deny_gid']);
131 $o = 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 = L10n::t('Visible to') . ' (' . 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' => L10n::t('Save Settings'),
204 '$global' => ['remotepermschoice', L10n::t('Global'), 1, L10n::t('The posts of every user on this server show the post recipients'), Config::get('remote_perms', 'global') == 1],
205 '$individual' => ['remotepermschoice', L10n::t('Individual'), 2, L10n::t('Each user chooses whether his/her posts show the post recipients'), 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 Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
212 info(L10n::t('Settings updated.'). EOL);