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\PConfig;
13 function remote_permissions_install() {
14 register_hook('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
15 register_hook('plugin_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
16 register_hook('plugin_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
19 function remote_permissions_uninstall() {
20 unregister_hook('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
21 unregister_hook('plugin_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
22 unregister_hook('plugin_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
25 function remote_permissions_settings(&$a,&$o) {
30 $global = Config::get("remote_perms", "global");
34 /* Add our stylesheet to the page so we can make our settings look nice */
36 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
38 /* Get the current state of our config variable */
40 $remote_perms = PConfig::get(local_user(),'remote_perms','show');
42 /* Add some HTML to the existing form */
44 // $t = file_get_contents("addon/remote_permissions/settings.tpl" );
45 $t = get_markup_template("settings.tpl", "addon/remote_permissions/" );
46 $o .= replace_macros($t, array(
47 '$remote_perms_title' => t('Remote Permissions Settings'),
48 '$remote_perms_label' => t('Allow recipients of your private posts to see the other recipients of the posts'),
49 '$checked' => (($remote_perms == 1) ? 'checked="checked"' : ''),
50 '$submit' => t('Save Settings')
55 function remote_permissions_settings_post($a,$post) {
56 if(! local_user() || (! x($_POST,'remote-perms-submit')))
59 PConfig::set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
60 info( t('Remote Permissions settings updated.') . EOL);
63 function remote_permissions_content($a, $item_copy) {
65 if($item_copy['uid'] != local_user())
68 if(Config::get('remote_perms','global') == 0) {
69 // Admin has set Individual choice. We need to find
70 // the original poster. First, get the contact's info
71 $r = q("SELECT nick, url FROM contact WHERE id = %d LIMIT 1",
72 intval($item_copy['contact-id'])
77 // Find out if the contact lives here
78 $baseurl = $a->get_baseurl();
79 $baseurl = substr($baseurl, strpos($baseurl, '://') + 3);
80 if(strpos($r[0]['url'], $baseurl) === false)
83 // The contact lives here. Get his/her user info
84 $nick = $r[0]['nick'];
85 $r = q("SELECT uid FROM user WHERE nickname = '%s' LIMIT 1",
91 if(PConfig::get($r[0]['uid'],'remote_perms','show') == 0)
95 if(($item_copy['private'] == 1) && (! strlen($item_copy['allow_cid'])) && (! strlen($item_copy['allow_gid']))
96 && (! strlen($item_copy['deny_cid'])) && (! strlen($item_copy['deny_gid']))) {
98 $allow_names = array();
100 // Check for the original post here -- that's the only way
101 // to definitely get all of the recipients
103 if($item_copy['uri'] === $item_copy['parent-uri']) {
104 // Lockview for a top-level post
105 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s' AND type = 'wall' LIMIT 1",
106 dbesc($item_copy['uri'])
110 // Lockview for a comment
111 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s'
112 AND parent = ( SELECT id FROM item WHERE uri = '%s' AND type = 'wall' ) LIMIT 1",
113 dbesc($item_copy['uri']),
114 dbesc($item_copy['parent-uri'])
121 $allowed_users = expand_acl($item['allow_cid']);
122 $allowed_groups = expand_acl($item['allow_gid']);
123 $deny_users = expand_acl($item['deny_cid']);
124 $deny_groups = expand_acl($item['deny_gid']);
126 $o = t('Visible to:') . '<br />';
130 if(count($allowed_groups)) {
131 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
132 dbesc(implode(', ', $allowed_groups))
135 $allow[] = $rr['contact-id'];
137 $allow = array_unique($allow + $allowed_users);
139 if(count($deny_groups)) {
140 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
141 dbesc(implode(', ', $deny_groups))
144 $deny[] = $rr['contact-id'];
146 $deny = $deny + $deny_users;
150 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
151 dbesc(implode(', ', array_diff($allow, $deny)))
154 $allow_names[] = $rr['name'];
158 // We don't have the original post. Let's try for the next best thing:
159 // checking who else has the post on our own server. Note that comments
160 // that were sent to Diaspora and were relayed to others on our server
161 // will have different URIs than the original. We can match the GUID for
163 $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
164 dbesc($item_copy['uri']),
165 dbesc($item_copy['guid'])
172 $allow[] = $rr['uid'];
174 $r = q("SELECT username FROM user WHERE uid IN ( %s )",
175 dbesc(implode(', ', $allow))
180 $o = t('Visible to') . ' (' . t('may only be a partial list') . '):<br />';
183 $allow_names[] = $rr['username'];
186 // Sort the names alphabetically, case-insensitive
187 natcasesort($allow_names);
188 echo $o . implode(', ', $allow_names);
195 function remote_permissions_plugin_admin(&$a, &$o){
196 $t = get_markup_template( "admin.tpl", "addon/remote_permissions/" );
197 $o = replace_macros($t, array(
198 '$submit' => t('Save Settings'),
199 '$global' => array('remotepermschoice', t('Global'), 1, t('The posts of every user on this server show the post recipients'), Config::get('remote_perms', 'global') == 1),
200 '$individual' => array('remotepermschoice', t('Individual'), 2, t('Each user chooses whether his/her posts show the post recipients'), Config::get('remote_perms', 'global') == 0)
204 function remote_permissions_plugin_admin_post(&$a){
205 $choice = ((x($_POST,'remotepermschoice')) ? notags(trim($_POST['remotepermschoice'])) : '');
206 Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
207 info( t('Settings updated.'). EOL );