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');
50 $decoded = urldecode($item->getAttribute('href'));
51 if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
52 $ret['nick'] = $matches[1];
56 // Pull out hCard profile elements
58 $items = $dom->getElementsByTagName('*');
59 foreach($items as $item) {
60 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
61 $level2 = $item->getElementsByTagName('*');
62 foreach($level2 as $x) {
63 if(attribute_contains($x->getAttribute('class'),'fn'))
64 $ret['fn'] = $x->textContent;
65 if(attribute_contains($x->getAttribute('class'),'photo'))
66 $ret['photo'] = $x->getAttribute('src');
67 if(attribute_contains($x->getAttribute('class'),'key'))
68 $ret['key'] = $x->textContent;
81 if(! function_exists('validate_dfrn')) {
82 function validate_dfrn($a) {
86 if(! x($a,'dfrn-request'))
88 if(! x($a,'dfrn-confirm'))
90 if(! x($a,'dfrn-notify'))
92 if(! x($a,'dfrn-poll'))
97 if(! function_exists('scrape_meta')) {
98 function scrape_meta($url) {
104 logger('scrape_meta: url=' . $url);
106 $s = fetch_url($url);
111 $headers = $a->get_curl_headers();
112 logger('scrape_meta: headers=' . $headers, LOGGER_DEBUG);
114 $lines = explode("\n",$headers);
116 foreach($lines as $line) {
117 // don't try and run feeds through the html5 parser
118 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
125 $dom = HTML5_Parser::parse($s);
130 $items = $dom->getElementsByTagName('meta');
132 // get DFRN link elements
134 foreach($items as $item) {
135 $x = $item->getAttribute('name');
136 if(substr($x,0,5) == "dfrn-")
137 $ret[$x] = $item->getAttribute('content');
144 if(! function_exists('scrape_vcard')) {
145 function scrape_vcard($url) {
151 logger('scrape_vcard: url=' . $url);
153 $s = fetch_url($url);
158 $headers = $a->get_curl_headers();
159 $lines = explode("\n",$headers);
161 foreach($lines as $line) {
162 // don't try and run feeds through the html5 parser
163 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
168 $dom = HTML5_Parser::parse($s);
173 // Pull out hCard profile elements
177 $items = $dom->getElementsByTagName('*');
178 foreach($items as $item) {
179 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
180 $level2 = $item->getElementsByTagName('*');
181 foreach($level2 as $x) {
182 if(attribute_contains($x->getAttribute('class'),'fn'))
183 $ret['fn'] = $x->textContent;
184 if((attribute_contains($x->getAttribute('class'),'photo'))
185 || (attribute_contains($x->getAttribute('class'),'avatar'))) {
186 $size = intval($x->getAttribute('width'));
187 if(($size > $largest_photo) || (! $largest_photo)) {
188 $ret['photo'] = $x->getAttribute('src');
189 $largest_photo = $size;
192 if((attribute_contains($x->getAttribute('class'),'nickname'))
193 || (attribute_contains($x->getAttribute('class'),'uid')))
194 $ret['nick'] = $x->textContent;
203 if(! function_exists('scrape_feed')) {
204 function scrape_feed($url) {
209 $s = fetch_url($url);
214 $headers = $a->get_curl_headers();
215 logger('scrape_feed: headers=' . $headers, LOGGER_DEBUG);
217 $lines = explode("\n",$headers);
219 foreach($lines as $line) {
220 if(stristr($line,'content-type:')) {
221 if(stristr($line,'application/atom+xml') || stristr($s,'<feed')) {
222 $ret['feed_atom'] = $url;
225 if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
226 $ret['feed_rss'] = $url;
233 $dom = HTML5_Parser::parse($s);
239 $items = $dom->getElementsByTagName('img');
241 // get img elements (twitter)
244 foreach($items as $item) {
245 $x = $item->getAttribute('id');
246 if($x === 'profile-image') {
247 $ret['photo'] = $item->getAttribute('src');
253 $head = $dom->getElementsByTagName('base');
255 foreach($head as $head0) {
256 $basename = $head0->getAttribute('href');
261 $basename = substr($url,0,strrpos($url,'/')) . '/';
263 $items = $dom->getElementsByTagName('link');
265 // get Atom/RSS link elements, take the first one of either.
268 foreach($items as $item) {
269 $x = $item->getAttribute('rel');
270 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
271 if(! x($ret,'feed_atom'))
272 $ret['feed_atom'] = $item->getAttribute('href');
274 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
275 if(! x($ret,'feed_rss'))
276 $ret['feed_rss'] = $item->getAttribute('href');
281 // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
283 if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
284 $ret['feed_atom'] = $basename . $ret['feed_atom'];
285 if(x($ret,'feed_rss') && (! strstr($ret['feed_rss'],'://')))
286 $ret['feed_rss'] = $basename . $ret['feed_rss'];
292 function probe_url($url) {
293 require_once('include/email.php');
304 $email_conversant = false;
306 $twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
308 $at_addr = ((strpos($url,'@') !== false) ? true : false);
312 if(strpos($url,'mailto:') !== false && $at_addr) {
313 $url = str_replace('mailto:','',$url);
320 logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
321 foreach($links as $link) {
322 if($link['@attributes']['rel'] === NAMESPACE_ZOT)
323 $zot = unamp($link['@attributes']['href']);
324 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
325 $dfrn = unamp($link['@attributes']['href']);
326 if($link['@attributes']['rel'] === 'salmon')
327 $notify = unamp($link['@attributes']['href']);
328 if($link['@attributes']['rel'] === NAMESPACE_FEED)
329 $poll = unamp($link['@attributes']['href']);
330 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
331 $hcard = unamp($link['@attributes']['href']);
332 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
333 $profile = unamp($link['@attributes']['href']);
334 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location') {
335 $diaspora_base = unamp($link['@attributes']['href']);
338 if($link['@attributes']['rel'] === 'http://joindiaspora.com/guid') {
339 $diaspora_guid = unamp($link['@attributes']['href']);
342 if($link['@attributes']['rel'] === 'diaspora-public-key') {
343 $diaspora_key = base64_decode(unamp($link['@attributes']['href']));
344 $pubkey = rsatopem($diaspora_key);
349 // Status.Net can have more than one profile URL. We need to match the profile URL
350 // to a contact on incoming messages to prevent spam, and we won't know which one
351 // to match. So in case of two, one of them is stored as an alias. Only store URL's
352 // and not webfinger user@host aliases. If they've got more than two non-email style
353 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
354 // otherwise we're screwed.
356 foreach($links as $link) {
357 if($link['@attributes']['rel'] === 'alias') {
358 if(strpos($link['@attributes']['href'],'@') === false) {
359 if(isset($profile)) {
360 if($link['@attributes']['href'] !== $profile)
361 $alias = unamp($link['@attributes']['href']);
364 $profile = unamp($link['@attributes']['href']);
374 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
375 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
378 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
381 if(count($x) && count($r)) {
382 $mailbox = construct_mailbox_name($r[0]);
384 openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
385 $mbox = email_connect($mailbox,$r[0]['user'],$password);
389 $msgs = email_poll($mbox,$orig_url);
392 $network = NETWORK_MAIL;
393 $name = substr($url,0,strpos($url,'@'));
394 $profile = 'http://' . substr($url,strpos($url,'@')+1);
395 // fix nick character range
396 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
397 $notify = 'smtp ' . random_string();
398 $poll = 'email ' . random_string();
400 $x = email_msg_meta($mbox,$msgs[0]);
401 if(stristr($x->from,$orig_url))
402 $adr = imap_rfc822_parse_adrlist($x->from,'');
403 elseif(stristr($x->to,$orig_url))
404 $adr = imap_rfc822_parse_adrlist($x->to,'');
405 if(isset($adr) && strlen($adr[0]->personal))
406 $vcard['fn'] = notags($adr[0]->personal);
415 $s = fetch_url($zot);
417 $j = json_decode($s);
419 $network = NETWORK_ZOT;
421 'fn' => $j->fullname,
422 'nick' => $j->nickname,
427 $pubkey = $j->pubkey;
434 $ret = scrape_dfrn($dfrn);
435 if(is_array($ret) && x($ret,'dfrn-request')) {
436 $network = NETWORK_DFRN;
437 $request = $ret['dfrn-request'];
438 $confirm = $ret['dfrn-confirm'];
439 $notify = $ret['dfrn-notify'];
440 $poll = $ret['dfrn-poll'];
444 if($diaspora && $diaspora_base && $diaspora_guid) {
445 $notify = $diaspora_base . 'receive/post/' . $diaspora_guid;
447 $addr = str_replace('acct:', '', $url);
450 if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
452 $network = NETWORK_DIASPORA;
454 $network = NETWORK_OSTATUS;
458 $vcard = scrape_vcard($hcard);
460 // Google doesn't use absolute url in profile photos
462 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
463 $h = @parse_url($hcard);
465 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
468 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
472 logger('twitter: setup');
473 $tid = basename($url);
474 $tapi = 'https://api.twitter.com/1/statuses/user_timeline.rss';
476 $poll = $tapi . '?user_id=' . $tid;
478 $poll = $tapi . '?screen_name=' . $tid;
479 $profile = 'http://twitter.com/#!/' . $tid;
484 $vcard['fn'] = $vcard['nick'];
488 if($twitter || ! $poll)
490 if((! isset($vcard)) || (! $profile))
492 if(($at_addr) && (! count($links)))
497 $feedret = scrape_feed(($poll) ? $poll : $url);
498 logger('probe_url: scrape_feed returns: ' . print_r($feedret,true), LOGGER_DATA);
499 if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
500 $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
505 if(x($feedret,'photo') && (! x($vcard,'photo')))
506 $vcard['photo'] = $feedret['photo'];
507 require_once('library/simplepie/simplepie.inc');
508 $feed = new SimplePie();
509 $xml = fetch_url($poll);
511 logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
514 logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), $LOGGER_DATA);
516 $feed->set_raw_data($xml);
520 logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
523 if(! x($vcard,'photo'))
524 $vcard['photo'] = $feed->get_image_url();
525 $author = $feed->get_author();
528 $vcard['fn'] = unxmlify(trim($author->get_name()));
530 $vcard['fn'] = trim(unxmlify($author->get_email()));
531 if(strpos($vcard['fn'],'@') !== false)
532 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
533 $email = unxmlify($author->get_email());
534 if(! $profile && $author->get_link())
535 $profile = trim(unxmlify($author->get_link()));
536 if(! $vcard['photo']) {
537 $rawtags = $feed->get_feed_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
539 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
540 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
541 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
546 $item = $feed->get_item(0);
548 $author = $item->get_author();
550 $vcard['fn'] = trim(unxmlify($author->get_name()));
552 $vcard['fn'] = trim(unxmlify($author->get_email()));
553 if(strpos($vcard['fn'],'@') !== false)
554 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
555 $email = unxmlify($author->get_email());
556 if(! $profile && $author->get_link())
557 $profile = trim(unxmlify($author->get_link()));
559 if(! $vcard['photo']) {
560 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
561 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
562 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
564 if(! $vcard['photo']) {
565 $rawtags = $item->get_item_tags( SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
567 $elems = $rawtags[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10];
568 if((x($elems,'link')) && ($elems['link'][0]['attribs']['']['rel'] === 'photo'))
569 $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
575 if((! $vcard['photo']) && strlen($email))
576 $vcard['photo'] = gravatar_img($email);
577 if($poll === $profile)
578 $lnk = $feed->get_permalink();
579 if(isset($lnk) && strlen($lnk))
582 if(! (x($vcard,'fn')))
583 $vcard['fn'] = notags($feed->get_title());
584 if(! (x($vcard,'fn')))
585 $vcard['fn'] = notags($feed->get_description());
587 if(strpos($vcard['fn'],'Twitter / ') !== false) {
588 $vcard['fn'] = substr($vcard['fn'],strpos($vcard['fn'],'/')+1);
589 $vcard['fn'] = trim($vcard['fn']);
591 if(! x($vcard,'nick')) {
592 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
593 if(strpos($vcard['nick'],' '))
594 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
603 if(! x($vcard,'photo')) {
605 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
611 $vcard['fn'] = notags($vcard['fn']);
612 $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
615 $result['name'] = $vcard['fn'];
616 $result['nick'] = $vcard['nick'];
617 $result['url'] = $profile;
618 $result['addr'] = $addr;
619 $result['notify'] = $notify;
620 $result['poll'] = $poll;
621 $result['request'] = $request;
622 $result['confirm'] = $confirm;
623 $result['photo'] = $vcard['photo'];
624 $result['priority'] = $priority;
625 $result['network'] = $network;
626 $result['alias'] = $alias;
627 $result['pubkey'] = $pubkey;
629 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);