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'];
304 define ( 'PROBE_NORMAL', 0);
305 define ( 'PROBE_DIASPORA', 1);
307 function probe_url($url, $mode = PROBE_NORMAL) {
308 require_once('include/email.php');
319 $email_conversant = false;
321 $twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
323 $at_addr = ((strpos($url,'@') !== false) ? true : false);
327 if(strpos($url,'mailto:') !== false && $at_addr) {
328 $url = str_replace('mailto:','',$url);
335 logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
336 foreach($links as $link) {
337 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
338 $zot = unamp($link['@attributes']['href']);
339 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
340 $dfrn = unamp($link['@attributes']['href']);
341 if($link['@attributes']['rel'] === 'salmon')
342 $notify = unamp($link['@attributes']['href']);
343 if($link['@attributes']['rel'] === NAMESPACE_FEED)
344 $poll = unamp($link['@attributes']['href']);
345 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
346 $hcard = unamp($link['@attributes']['href']);
347 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
348 $profile = unamp($link['@attributes']['href']);
349 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
350 $diaspora_base = unamp($link['@attributes']['href']);
353 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
354 $diaspora_guid = unamp($link['@attributes']['href']);
357 if($link['@attributes']['rel'] === 'diaspora-public-key') {
358 $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
359 $pubkey = rsatopem($diaspora_key);
364 // Status.Net can have more than one profile URL. We need to match the profile URL
365 // to a contact on incoming messages to prevent spam, and we won't know which one
366 // to match. So in case of two, one of them is stored as an alias. Only store URL's
367 // and not webfinger user@host aliases. If they've got more than two non-email style
368 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
369 // otherwise we're screwed.
371 foreach($links as $link) {
372 if($link['@attributes']['rel'] === 'alias') {
373 if(strpos($link['@attributes']['href'],'@') === false) {
374 if(isset($profile)) {
375 if($link['@attributes']['href'] !== $profile)
376 $alias = unamp($link['@attributes']['href']);
379 $profile = unamp($link['@attributes']['href']);
384 elseif($mode == PROBE_NORMAL) {
389 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
390 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
393 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
396 if(count($x) && count($r)) {
397 $mailbox = construct_mailbox_name($r[0]);
399 openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
400 $mbox = email_connect($mailbox,$r[0]['user'],$password);
404 $msgs = email_poll($mbox,$orig_url);
407 $network = NETWORK_MAIL;
408 $name = substr($url,0,strpos($url,'@'));
409 $profile = 'http://' . substr($url,strpos($url,'@')+1);
410 // fix nick character range
411 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
412 $notify = 'smtp ' . random_string();
413 $poll = 'email ' . random_string();
415 $x = email_msg_meta($mbox,$msgs[0]);
416 if(stristr($x->from,$orig_url))
417 $adr = imap_rfc822_parse_adrlist($x->from,'');
418 elseif(stristr($x->to,$orig_url))
419 $adr = imap_rfc822_parse_adrlist($x->to,'');
420 if(isset($adr) && strlen($adr[0]->personal))
421 $vcard['fn'] = notags($adr[0]->personal);
429 if($mode == PROBE_NORMAL) {
431 $s = fetch_url($zot);
433 $j = json_decode($s);
435 $network = NETWORK_ZOT;
437 'fn' => $j->fullname,
438 'nick' => $j->nickname,
443 $pubkey = $j->pubkey;
450 $ret = scrape_dfrn($dfrn);
451 if(is_array($ret) && x($ret,'dfrn-request')) {
452 $network = NETWORK_DFRN;
453 $request = $ret['dfrn-request'];
454 $confirm = $ret['dfrn-confirm'];
455 $notify = $ret['dfrn-notify'];
456 $poll = $ret['dfrn-poll'];
459 $vcard['fn'] = $ret['fn'];
460 $vcard['nick'] = $ret['nick'];
461 $vcard['photo'] = $ret['photo'];
466 if($diaspora && $diaspora_base && $diaspora_guid) {
467 if($mode == PROBE_DIASPORA || ! $notify)
468 $notify = $diaspora_base . 'receive/post/' . $diaspora_guid;
470 $addr = str_replace('acct:', '', $url);
473 if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
475 $network = NETWORK_DIASPORA;
477 $network = NETWORK_OSTATUS;
480 if($hcard && ! $vcard) {
481 $vcard = scrape_vcard($hcard);
483 // Google doesn't use absolute url in profile photos
485 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
486 $h = @parse_url($hcard);
488 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
491 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
495 logger('twitter: setup');
496 $tid = basename($url);
497 $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
499 $poll = $tapi . '?user_id=' . $tid;
501 $poll = $tapi . '?screen_name=' . $tid;
502 $profile = 'http://twitter.com/#!/' . $tid;
507 $vcard['fn'] = $vcard['nick'];
511 if($twitter || ! $poll)
513 if((! isset($vcard)) || (! $profile))
515 if(($at_addr) && (! count($links)))
520 $feedret = scrape_feed(($poll) ? $poll : $url);
521 logger('probe_url: scrape_feed returns: ' . print_r($feedret,true), LOGGER_DATA);
522 if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
523 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
528 if(x($feedret,'photo') && (! x($vcard,'photo')))
529 $vcard['photo'] = $feedret['photo'];
530 require_once('library/simplepie/simplepie.inc');
531 $feed = new SimplePie();
532 $xml = fetch_url($poll);
534 logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
537 logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), $LOGGER_DATA);
539 $feed->set_raw_data($xml);
543 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
546 if(! x($vcard,'photo'))
547 $vcard['photo'] = $feed->get_image_url();
548 $author = $feed->get_author();
551 $vcard['fn'] = unxmlify(trim($author->get_name()));
553 $vcard['fn'] = trim(unxmlify($author->get_email()));
554 if(strpos($vcard['fn'],'@') !== false)
555 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
556 $email = unxmlify($author->get_email());
557 if(! $profile && $author->get_link())
558 $profile = trim(unxmlify($author->get_link()));
559 if(! $vcard['photo']) {
560 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
562 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
563 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
564 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
569 $item = $feed->get_item(0);
571 $author = $item->get_author();
573 $vcard['fn'] = trim(unxmlify($author->get_name()));
575 $vcard['fn'] = trim(unxmlify($author->get_email()));
576 if(strpos($vcard['fn'],'@') !== false)
577 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
578 $email = unxmlify($author->get_email());
579 if(! $profile && $author->get_link())
580 $profile = trim(unxmlify($author->get_link()));
582 if(! $vcard['photo']) {
583 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
584 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
585 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
587 if(! $vcard['photo']) {
588 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
590 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
591 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
592 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
598 if((! $vcard['photo']) && strlen($email))
599 $vcard['photo'] = gravatar_img($email);
600 if($poll === $profile)
601 $lnk = $feed->get_permalink();
602 if(isset($lnk) && strlen($lnk))
605 if(! (x($vcard,'fn')))
606 $vcard['fn'] = notags($feed->get_title());
607 if(! (x($vcard,'fn')))
608 $vcard['fn'] = notags($feed->get_description());
610 if(strpos($vcard['fn'],'Twitter / ') !== false) {
611 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
612 $vcard['fn'] = trim($vcard['fn']);
614 if(! x($vcard,'nick')) {
615 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
616 if(strpos($vcard['nick'],' '))
617 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
626 if(! x($vcard,'photo')) {
628 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
634 $vcard['fn'] = notags($vcard['fn']);
635 $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
638 $result['name'] = $vcard['fn'];
639 $result['nick'] = $vcard['nick'];
640 $result['url'] = $profile;
641 $result['addr'] = $addr;
642 $result['notify'] = $notify;
643 $result['poll'] = $poll;
644 $result['request'] = $request;
645 $result['confirm'] = $confirm;
646 $result['photo'] = $vcard['photo'];
647 $result['priority'] = $priority;
648 $result['network'] = $network;
649 $result['alias'] = $alias;
650 $result['pubkey'] = $pubkey;
652 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);