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>
10 use Friendica\Core\Hook;
11 use Friendica\Core\Logger;
14 function leistungsschutzrecht_install()
16 Hook::register('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
17 Hook::register('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
18 Hook::register('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
21 function leistungsschutzrecht_getsiteinfo(array &$siteinfo) {
22 if (!isset($siteinfo['url']) || empty($siteinfo['type'])) {
26 // Avoid any third party pictures, to avoid copyright issues
27 if (!in_array($siteinfo['type'], ['photo', 'video']) && DI::config()->get('leistungsschutzrecht', 'suppress_photos', false)) {
28 unset($siteinfo['image']);
29 unset($siteinfo['images']);
32 if (!leistungsschutzrecht_is_member_site($siteinfo['url'])) {
36 if (!empty($siteinfo['text'])) {
37 $siteinfo['text'] = leistungsschutzrecht_cuttext($siteinfo['text']);
40 unset($siteinfo['keywords']);
43 function leistungsschutzrecht_cuttext(string $text): string
45 $text = str_replace(["\r", "\n"], [' ', ' '], $text);
49 $text = str_replace(' ', ' ', $text);
50 } while ($oldtext != $text);
52 $words = explode(' ', $text);
58 foreach ($words as $word) {
65 if (++$count >= $limit) {
66 if (sizeof($words) > $limit) {
76 function leistungsschutzrecht_fetchsites()
78 // This list works - but question is how current it is
79 $url = 'https://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt';
80 $sitelist = DI::httpClient()->fetch($url);
81 $siteurls = explode(',', $sitelist);
83 $whitelist = ['tagesschau.de', 'heute.de', 'wdr.de'];
86 foreach ($siteurls as $site) {
87 if (!in_array($site, $whitelist)) {
88 $sites[$site] = $site;
92 // I would prefer parsing the list from the original site, but I haven't found a list.
93 // The following stays here to possibly reenable it in the future without having to reinvent the wheel completely.
97 $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
99 $site = Network::fetchUrl($url);
101 $doc = new DOMDocument();
102 @$doc->loadHTML($site);
104 $xpath = new DomXPath($doc);
105 $list = $xpath->query("//td/a");
106 foreach ($list as $node) {
108 if ($node->attributes->length)
109 foreach ($node->attributes as $attribute)
110 $attr[$attribute->name] = $attribute->value;
112 if (isset($attr["href"])) {
113 $urldata = parse_url($attr["href"]);
115 if (isset($urldata["host"]) && !isset($urldata["path"])) {
116 $cleanedurlpart = explode("%", $urldata["host"]);
118 $hostname = explode(".", $cleanedurlpart[0]);
119 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
120 $sites[$site] = $site;
126 if (sizeof($sites)) {
127 DI::config()->set('leistungsschutzrecht', 'sites',$sites);
131 function leistungsschutzrecht_is_member_site(string $url): bool
133 $sites = DI::config()->get('leistungsschutzrecht', 'sites');
139 if (sizeof($sites) == 0) {
143 $urldata = parse_url($url);
145 if (!isset($urldata['host'])) {
149 $cleanedurlpart = explode('%', $urldata['host']);
151 $hostname = explode('.', $cleanedurlpart[0]);
152 if (empty($hostname)) {
156 if (count($hostname) <= 2) {
160 $site = $hostname[sizeof($hostname) - 2] . '.' . $hostname[sizeof($hostname) - 1];
162 return isset($sites[$site]);
165 function leistungsschutzrecht_cron($b)
167 $last = DI::keyValue()->get('leistungsschutzrecht_last_poll');
170 $next = $last + 86400;
171 if ($next > time()) {
172 Logger::notice('poll intervall not reached');
176 leistungsschutzrecht_fetchsites();
177 DI::keyValue()->set('leistungsschutzrecht_last_poll', time());