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