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