]> git.mxchange.org Git - friendica-addons.git/blob - remote_permissions/remote_permissions.php
Update obsolete App::getBaseUrl calls to DI::baseUrl
[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\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;
16 use Friendica\DI;
17 use Friendica\Util\Strings;
18
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');
23 }
24
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');
29 }
30
31 function remote_permissions_settings(&$a,&$o) {
32
33         if(! local_user())
34                 return;
35
36         $global = Config::get("remote_perms", "global");
37         if($global == 1)
38                 return;
39
40         /* Add our stylesheet to the page so we can make our settings look nice */
41
42         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
43
44         /* Get the current state of our config variable */
45
46         $remote_perms = PConfig::get(local_user(),'remote_perms','show');
47
48         /* Add some HTML to the existing form */
49
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')
57         ]);
58
59 }
60
61 function remote_permissions_settings_post($a,$post) {
62         if(! local_user() || empty($_POST['remote-perms-submit']))
63                 return;
64
65         PConfig::set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
66         info(L10n::t('Remote Permissions settings updated.') . EOL);
67 }
68
69 function remote_permissions_content($a, $item_copy) {
70
71         if($item_copy['uid'] != local_user())
72                 return;
73
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'])
79                 );
80                 if(! $r)
81                         return;
82
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)
87                         return;
88
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",
92                        DBA::escape($nick)
93                 );
94                 if(! $r)
95                         return;
96
97                 if(PConfig::get($r[0]['uid'],'remote_perms','show') == 0)
98                         return;
99         }
100
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']))) {
103
104                 $allow_names = [];
105
106                 // Check for the original post here -- that's the only way
107                 // to definitely get all of the recipients
108
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'])
113                         );
114                 }
115                 else {
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'])
121                         );
122                 }
123                 if($r) {
124
125                         $item = $r[0];
126
127                         $aclFormatter = DI::aclFormatter();
128
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']);
133
134                         $o = L10n::t('Visible to:') . '<br />';
135                         $allow = [];
136                         $deny = [];
137
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))
141                                 );
142                                 foreach($r as $rr)
143                                         $allow[] = $rr['contact-id'];
144                         }
145                         $allow = array_unique($allow + $allowed_users);
146
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))
150                                 );
151                                 foreach($r as $rr)
152                                         $deny[] = $rr['contact-id'];
153                         }
154                         $deny = $deny + $deny_users;
155
156                         if($allow)
157                         {
158                                 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
159                                            DBA::escape(implode(', ', array_diff($allow, $deny)))
160                                 );
161                                 foreach($r as $rr)
162                                         $allow_names[] = $rr['name'];
163                         }
164                 }
165                 else {
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
170                         // those
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'])
174                         );
175                         if(! $r)
176                                 return;
177
178                         $allow = [];
179                         foreach($r as $rr)
180                                 $allow[] = $rr['uid'];
181
182                         $r = q("SELECT username FROM user WHERE uid IN ( %s )",
183                                 DBA::escape(implode(', ', $allow))
184                         );
185                         if(! $r)
186                                 return;
187
188                         $o = L10n::t('Visible to') . ' (' . L10n::t('may only be a partial list') . '):<br />';
189
190                         foreach($r as $rr)
191                                 $allow_names[] = $rr['username'];
192                 }
193
194                 // Sort the names alphabetically, case-insensitive
195                 natcasesort($allow_names);
196                 echo $o . implode(', ', $allow_names);
197                 exit();
198         }
199
200         return;
201 }
202
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]
209         ]);
210 }
211
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);
216 }