3 * Name: Leistungsschutzrecht
4 * Description: Only useful in germany: Remove data from snippets from members of the VG Media
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
9 use Friendica\Core\Hook;
10 use Friendica\Core\Logger;
13 function leistungsschutzrecht_install() {
14 Hook::register('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
15 Hook::register('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
16 Hook::register('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
19 function leistungsschutzrecht_getsiteinfo($a, &$siteinfo) {
20 if (!isset($siteinfo["url"]) || empty($siteinfo['type'])) {
24 // Avoid any third party pictures, to avoid copyright issues
25 if (!in_array($siteinfo['type'], ['photo', 'video']) && DI::config()->get('leistungsschutzrecht', 'suppress_photos', false)) {
26 unset($siteinfo["image"]);
27 unset($siteinfo["images"]);
30 if (!leistungsschutzrecht_is_member_site($siteinfo["url"])) {
34 if (!empty($siteinfo["text"])) {
35 $siteinfo["text"] = leistungsschutzrecht_cuttext($siteinfo["text"]);
38 unset($siteinfo["keywords"]);
41 function leistungsschutzrecht_cuttext($text) {
42 $text = str_replace(["\r", "\n"], [" ", " "], $text);
46 $text = str_replace(" ", " ", $text);
47 } while ($oldtext != $text);
49 $words = explode(" ", $text);
55 foreach ($words as $word) {
61 if (++$count >= $limit) {
62 if (sizeof($words) > $limit)
71 function leistungsschutzrecht_fetchsites()
73 // This list works - but question is how current it is
74 $url = "http://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt";
75 $sitelist = DI::httpClient()->fetch($url);
76 $siteurls = explode(',', $sitelist);
78 $whitelist = ['tagesschau.de', 'heute.de', 'wdr.de'];
81 foreach ($siteurls as $site) {
82 if (!in_array($site, $whitelist)) {
83 $sites[$site] = $site;
87 // I would prefer parsing the list from the original site, but I haven't found a list.
88 // The following stays here to possibly reenable it in the future without having to reinvent the wheel completely.
92 $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
94 $site = Network::fetchUrl($url);
96 $doc = new DOMDocument();
97 @$doc->loadHTML($site);
99 $xpath = new DomXPath($doc);
100 $list = $xpath->query("//td/a");
101 foreach ($list as $node) {
103 if ($node->attributes->length)
104 foreach ($node->attributes as $attribute)
105 $attr[$attribute->name] = $attribute->value;
107 if (isset($attr["href"])) {
108 $urldata = parse_url($attr["href"]);
110 if (isset($urldata["host"]) && !isset($urldata["path"])) {
111 $cleanedurlpart = explode("%", $urldata["host"]);
113 $hostname = explode(".", $cleanedurlpart[0]);
114 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
115 $sites[$site] = $site;
121 if (sizeof($sites)) {
122 DI::config()->set('leistungsschutzrecht','sites',$sites);
126 function leistungsschutzrecht_is_member_site($url) {
127 $sites = DI::config()->get('leistungsschutzrecht','sites');
132 if (sizeof($sites) == 0)
135 $urldata = parse_url($url);
137 if (!isset($urldata["host"]))
140 $cleanedurlpart = explode("%", $urldata["host"]);
142 $hostname = explode(".", $cleanedurlpart[0]);
143 if (empty($hostname)) {
147 if (count($hostname) <= 2) {
151 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
153 return (isset($sites[$site]));
156 function leistungsschutzrecht_cron($a,$b) {
157 $last = DI::config()->get('leistungsschutzrecht','last_poll');
160 $next = $last + 86400;
162 Logger::log('poll intervall not reached');
166 leistungsschutzrecht_fetchsites();
167 DI::config()->set('leistungsschutzrecht','last_poll', time());