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);
39 $items = $dom->getElementsByTagName('link');
41 // get DFRN link elements
43 foreach($items as $item) {
44 $x = $item->getAttribute('rel');
45 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
46 $ret['feed_atom'] = $item->getAttribute('href');
47 if(substr($x,0,5) == "dfrn-") {
48 $ret[$x] = $item->getAttribute('href');
51 $decoded = urldecode($item->getAttribute('href'));
52 if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
53 $ret['nick'] = $matches[1];
57 // Pull out hCard profile elements
61 $items = $dom->getElementsByTagName('*');
62 foreach($items as $item) {
63 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
64 $level2 = $item->getElementsByTagName('*');
65 foreach($level2 as $x) {
66 if(attribute_contains($x->getAttribute('class'),'fn')) {
67 $ret['fn'] = $x->textContent;
69 if((attribute_contains($x->getAttribute('class'),'photo'))
70 || (attribute_contains($x->getAttribute('class'),'avatar'))) {
71 $size = intval($x->getAttribute('width'));
72 // dfrn prefers 175, so if we find this, we set largest_size so it can't be topped.
73 if(($size > $largest_photo) || ($size == 175) || (! $largest_photo)) {
74 $ret['photo'] = $x->getAttribute('src');
75 $largest_photo = (($size == 175) ? 9999 : $size);
78 if(attribute_contains($x->getAttribute('class'),'key')) {
79 $ret['key'] = $x->textContent;
93 if(! function_exists('validate_dfrn')) {
94 function validate_dfrn($a) {
98 if(! x($a,'dfrn-request'))
100 if(! x($a,'dfrn-confirm'))
102 if(! x($a,'dfrn-notify'))
104 if(! x($a,'dfrn-poll'))
109 if(! function_exists('scrape_meta')) {
110 function scrape_meta($url) {
116 logger('scrape_meta: url=' . $url);
118 $s = fetch_url($url);
123 $headers = $a->get_curl_headers();
124 logger('scrape_meta: headers=' . $headers, LOGGER_DEBUG);
126 $lines = explode("\n",$headers);
128 foreach($lines as $line) {
129 // don't try and run feeds through the html5 parser
130 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
137 $dom = HTML5_Parser::parse($s);
142 $items = $dom->getElementsByTagName('meta');
144 // get DFRN link elements
146 foreach($items as $item) {
147 $x = $item->getAttribute('name');
148 if(substr($x,0,5) == "dfrn-")
149 $ret[$x] = $item->getAttribute('content');
156 if(! function_exists('scrape_vcard')) {
157 function scrape_vcard($url) {
163 logger('scrape_vcard: url=' . $url);
165 $s = fetch_url($url);
170 $headers = $a->get_curl_headers();
171 $lines = explode("\n",$headers);
173 foreach($lines as $line) {
174 // don't try and run feeds through the html5 parser
175 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
180 $dom = HTML5_Parser::parse($s);
185 // Pull out hCard profile elements
189 $items = $dom->getElementsByTagName('*');
190 foreach($items as $item) {
191 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
192 $level2 = $item->getElementsByTagName('*');
193 foreach($level2 as $x) {
194 if(attribute_contains($x->getAttribute('class'),'fn'))
195 $ret['fn'] = $x->textContent;
196 if((attribute_contains($x->getAttribute('class'),'photo'))
197 || (attribute_contains($x->getAttribute('class'),'avatar'))) {
198 $size = intval($x->getAttribute('width'));
199 if(($size > $largest_photo) || (! $largest_photo)) {
200 $ret['photo'] = $x->getAttribute('src');
201 $largest_photo = $size;
204 if((attribute_contains($x->getAttribute('class'),'nickname'))
205 || (attribute_contains($x->getAttribute('class'),'uid'))) {
206 $ret['nick'] = $x->textContent;
216 if(! function_exists('scrape_feed')) {
217 function scrape_feed($url) {
222 $s = fetch_url($url);
227 $headers = $a->get_curl_headers();
228 logger('scrape_feed: headers=' . $headers, LOGGER_DEBUG);
230 $lines = explode("\n",$headers);
232 foreach($lines as $line) {
233 if(stristr($line,'content-type:')) {
234 if(stristr($line,'application/atom+xml') || stristr($s,'<feed')) {
235 $ret['feed_atom'] = $url;
238 if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
239 $ret['feed_rss'] = $url;
246 $dom = HTML5_Parser::parse($s);
252 $items = $dom->getElementsByTagName('img');
254 // get img elements (twitter)
257 foreach($items as $item) {
258 $x = $item->getAttribute('id');
259 if($x === 'profile-image') {
260 $ret['photo'] = $item->getAttribute('src');
266 $head = $dom->getElementsByTagName('base');
268 foreach($head as $head0) {
269 $basename = $head0->getAttribute('href');
274 $basename = substr($url,0,strrpos($url,'/')) . '/';
276 $items = $dom->getElementsByTagName('link');
278 // get Atom/RSS link elements, take the first one of either.
281 foreach($items as $item) {
282 $x = $item->getAttribute('rel');
283 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
284 if(! x($ret,'feed_atom'))
285 $ret['feed_atom'] = $item->getAttribute('href');
287 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
288 if(! x($ret,'feed_rss'))
289 $ret['feed_rss'] = $item->getAttribute('href');
294 // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
296 if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
297 $ret['feed_atom'] = $basename . $ret['feed_atom'];
298 if(x($ret,'feed_rss') && (! strstr($ret['feed_rss'],'://')))
299 $ret['feed_rss'] = $basename . $ret['feed_rss'];
307 * Probe a network address to discover what kind of protocols we need to communicate with it.
309 * Warning: this function is a bit touchy and there are some subtle dependencies within the logic flow.
316 * PROBE_DIASPORA has a bias towards returning Diaspora information
317 * while PROBE_NORMAL has a bias towards dfrn/zot - in the case where
318 * an address (such as a Friendika address) supports more than one type
324 define ( 'PROBE_NORMAL', 0);
325 define ( 'PROBE_DIASPORA', 1);
327 function probe_url($url, $mode = PROBE_NORMAL) {
328 require_once('include/email.php');
341 $email_conversant = false;
343 $twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
345 $at_addr = ((strpos($url,'@') !== false) ? true : false);
349 if(strpos($url,'mailto:') !== false && $at_addr) {
350 $url = str_replace('mailto:','',$url);
359 logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
360 foreach($links as $link) {
361 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
362 $zot = unamp($link['@attributes']['href']);
363 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
364 $dfrn = unamp($link['@attributes']['href']);
365 if($link['@attributes']['rel'] === 'salmon')
366 $notify = unamp($link['@attributes']['href']);
367 if($link['@attributes']['rel'] === NAMESPACE_FEED)
368 $poll = unamp($link['@attributes']['href']);
369 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
370 $hcard = unamp($link['@attributes']['href']);
371 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
372 $profile = unamp($link['@attributes']['href']);
373 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
374 $diaspora_base = unamp($link['@attributes']['href']);
377 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
378 $diaspora_guid = unamp($link['@attributes']['href']);
381 if($link['@attributes']['rel'] === 'diaspora-public-key') {
382 $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
383 $pubkey = rsatopem($diaspora_key);
388 // Status.Net can have more than one profile URL. We need to match the profile URL
389 // to a contact on incoming messages to prevent spam, and we won't know which one
390 // to match. So in case of two, one of them is stored as an alias. Only store URL's
391 // and not webfinger user@host aliases. If they've got more than two non-email style
392 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
393 // otherwise we're screwed.
395 foreach($links as $link) {
396 if($link['@attributes']['rel'] === 'alias') {
397 if(strpos($link['@attributes']['href'],'@') === false) {
398 if(isset($profile)) {
399 if($link['@attributes']['href'] !== $profile)
400 $alias = unamp($link['@attributes']['href']);
403 $profile = unamp($link['@attributes']['href']);
408 elseif($mode == PROBE_NORMAL) {
413 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
414 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
417 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
420 if(count($x) && count($r)) {
421 $mailbox = construct_mailbox_name($r[0]);
423 openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
424 $mbox = email_connect($mailbox,$r[0]['user'],$password);
428 $msgs = email_poll($mbox,$orig_url);
431 $network = NETWORK_MAIL;
432 $name = substr($url,0,strpos($url,'@'));
433 $profile = 'http://' . substr($url,strpos($url,'@')+1);
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,'');
444 if(isset($adr) && strlen($adr[0]->personal))
445 $vcard['fn'] = notags($adr[0]->personal);
453 if($mode == PROBE_NORMAL) {
455 $s = fetch_url($zot);
457 $j = json_decode($s);
459 $network = NETWORK_ZOT;
461 'fn' => $j->fullname,
462 'nick' => $j->nickname,
467 $pubkey = $j->pubkey;
474 $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
475 if(is_array($ret) && x($ret,'dfrn-request')) {
476 $network = NETWORK_DFRN;
477 $request = $ret['dfrn-request'];
478 $confirm = $ret['dfrn-confirm'];
479 $notify = $ret['dfrn-notify'];
480 $poll = $ret['dfrn-poll'];
483 $vcard['fn'] = $ret['fn'];
484 $vcard['nick'] = $ret['nick'];
485 $vcard['photo'] = $ret['photo'];
490 if($diaspora && $diaspora_base && $diaspora_guid) {
491 if($mode == PROBE_DIASPORA || ! $notify)
492 $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
494 $addr = str_replace('acct:', '', $url);
497 if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
499 $network = NETWORK_DIASPORA;
501 $network = NETWORK_OSTATUS;
504 if($hcard && ! $vcard) {
505 $vcard = scrape_vcard($hcard);
507 // Google doesn't use absolute url in profile photos
509 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
510 $h = @parse_url($hcard);
512 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
515 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
519 logger('twitter: setup');
520 $tid = basename($url);
521 $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
523 $poll = $tapi . '?user_id=' . $tid;
525 $poll = $tapi . '?screen_name=' . $tid;
526 $profile = 'http://twitter.com/#!/' . $tid;
531 $vcard['fn'] = $vcard['nick'];
535 if($twitter || ! $poll)
537 if((! isset($vcard)) || (! $profile))
539 if(($at_addr) && (! count($links)))
544 $feedret = scrape_feed(($poll) ? $poll : $url);
545 logger('probe_url: scrape_feed returns: ' . print_r($feedret,true), LOGGER_DATA);
546 if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
547 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
552 if(x($feedret,'photo') && (! x($vcard,'photo')))
553 $vcard['photo'] = $feedret['photo'];
554 require_once('library/simplepie/simplepie.inc');
555 $feed = new SimplePie();
556 $xml = fetch_url($poll);
558 logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
561 logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), $LOGGER_DATA);
563 $feed->set_raw_data($xml);
567 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
570 if(! x($vcard,'photo'))
571 $vcard['photo'] = $feed->get_image_url();
572 $author = $feed->get_author();
575 $vcard['fn'] = unxmlify(trim($author->get_name()));
577 $vcard['fn'] = trim(unxmlify($author->get_email()));
578 if(strpos($vcard['fn'],'@') !== false)
579 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
580 $email = unxmlify($author->get_email());
581 if(! $profile && $author->get_link())
582 $profile = trim(unxmlify($author->get_link()));
583 if(! $vcard['photo']) {
584 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
586 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
587 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
588 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
593 $item = $feed->get_item(0);
595 $author = $item->get_author();
597 $vcard['fn'] = trim(unxmlify($author->get_name()));
599 $vcard['fn'] = trim(unxmlify($author->get_email()));
600 if(strpos($vcard['fn'],'@') !== false)
601 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
602 $email = unxmlify($author->get_email());
603 if(! $profile && $author->get_link())
604 $profile = trim(unxmlify($author->get_link()));
606 if(! $vcard['photo']) {
607 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
608 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
609 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
611 if(! $vcard['photo']) {
612 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
614 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
615 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
616 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
622 if((! $vcard['photo']) && strlen($email))
623 $vcard['photo'] = gravatar_img($email);
624 if($poll === $profile)
625 $lnk = $feed->get_permalink();
626 if(isset($lnk) && strlen($lnk))
629 if(! (x($vcard,'fn')))
630 $vcard['fn'] = notags($feed->get_title());
631 if(! (x($vcard,'fn')))
632 $vcard['fn'] = notags($feed->get_description());
634 if(strpos($vcard['fn'],'Twitter / ') !== false) {
635 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
636 $vcard['fn'] = trim($vcard['fn']);
638 if(! x($vcard,'nick')) {
639 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
640 if(strpos($vcard['nick'],' '))
641 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
644 $network = NETWORK_FEED;
650 if(! x($vcard,'photo')) {
652 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
658 // No human could be associated with this link, use the URL as the contact name
660 if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
663 $vcard['fn'] = notags($vcard['fn']);
664 $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
666 $result['name'] = $vcard['fn'];
667 $result['nick'] = $vcard['nick'];
668 $result['url'] = $profile;
669 $result['addr'] = $addr;
670 $result['notify'] = $notify;
671 $result['poll'] = $poll;
672 $result['request'] = $request;
673 $result['confirm'] = $confirm;
674 $result['photo'] = $vcard['photo'];
675 $result['priority'] = $priority;
676 $result['network'] = $network;
677 $result['alias'] = $alias;
678 $result['pubkey'] = $pubkey;
680 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);