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>
8 use Friendica\Core\Hook;
9 use Friendica\Core\Logger;
11 use Friendica\Util\Network;
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');
20 function leistungsschutzrecht_uninstall() {
21 Hook::unregister('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
22 Hook::unregister('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
23 Hook::unregister('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
26 function leistungsschutzrecht_getsiteinfo($a, &$siteinfo) {
27 if (!isset($siteinfo["url"]) || empty($siteinfo['type'])) {
31 // Avoid any third party pictures, to avoid copyright issues
32 if (!in_array($siteinfo['type'], ['photo', 'video']) && DI::config()->get('leistungsschutzrecht', 'suppress_photos', false)) {
33 unset($siteinfo["image"]);
34 unset($siteinfo["images"]);
37 if (!leistungsschutzrecht_is_member_site($siteinfo["url"])) {
41 if (!empty($siteinfo["text"])) {
42 $siteinfo["text"] = leistungsschutzrecht_cuttext($siteinfo["text"]);
45 unset($siteinfo["keywords"]);
48 function leistungsschutzrecht_cuttext($text) {
49 $text = str_replace(["\r", "\n"], [" ", " "], $text);
53 $text = str_replace(" ", " ", $text);
54 } while ($oldtext != $text);
56 $words = explode(" ", $text);
62 foreach ($words as $word) {
68 if (++$count >= $limit) {
69 if (sizeof($words) > $limit)
78 function leistungsschutzrecht_fetchsites()
80 // This list works - but question is how current it is
81 $url = "http://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt";
82 $sitelist = Network::fetchUrl($url);
83 $siteurls = explode(',', $sitelist);
85 $whitelist = ['tagesschau.de', 'heute.de', 'wdr.de'];
88 foreach ($siteurls as $site) {
89 if (!in_array($site, $whitelist)) {
90 $sites[$site] = $site;
94 // I would prefer parsing the list from the original site, but I haven't found a list.
95 // The following stays here to possibly reenable it in the future without having to reinvent the wheel completely.
99 $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
101 $site = Network::fetchUrl($url);
103 $doc = new DOMDocument();
104 @$doc->loadHTML($site);
106 $xpath = new DomXPath($doc);
107 $list = $xpath->query("//td/a");
108 foreach ($list as $node) {
110 if ($node->attributes->length)
111 foreach ($node->attributes as $attribute)
112 $attr[$attribute->name] = $attribute->value;
114 if (isset($attr["href"])) {
115 $urldata = parse_url($attr["href"]);
117 if (isset($urldata["host"]) && !isset($urldata["path"])) {
118 $cleanedurlpart = explode("%", $urldata["host"]);
120 $hostname = explode(".", $cleanedurlpart[0]);
121 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
122 $sites[$site] = $site;
128 if (sizeof($sites)) {
129 DI::config()->set('leistungsschutzrecht','sites',$sites);
133 function leistungsschutzrecht_is_member_site($url) {
134 $sites = DI::config()->get('leistungsschutzrecht','sites');
139 if (sizeof($sites) == 0)
142 $urldata = parse_url($url);
144 if (!isset($urldata["host"]))
147 $cleanedurlpart = explode("%", $urldata["host"]);
149 $hostname = explode(".", $cleanedurlpart[0]);
150 if (empty($hostname)) {
154 if (count($hostname) <= 2) {
158 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
160 return (isset($sites[$site]));
163 function leistungsschutzrecht_cron($a,$b) {
164 $last = DI::config()->get('leistungsschutzrecht','last_poll');
167 $next = $last + 86400;
169 Logger::log('poll intervall not reached');
173 leistungsschutzrecht_fetchsites();
174 DI::config()->set('leistungsschutzrecht','last_poll', time());