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://joindiaspora.com/seed_location') {
373 $diaspora_base = unamp($link['@attributes']['href']);
376 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
377 $diaspora_guid = unamp($link['@attributes']['href']);
380 if($link['@attributes']['rel'] === 'diaspora-public-key') {
381 $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
382 $pubkey = rsatopem($diaspora_key);
387 // Status.Net can have more than one profile URL. We need to match the profile URL
388 // to a contact on incoming messages to prevent spam, and we won't know which one
389 // to match. So in case of two, one of them is stored as an alias. Only store URL's
390 // and not webfinger user@host aliases. If they've got more than two non-email style
391 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
392 // otherwise we're screwed.
394 foreach($links as $link) {
395 if($link['@attributes']['rel'] === 'alias') {
396 if(strpos($link['@attributes']['href'],'@') === false) {
397 if(isset($profile)) {
398 if($link['@attributes']['href'] !== $profile)
399 $alias = unamp($link['@attributes']['href']);
402 $profile = unamp($link['@attributes']['href']);
407 elseif($mode == PROBE_NORMAL) {
412 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
413 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
416 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
419 if(count($x) && count($r)) {
420 $mailbox = construct_mailbox_name($r[0]);
422 openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
423 $mbox = email_connect($mailbox,$r[0]['user'],$password);
427 $msgs = email_poll($mbox,$orig_url);
430 $network = NETWORK_MAIL;
431 $name = substr($url,0,strpos($url,'@'));
432 $phost = substr($url,strpos($url,'@')+1);
433 $profile = 'http://' . $phost;
434 // fix nick character range
435 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
436 $notify = 'smtp ' . random_string();
437 $poll = 'email ' . random_string();
439 $x = email_msg_meta($mbox,$msgs[0]);
440 if(stristr($x->from,$orig_url))
441 $adr = imap_rfc822_parse_adrlist($x->from,'');
442 elseif(stristr($x->to,$orig_url))
443 $adr = imap_rfc822_parse_adrlist($x->to,'');
445 foreach($adr as $feadr) {
446 if((strcasecmp($feadr->mailbox,$name) == 0)
447 &&(strcasecmp($feadr->host,$phost) == 0)
448 && (strlen($feadr->personal))) {
449 $vcard['fn'] = notags($feadr->personal);
460 if($mode == PROBE_NORMAL) {
462 $s = fetch_url($zot);
464 $j = json_decode($s);
466 $network = NETWORK_ZOT;
468 'fn' => $j->fullname,
469 'nick' => $j->nickname,
474 $pubkey = $j->pubkey;
481 $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
482 if(is_array($ret) && x($ret,'dfrn-request')) {
483 $network = NETWORK_DFRN;
484 $request = $ret['dfrn-request'];
485 $confirm = $ret['dfrn-confirm'];
486 $notify = $ret['dfrn-notify'];
487 $poll = $ret['dfrn-poll'];
490 $vcard['fn'] = $ret['fn'];
491 $vcard['nick'] = $ret['nick'];
492 $vcard['photo'] = $ret['photo'];
497 if($diaspora && $diaspora_base && $diaspora_guid) {
498 if($mode == PROBE_DIASPORA || ! $notify) {
499 $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
500 $batch = $diaspora_base . 'receive/public' ;
503 $addr = str_replace('acct:', '', $url);
506 if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
508 $network = NETWORK_DIASPORA;
510 $network = NETWORK_OSTATUS;
513 if($hcard && ! $vcard) {
514 $vcard = scrape_vcard($hcard);
516 // Google doesn't use absolute url in profile photos
518 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
519 $h = @parse_url($hcard);
521 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
524 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
528 logger('twitter: setup');
529 $tid = basename($url);
530 $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
532 $poll = $tapi . '?user_id=' . $tid;
534 $poll = $tapi . '?screen_name=' . $tid;
535 $profile = 'http://twitter.com/#!/' . $tid;
536 $vcard['photo'] = 'https://api.twitter.com/1/users/profile_image/' . $tid;
537 $vcard['nick'] = $tid;
538 $vcard['fn'] = $tid . '@twitter';
543 $vcard['fn'] = $vcard['nick'];
547 if($twitter || ! $poll)
549 if((! isset($vcard)) || (! x($vcard,'fn')) || (! $profile))
551 if(($at_addr) && (! count($links)))
556 $feedret = scrape_feed(($poll) ? $poll : $url);
557 logger('probe_url: scrape_feed returns: ' . print_r($feedret,true), LOGGER_DATA);
558 if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
559 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
564 if(x($feedret,'photo') && (! x($vcard,'photo')))
565 $vcard['photo'] = $feedret['photo'];
566 require_once('library/simplepie/simplepie.inc');
567 $feed = new SimplePie();
568 $xml = fetch_url($poll);
570 logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
573 logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), $LOGGER_DATA);
575 $feed->set_raw_data($xml);
579 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
582 if(! x($vcard,'photo'))
583 $vcard['photo'] = $feed->get_image_url();
584 $author = $feed->get_author();
587 $vcard['fn'] = unxmlify(trim($author->get_name()));
589 $vcard['fn'] = trim(unxmlify($author->get_email()));
590 if(strpos($vcard['fn'],'@') !== false)
591 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
592 $email = unxmlify($author->get_email());
593 if(! $profile && $author->get_link())
594 $profile = trim(unxmlify($author->get_link()));
595 if(! $vcard['photo']) {
596 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
598 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
599 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
600 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
605 $item = $feed->get_item(0);
607 $author = $item->get_author();
609 $vcard['fn'] = trim(unxmlify($author->get_name()));
611 $vcard['fn'] = trim(unxmlify($author->get_email()));
612 if(strpos($vcard['fn'],'@') !== false)
613 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
614 $email = unxmlify($author->get_email());
615 if(! $profile && $author->get_link())
616 $profile = trim(unxmlify($author->get_link()));
618 if(! $vcard['photo']) {
619 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
620 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
621 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
623 if(! $vcard['photo']) {
624 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
626 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
627 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
628 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
634 if((! $vcard['photo']) && strlen($email))
635 $vcard['photo'] = gravatar_img($email);
636 if($poll === $profile)
637 $lnk = $feed->get_permalink();
638 if(isset($lnk) && strlen($lnk))
641 if(! (x($vcard,'fn')))
642 $vcard['fn'] = notags($feed->get_title());
643 if(! (x($vcard,'fn')))
644 $vcard['fn'] = notags($feed->get_description());
646 if(strpos($vcard['fn'],'Twitter / ') !== false) {
647 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
648 $vcard['fn'] = trim($vcard['fn']);
650 if(! x($vcard,'nick')) {
651 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
652 if(strpos($vcard['nick'],' '))
653 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
656 $network = NETWORK_FEED;
662 if(! x($vcard,'photo')) {
664 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
670 // No human could be associated with this link, use the URL as the contact name
672 if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
675 $vcard['fn'] = notags($vcard['fn']);
676 $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
678 $result['name'] = $vcard['fn'];
679 $result['nick'] = $vcard['nick'];
680 $result['url'] = $profile;
681 $result['addr'] = $addr;
682 $result['batch'] = $batch;
683 $result['notify'] = $notify;
684 $result['poll'] = $poll;
685 $result['request'] = $request;
686 $result['confirm'] = $confirm;
687 $result['photo'] = $vcard['photo'];
688 $result['priority'] = $priority;
689 $result['network'] = $network;
690 $result['alias'] = $alias;
691 $result['pubkey'] = $pubkey;
693 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);