]> git.mxchange.org Git - friendica.git/blob - include/Scrape.php
updated poormancron plugin
[friendica.git] / include / Scrape.php
1 <?php
2
3 require_once('library/HTML5/Parser.php');
4
5 if(! function_exists('scrape_dfrn')) {
6 function scrape_dfrn($url) {
7
8         $ret = array();
9         $s = fetch_url($url);
10
11         if(! $s) 
12                 return $ret;
13
14         $dom = HTML5_Parser::parse($s);
15
16         if(! $dom)
17                 return $ret;
18
19         $items = $dom->getElementsByTagName('link');
20
21         // get DFRN link elements
22
23         foreach($items as $item) {
24                 $x = $item->getAttribute('rel');
25                 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
26                         $ret['feed_atom'] = $item->getAttribute('href');
27                 if(substr($x,0,5) == "dfrn-")
28                         $ret[$x] = $item->getAttribute('href');
29                 if($x === 'lrdd') {
30                         $decoded = urldecode($item->getAttribute('href'));
31                         if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
32                                 $ret['nick'] = $matches[1];
33                 }
34         }
35
36         // Pull out hCard profile elements
37
38         $items = $dom->getElementsByTagName('*');
39         foreach($items as $item) {
40                 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
41                         $level2 = $item->getElementsByTagName('*');
42                         foreach($level2 as $x) {
43                                 if(attribute_contains($x->getAttribute('class'),'fn'))
44                                         $ret['fn'] = $x->textContent;
45                                 if(attribute_contains($x->getAttribute('class'),'photo'))
46                                         $ret['photo'] = $x->getAttribute('src');
47                                 if(attribute_contains($x->getAttribute('class'),'key'))
48                                         $ret['key'] = $x->textContent;
49                         }
50                 }
51         }
52
53         return $ret;
54 }}
55
56
57
58
59
60
61 if(! function_exists('validate_dfrn')) {
62 function validate_dfrn($a) {
63         $errors = 0;
64         if(! x($a,'key'))
65                 $errors ++;
66         if(! x($a,'dfrn-request'))
67                 $errors ++;
68         if(! x($a,'dfrn-confirm'))
69                 $errors ++;
70         if(! x($a,'dfrn-notify'))
71                 $errors ++;
72         if(! x($a,'dfrn-poll'))
73                 $errors ++;
74         return $errors;
75 }}
76
77 if(! function_exists('scrape_meta')) {
78 function scrape_meta($url) {
79
80         $ret = array();
81         $s = fetch_url($url);
82
83         if(! $s) 
84                 return $ret;
85
86         $dom = HTML5_Parser::parse($s);
87
88         if(! $dom)
89                 return $ret;
90
91         $items = $dom->getElementsByTagName('meta');
92
93         // get DFRN link elements
94
95         foreach($items as $item) {
96                 $x = $item->getAttribute('name');
97                 if(substr($x,0,5) == "dfrn-")
98                         $ret[$x] = $item->getAttribute('content');
99         }
100
101         return $ret;
102 }}
103
104
105 if(! function_exists('scrape_vcard')) {
106 function scrape_vcard($url) {
107
108         $ret = array();
109         $s = fetch_url($url);
110
111         if(! $s) 
112                 return $ret;
113
114         $dom = HTML5_Parser::parse($s);
115
116         if(! $dom)
117                 return $ret;
118
119         // Pull out hCard profile elements
120
121         $items = $dom->getElementsByTagName('*');
122         foreach($items as $item) {
123                 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
124                         $level2 = $item->getElementsByTagName('*');
125                         foreach($level2 as $x) {
126                                 if(attribute_contains($x->getAttribute('class'),'fn'))
127                                         $ret['fn'] = $x->textContent;
128                                 if((attribute_contains($x->getAttribute('class'),'photo'))
129                                         || (attribute_contains($x->getAttribute('class'),'avatar')))
130                                         $ret['photo'] = $x->getAttribute('src');
131                                 if((attribute_contains($x->getAttribute('class'),'nickname'))
132                                         || (attribute_contains($x->getAttribute('class'),'uid')))
133                                         $ret['nick'] = $x->textContent;
134                         }
135                 }
136         }
137
138         return $ret;
139 }}
140
141
142 if(! function_exists('scrape_feed')) {
143 function scrape_feed($url) {
144
145         $ret = array();
146         $s = fetch_url($url);
147
148         if(! $s) 
149                 return $ret;
150
151         $dom = HTML5_Parser::parse($s);
152
153         if(! $dom)
154                 return $ret;
155
156         $items = $dom->getElementsByTagName('link');
157
158         // get Atom link elements
159
160         foreach($items as $item) {
161                 $x = $item->getAttribute('rel');
162                 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
163                         $ret['feed_atom'] = $item->getAttribute('href');
164         }
165
166         return $ret;
167 }}