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\PConfig;
14 use Friendica\Core\Renderer;
15 use Friendica\Database\DBA;
17 use Friendica\Util\Strings;
19 function remote_permissions_install() {
20 Hook::register('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
21 Hook::register('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
22 Hook::register('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
25 function remote_permissions_uninstall() {
26 Hook::unregister('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
27 Hook::unregister('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
28 Hook::unregister('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
31 function remote_permissions_settings(&$a,&$o) {
36 $global = Config::get("remote_perms", "global");
40 /* Add our stylesheet to the page so we can make our settings look nice */
42 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
44 /* Get the current state of our config variable */
46 $remote_perms = DI::pConfig()->get(local_user(),'remote_perms','show');
48 /* Add some HTML to the existing form */
50 // $t = file_get_contents("addon/remote_permissions/settings.tpl" );
51 $t = Renderer::getMarkupTemplate("settings.tpl", "addon/remote_permissions/" );
52 $o .= Renderer::replaceMacros($t, [
53 '$remote_perms_title' => L10n::t('Remote Permissions Settings'),
54 '$remote_perms_label' => L10n::t('Allow recipients of your private posts to see the other recipients of the posts'),
55 '$checked' => (($remote_perms == 1) ? 'checked="checked"' : ''),
56 '$submit' => L10n::t('Save Settings')
61 function remote_permissions_settings_post($a,$post) {
62 if(! local_user() || empty($_POST['remote-perms-submit']))
65 DI::pConfig()->set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
66 info(L10n::t('Remote Permissions settings updated.') . EOL);
69 function remote_permissions_content($a, $item_copy) {
71 if($item_copy['uid'] != local_user())
74 if(Config::get('remote_perms','global') == 0) {
75 // Admin has set Individual choice. We need to find
76 // the original poster. First, get the contact's info
77 $r = q("SELECT nick, url FROM contact WHERE id = %d LIMIT 1",
78 intval($item_copy['contact-id'])
83 // Find out if the contact lives here
84 $baseurl = DI::baseUrl()->get();
85 $baseurl = substr($baseurl, strpos($baseurl, '://') + 3);
86 if(strpos($r[0]['url'], $baseurl) === false)
89 // The contact lives here. Get his/her user info
90 $nick = $r[0]['nick'];
91 $r = q("SELECT uid FROM user WHERE nickname = '%s' LIMIT 1",
97 if(DI::pConfig()->get($r[0]['uid'],'remote_perms','show') == 0)
101 if(($item_copy['private'] == 1) && (! strlen($item_copy['allow_cid'])) && (! strlen($item_copy['allow_gid']))
102 && (! strlen($item_copy['deny_cid'])) && (! strlen($item_copy['deny_gid']))) {
106 // Check for the original post here -- that's the only way
107 // to definitely get all of the recipients
109 if($item_copy['uri'] === $item_copy['parent-uri']) {
110 // Lockview for a top-level post
111 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s' AND type = 'wall' LIMIT 1",
112 DBA::escape($item_copy['uri'])
116 // Lockview for a comment
117 $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s'
118 AND parent = ( SELECT id FROM item WHERE uri = '%s' AND type = 'wall' ) LIMIT 1",
119 DBA::escape($item_copy['uri']),
120 DBA::escape($item_copy['parent-uri'])
127 $aclFormatter = DI::aclFormatter();
129 $allowed_users = $aclFormatter->expand($item['allow_cid']);
130 $allowed_groups = $aclFormatter->expand($item['allow_gid']);
131 $deny_users = $aclFormatter->expand($item['deny_cid']);
132 $deny_groups = $aclFormatter->expand($item['deny_gid']);
134 $o = L10n::t('Visible to:') . '<br />';
138 if(count($allowed_groups)) {
139 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
140 DBA::escape(implode(', ', $allowed_groups))
143 $allow[] = $rr['contact-id'];
145 $allow = array_unique($allow + $allowed_users);
147 if(count($deny_groups)) {
148 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
149 DBA::escape(implode(', ', $deny_groups))
152 $deny[] = $rr['contact-id'];
154 $deny = $deny + $deny_users;
158 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
159 DBA::escape(implode(', ', array_diff($allow, $deny)))
162 $allow_names[] = $rr['name'];
166 // We don't have the original post. Let's try for the next best thing:
167 // checking who else has the post on our own server. Note that comments
168 // that were sent to Diaspora and were relayed to others on our server
169 // will have different URIs than the original. We can match the GUID for
171 $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
172 DBA::escape($item_copy['uri']),
173 DBA::escape($item_copy['guid'])
180 $allow[] = $rr['uid'];
182 $r = q("SELECT username FROM user WHERE uid IN ( %s )",
183 DBA::escape(implode(', ', $allow))
188 $o = L10n::t('Visible to') . ' (' . L10n::t('may only be a partial list') . '):<br />';
191 $allow_names[] = $rr['username'];
194 // Sort the names alphabetically, case-insensitive
195 natcasesort($allow_names);
196 echo $o . implode(', ', $allow_names);
203 function remote_permissions_addon_admin(&$a, &$o){
204 $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/remote_permissions/" );
205 $o = Renderer::replaceMacros($t, [
206 '$submit' => L10n::t('Save Settings'),
207 '$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],
208 '$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]
212 function remote_permissions_addon_admin_post(&$a){
213 $choice = (!empty($_POST['remotepermschoice']) ? Strings::escapeTags(trim($_POST['remotepermschoice'])) : '');
214 Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
215 info(L10n::t('Settings updated.'). EOL);