Merge pull request #947 from nupplaphil/task/di_l10n
[friendica-addons.git/.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\Renderer;
13 use Friendica\Database\DBA;
14 use Friendica\DI;
15 use Friendica\Util\Strings;
16
17 function remote_permissions_install() {
18         Hook::register('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
19         Hook::register('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
20         Hook::register('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
21 }
22
23 function remote_permissions_uninstall() {
24         Hook::unregister('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
25         Hook::unregister('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
26         Hook::unregister('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
27 }
28
29 function remote_permissions_settings(&$a,&$o) {
30
31         if(! local_user())
32                 return;
33
34         $global = Config::get("remote_perms", "global");
35         if($global == 1)
36                 return;
37
38         /* Add our stylesheet to the page so we can make our settings look nice */
39
40         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
41
42         /* Get the current state of our config variable */
43
44         $remote_perms = DI::pConfig()->get(local_user(),'remote_perms','show');
45
46         /* Add some HTML to the existing form */
47
48 //      $t = file_get_contents("addon/remote_permissions/settings.tpl" );
49         $t = Renderer::getMarkupTemplate("settings.tpl", "addon/remote_permissions/" );
50         $o .= Renderer::replaceMacros($t, [
51                 '$remote_perms_title' => DI::l10n()->t('Remote Permissions Settings'),
52                 '$remote_perms_label' => DI::l10n()->t('Allow recipients of your private posts to see the other recipients of the posts'),
53                 '$checked' => (($remote_perms == 1) ? 'checked="checked"' : ''),
54                 '$submit' => DI::l10n()->t('Save Settings')
55         ]);
56
57 }
58
59 function remote_permissions_settings_post($a,$post) {
60         if(! local_user() || empty($_POST['remote-perms-submit']))
61                 return;
62
63         DI::pConfig()->set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
64         info(DI::l10n()->t('Remote Permissions settings updated.') . EOL);
65 }
66
67 function remote_permissions_content($a, $item_copy) {
68
69         if($item_copy['uid'] != local_user())
70                 return;
71
72         if(Config::get('remote_perms','global') == 0) {
73                 // Admin has set Individual choice. We need to find
74                 // the original poster. First, get the contact's info
75                 $r = q("SELECT nick, url FROM contact WHERE id = %d LIMIT 1",
76                        intval($item_copy['contact-id'])
77                 );
78                 if(! $r)
79                         return;
80
81                 // Find out if the contact lives here
82                 $baseurl = DI::baseUrl()->get();
83                 $baseurl = substr($baseurl, strpos($baseurl, '://') + 3);
84                 if(strpos($r[0]['url'], $baseurl) === false)
85                         return;
86
87                 // The contact lives here. Get his/her user info
88                 $nick = $r[0]['nick'];
89                 $r = q("SELECT uid FROM user WHERE nickname = '%s' LIMIT 1",
90                        DBA::escape($nick)
91                 );
92                 if(! $r)
93                         return;
94
95                 if(DI::pConfig()->get($r[0]['uid'],'remote_perms','show') == 0)
96                         return;
97         }
98
99         if(($item_copy['private'] == 1) && (! strlen($item_copy['allow_cid'])) && (! strlen($item_copy['allow_gid']))
100                 && (! strlen($item_copy['deny_cid'])) && (! strlen($item_copy['deny_gid']))) {
101
102                 $allow_names = [];
103
104                 // Check for the original post here -- that's the only way
105                 // to definitely get all of the recipients
106
107                 if($item_copy['uri'] === $item_copy['parent-uri']) {
108                         // Lockview for a top-level post
109                         $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s' AND type = 'wall' LIMIT 1",
110                                    DBA::escape($item_copy['uri'])
111                         );
112                 }
113                 else {
114                         // Lockview for a comment
115                         $r = q("SELECT allow_cid, allow_gid, deny_cid, deny_gid FROM item WHERE uri = '%s'
116                                 AND parent = ( SELECT id FROM item WHERE uri = '%s' AND type = 'wall' ) LIMIT 1",
117                                    DBA::escape($item_copy['uri']),
118                                    DBA::escape($item_copy['parent-uri'])
119                         );
120                 }
121                 if($r) {
122
123                         $item = $r[0];
124
125                         $aclFormatter = DI::aclFormatter();
126
127                         $allowed_users = $aclFormatter->expand($item['allow_cid']);
128                         $allowed_groups = $aclFormatter->expand($item['allow_gid']);
129                         $deny_users = $aclFormatter->expand($item['deny_cid']);
130                         $deny_groups = $aclFormatter->expand($item['deny_gid']);
131
132                         $o = DI::l10n()->t('Visible to:') . '<br />';
133                         $allow = [];
134                         $deny = [];
135
136                         if(count($allowed_groups)) {
137                                 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
138                                         DBA::escape(implode(', ', $allowed_groups))
139                                 );
140                                 foreach($r as $rr)
141                                         $allow[] = $rr['contact-id'];
142                         }
143                         $allow = array_unique($allow + $allowed_users);
144
145                         if(count($deny_groups)) {
146                                 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
147                                         DBA::escape(implode(', ', $deny_groups))
148                                 );
149                                 foreach($r as $rr)
150                                         $deny[] = $rr['contact-id'];
151                         }
152                         $deny = $deny + $deny_users;
153
154                         if($allow)
155                         {
156                                 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
157                                            DBA::escape(implode(', ', array_diff($allow, $deny)))
158                                 );
159                                 foreach($r as $rr)
160                                         $allow_names[] = $rr['name'];
161                         }
162                 }
163                 else {
164                         // We don't have the original post. Let's try for the next best thing:
165                         // checking who else has the post on our own server. Note that comments
166                         // that were sent to Diaspora and were relayed to others on our server
167                         // will have different URIs than the original. We can match the GUID for
168                         // those
169                         $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
170                                    DBA::escape($item_copy['uri']),
171                                DBA::escape($item_copy['guid'])
172                         );
173                         if(! $r)
174                                 return;
175
176                         $allow = [];
177                         foreach($r as $rr)
178                                 $allow[] = $rr['uid'];
179
180                         $r = q("SELECT username FROM user WHERE uid IN ( %s )",
181                                 DBA::escape(implode(', ', $allow))
182                         );
183                         if(! $r)
184                                 return;
185
186                         $o = DI::l10n()->t('Visible to') . ' (' . DI::l10n()->t('may only be a partial list') . '):<br />';
187
188                         foreach($r as $rr)
189                                 $allow_names[] = $rr['username'];
190                 }
191
192                 // Sort the names alphabetically, case-insensitive
193                 natcasesort($allow_names);
194                 echo $o . implode(', ', $allow_names);
195                 exit();
196         }
197
198         return;
199 }
200
201 function remote_permissions_addon_admin(&$a, &$o){
202         $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/remote_permissions/" );
203         $o = Renderer::replaceMacros($t, [
204                 '$submit' => DI::l10n()->t('Save Settings'),
205                 '$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],
206                 '$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]
207         ]);
208 }
209
210 function remote_permissions_addon_admin_post(&$a){
211         $choice =       (!empty($_POST['remotepermschoice'])            ? Strings::escapeTags(trim($_POST['remotepermschoice']))        : '');
212         Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
213         info(DI::l10n()->t('Settings updated.'). EOL);
214 }