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\BaseObject;
11 use Friendica\Core\Config;
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
16 use Friendica\Database\DBA;
17 use Friendica\Util\ACLFormatter;
18 use Friendica\Util\Strings;
20 function remote_permissions_install() {
21 Hook::register('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
22 Hook::register('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
23 Hook::register('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
26 function remote_permissions_uninstall() {
27 Hook::unregister('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
28 Hook::unregister('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
29 Hook::unregister('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
32 function remote_permissions_settings(&$a,&$o) {
37 $global = Config::get("remote_perms", "global");
41 /* Add our stylesheet to the page so we can make our settings look nice */
43 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
45 /* Get the current state of our config variable */
47 $remote_perms = PConfig::get(local_user(),'remote_perms','show');
49 /* Add some HTML to the existing form */
51 // $t = file_get_contents("addon/remote_permissions/settings.tpl" );
52 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/remote_permissions/" );
53 $o .= Renderer::replaceMacros($t, [
54 '$remote_perms_title' => L10n::t('Remote Permissions Settings'),
55 '$remote_perms_label' => L10n::t('Allow recipients of your private posts to see the other recipients of the posts'),
56 '$checked' => (($remote_perms == 1) ? 'checked="checked"' : ''),
57 '$submit' => L10n::t('Save Settings')
62 function remote_permissions_settings_post($a,$post) {
63 if(! local_user() || empty($_POST['remote-perms-submit']))
66 PConfig::set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
67 info(L10n::t('Remote Permissions settings updated.') . EOL);
70 function remote_permissions_content($a, $item_copy) {
72 if($item_copy['uid'] != local_user())
75 if(Config::get('remote_perms','global') == 0) {
76 // Admin has set Individual choice. We need to find
77 // the original poster. First, get the contact's info
78 $r = q("SELECT nick, url FROM contact WHERE id = %d LIMIT 1",
79 intval($item_copy['contact-id'])
84 // Find out if the contact lives here
85 $baseurl = $a->getBaseURL();
86 $baseurl = substr($baseurl, strpos($baseurl, '://') + 3);
87 if(strpos($r[0]['url'], $baseurl) === false)
90 // The contact lives here. Get his/her user info
91 $nick = $r[0]['nick'];
92 $r = q("SELECT uid FROM user WHERE nickname = '%s' LIMIT 1",
98 if(PConfig::get($r[0]['uid'],'remote_perms','show') == 0)
102 if(($item_copy['private'] == 1) && (! strlen($item_copy['allow_cid'])) && (! strlen($item_copy['allow_gid']))
103 && (! strlen($item_copy['deny_cid'])) && (! strlen($item_copy['deny_gid']))) {
107 // Check for the original post here -- that's the only way
108 // to definitely get all of the recipients
110 if($item_copy['uri'] === $item_copy['parent-uri']) {
111 // Lockview for a top-level post
112 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s' AND type = 'wall' LIMIT 1",
113 DBA::escape($item_copy['uri'])
117 // Lockview for a comment
118 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s'
119 AND parent = ( SELECT id FROM item WHERE uri = '%s' AND type = 'wall' ) LIMIT 1",
120 DBA::escape($item_copy['uri']),
121 DBA::escape($item_copy['parent-uri'])
128 /** @var ACLFormatter $aclFormatter */
129 $aclFormatter = BaseObject::getClass(ACLFormatter::class);
131 $allowed_users = $aclFormatter->expand($item['allow_cid']);
132 $allowed_groups = $aclFormatter->expand($item['allow_gid']);
133 $deny_users = $aclFormatter->expand($item['deny_cid']);
134 $deny_groups = $aclFormatter->expand($item['deny_gid']);
136 $o = L10n::t('Visible to:') . '<br />';
140 if(count($allowed_groups)) {
141 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
142 DBA::escape(implode(', ', $allowed_groups))
145 $allow[] = $rr['contact-id'];
147 $allow = array_unique($allow + $allowed_users);
149 if(count($deny_groups)) {
150 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
151 DBA::escape(implode(', ', $deny_groups))
154 $deny[] = $rr['contact-id'];
156 $deny = $deny + $deny_users;
160 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
161 DBA::escape(implode(', ', array_diff($allow, $deny)))
164 $allow_names[] = $rr['name'];
168 // We don't have the original post. Let's try for the next best thing:
169 // checking who else has the post on our own server. Note that comments
170 // that were sent to Diaspora and were relayed to others on our server
171 // will have different URIs than the original. We can match the GUID for
173 $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
174 DBA::escape($item_copy['uri']),
175 DBA::escape($item_copy['guid'])
182 $allow[] = $rr['uid'];
184 $r = q("SELECT username FROM user WHERE uid IN ( %s )",
185 DBA::escape(implode(', ', $allow))
190 $o = L10n::t('Visible to') . ' (' . L10n::t('may only be a partial list') . '):<br />';
193 $allow_names[] = $rr['username'];
196 // Sort the names alphabetically, case-insensitive
197 natcasesort($allow_names);
198 echo $o . implode(', ', $allow_names);
205 function remote_permissions_addon_admin(&$a, &$o){
206 $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/remote_permissions/" );
207 $o = Renderer::replaceMacros($t, [
208 '$submit' => L10n::t('Save Settings'),
209 '$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],
210 '$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]
214 function remote_permissions_addon_admin_post(&$a){
215 $choice = (!empty($_POST['remotepermschoice']) ? Strings::escapeTags(trim($_POST['remotepermschoice'])) : '');
216 Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
217 info(L10n::t('Settings updated.'). EOL);