Use short form array syntax everywhere
[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 use Friendica\Core\Config;
10
11 function leistungsschutzrecht_install() {
12         register_hook('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
13         register_hook('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
14         register_hook('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
15 }
16
17
18 function leistungsschutzrecht_uninstall() {
19         unregister_hook('cron', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_cron');
20         unregister_hook('getsiteinfo', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
21         unregister_hook('page_info_data', 'addon/leistungsschutzrecht/leistungsschutzrecht.php', 'leistungsschutzrecht_getsiteinfo');
22 }
23
24 function leistungsschutzrecht_getsiteinfo($a, &$siteinfo) {
25         if (!isset($siteinfo["url"]))
26                 return;
27
28         if (!leistungsschutzrecht_is_member_site($siteinfo["url"]))
29                 return;
30
31         //$siteinfo["title"] = $siteinfo["url"];
32         $siteinfo["text"] = leistungsschutzrecht_cuttext($siteinfo["text"]);
33         unset($siteinfo["image"]);
34         unset($siteinfo["images"]);
35         unset($siteinfo["keywords"]);
36 }
37
38 function leistungsschutzrecht_cuttext($text) {
39         $text = str_replace(["\r", "\n"], [" ", " "], $text);
40
41         do {
42                 $oldtext = $text;
43                 $text = str_replace("  ", " ", $text);
44         } while ($oldtext != $text);
45
46         $words = explode(" ", $text);
47
48         $text = "";
49         $count = 0;
50         $limit = 7;
51
52         foreach ($words as $word) {
53                 if ($text != "")
54                         $text .= " ";
55
56                 $text .= $word;
57
58                 if (++$count >= $limit) {
59                         if (sizeof($words) > $limit)
60                                 $text .= " ...";
61
62                         break;
63                 }
64         }
65         return $text;
66 }
67
68 function leistungsschutzrecht_fetchsites() {
69         require_once("include/network.php");
70
71         // This list works - but question is how current it is
72         $url = "http://leistungsschutzrecht-stoppen.d-64.org/blacklist.txt";
73         $sitelist = fetch_url($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 = fetch_url($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 ?>