]> git.mxchange.org Git - friendica.git/blob - include/Scrape.php
add nicknames to contact records (going forward and retroactive)
[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                 if($x === 'lrdd') {
37                         $decoded = urldecode($item->getAttribute('href'));
38                         if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
39                                 $ret['nick'] = $matches[1];
40                 }
41         }
42
43         // Pull out hCard profile elements
44
45         $items = $dom->getElementsByTagName('*');
46         foreach($items as $item) {
47                 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
48                         $level2 = $item->getElementsByTagName('*');
49                         foreach($level2 as $x) {
50                                 if(attribute_contains($x->getAttribute('class'),'fn'))
51                                         $ret['fn'] = $x->textContent;
52                                 if(attribute_contains($x->getAttribute('class'),'photo'))
53                                         $ret['photo'] = $x->getAttribute('src');
54                                 if(attribute_contains($x->getAttribute('class'),'key'))
55                                         $ret['key'] = $x->textContent;
56                         }
57                 }
58         }
59
60         return $ret;
61 }}
62
63
64
65
66
67
68 if(! function_exists('validate_dfrn')) {
69 function validate_dfrn($a) {
70         $errors = 0;
71         if(! x($a,'key'))
72                 $errors ++;
73         if(! x($a,'dfrn-request'))
74                 $errors ++;
75         if(! x($a,'dfrn-confirm'))
76                 $errors ++;
77         if(! x($a,'dfrn-notify'))
78                 $errors ++;
79         if(! x($a,'dfrn-poll'))
80                 $errors ++;
81         return $errors;
82 }}
83
84 if(! function_exists('scrape_meta')) {
85 function scrape_meta($url) {
86
87         $ret = array();
88         $s = fetch_url($url);
89
90         if(! $s) 
91                 return $ret;
92
93         $dom = HTML5_Parser::parse($s);
94
95         if(! $dom)
96                 return $ret;
97
98         $items = $dom->getElementsByTagName('meta');
99
100         // get DFRN link elements
101
102         foreach($items as $item) {
103                 $x = $item->getAttribute('name');
104                 if(substr($x,0,5) == "dfrn-")
105                         $ret[$x] = $item->getAttribute('content');
106         }
107
108         return $ret;
109 }}