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\L10n;
13 use Friendica\Core\Renderer;
14 use Friendica\Database\DBA;
16 use Friendica\Util\Strings;
18 function remote_permissions_install() {
19 Hook::register('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
20 Hook::register('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
21 Hook::register('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
24 function remote_permissions_uninstall() {
25 Hook::unregister('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
26 Hook::unregister('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
27 Hook::unregister('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 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
43 /* Get the current state of our config variable */
45 $remote_perms = DI::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' => DI::l10n()->t('Remote Permissions Settings'),
53 '$remote_perms_label' => DI::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' => DI::l10n()->t('Save Settings')
60 function remote_permissions_settings_post($a,$post) {
61 if(! local_user() || empty($_POST['remote-perms-submit']))
64 DI::pConfig()->set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
65 info(DI::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 = DI::baseUrl()->get();
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(DI::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 $aclFormatter = DI::aclFormatter();
128 $allowed_users = $aclFormatter->expand($item['allow_cid']);
129 $allowed_groups = $aclFormatter->expand($item['allow_gid']);
130 $deny_users = $aclFormatter->expand($item['deny_cid']);
131 $deny_groups = $aclFormatter->expand($item['deny_gid']);
133 $o = DI::l10n()->t('Visible to:') . '<br />';
137 if(count($allowed_groups)) {
138 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
139 DBA::escape(implode(', ', $allowed_groups))
142 $allow[] = $rr['contact-id'];
144 $allow = array_unique($allow + $allowed_users);
146 if(count($deny_groups)) {
147 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
148 DBA::escape(implode(', ', $deny_groups))
151 $deny[] = $rr['contact-id'];
153 $deny = $deny + $deny_users;
157 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
158 DBA::escape(implode(', ', array_diff($allow, $deny)))
161 $allow_names[] = $rr['name'];
165 // We don't have the original post. Let's try for the next best thing:
166 // checking who else has the post on our own server. Note that comments
167 // that were sent to Diaspora and were relayed to others on our server
168 // will have different URIs than the original. We can match the GUID for
170 $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
171 DBA::escape($item_copy['uri']),
172 DBA::escape($item_copy['guid'])
179 $allow[] = $rr['uid'];
181 $r = q("SELECT username FROM user WHERE uid IN ( %s )",
182 DBA::escape(implode(', ', $allow))
187 $o = DI::l10n()->t('Visible to') . ' (' . DI::l10n()->t('may only be a partial list') . '):<br />';
190 $allow_names[] = $rr['username'];
193 // Sort the names alphabetically, case-insensitive
194 natcasesort($allow_names);
195 echo $o . implode(', ', $allow_names);
202 function remote_permissions_addon_admin(&$a, &$o){
203 $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/remote_permissions/" );
204 $o = Renderer::replaceMacros($t, [
205 '$submit' => DI::l10n()->t('Save Settings'),
206 '$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],
207 '$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]
211 function remote_permissions_addon_admin_post(&$a){
212 $choice = (!empty($_POST['remotepermschoice']) ? Strings::escapeTags(trim($_POST['remotepermschoice'])) : '');
213 Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
214 info(DI::l10n()->t('Settings updated.'). EOL);