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