]> git.mxchange.org Git - friendica.git/blob - include/Scrape.php
lots of facebook fixes
[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         $a = get_app();
9
10         $ret = array();
11
12         logger('scrape_dfrn: url=' . $url);
13
14         $s = fetch_url($url);
15
16         if(! $s) 
17                 return $ret;
18
19         $headers = $a->get_curl_headers();
20         logger('scrape_dfrn: headers=' . $headers, LOGGER_DEBUG);
21
22
23         $lines = explode("\n",$headers);
24         if(count($lines)) {
25                 foreach($lines as $line) {                              
26                         // don't try and run feeds through the html5 parser
27                         if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
28                                 return ret;
29                 }
30         }
31
32
33         $dom = HTML5_Parser::parse($s);
34
35         if(! $dom)
36                 return $ret;
37
38         $items = $dom->getElementsByTagName('link');
39
40         // get DFRN link elements
41
42         foreach($items as $item) {
43                 $x = $item->getAttribute('rel');
44                 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
45                         $ret['feed_atom'] = $item->getAttribute('href');
46                 if(substr($x,0,5) == "dfrn-")
47                         $ret[$x] = $item->getAttribute('href');
48                 if($x === 'lrdd') {
49                         $decoded = urldecode($item->getAttribute('href'));
50                         if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
51                                 $ret['nick'] = $matches[1];
52                 }
53         }
54
55         // Pull out hCard profile elements
56
57         $items = $dom->getElementsByTagName('*');
58         foreach($items as $item) {
59                 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
60                         $level2 = $item->getElementsByTagName('*');
61                         foreach($level2 as $x) {
62                                 if(attribute_contains($x->getAttribute('class'),'fn'))
63                                         $ret['fn'] = $x->textContent;
64                                 if(attribute_contains($x->getAttribute('class'),'photo'))
65                                         $ret['photo'] = $x->getAttribute('src');
66                                 if(attribute_contains($x->getAttribute('class'),'key'))
67                                         $ret['key'] = $x->textContent;
68                         }
69                 }
70         }
71
72         return $ret;
73 }}
74
75
76
77
78
79
80 if(! function_exists('validate_dfrn')) {
81 function validate_dfrn($a) {
82         $errors = 0;
83         if(! x($a,'key'))
84                 $errors ++;
85         if(! x($a,'dfrn-request'))
86                 $errors ++;
87         if(! x($a,'dfrn-confirm'))
88                 $errors ++;
89         if(! x($a,'dfrn-notify'))
90                 $errors ++;
91         if(! x($a,'dfrn-poll'))
92                 $errors ++;
93         return $errors;
94 }}
95
96 if(! function_exists('scrape_meta')) {
97 function scrape_meta($url) {
98
99         $a = get_app();
100
101         $ret = array();
102
103         logger('scrape_meta: url=' . $url);
104
105         $s = fetch_url($url);
106
107         if(! $s) 
108                 return $ret;
109
110         $headers = $a->get_curl_headers();
111         logger('scrape_meta: headers=' . $headers, LOGGER_DEBUG);
112
113         $lines = explode("\n",$headers);
114         if(count($lines)) {
115                 foreach($lines as $line) {                              
116                         // don't try and run feeds through the html5 parser
117                         if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
118                                 return ret;
119                 }
120         }
121
122
123
124         $dom = HTML5_Parser::parse($s);
125
126         if(! $dom)
127                 return $ret;
128
129         $items = $dom->getElementsByTagName('meta');
130
131         // get DFRN link elements
132
133         foreach($items as $item) {
134                 $x = $item->getAttribute('name');
135                 if(substr($x,0,5) == "dfrn-")
136                         $ret[$x] = $item->getAttribute('content');
137         }
138
139         return $ret;
140 }}
141
142
143 if(! function_exists('scrape_vcard')) {
144 function scrape_vcard($url) {
145
146         $a = get_app();
147
148         $ret = array();
149
150         logger('scrape_vcard: url=' . $url);
151
152         $s = fetch_url($url);
153
154         if(! $s) 
155                 return $ret;
156
157         $headers = $a->get_curl_headers();
158         $lines = explode("\n",$headers);
159         if(count($lines)) {
160                 foreach($lines as $line) {                              
161                         // don't try and run feeds through the html5 parser
162                         if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
163                                 return ret;
164                 }
165         }
166
167         $dom = HTML5_Parser::parse($s);
168
169         if(! $dom)
170                 return $ret;
171
172         // Pull out hCard profile elements
173
174         $items = $dom->getElementsByTagName('*');
175         foreach($items as $item) {
176                 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
177                         $level2 = $item->getElementsByTagName('*');
178                         foreach($level2 as $x) {
179                                 if(attribute_contains($x->getAttribute('class'),'fn'))
180                                         $ret['fn'] = $x->textContent;
181                                 if((attribute_contains($x->getAttribute('class'),'photo'))
182                                         || (attribute_contains($x->getAttribute('class'),'avatar')))
183                                         $ret['photo'] = $x->getAttribute('src');
184                                 if((attribute_contains($x->getAttribute('class'),'nickname'))
185                                         || (attribute_contains($x->getAttribute('class'),'uid')))
186                                         $ret['nick'] = $x->textContent;
187                         }
188                 }
189         }
190
191         return $ret;
192 }}
193
194
195 if(! function_exists('scrape_feed')) {
196 function scrape_feed($url) {
197
198         $a = get_app();
199
200         $ret = array();
201         $s = fetch_url($url);
202
203         if(! $s) 
204                 return $ret;
205
206         $headers = $a->get_curl_headers();
207         logger('scrape_feed: headers=' . $headers, LOGGER_DEBUG);
208
209         $lines = explode("\n",$headers);
210         if(count($lines)) {
211                 foreach($lines as $line) {                              
212                         if(stristr($line,'content-type:')) {
213                                 if(stristr($line,'application/atom+xml') || stristr($s,'<feed')) {
214                                         $ret['feed_atom'] = $url;
215                                         return $ret;
216                                 }
217                                 if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
218                                         $ret['feed_rss'] = $url;
219                                         return $ret;
220                                 }
221                         }
222                 }
223         }
224
225         $dom = HTML5_Parser::parse($s);
226
227         if(! $dom)
228                 return $ret;
229
230
231         $items = $dom->getElementsByTagName('img');
232
233         // get img elements (twitter)
234
235         if($items) {
236                 foreach($items as $item) {
237                         $x = $item->getAttribute('id');
238                         if($x === 'profile-image') {
239                                 $ret['photo'] = $item->getAttribute('src');
240                         }
241                 }
242         }
243
244         $items = $dom->getElementsByTagName('link');
245
246         // get Atom/RSS link elements, take the first one of either.
247
248         if($items) {
249                 foreach($items as $item) {
250                         $x = $item->getAttribute('rel');
251                         if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
252                                 if(! x($ret,'feed_atom'))
253                                         $ret['feed_atom'] = $item->getAttribute('href');
254                         }
255                         if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
256                                 if(! x($ret,'feed_rss'))
257                                         $ret['feed_rss'] = $item->getAttribute('href');
258                         }
259                 }       
260         }
261
262         return $ret;
263 }}
264
265
266 function probe_url($url) {
267         require_once('include/email.php');
268
269         $result = array();
270
271         if(! $url)
272                 return $result;
273
274         $diaspora = false;      
275         $email_conversant = false;
276
277         if($url) {
278                 $links = lrdd($url);
279
280                 if(count($links)) {
281                         foreach($links as $link) {
282                                 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
283                                         $dfrn = unamp($link['@attributes']['href']);
284                                 if($link['@attributes']['rel'] === 'salmon')
285                                         $notify = unamp($link['@attributes']['href']);
286                                 if($link['@attributes']['rel'] === NAMESPACE_FEED)
287                                         $poll = unamp($link['@attributes']['href']);
288                                 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
289                                         $hcard = unamp($link['@attributes']['href']);
290                                 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
291                                         $profile = unamp($link['@attributes']['href']);
292                                 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location')
293                                         $diaspora = true;
294                         }
295
296                         // Status.Net can have more than one profile URL. We need to match the profile URL
297                         // to a contact on incoming messages to prevent spam, and we won't know which one
298                         // to match. So in case of two, one of them is stored as an alias. Only store URL's
299                         // and not webfinger user@host aliases. If they've got more than two non-email style
300                         // aliases, let's hope we're lucky and get one that matches the feed author-uri because 
301                         // otherwise we're screwed.
302
303                         foreach($links as $link) {
304                                 if($link['@attributes']['rel'] === 'alias') {
305                                         if(strpos($link['@attributes']['href'],'@') === false) {
306                                                 if(isset($profile)) {
307                                                         if($link['@attributes']['href'] !== $profile)
308                                                                 $alias = unamp($link['@attributes']['href']);
309                                                 }
310                                                 else
311                                                         $profile = unamp($link['@attributes']['href']);
312                                         }
313                                 }
314                         }
315                 }
316                 else {
317
318                         // Check email
319
320                         $orig_url = $url;
321                         if((strpos($orig_url,'@')) && validate_email($orig_url)) {
322                                 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
323                                         intval(local_user())
324                                 );
325                                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
326                                         intval(local_user())
327                                 );
328                                 if(count($x) && count($r)) {
329                                     $mailbox = construct_mailbox_name($r[0]);
330                                         $password = '';
331                                         openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
332                                         $mbox = email_connect($mailbox,$r[0]['user'],$password);
333                                         unset($password);
334                                 }
335                                 if($mbox) {
336                                         $msgs = email_poll($mbox,$orig_url);
337                                         if(count($msgs)) {
338                                                 $addr = $orig_url;
339                                                 $network = NETWORK_MAIL;
340                                                 $name = substr($url,0,strpos($url,'@'));
341                                                 $profile = 'http://' . substr($url,strpos($url,'@')+1);
342                                                 // fix nick character range
343                                                 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
344                                                 $notify = 'smtp ' . random_string();
345                                                 $poll = 'email ' . random_string();
346                                                 $priority = 0;
347                                                 $x = email_msg_meta($mbox,$msgs[0]);
348                                                 $adr = imap_rfc822_parse_adrlist($x->from,'');
349                                                 if(strlen($adr[0]->personal))
350                                                         $vcard['fn'] = notags($adr[0]->personal);
351                                         }
352                                         imap_close($mbox);
353                                 }
354                         }
355                 }
356         }       
357
358         if(strlen($dfrn)) {
359                 $ret = scrape_dfrn($dfrn);
360                 if(is_array($ret) && x($ret,'dfrn-request')) {
361                         $network = NETWORK_DFRN;
362                         $request = $ret['dfrn-request'];
363                         $confirm = $ret['dfrn-confirm'];
364                         $notify  = $ret['dfrn-notify'];
365                         $poll    = $ret['dfrn-poll'];
366                 }
367         }
368
369         if($network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
370                 $network  = NETWORK_OSTATUS;
371                 $priority = 0;
372
373                 if($hcard) {
374                         $vcard = scrape_vcard($hcard);
375
376                         // Google doesn't use absolute url in profile photos
377         
378                         if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
379                                 $h = @parse_url($hcard);
380                                 if($h)
381                                         $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
382                         }
383                 
384                         logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
385                 }
386
387                 if(! $profile) {
388                         if($diaspora)
389                                 $profile = $hcard;
390                         else
391                                 $profile = $url;
392                 }
393
394                 if(! x($vcard,'fn'))
395                         if(x($vcard,'nick'))
396                                 $vcard['fn'] = $vcard['nick'];
397
398                 if((! isset($vcard)) && (! $poll)) {
399
400                         $ret = scrape_feed($url);
401                         logger('probe_url: scrape_feed returns: ' . print_r($ret,true), LOGGER_DATA);
402                         if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
403                                 $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
404                                 $vcard = array();
405                                         if(x($ret,'photo'))
406                                                 $vcard['photo'] = $ret['photo'];
407                                 require_once('simplepie/simplepie.inc');
408                             $feed = new SimplePie();
409                                 $xml = fetch_url($poll);
410
411                         $feed->set_raw_data($xml);
412
413                             $feed->init();
414
415                                 if(! x($vcard,'photo'))
416                                         $vcard['photo'] = $feed->get_image_url();
417                                 $author = $feed->get_author();
418                                 if($author) {                   
419                                         $vcard['fn'] = unxmlify(trim($author->get_name()));
420                                         if(! $vcard['fn'])
421                                                 $vcard['fn'] = trim(unxmlify($author->get_email()));
422                                         if(strpos($vcard['fn'],'@') !== false)
423                                                 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
424                                         $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
425                                         if(strpos($vcard['nick'],' '))
426                                                 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
427                                         $email = unxmlify($author->get_email());
428                                 }
429                                 else {
430                                         $item = $feed->get_item(0);
431                                         if($item) {
432                                                 $author = $item->get_author();
433                                                 if($author) {                   
434                                                         $vcard['fn'] = trim(unxmlify($author->get_name()));
435                                                         if(! $vcard['fn'])
436                                                                 $vcard['fn'] = trim(unxmlify($author->get_email()));
437                                                         if(strpos($vcard['fn'],'@') !== false)
438                                                                 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
439                                                         $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
440                                                         if(strpos($vcard['nick'],' '))
441                                                                 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
442                                                         $email = unxmlify($author->get_email());
443                                                 }
444                                                 if(! $vcard['photo']) {
445                                                         $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
446                                                         if($rawmedia && $rawmedia[0]['attribs']['']['url'])
447                                                                 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
448                                                 }
449                                         }
450                                 }
451                                 if((! $vcard['photo']) && strlen($email))
452                                         $vcard['photo'] = gravatar_img($email);
453                                 if($poll === $profile)
454                                         $lnk = $feed->get_permalink();
455                                 if(isset($lnk) && strlen($lnk))
456                                         $profile = $lnk;        
457                                 if(! (x($vcard,'fn')))
458                                         $vcard['fn'] = notags($feed->get_title());
459                                 if(! (x($vcard,'fn')))
460                                         $vcard['fn'] = notags($feed->get_description());
461                                 $network = 'feed';
462                                 $priority = 2;
463                         }
464                 }
465         }
466
467         if(! x($vcard,'photo')) {
468                 $a = get_app();
469                 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ; 
470         }
471         $vcard['fn'] = notags($vcard['fn']);
472         $vcard['nick'] = notags($vcard['nick']);
473
474
475         $result['name'] = $vcard['fn'];
476         $result['nick'] = $vcard['nick'];
477         $result['url'] = $profile;
478         $result['addr'] = $addr;
479         $result['notify'] = $notify;
480         $result['poll'] = $poll;
481         $result['request'] = $request;
482         $result['confirm'] = $confirm;
483         $result['photo'] = $vcard['photo'];
484         $result['priority'] = $priority;
485         $result['network'] = $network;
486         $result['alias'] = $alias;
487
488         logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);
489
490         return $result;
491 }