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);
236 $headers = $a->get_curl_headers();
237 logger('scrape_feed: headers=' . $headers, LOGGER_DEBUG);
239 $lines = explode("\n",$headers);
241 foreach($lines as $line) {
242 if(stristr($line,'content-type:')) {
243 if(stristr($line,'application/atom+xml') || stristr($s,'<feed')) {
244 $ret['feed_atom'] = $url;
247 if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
248 $ret['feed_rss'] = $url;
256 $dom = HTML5_Parser::parse($s);
257 } catch (DOMException $e) {
258 logger('scrape_feed: parse error: ' . $e);
265 $head = $dom->getElementsByTagName('base');
267 foreach($head as $head0) {
268 $basename = $head0->getAttribute('href');
273 $basename = substr($url,0,strrpos($url,'/')) . '/';
275 $items = $dom->getElementsByTagName('link');
277 // get Atom/RSS link elements, take the first one of either.
280 foreach($items as $item) {
281 $x = $item->getAttribute('rel');
282 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
283 if(! x($ret,'feed_atom'))
284 $ret['feed_atom'] = $item->getAttribute('href');
286 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
287 if(! x($ret,'feed_rss'))
288 $ret['feed_rss'] = $item->getAttribute('href');
293 // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
295 if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
296 $ret['feed_atom'] = $basename . $ret['feed_atom'];
297 if(x($ret,'feed_rss') && (! strstr($ret['feed_rss'],'://')))
298 $ret['feed_rss'] = $basename . $ret['feed_rss'];
306 * Probe a network address to discover what kind of protocols we need to communicate with it.
308 * Warning: this function is a bit touchy and there are some subtle dependencies within the logic flow.
315 * PROBE_DIASPORA has a bias towards returning Diaspora information
316 * while PROBE_NORMAL has a bias towards dfrn/zot - in the case where
317 * an address (such as a Friendika address) supports more than one type
323 define ( 'PROBE_NORMAL', 0);
324 define ( 'PROBE_DIASPORA', 1);
326 function probe_url($url, $mode = PROBE_NORMAL) {
327 require_once('include/email.php');
340 $email_conversant = false;
342 $twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
344 $at_addr = ((strpos($url,'@') !== false) ? true : false);
348 if(strpos($url,'mailto:') !== false && $at_addr) {
349 $url = str_replace('mailto:','',$url);
358 logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
359 foreach($links as $link) {
360 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
361 $zot = unamp($link['@attributes']['href']);
362 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
363 $dfrn = unamp($link['@attributes']['href']);
364 if($link['@attributes']['rel'] === 'salmon')
365 $notify = unamp($link['@attributes']['href']);
366 if($link['@attributes']['rel'] === NAMESPACE_FEED)
367 $poll = unamp($link['@attributes']['href']);
368 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
369 $hcard = unamp($link['@attributes']['href']);
370 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
371 $profile = unamp($link['@attributes']['href']);
372 if($link['@attributes']['rel'] === 'http://portablecontacts.net/spec/1.0')
373 $poco = unamp($link['@attributes']['href']);
374 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
375 $diaspora_base = unamp($link['@attributes']['href']);
378 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
379 $diaspora_guid = unamp($link['@attributes']['href']);
382 if($link['@attributes']['rel'] === 'diaspora-public-key') {
383 $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
384 $pubkey = rsatopem($diaspora_key);
389 // Status.Net can have more than one profile URL. We need to match the profile URL
390 // to a contact on incoming messages to prevent spam, and we won't know which one
391 // to match. So in case of two, one of them is stored as an alias. Only store URL's
392 // and not webfinger user@host aliases. If they've got more than two non-email style
393 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
394 // otherwise we're screwed.
396 foreach($links as $link) {
397 if($link['@attributes']['rel'] === 'alias') {
398 if(strpos($link['@attributes']['href'],'@') === false) {
399 if(isset($profile)) {
400 if($link['@attributes']['href'] !== $profile)
401 $alias = unamp($link['@attributes']['href']);
404 $profile = unamp($link['@attributes']['href']);
409 elseif($mode == PROBE_NORMAL) {
414 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
415 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
418 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
421 if(count($x) && count($r)) {
422 $mailbox = construct_mailbox_name($r[0]);
424 openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
425 $mbox = email_connect($mailbox,$r[0]['user'],$password);
429 $msgs = email_poll($mbox,$orig_url);
432 $network = NETWORK_MAIL;
433 $name = substr($url,0,strpos($url,'@'));
434 $phost = substr($url,strpos($url,'@')+1);
435 $profile = 'http://' . $phost;
436 // fix nick character range
437 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
438 $notify = 'smtp ' . random_string();
439 $poll = 'email ' . random_string();
441 $x = email_msg_meta($mbox,$msgs[0]);
442 if(stristr($x->from,$orig_url))
443 $adr = imap_rfc822_parse_adrlist($x->from,'');
444 elseif(stristr($x->to,$orig_url))
445 $adr = imap_rfc822_parse_adrlist($x->to,'');
447 foreach($adr as $feadr) {
448 if((strcasecmp($feadr->mailbox,$name) == 0)
449 &&(strcasecmp($feadr->host,$phost) == 0)
450 && (strlen($feadr->personal))) {
451 $vcard['fn'] = notags($feadr->personal);
462 if($mode == PROBE_NORMAL) {
464 $s = fetch_url($zot);
466 $j = json_decode($s);
468 $network = NETWORK_ZOT;
470 'fn' => $j->fullname,
471 'nick' => $j->nickname,
476 $pubkey = $j->pubkey;
483 $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
484 if(is_array($ret) && x($ret,'dfrn-request')) {
485 $network = NETWORK_DFRN;
486 $request = $ret['dfrn-request'];
487 $confirm = $ret['dfrn-confirm'];
488 $notify = $ret['dfrn-notify'];
489 $poll = $ret['dfrn-poll'];
492 $vcard['fn'] = $ret['fn'];
493 $vcard['nick'] = $ret['nick'];
494 $vcard['photo'] = $ret['photo'];
499 if($diaspora && $diaspora_base && $diaspora_guid) {
500 if($mode == PROBE_DIASPORA || ! $notify) {
501 $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
502 $batch = $diaspora_base . 'receive/public' ;
505 $addr = str_replace('acct:', '', $url);
508 if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
510 $network = NETWORK_DIASPORA;
512 $network = NETWORK_OSTATUS;
515 if($hcard && ! $vcard) {
516 $vcard = scrape_vcard($hcard);
518 // Google doesn't use absolute url in profile photos
520 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
521 $h = @parse_url($hcard);
523 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
526 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
530 logger('twitter: setup');
531 $tid = basename($url);
532 $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
534 $poll = $tapi . '?user_id=' . $tid;
536 $poll = $tapi . '?screen_name=' . $tid;
537 $profile = 'http://twitter.com/#!/' . $tid;
538 $vcard['photo'] = 'https://api.twitter.com/1/users/profile_image/' . $tid;
539 $vcard['nick'] = $tid;
540 $vcard['fn'] = $tid . '@twitter';
545 $vcard['fn'] = $vcard['nick'];
549 if($twitter || ! $poll)
551 if((! isset($vcard)) || (! x($vcard,'fn')) || (! $profile))
553 if(($at_addr) && (! count($links)))
558 $feedret = scrape_feed(($poll) ? $poll : $url);
559 logger('probe_url: scrape_feed returns: ' . print_r($feedret,true), LOGGER_DATA);
560 if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
561 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
566 if(x($feedret,'photo') && (! x($vcard,'photo')))
567 $vcard['photo'] = $feedret['photo'];
568 require_once('library/simplepie/simplepie.inc');
569 $feed = new SimplePie();
570 $xml = fetch_url($poll);
572 logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
575 logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), $LOGGER_DATA);
577 $feed->set_raw_data($xml);
581 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
584 if(! x($vcard,'photo'))
585 $vcard['photo'] = $feed->get_image_url();
586 $author = $feed->get_author();
589 $vcard['fn'] = unxmlify(trim($author->get_name()));
591 $vcard['fn'] = trim(unxmlify($author->get_email()));
592 if(strpos($vcard['fn'],'@') !== false)
593 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
594 $email = unxmlify($author->get_email());
595 if(! $profile && $author->get_link())
596 $profile = trim(unxmlify($author->get_link()));
597 if(! $vcard['photo']) {
598 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
600 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
601 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
602 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
607 $item = $feed->get_item(0);
609 $author = $item->get_author();
611 $vcard['fn'] = trim(unxmlify($author->get_name()));
613 $vcard['fn'] = trim(unxmlify($author->get_email()));
614 if(strpos($vcard['fn'],'@') !== false)
615 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
616 $email = unxmlify($author->get_email());
617 if(! $profile && $author->get_link())
618 $profile = trim(unxmlify($author->get_link()));
620 if(! $vcard['photo']) {
621 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
622 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
623 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
625 if(! $vcard['photo']) {
626 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
628 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
629 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
630 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
636 if((! $vcard['photo']) && strlen($email))
637 $vcard['photo'] = gravatar_img($email);
638 if($poll === $profile)
639 $lnk = $feed->get_permalink();
640 if(isset($lnk) && strlen($lnk))
643 if(! (x($vcard,'fn')))
644 $vcard['fn'] = notags($feed->get_title());
645 if(! (x($vcard,'fn')))
646 $vcard['fn'] = notags($feed->get_description());
648 if(strpos($vcard['fn'],'Twitter / ') !== false) {
649 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
650 $vcard['fn'] = trim($vcard['fn']);
652 if(! x($vcard,'nick')) {
653 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
654 if(strpos($vcard['nick'],' '))
655 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
658 $network = NETWORK_FEED;
664 if(! x($vcard,'photo')) {
666 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
672 // No human could be associated with this link, use the URL as the contact name
674 if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
677 $vcard['fn'] = notags($vcard['fn']);
678 $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
680 $result['name'] = $vcard['fn'];
681 $result['nick'] = $vcard['nick'];
682 $result['url'] = $profile;
683 $result['addr'] = $addr;
684 $result['batch'] = $batch;
685 $result['notify'] = $notify;
686 $result['poll'] = $poll;
687 $result['request'] = $request;
688 $result['confirm'] = $confirm;
689 $result['poco'] = $poco;
690 $result['photo'] = $vcard['photo'];
691 $result['priority'] = $priority;
692 $result['network'] = $network;
693 $result['alias'] = $alias;
694 $result['pubkey'] = $pubkey;
696 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);