move to Renderer class
[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\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\Renderer;
15 use Friendica\Database\DBA;
16
17 function remote_permissions_install() {
18         Addon::registerHook('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
19         Addon::registerHook('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
20         Addon::registerHook('addon_settings_post', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings_post');
21 }
22
23 function remote_permissions_uninstall() {
24         Addon::unregisterHook('lockview_content', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_content');
25         Addon::unregisterHook('addon_settings', 'addon/remote_permissions/remote_permissions.php', 'remote_permissions_settings');
26         Addon::unregisterHook('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         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/remote_permissions/settings.css' . '" media="all" />' . "\r\n";
41
42         /* Get the current state of our config variable */
43
44         $remote_perms = 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' => L10n::t('Remote Permissions Settings'),
52                 '$remote_perms_label' => 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' => L10n::t('Save Settings')
55         ]);
56
57 }
58
59 function remote_permissions_settings_post($a,$post) {
60         if(! local_user() || (! x($_POST,'remote-perms-submit')))
61                 return;
62
63         PConfig::set(local_user(),'remote_perms','show',intval($_POST['remote-perms']));
64         info(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 = $a->getBaseURL();
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(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                         $allowed_users = expand_acl($item['allow_cid']);
126                         $allowed_groups = expand_acl($item['allow_gid']);
127                         $deny_users = expand_acl($item['deny_cid']);
128                         $deny_groups = expand_acl($item['deny_gid']);
129
130                         $o = L10n::t('Visible to:') . '<br />';
131                         $allow = [];
132                         $deny = [];
133
134                         if(count($allowed_groups)) {
135                                 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
136                                         DBA::escape(implode(', ', $allowed_groups))
137                                 );
138                                 foreach($r as $rr)
139                                         $allow[] = $rr['contact-id'];
140                         }
141                         $allow = array_unique($allow + $allowed_users);
142
143                         if(count($deny_groups)) {
144                                 $r = q("SELECT DISTINCT `contact-id` FROM group_member WHERE gid IN ( %s )",
145                                         DBA::escape(implode(', ', $deny_groups))
146                                 );
147                                 foreach($r as $rr)
148                                         $deny[] = $rr['contact-id'];
149                         }
150                         $deny = $deny + $deny_users;
151
152                         if($allow)
153                         {
154                                 $r = q("SELECT name FROM contact WHERE id IN ( %s )",
155                                            DBA::escape(implode(', ', array_diff($allow, $deny)))
156                                 );
157                                 foreach($r as $rr)
158                                         $allow_names[] = $rr['name'];
159                         }
160                 }
161                 else {
162                         // We don't have the original post. Let's try for the next best thing:
163                         // checking who else has the post on our own server. Note that comments
164                         // that were sent to Diaspora and were relayed to others on our server
165                         // will have different URIs than the original. We can match the GUID for
166                         // those
167                         $r = q("SELECT `uid` FROM item WHERE uri = '%s' OR guid = '%s'",
168                                    DBA::escape($item_copy['uri']),
169                                DBA::escape($item_copy['guid'])
170                         );
171                         if(! $r)
172                                 return;
173
174                         $allow = [];
175                         foreach($r as $rr)
176                                 $allow[] = $rr['uid'];
177
178                         $r = q("SELECT username FROM user WHERE uid IN ( %s )",
179                                 DBA::escape(implode(', ', $allow))
180                         );
181                         if(! $r)
182                                 return;
183
184                         $o = L10n::t('Visible to') . ' (' . L10n::t('may only be a partial list') . '):<br />';
185
186                         foreach($r as $rr)
187                                 $allow_names[] = $rr['username'];
188                 }
189
190                 // Sort the names alphabetically, case-insensitive
191                 natcasesort($allow_names);
192                 echo $o . implode(', ', $allow_names);
193                 killme();
194         }
195
196         return;
197 }
198
199 function remote_permissions_addon_admin(&$a, &$o){
200         $t = Renderer::getMarkupTemplate( "admin.tpl", "addon/remote_permissions/" );
201         $o = Renderer::replaceMacros($t, [
202                 '$submit' => L10n::t('Save Settings'),
203                 '$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],
204                 '$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]
205         ]);
206 }
207
208 function remote_permissions_addon_admin_post(&$a){
209         $choice =       ((x($_POST,'remotepermschoice'))                ? notags(trim($_POST['remotepermschoice']))     : '');
210         Config::set('remote_perms','global',($choice == 1 ? 1 : 0));
211         info(L10n::t('Settings updated.'). EOL);
212 }