3 require_once('library/HTML5/Parser.php');
4 require_once('include/crypto.php');
6 if(! function_exists('scrape_dfrn')) {
7 function scrape_dfrn($url) {
13 logger('scrape_dfrn: url=' . $url);
20 $headers = $a->get_curl_headers();
21 logger('scrape_dfrn: headers=' . $headers, LOGGER_DEBUG);
24 $lines = explode("\n",$headers);
26 foreach($lines as $line) {
27 // don't try and run feeds through the html5 parser
28 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
34 $dom = HTML5_Parser::parse($s);
35 } catch (DOMException $e) {
36 logger('scrape_dfrn: parse error: ' . $e);
42 $items = $dom->getElementsByTagName('link');
44 // get DFRN link elements
46 foreach($items as $item) {
47 $x = $item->getAttribute('rel');
48 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
49 $ret['feed_atom'] = $item->getAttribute('href');
50 if(substr($x,0,5) == "dfrn-") {
51 $ret[$x] = $item->getAttribute('href');
54 $decoded = urldecode($item->getAttribute('href'));
55 if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
56 $ret['nick'] = $matches[1];
60 // Pull out hCard profile elements
64 $items = $dom->getElementsByTagName('*');
65 foreach($items as $item) {
66 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
67 $level2 = $item->getElementsByTagName('*');
68 foreach($level2 as $x) {
69 if(attribute_contains($x->getAttribute('class'),'fn')) {
70 $ret['fn'] = $x->textContent;
72 if((attribute_contains($x->getAttribute('class'),'photo'))
73 || (attribute_contains($x->getAttribute('class'),'avatar'))) {
74 $size = intval($x->getAttribute('width'));
75 // dfrn prefers 175, so if we find this, we set largest_size so it can't be topped.
76 if(($size > $largest_photo) || ($size == 175) || (! $largest_photo)) {
77 $ret['photo'] = $x->getAttribute('src');
78 $largest_photo = (($size == 175) ? 9999 : $size);
81 if(attribute_contains($x->getAttribute('class'),'key')) {
82 $ret['key'] = $x->textContent;
96 if(! function_exists('validate_dfrn')) {
97 function validate_dfrn($a) {
101 if(! x($a,'dfrn-request'))
103 if(! x($a,'dfrn-confirm'))
105 if(! x($a,'dfrn-notify'))
107 if(! x($a,'dfrn-poll'))
112 if(! function_exists('scrape_meta')) {
113 function scrape_meta($url) {
119 logger('scrape_meta: url=' . $url);
121 $s = fetch_url($url);
126 $headers = $a->get_curl_headers();
127 logger('scrape_meta: headers=' . $headers, LOGGER_DEBUG);
129 $lines = explode("\n",$headers);
131 foreach($lines as $line) {
132 // don't try and run feeds through the html5 parser
133 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
139 $dom = HTML5_Parser::parse($s);
140 } catch (DOMException $e) {
141 logger('scrape_meta: parse error: ' . $e);
147 $items = $dom->getElementsByTagName('meta');
149 // get DFRN link elements
151 foreach($items as $item) {
152 $x = $item->getAttribute('name');
153 if(substr($x,0,5) == "dfrn-")
154 $ret[$x] = $item->getAttribute('content');
161 if(! function_exists('scrape_vcard')) {
162 function scrape_vcard($url) {
168 logger('scrape_vcard: url=' . $url);
170 $s = fetch_url($url);
175 $headers = $a->get_curl_headers();
176 $lines = explode("\n",$headers);
178 foreach($lines as $line) {
179 // don't try and run feeds through the html5 parser
180 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
186 $dom = HTML5_Parser::parse($s);
187 } catch (DOMException $e) {
188 logger('scrape_vcard: parse error: ' . $e);
194 // Pull out hCard profile elements
198 $items = $dom->getElementsByTagName('*');
199 foreach($items as $item) {
200 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
201 $level2 = $item->getElementsByTagName('*');
202 foreach($level2 as $x) {
203 if(attribute_contains($x->getAttribute('class'),'fn'))
204 $ret['fn'] = $x->textContent;
205 if((attribute_contains($x->getAttribute('class'),'photo'))
206 || (attribute_contains($x->getAttribute('class'),'avatar'))) {
207 $size = intval($x->getAttribute('width'));
208 if(($size > $largest_photo) || (! $largest_photo)) {
209 $ret['photo'] = $x->getAttribute('src');
210 $largest_photo = $size;
213 if((attribute_contains($x->getAttribute('class'),'nickname'))
214 || (attribute_contains($x->getAttribute('class'),'uid'))) {
215 $ret['nick'] = $x->textContent;
225 if(! function_exists('scrape_feed')) {
226 function scrape_feed($url) {
231 $s = fetch_url($url);
233 $headers = $a->get_curl_headers();
234 $code = $a->get_curl_code();
236 logger('scrape_feed: returns: ' . $code . ' headers=' . $headers, LOGGER_DEBUG);
239 logger('scrape_feed: no data returned for ' . $url);
244 $lines = explode("\n",$headers);
246 foreach($lines as $line) {
247 if(stristr($line,'content-type:')) {
248 if(stristr($line,'application/atom+xml') || stristr($s,'<feed')) {
249 $ret['feed_atom'] = $url;
252 if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
253 $ret['feed_rss'] = $url;
258 // perhaps an RSS version 1 feed with a generic or incorrect content-type?
259 if(stristr($s,'</item>')) {
260 $ret['feed_rss'] = $url;
266 $dom = HTML5_Parser::parse($s);
267 } catch (DOMException $e) {
268 logger('scrape_feed: parse error: ' . $e);
272 logger('scrape_feed: failed to parse.');
277 $head = $dom->getElementsByTagName('base');
279 foreach($head as $head0) {
280 $basename = $head0->getAttribute('href');
285 $basename = substr($url,0,strrpos($url,'/')) . '/';
287 $items = $dom->getElementsByTagName('link');
289 // get Atom/RSS link elements, take the first one of either.
292 foreach($items as $item) {
293 $x = $item->getAttribute('rel');
294 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
295 if(! x($ret,'feed_atom'))
296 $ret['feed_atom'] = $item->getAttribute('href');
298 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
299 if(! x($ret,'feed_rss'))
300 $ret['feed_rss'] = $item->getAttribute('href');
305 // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
307 if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
308 $ret['feed_atom'] = $basename . $ret['feed_atom'];
309 if(x($ret,'feed_rss') && (! strstr($ret['feed_rss'],'://')))
310 $ret['feed_rss'] = $basename . $ret['feed_rss'];
318 * Probe a network address to discover what kind of protocols we need to communicate with it.
320 * Warning: this function is a bit touchy and there are some subtle dependencies within the logic flow.
327 * PROBE_DIASPORA has a bias towards returning Diaspora information
328 * while PROBE_NORMAL has a bias towards dfrn/zot - in the case where
329 * an address (such as a Friendica address) supports more than one type
335 define ( 'PROBE_NORMAL', 0);
336 define ( 'PROBE_DIASPORA', 1);
338 function probe_url($url, $mode = PROBE_NORMAL) {
339 require_once('include/email.php');
352 $email_conversant = false;
354 $twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
356 $at_addr = ((strpos($url,'@') !== false) ? true : false);
360 if(strpos($url,'mailto:') !== false && $at_addr) {
361 $url = str_replace('mailto:','',$url);
370 logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
371 foreach($links as $link) {
372 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
373 $zot = unamp($link['@attributes']['href']);
374 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
375 $dfrn = unamp($link['@attributes']['href']);
376 if($link['@attributes']['rel'] === 'salmon')
377 $notify = unamp($link['@attributes']['href']);
378 if($link['@attributes']['rel'] === NAMESPACE_FEED)
379 $poll = unamp($link['@attributes']['href']);
380 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
381 $hcard = unamp($link['@attributes']['href']);
382 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
383 $profile = unamp($link['@attributes']['href']);
384 if($link['@attributes']['rel'] === 'http://portablecontacts.net/spec/1.0')
385 $poco = unamp($link['@attributes']['href']);
386 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
387 $diaspora_base = unamp($link['@attributes']['href']);
390 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
391 $diaspora_guid = unamp($link['@attributes']['href']);
394 if($link['@attributes']['rel'] === 'diaspora-public-key') {
395 $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
396 $pubkey = rsatopem($diaspora_key);
401 // Status.Net can have more than one profile URL. We need to match the profile URL
402 // to a contact on incoming messages to prevent spam, and we won't know which one
403 // to match. So in case of two, one of them is stored as an alias. Only store URL's
404 // and not webfinger user@host aliases. If they've got more than two non-email style
405 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
406 // otherwise we're screwed.
408 foreach($links as $link) {
409 if($link['@attributes']['rel'] === 'alias') {
410 if(strpos($link['@attributes']['href'],'@') === false) {
411 if(isset($profile)) {
412 if($link['@attributes']['href'] !== $profile)
413 $alias = unamp($link['@attributes']['href']);
416 $profile = unamp($link['@attributes']['href']);
421 elseif($mode == PROBE_NORMAL) {
426 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
427 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
430 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
433 if(count($x) && count($r)) {
434 $mailbox = construct_mailbox_name($r[0]);
436 openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
437 $mbox = email_connect($mailbox,$r[0]['user'],$password);
441 $msgs = email_poll($mbox,$orig_url);
444 $network = NETWORK_MAIL;
445 $name = substr($url,0,strpos($url,'@'));
446 $phost = substr($url,strpos($url,'@')+1);
447 $profile = 'http://' . $phost;
448 // fix nick character range
449 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => avatar_img($url));
450 $notify = 'smtp ' . random_string();
451 $poll = 'email ' . random_string();
453 $x = email_msg_meta($mbox,$msgs[0]);
454 if(stristr($x->from,$orig_url))
455 $adr = imap_rfc822_parse_adrlist($x->from,'');
456 elseif(stristr($x->to,$orig_url))
457 $adr = imap_rfc822_parse_adrlist($x->to,'');
459 foreach($adr as $feadr) {
460 if((strcasecmp($feadr->mailbox,$name) == 0)
461 &&(strcasecmp($feadr->host,$phost) == 0)
462 && (strlen($feadr->personal))) {
464 $personal = imap_mime_header_decode($feadr->personal);
466 foreach($personal as $perspart)
467 if ($perspart->charset != "default")
468 $vcard['fn'] .= iconv($perspart->charset, 'UTF-8//IGNORE', $perspart->text);
470 $vcard['fn'] .= $perspart->text;
472 $vcard['fn'] = notags($vcard['fn']);
483 if($mode == PROBE_NORMAL) {
485 $s = fetch_url($zot);
487 $j = json_decode($s);
489 $network = NETWORK_ZOT;
491 'fn' => $j->fullname,
492 'nick' => $j->nickname,
497 $pubkey = $j->pubkey;
504 $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
505 if(is_array($ret) && x($ret,'dfrn-request')) {
506 $network = NETWORK_DFRN;
507 $request = $ret['dfrn-request'];
508 $confirm = $ret['dfrn-confirm'];
509 $notify = $ret['dfrn-notify'];
510 $poll = $ret['dfrn-poll'];
513 $vcard['fn'] = $ret['fn'];
514 $vcard['nick'] = $ret['nick'];
515 $vcard['photo'] = $ret['photo'];
520 if($diaspora && $diaspora_base && $diaspora_guid) {
521 if($mode == PROBE_DIASPORA || ! $notify) {
522 $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
523 $batch = $diaspora_base . 'receive/public' ;
526 $addr = str_replace('acct:', '', $url);
529 if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
531 $network = NETWORK_DIASPORA;
533 $network = NETWORK_OSTATUS;
536 if($hcard && ! $vcard) {
537 $vcard = scrape_vcard($hcard);
539 // Google doesn't use absolute url in profile photos
541 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
542 $h = @parse_url($hcard);
544 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
547 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
551 logger('twitter: setup');
552 $tid = basename($url);
553 $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
555 $poll = $tapi . '?user_id=' . $tid;
557 $poll = $tapi . '?screen_name=' . $tid;
558 $profile = 'http://twitter.com/#!/' . $tid;
559 $vcard['photo'] = 'https://api.twitter.com/1/users/profile_image/' . $tid;
560 $vcard['nick'] = $tid;
561 $vcard['fn'] = $tid . '@twitter';
566 $vcard['fn'] = $vcard['nick'];
570 if($twitter || ! $poll)
572 if((! isset($vcard)) || (! x($vcard,'fn')) || (! $profile))
574 if(($at_addr) && (! count($links)))
579 $feedret = scrape_feed(($poll) ? $poll : $url);
580 logger('probe_url: scrape_feed ' . (($poll)? $poll : $url) . ' returns: ' . print_r($feedret,true), LOGGER_DATA);
581 if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
582 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
587 if(x($feedret,'photo') && (! x($vcard,'photo')))
588 $vcard['photo'] = $feedret['photo'];
589 require_once('library/simplepie/simplepie.inc');
590 $feed = new SimplePie();
591 $xml = fetch_url($poll);
593 logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
596 logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), LOGGER_DATA);
598 $feed->set_raw_data($xml);
602 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
605 if(! x($vcard,'photo'))
606 $vcard['photo'] = $feed->get_image_url();
607 $author = $feed->get_author();
610 $vcard['fn'] = unxmlify(trim($author->get_name()));
612 $vcard['fn'] = trim(unxmlify($author->get_email()));
613 if(strpos($vcard['fn'],'@') !== false)
614 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
615 $email = unxmlify($author->get_email());
616 if(! $profile && $author->get_link())
617 $profile = trim(unxmlify($author->get_link()));
618 if(! $vcard['photo']) {
619 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
621 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
622 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
623 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
628 $item = $feed->get_item(0);
630 $author = $item->get_author();
632 $vcard['fn'] = trim(unxmlify($author->get_name()));
634 $vcard['fn'] = trim(unxmlify($author->get_email()));
635 if(strpos($vcard['fn'],'@') !== false)
636 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
637 $email = unxmlify($author->get_email());
638 if(! $profile && $author->get_link())
639 $profile = trim(unxmlify($author->get_link()));
641 if(! $vcard['photo']) {
642 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
643 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
644 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
646 if(! $vcard['photo']) {
647 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
649 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
650 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
651 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
657 if((! $vcard['photo']) && strlen($email))
658 $vcard['photo'] = avatar_img($email);
659 if($poll === $profile)
660 $lnk = $feed->get_permalink();
661 if(isset($lnk) && strlen($lnk))
664 if(! (x($vcard,'fn')))
665 $vcard['fn'] = notags($feed->get_title());
666 if(! (x($vcard,'fn')))
667 $vcard['fn'] = notags($feed->get_description());
669 if(strpos($vcard['fn'],'Twitter / ') !== false) {
670 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
671 $vcard['fn'] = trim($vcard['fn']);
673 if(! x($vcard,'nick')) {
674 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
675 if(strpos($vcard['nick'],' '))
676 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
679 $network = NETWORK_FEED;
685 if(! x($vcard,'photo')) {
687 $vcard['photo'] = $a->get_baseurl() . '/images/person-175.jpg' ;
693 // No human could be associated with this link, use the URL as the contact name
695 if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
698 $vcard['fn'] = notags($vcard['fn']);
699 $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
701 $result['name'] = $vcard['fn'];
702 $result['nick'] = $vcard['nick'];
703 $result['url'] = $profile;
704 $result['addr'] = $addr;
705 $result['batch'] = $batch;
706 $result['notify'] = $notify;
707 $result['poll'] = $poll;
708 $result['request'] = $request;
709 $result['confirm'] = $confirm;
710 $result['poco'] = $poco;
711 $result['photo'] = $vcard['photo'];
712 $result['priority'] = $priority;
713 $result['network'] = $network;
714 $result['alias'] = $alias;
715 $result['pubkey'] = $pubkey;
717 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);