]> git.mxchange.org Git - friendica-addons.git/blob - leistungsschutzrecht/leistungsschutzrecht.php
Replace and/AND and or/OR by && and ||
[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         $sites = array();
70
71         $url = "http://www.vg-media.de/lizenzen/digitale-verlegerische-angebote/wahrnehmungsberechtigte-digitale-verlegerische-angebote.html";
72
73         $site = fetch_url($url);
74
75         $doc = new DOMDocument();
76         @$doc->loadHTML($site);
77
78         $xpath = new DomXPath($doc);
79         $list = $xpath->query("//td/a");
80         foreach ($list as $node) {
81                 $attr = array();
82                 if ($node->attributes->length)
83                         foreach ($node->attributes as $attribute)
84                                 $attr[$attribute->name] = $attribute->value;
85
86                 if (isset($attr["href"])) {
87                         $urldata = parse_url($attr["href"]);
88
89                         if (isset($urldata["host"]) && !isset($urldata["path"])) {
90                                 $cleanedurlpart = explode("%", $urldata["host"]);
91
92                                 $hostname = explode(".", $cleanedurlpart[0]);
93                                 $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
94                                 $sites[$site] = $site;
95                         }
96                 }
97         }
98
99         if (sizeof($sites)) {
100                 set_config('leistungsschutzrecht','sites',$sites);
101         }
102 }
103
104 function leistungsschutzrecht_is_member_site($url) {
105         $sites = get_config('leistungsschutzrecht','sites');
106
107         if ($sites == "")
108                 return(false);
109
110         if (sizeof($sites) == 0)
111                 return(false);
112
113         $urldata = parse_url($url);
114
115         if (!isset($urldata["host"]))
116                 return(false);
117
118         $cleanedurlpart = explode("%", $urldata["host"]);
119
120         $hostname = explode(".", $cleanedurlpart[0]);
121         $site = $hostname[sizeof($hostname) - 2].".".$hostname[sizeof($hostname) - 1];
122
123         return (isset($sites[$site]));
124 }
125
126 function leistungsschutzrecht_cron($a,$b) {
127         $last = get_config('leistungsschutzrecht','last_poll');
128
129         if($last) {
130                 $next = $last + 86400;
131                 if($next > time()) {
132                         logger('poll intervall not reached');
133                         return;
134                 }
135         }
136         leistungsschutzrecht_fetchsites();
137         set_config('leistungsschutzrecht','last_poll', time());
138 }
139 ?>