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