Merge pull request #717 from MrPetovan/task/advancedcontentfilter-suppress-evaluation...
[friendica-addons.git/.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 use Friendica\Core\Addon;
9 use Friendica\Core\Config;
10 use Friendica\Util\Network;
11
12 function leistungsschutzrecht_install() {
13         Addon::registerHook('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
14         Addon::registerHook('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
15         Addon::registerHook('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
16 }
17
18
19 function leistungsschutzrecht_uninstall() {
20         Addon::unregisterHook('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
21         Addon::unregisterHook('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
22         Addon::unregisterHook('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
23 }
24
25 function leistungsschutzrecht_getsiteinfo($a, &$siteinfo) {
26         if (!isset($siteinfo["url"])) {
27                 return;
28         }
29
30         if (!leistungsschutzrecht_is_member_site($siteinfo["url"])) {
31                 return;
32         }
33
34         //$siteinfo["title"] = $siteinfo["url"];
35
36         if (!empty($siteinfo["text"])) {
37                 $siteinfo["text"] = leistungsschutzrecht_cuttext($siteinfo["text"]);
38         }
39
40         unset($siteinfo["image"]);
41         unset($siteinfo["images"]);
42         unset($siteinfo["keywords"]);
43 }
44
45 function leistungsschutzrecht_cuttext($text) {
46         $text = str_replace(["\r", "\n"], [" ", " "], $text);
47
48         do {
49                 $oldtext = $text;
50                 $text = str_replace("  ", " ", $text);
51         } while ($oldtext != $text);
52
53         $words = explode(" ", $text);
54
55         $text = "";
56         $count = 0;
57         $limit = 7;
58
59         foreach ($words as $word) {
60                 if ($text != "")
61                         $text .= " ";
62
63                 $text .= $word;
64
65                 if (++$count >= $limit) {
66                         if (sizeof($words) > $limit)
67                                 $text .= " ...";
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 = "http://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt";
79         $sitelist = Network::fetchUrl($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                 Config::set('leistungsschutzrecht','sites',$sites);
127         }
128 }
129
130 function leistungsschutzrecht_is_member_site($url) {
131         $sites = Config::get('leistungsschutzrecht','sites');
132
133         if ($sites == "")
134                 return(false);
135
136         if (sizeof($sites) == 0)
137                 return(false);
138
139         $urldata = parse_url($url);
140
141         if (!isset($urldata["host"]))
142                 return(false);
143
144         $cleanedurlpart = explode("%", $urldata["host"]);
145
146         $hostname = explode(".", $cleanedurlpart[0]);
147         if (empty($hostname)) {
148                 return false;
149         }
150
151         if (count($hostname) <= 2) {
152                 return false;
153         }
154
155         $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
156
157         return (isset($sites[$site]));
158 }
159
160 function leistungsschutzrecht_cron($a,$b) {
161         $last = Config::get('leistungsschutzrecht','last_poll');
162
163         if($last) {
164                 $next = $last + 86400;
165                 if($next > time()) {
166                         logger('poll intervall not reached');
167                         return;
168                 }
169         }
170         leistungsschutzrecht_fetchsites();
171         Config::set('leistungsschutzrecht','last_poll', time());
172 }
173 ?>