Official Addons for the Friendica Communications Platform. (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
6.7 KiB

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