]> git.mxchange.org Git - friendica-addons.git/blob - leistungsschutzrecht/leistungsschutzrecht.php
Merge pull request #1009 from MrPetovan/task/8929-remove-redundant-uninstall
[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\Core\Hook;
10 use Friendica\Core\Logger;
11 use Friendica\DI;
12
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');
17 }
18
19 function leistungsschutzrecht_getsiteinfo($a, &$siteinfo) {
20         if (!isset($siteinfo["url"]) || empty($siteinfo['type'])) {
21                 return;
22         }
23
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"]);
28         }
29
30         if (!leistungsschutzrecht_is_member_site($siteinfo["url"])) {
31                 return;
32         }
33
34         if (!empty($siteinfo["text"])) {
35                 $siteinfo["text"] = leistungsschutzrecht_cuttext($siteinfo["text"]);
36         }
37
38         unset($siteinfo["keywords"]);
39 }
40
41 function leistungsschutzrecht_cuttext($text) {
42         $text = str_replace(["\r", "\n"], [" ", " "], $text);
43
44         do {
45                 $oldtext = $text;
46                 $text = str_replace("  ", " ", $text);
47         } while ($oldtext != $text);
48
49         $words = explode(" ", $text);
50
51         $text = "";
52         $count = 0;
53         $limit = 7;
54
55         foreach ($words as $word) {
56                 if ($text != "")
57                         $text .= " ";
58
59                 $text .= $word;
60
61                 if (++$count >= $limit) {
62                         if (sizeof($words) > $limit)
63                                 $text .= " ...";
64
65                         break;
66                 }
67         }
68         return $text;
69 }
70
71 function leistungsschutzrecht_fetchsites()
72 {
73         // This list works - but question is how current it is
74         $url = "http://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt";
75         $sitelist = DI::httpRequest()->fetch($url);
76         $siteurls = explode(',', $sitelist);
77
78         $whitelist = ['tagesschau.de', 'heute.de', 'wdr.de'];
79
80         $sites = [];
81         foreach ($siteurls as $site) {
82                 if (!in_array($site, $whitelist)) {
83                         $sites[$site] = $site;
84                 }
85         }
86
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.
89 /*
90         $sites = array();
91
92         $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
93
94         $site = Network::fetchUrl($url);
95
96         $doc = new DOMDocument();
97         @$doc->loadHTML($site);
98
99         $xpath = new DomXPath($doc);
100         $list = $xpath->query("//td/a");
101         foreach ($list as $node) {
102                 $attr = array();
103                 if ($node->attributes->length)
104                         foreach ($node->attributes as $attribute)
105                                 $attr[$attribute->name] = $attribute->value;
106
107                 if (isset($attr["href"])) {
108                         $urldata = parse_url($attr["href"]);
109
110                         if (isset($urldata["host"]) && !isset($urldata["path"])) {
111                                 $cleanedurlpart = explode("%", $urldata["host"]);
112
113                                 $hostname = explode(".", $cleanedurlpart[0]);
114                                 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
115                                 $sites[$site] = $site;
116                         }
117                 }
118         }
119 */
120
121         if (sizeof($sites)) {
122                 DI::config()->set('leistungsschutzrecht','sites',$sites);
123         }
124 }
125
126 function leistungsschutzrecht_is_member_site($url) {
127         $sites = DI::config()->get('leistungsschutzrecht','sites');
128
129         if ($sites == "")
130                 return(false);
131
132         if (sizeof($sites) == 0)
133                 return(false);
134
135         $urldata = parse_url($url);
136
137         if (!isset($urldata["host"]))
138                 return(false);
139
140         $cleanedurlpart = explode("%", $urldata["host"]);
141
142         $hostname = explode(".", $cleanedurlpart[0]);
143         if (empty($hostname)) {
144                 return false;
145         }
146
147         if (count($hostname) <= 2) {
148                 return false;
149         }
150
151         $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
152
153         return (isset($sites[$site]));
154 }
155
156 function leistungsschutzrecht_cron($a,$b) {
157         $last = DI::config()->get('leistungsschutzrecht','last_poll');
158
159         if($last) {
160                 $next = $last + 86400;
161                 if($next > time()) {
162                         Logger::log('poll intervall not reached');
163                         return;
164                 }
165         }
166         leistungsschutzrecht_fetchsites();
167         DI::config()->set('leistungsschutzrecht','last_poll', time());
168 }
169 ?>