]> git.mxchange.org Git - friendica-addons.git/blob - remote_permissions/remote_permissions.php
ad6d7b251ee81173c01eb656c91f87bba28419c5
[friendica-addons.git] / remote_permissions / remote_permissions.php
1 <?php
2 /**
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
5  * Version: 1.0
6  * Author: Zach <https://f.shmuz.in/profile/techcity>
7  * Status: Unsupported
8  */
9
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;
19
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');
24 }
25
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');
30 }
31
32 function remote_permissions_settings(&$a,&$o) {
33
34         if(! local_user())
35                 return;
36
37         $global = Config::get("remote_perms", "global");
38         if($global == 1)
39                 return;
40
41         /* Add our stylesheet to the page so we can make our settings look nice */
42
43         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
44
45         /* Get the current state of our config variable */
46
47         $remote_perms = PConfig::get(local_user(),'remote_perms','show');
48
49         /* Add some HTML to the existing form */
50
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')
58         ]);
59
60 }
61
62 function remote_permissions_settings_post($a,$post) {
63         if(! local_user() || empty($_POST['remote-perms-submit']))
64                 return;
65
66         PConfig::set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
67         info(L10n::t('Remote Permissions settings updated.') . EOL);
68 }
69
70 function remote_permissions_content($a, $item_copy) {
71
72         if($item_copy['uid'] != local_user())
73                 return;
74
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'])
80                 );
81                 if(! $r)
82                         return;
83
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)
88                         return;
89
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",
93                        DBA::escape($nick)
94                 );
95                 if(! $r)
96                         return;
97
98                 if(PConfig::get($r[0]['uid'],'remote_perms','show') == 0)
99                         return;
100         }
101
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']))) {
104
105                 $allow_names = [];
106
107                 // Check for the original post here -- that's the only way
108                 // to definitely get all of the recipients
109
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'])
114                         );
115                 }
116                 else {
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'])
122                         );
123                 }
124                 if($r) {
125
126                         $item = $r[0];
127
128                         /** @var ACLFormatter $aclFormatter */
129                         $aclFormatter = BaseObject::getClass(ACLFormatter::class);
130
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']);
135
136                         $o = L10n::t('Visible to:') . '<br />';
137                         $allow = [];
138                         $deny = [];
139
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))
143                                 );
144                                 foreach($r as $rr)
145                                         $allow[] = $rr['contact-id'];
146                         }
147                         $allow = array_unique($allow + $allowed_users);
148
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))
152                                 );
153                                 foreach($r as $rr)
154                                         $deny[] = $rr['contact-id'];
155                         }
156                         $deny = $deny + $deny_users;
157
158                         if($allow)
159                         {
160                                 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
161                                            DBA::escape(implode(', ', array_diff($allow, $deny)))
162                                 );
163                                 foreach($r as $rr)
164                                         $allow_names[] = $rr['name'];
165                         }
166                 }
167                 else {
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
172                         // those
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'])
176                         );
177                         if(! $r)
178                                 return;
179
180                         $allow = [];
181                         foreach($r as $rr)
182                                 $allow[] = $rr['uid'];
183
184                         $r = q("SELECT username FROM user WHERE uid IN ( %s )",
185                                 DBA::escape(implode(', ', $allow))
186                         );
187                         if(! $r)
188                                 return;
189
190                         $o = L10n::t('Visible to') . ' (' . L10n::t('may only be a partial list') . '):<br />';
191
192                         foreach($r as $rr)
193                                 $allow_names[] = $rr['username'];
194                 }
195
196                 // Sort the names alphabetically, case-insensitive
197                 natcasesort($allow_names);
198                 echo $o . implode(', ', $allow_names);
199                 exit();
200         }
201
202         return;
203 }
204
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]
211         ]);
212 }
213
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);
218 }