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