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.

169 lines
4.2 KiB

  1. <?php
  2. /**
  3. * Name: Leistungsschutzrecht
  4. * Description: Only useful in germany: Remove data from snippets from members of the VG Media
  5. * Version: 0.1
  6. * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
  7. */
  8. use Friendica\Core\Hook;
  9. use Friendica\Core\Logger;
  10. use Friendica\DI;
  11. function leistungsschutzrecht_install() {
  12. Hook::register('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
  13. Hook::register('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
  14. Hook::register('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
  15. }
  16. function leistungsschutzrecht_getsiteinfo($a, &$siteinfo) {
  17. if (!isset($siteinfo["url"]) || empty($siteinfo['type'])) {
  18. return;
  19. }
  20. // Avoid any third party pictures, to avoid copyright issues
  21. if (!in_array($siteinfo['type'], ['photo', 'video']) && DI::config()->get('leistungsschutzrecht', 'suppress_photos', false)) {
  22. unset($siteinfo["image"]);
  23. unset($siteinfo["images"]);
  24. }
  25. if (!leistungsschutzrecht_is_member_site($siteinfo["url"])) {
  26. return;
  27. }
  28. if (!empty($siteinfo["text"])) {
  29. $siteinfo["text"] = leistungsschutzrecht_cuttext($siteinfo["text"]);
  30. }
  31. unset($siteinfo["keywords"]);
  32. }
  33. function leistungsschutzrecht_cuttext($text) {
  34. $text = str_replace(["\r", "\n"], [" ", " "], $text);
  35. do {
  36. $oldtext = $text;
  37. $text = str_replace(" ", " ", $text);
  38. } while ($oldtext != $text);
  39. $words = explode(" ", $text);
  40. $text = "";
  41. $count = 0;
  42. $limit = 7;
  43. foreach ($words as $word) {
  44. if ($text != "")
  45. $text .= " ";
  46. $text .= $word;
  47. if (++$count >= $limit) {
  48. if (sizeof($words) > $limit)
  49. $text .= " ...";
  50. break;
  51. }
  52. }
  53. return $text;
  54. }
  55. function leistungsschutzrecht_fetchsites()
  56. {
  57. // This list works - but question is how current it is
  58. $url = "http://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt";
  59. $sitelist = DI::httpRequest()->fetch($url);
  60. $siteurls = explode(',', $sitelist);
  61. $whitelist = ['tagesschau.de', 'heute.de', 'wdr.de'];
  62. $sites = [];
  63. foreach ($siteurls as $site) {
  64. if (!in_array($site, $whitelist)) {
  65. $sites[$site] = $site;
  66. }
  67. }
  68. // I would prefer parsing the list from the original site, but I haven't found a list.
  69. // The following stays here to possibly reenable it in the future without having to reinvent the wheel completely.
  70. /*
  71. $sites = array();
  72. $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
  73. $site = Network::fetchUrl($url);
  74. $doc = new DOMDocument();
  75. @$doc->loadHTML($site);
  76. $xpath = new DomXPath($doc);
  77. $list = $xpath->query("//td/a");
  78. foreach ($list as $node) {
  79. $attr = array();
  80. if ($node->attributes->length)
  81. foreach ($node->attributes as $attribute)
  82. $attr[$attribute->name] = $attribute->value;
  83. if (isset($attr["href"])) {
  84. $urldata = parse_url($attr["href"]);
  85. if (isset($urldata["host"]) && !isset($urldata["path"])) {
  86. $cleanedurlpart = explode("%", $urldata["host"]);
  87. $hostname = explode(".", $cleanedurlpart[0]);
  88. $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
  89. $sites[$site] = $site;
  90. }
  91. }
  92. }
  93. */
  94. if (sizeof($sites)) {
  95. DI::config()->set('leistungsschutzrecht','sites',$sites);
  96. }
  97. }
  98. function leistungsschutzrecht_is_member_site($url) {
  99. $sites = DI::config()->get('leistungsschutzrecht','sites');
  100. if ($sites == "")
  101. return(false);
  102. if (sizeof($sites) == 0)
  103. return(false);
  104. $urldata = parse_url($url);
  105. if (!isset($urldata["host"]))
  106. return(false);
  107. $cleanedurlpart = explode("%", $urldata["host"]);
  108. $hostname = explode(".", $cleanedurlpart[0]);
  109. if (empty($hostname)) {
  110. return false;
  111. }
  112. if (count($hostname) <= 2) {
  113. return false;
  114. }
  115. $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
  116. return (isset($sites[$site]));
  117. }
  118. function leistungsschutzrecht_cron($a,$b) {
  119. $last = DI::config()->get('leistungsschutzrecht','last_poll');
  120. if($last) {
  121. $next = $last + 86400;
  122. if($next > time()) {
  123. Logger::log('poll intervall not reached');
  124. return;
  125. }
  126. }
  127. leistungsschutzrecht_fetchsites();
  128. DI::config()->set('leistungsschutzrecht','last_poll', time());
  129. }
  130. ?>