]> git.mxchange.org Git - friendica-addons.git/blob - leistungsschutzrecht/leistungsschutzrecht.php
Merge pull request #556 from tobiasd/20180321-nl
[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\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         if (!leistungsschutzrecht_is_member_site($siteinfo["url"]))
30                 return;
31
32         //$siteinfo["title"] = $siteinfo["url"];
33         $siteinfo["text"] = leistungsschutzrecht_cuttext($siteinfo["text"]);
34         unset($siteinfo["image"]);
35         unset($siteinfo["images"]);
36         unset($siteinfo["keywords"]);
37 }
38
39 function leistungsschutzrecht_cuttext($text) {
40         $text = str_replace(["\r", "\n"], [" ", " "], $text);
41
42         do {
43                 $oldtext = $text;
44                 $text = str_replace("  ", " ", $text);
45         } while ($oldtext != $text);
46
47         $words = explode(" ", $text);
48
49         $text = "";
50         $count = 0;
51         $limit = 7;
52
53         foreach ($words as $word) {
54                 if ($text != "")
55                         $text .= " ";
56
57                 $text .= $word;
58
59                 if (++$count >= $limit) {
60                         if (sizeof($words) > $limit)
61                                 $text .= " ...";
62
63                         break;
64                 }
65         }
66         return $text;
67 }
68
69 function leistungsschutzrecht_fetchsites()
70 {
71         // This list works - but question is how current it is
72         $url = "http://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt";
73         $sitelist = Network::fetchUrl($url);
74         $siteurls = explode(',', $sitelist);
75
76         $whitelist = ['tagesschau.de', 'heute.de', 'wdr.de'];
77
78         $sites = [];
79         foreach ($siteurls as $site) {
80                 if (!in_array($site, $whitelist)) {
81                         $sites[$site] = $site;
82                 }
83         }
84
85         // I would prefer parsing the list from the original site, but I haven't found a list.
86         // The following stays here to possibly reenable it in the future without having to reinvent the wheel completely.
87 /*
88         $sites = array();
89
90         $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
91
92         $site = Network::fetchUrl($url);
93
94         $doc = new DOMDocument();
95         @$doc->loadHTML($site);
96
97         $xpath = new DomXPath($doc);
98         $list = $xpath->query("//td/a");
99         foreach ($list as $node) {
100                 $attr = array();
101                 if ($node->attributes->length)
102                         foreach ($node->attributes as $attribute)
103                                 $attr[$attribute->name] = $attribute->value;
104
105                 if (isset($attr["href"])) {
106                         $urldata = parse_url($attr["href"]);
107
108                         if (isset($urldata["host"]) && !isset($urldata["path"])) {
109                                 $cleanedurlpart = explode("%", $urldata["host"]);
110
111                                 $hostname = explode(".", $cleanedurlpart[0]);
112                                 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
113                                 $sites[$site] = $site;
114                         }
115                 }
116         }
117 */
118
119         if (sizeof($sites)) {
120                 Config::set('leistungsschutzrecht','sites',$sites);
121         }
122 }
123
124 function leistungsschutzrecht_is_member_site($url) {
125         $sites = Config::get('leistungsschutzrecht','sites');
126
127         if ($sites == "")
128                 return(false);
129
130         if (sizeof($sites) == 0)
131                 return(false);
132
133         $urldata = parse_url($url);
134
135         if (!isset($urldata["host"]))
136                 return(false);
137
138         $cleanedurlpart = explode("%", $urldata["host"]);
139
140         $hostname = explode(".", $cleanedurlpart[0]);
141         $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
142
143         return (isset($sites[$site]));
144 }
145
146 function leistungsschutzrecht_cron($a,$b) {
147         $last = Config::get('leistungsschutzrecht','last_poll');
148
149         if($last) {
150                 $next = $last + 86400;
151                 if($next > time()) {
152                         logger('poll intervall not reached');
153                         return;
154                 }
155         }
156         leistungsschutzrecht_fetchsites();
157         Config::set('leistungsschutzrecht','last_poll', time());
158 }
159 ?>