]> git.mxchange.org Git - friendica-addons.git/blob - leistungsschutzrecht/leistungsschutzrecht.php
23767f149591f2093366adf250477d8f919fbc78
[friendica-addons.git] / leistungsschutzrecht / leistungsschutzrecht.php
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
9 use Friendica\App;
10 use Friendica\Core\Hook;
11 use Friendica\Core\Logger;
12 use Friendica\DI;
13
14 function leistungsschutzrecht_install() {
15         Hook::register('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
16         Hook::register('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
17         Hook::register('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
18 }
19
20 function leistungsschutzrecht_getsiteinfo(App $a,array &$siteinfo) {
21         if (!isset($siteinfo['url']) || empty($siteinfo['type'])) {
22                 return;
23         }
24
25         // Avoid any third party pictures, to avoid copyright issues
26         if (!in_array($siteinfo['type'], ['photo', 'video']) && DI::config()->get('leistungsschutzrecht', 'suppress_photos', false)) {
27                 unset($siteinfo['image']);
28                 unset($siteinfo['images']);
29         }
30
31         if (!leistungsschutzrecht_is_member_site($siteinfo['url'])) {
32                 return;
33         }
34
35         if (!empty($siteinfo['text'])) {
36                 $siteinfo['text'] = leistungsschutzrecht_cuttext($siteinfo['text']);
37         }
38
39         unset($siteinfo['keywords']);
40 }
41
42 function leistungsschutzrecht_cuttext(string $text): string
43 {
44         $text = str_replace(["\r", "\n"], [' ', ' '], $text);
45
46         do {
47                 $oldtext = $text;
48                 $text = str_replace('  ', ' ', $text);
49         } while ($oldtext != $text);
50
51         $words = explode(' ', $text);
52
53         $text = '';
54         $count = 0;
55         $limit = 7;
56
57         foreach ($words as $word) {
58                 if ($text != '') {
59                         $text .= ' ';
60                 }
61
62                 $text .= $word;
63
64                 if (++$count >= $limit) {
65                         if (sizeof($words) > $limit) {
66                                 $text .= ' ...';
67                         }
68
69                         break;
70                 }
71         }
72         return $text;
73 }
74
75 function leistungsschutzrecht_fetchsites()
76 {
77         // This list works - but question is how current it is
78         $url = 'https://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt';
79         $sitelist = DI::httpClient()->fetch($url);
80         $siteurls = explode(',', $sitelist);
81
82         $whitelist = ['tagesschau.de', 'heute.de', 'wdr.de'];
83
84         $sites = [];
85         foreach ($siteurls as $site) {
86                 if (!in_array($site, $whitelist)) {
87                         $sites[$site] = $site;
88                 }
89         }
90
91         // I would prefer parsing the list from the original site, but I haven't found a list.
92         // The following stays here to possibly reenable it in the future without having to reinvent the wheel completely.
93 /*
94         $sites = array();
95
96         $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
97
98         $site = Network::fetchUrl($url);
99
100         $doc = new DOMDocument();
101         @$doc->loadHTML($site);
102
103         $xpath = new DomXPath($doc);
104         $list = $xpath->query("//td/a");
105         foreach ($list as $node) {
106                 $attr = array();
107                 if ($node->attributes->length)
108                         foreach ($node->attributes as $attribute)
109                                 $attr[$attribute->name] = $attribute->value;
110
111                 if (isset($attr["href"])) {
112                         $urldata = parse_url($attr["href"]);
113
114                         if (isset($urldata["host"]) && !isset($urldata["path"])) {
115                                 $cleanedurlpart = explode("%", $urldata["host"]);
116
117                                 $hostname = explode(".", $cleanedurlpart[0]);
118                                 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
119                                 $sites[$site] = $site;
120                         }
121                 }
122         }
123 */
124
125         if (sizeof($sites)) {
126                 DI::config()->set('leistungsschutzrecht','sites',$sites);
127         }
128 }
129
130 function leistungsschutzrecht_is_member_site(string $url)
131 {
132         $sites = DI::config()->get('leistungsschutzrecht','sites');
133
134         if ($sites == '') {
135                 return false;
136         }
137
138         if (sizeof($sites) == 0) {
139                 return false;
140         }
141
142         $urldata = parse_url($url);
143
144         if (!isset($urldata['host'])) {
145                 return false;
146         }
147
148         $cleanedurlpart = explode('%', $urldata['host']);
149
150         $hostname = explode('.', $cleanedurlpart[0]);
151         if (empty($hostname)) {
152                 return false;
153         }
154
155         if (count($hostname) <= 2) {
156                 return false;
157         }
158
159         $site = $hostname[sizeof($hostname) - 2] . '.' . $hostname[sizeof($hostname) - 1];
160
161         return (isset($sites[$site]));
162 }
163
164 function leistungsschutzrecht_cron(App $a,$b)
165 {
166         $last = DI::config()->get('leistungsschutzrecht', 'last_poll');
167
168         if ($last) {
169                 $next = $last + 86400;
170                 if ($next > time()) {
171                         Logger::notice('poll intervall not reached');
172                         return;
173                 }
174         }
175         leistungsschutzrecht_fetchsites();
176         DI::config()->set('leistungsschutzrecht', 'last_poll', time());
177 }