]> git.mxchange.org Git - friendica-addons.git/blob - leistungsschutzrecht/leistungsschutzrecht.php
audon/C/messages.po gelöscht
[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 {
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');
19 }
20
21 function leistungsschutzrecht_getsiteinfo(array &$siteinfo) {
22         if (!isset($siteinfo['url']) || empty($siteinfo['type'])) {
23                 return;
24         }
25
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']);
30         }
31
32         if (!leistungsschutzrecht_is_member_site($siteinfo['url'])) {
33                 return;
34         }
35
36         if (!empty($siteinfo['text'])) {
37                 $siteinfo['text'] = leistungsschutzrecht_cuttext($siteinfo['text']);
38         }
39
40         unset($siteinfo['keywords']);
41 }
42
43 function leistungsschutzrecht_cuttext(string $text): string
44 {
45         $text = str_replace(["\r", "\n"], [' ', ' '], $text);
46
47         do {
48                 $oldtext = $text;
49                 $text = str_replace('  ', ' ', $text);
50         } while ($oldtext != $text);
51
52         $words = explode(' ', $text);
53
54         $text = '';
55         $count = 0;
56         $limit = 7;
57
58         foreach ($words as $word) {
59                 if ($text != '') {
60                         $text .= ' ';
61                 }
62
63                 $text .= $word;
64
65                 if (++$count >= $limit) {
66                         if (sizeof($words) > $limit) {
67                                 $text .= ' ...';
68                         }
69
70                         break;
71                 }
72         }
73         return $text;
74 }
75
76 function leistungsschutzrecht_fetchsites()
77 {
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);
82
83         $whitelist = ['tagesschau.de', 'heute.de', 'wdr.de'];
84
85         $sites = [];
86         foreach ($siteurls as $site) {
87                 if (!in_array($site, $whitelist)) {
88                         $sites[$site] = $site;
89                 }
90         }
91
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.
94 /*
95         $sites = array();
96
97         $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
98
99         $site = Network::fetchUrl($url);
100
101         $doc = new DOMDocument();
102         @$doc->loadHTML($site);
103
104         $xpath = new DomXPath($doc);
105         $list = $xpath->query("//td/a");
106         foreach ($list as $node) {
107                 $attr = array();
108                 if ($node->attributes->length)
109                         foreach ($node->attributes as $attribute)
110                                 $attr[$attribute->name] = $attribute->value;
111
112                 if (isset($attr["href"])) {
113                         $urldata = parse_url($attr["href"]);
114
115                         if (isset($urldata["host"]) && !isset($urldata["path"])) {
116                                 $cleanedurlpart = explode("%", $urldata["host"]);
117
118                                 $hostname = explode(".", $cleanedurlpart[0]);
119                                 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
120                                 $sites[$site] = $site;
121                         }
122                 }
123         }
124 */
125
126         if (sizeof($sites)) {
127                 DI::config()->set('leistungsschutzrecht', 'sites',$sites);
128         }
129 }
130
131 function leistungsschutzrecht_is_member_site(string $url): bool
132 {
133         $sites = DI::config()->get('leistungsschutzrecht', 'sites');
134
135         if ($sites == '') {
136                 return false;
137         }
138
139         if (sizeof($sites) == 0) {
140                 return false;
141         }
142
143         $urldata = parse_url($url);
144
145         if (!isset($urldata['host'])) {
146                 return false;
147         }
148
149         $cleanedurlpart = explode('%', $urldata['host']);
150
151         $hostname = explode('.', $cleanedurlpart[0]);
152         if (empty($hostname)) {
153                 return false;
154         }
155
156         if (count($hostname) <= 2) {
157                 return false;
158         }
159
160         $site = $hostname[sizeof($hostname) - 2] . '.' . $hostname[sizeof($hostname) - 1];
161
162         return isset($sites[$site]);
163 }
164
165 function leistungsschutzrecht_cron($b)
166 {
167         $last = DI::keyValue()->get('leistungsschutzrecht_last_poll');
168
169         if ($last) {
170                 $next = $last + 86400;
171                 if ($next > time()) {
172                         Logger::notice('poll intervall not reached');
173                         return;
174                 }
175         }
176         leistungsschutzrecht_fetchsites();
177         DI::keyValue()->set('leistungsschutzrecht_last_poll', time());
178 }