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