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