]> git.mxchange.org Git - friendica.git/blob - include/Scrape.php
our implementation of "aspects" functionally complete
[friendica.git] / include / Scrape.php
1 <?php
2
3 require_once('library/HTML5/Parser.php');
4
5 if(! function_exists('attribute_contains')) {
6 function attribute_contains($attr,$s) {
7         $a = explode(' ', $attr);
8         if(count($a) && in_array($s,$a))
9                 return true;
10         return false;
11 }}
12
13
14 if(! function_exists('scrape_dfrn')) {
15 function scrape_dfrn($url) {
16
17         $ret = array();
18         $s = fetch_url($url);
19
20         if(! $s) 
21                 return $ret;
22
23         $dom = HTML5_Parser::parse($s);
24
25         if(! $dom)
26                 return $ret;
27
28         $items = $dom->getElementsByTagName('link');
29
30         // get DFRN link elements
31
32         foreach($items as $item) {
33                 $x = $item->getAttribute('rel');
34                 if(substr($x,0,5) == "dfrn-")
35                         $ret[$x] = $item->getAttribute('href');
36         }
37
38         // Pull out hCard profile elements
39
40         $items = $dom->getElementsByTagName('*');
41         foreach($items as $item) {
42                 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
43                         $level2 = $item->getElementsByTagName('*');
44                         foreach($level2 as $x) {
45                                 if(attribute_contains($x->getAttribute('class'),'fn'))
46                                         $ret['fn'] = $x->textContent;
47                                 if(attribute_contains($x->getAttribute('class'),'photo'))
48                                         $ret['photo'] = $x->getAttribute('src');
49                                 if(attribute_contains($x->getAttribute('class'),'key'))
50                                         $ret['key'] = $x->textContent;
51                         }
52                 }
53         }
54
55         return $ret;
56 }}
57
58
59
60
61
62
63 if(! function_exists('validate_dfrn')) {
64 function validate_dfrn($a) {
65         $errors = 0;
66         if(! x($a,'key'))
67                 $errors ++;
68         if(! x($a,'dfrn-request'))
69                 $errors ++;
70         if(! x($a,'dfrn-confirm'))
71                 $errors ++;
72         if(! x($a,'dfrn-notify'))
73                 $errors ++;
74         if(! x($a,'dfrn-poll'))
75                 $errors ++;
76         return $errors;
77 }}
78
79 if(! function_exists('scrape_meta')) {
80 function scrape_meta($url) {
81
82         $ret = array();
83         $s = fetch_url($url);
84
85         if(! $s) 
86                 return $ret;
87
88         $dom = HTML5_Parser::parse($s);
89
90         if(! $dom)
91                 return $ret;
92
93         $items = $dom->getElementsByTagName('meta');
94
95         // get DFRN link elements
96
97         foreach($items as $item) {
98                 $x = $item->getAttribute('name');
99                 if(substr($x,0,5) == "dfrn-")
100                         $ret[$x] = $item->getAttribute('content');
101         }
102
103         return $ret;
104 }}