Revert to stable version 3.5.4
[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         $whitelist = array('tagesschau.de', 'heute.de', 'wdr.de');
75
76         $sites = array();
77         foreach ($siteurls AS $site) {
78                 if (!in_array($site, $whitelist)) {
79                         $sites[$site] = $site;
80                 }
81         }
82
83         // I would prefer parsing the list from the original site, but I haven't found a list.
84         // The following stays here to possibly reenable it in the future without having to reinvent the wheel completely.
85 /*
86         $sites = array();
87
88         $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
89
90         $site = fetch_url($url);
91
92         $doc = new DOMDocument();
93         @$doc->loadHTML($site);
94
95         $xpath = new DomXPath($doc);
96         $list = $xpath->query("//td/a");
97         foreach ($list as $node) {
98                 $attr = array();
99                 if ($node->attributes->length)
100                         foreach ($node->attributes as $attribute)
101                                 $attr[$attribute->name] = $attribute->value;
102
103                 if (isset($attr["href"])) {
104                         $urldata = parse_url($attr["href"]);
105
106                         if (isset($urldata["host"]) && !isset($urldata["path"])) {
107                                 $cleanedurlpart = explode("%", $urldata["host"]);
108
109                                 $hostname = explode(".", $cleanedurlpart[0]);
110                                 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
111                                 $sites[$site] = $site;
112                         }
113                 }
114         }
115 */
116
117         if (sizeof($sites)) {
118                 set_config('leistungsschutzrecht','sites',$sites);
119         }
120 }
121
122 function leistungsschutzrecht_is_member_site($url) {
123         $sites = get_config('leistungsschutzrecht','sites');
124
125         if ($sites == "")
126                 return(false);
127
128         if (sizeof($sites) == 0)
129                 return(false);
130
131         $urldata = parse_url($url);
132
133         if (!isset($urldata["host"]))
134                 return(false);
135
136         $cleanedurlpart = explode("%", $urldata["host"]);
137
138         $hostname = explode(".", $cleanedurlpart[0]);
139         $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
140
141         return (isset($sites[$site]));
142 }
143
144 function leistungsschutzrecht_cron($a,$b) {
145         $last = get_config('leistungsschutzrecht','last_poll');
146
147         if($last) {
148                 $next = $last + 86400;
149                 if($next > time()) {
150                         logger('poll intervall not reached');
151                         return;
152                 }
153         }
154         leistungsschutzrecht_fetchsites();
155         set_config('leistungsschutzrecht','last_poll', time());
156 }
157 ?>