3 require_once('library/HTML5/Parser.php');
5 if(! function_exists('scrape_dfrn')) {
6 function scrape_dfrn($url) {
12 logger('scrape_dfrn: url=' . $url);
19 $headers = $a->get_curl_headers();
20 logger('scrape_dfrn: headers=' . $headers, LOGGER_DEBUG);
23 $lines = explode("\n",$headers);
25 foreach($lines as $line) {
26 // don't try and run feeds through the html5 parser
27 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
33 $dom = HTML5_Parser::parse($s);
38 $items = $dom->getElementsByTagName('link');
40 // get DFRN link elements
42 foreach($items as $item) {
43 $x = $item->getAttribute('rel');
44 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
45 $ret['feed_atom'] = $item->getAttribute('href');
46 if(substr($x,0,5) == "dfrn-")
47 $ret[$x] = $item->getAttribute('href');
49 $decoded = urldecode($item->getAttribute('href'));
50 if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
51 $ret['nick'] = $matches[1];
55 // Pull out hCard profile elements
57 $items = $dom->getElementsByTagName('*');
58 foreach($items as $item) {
59 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
60 $level2 = $item->getElementsByTagName('*');
61 foreach($level2 as $x) {
62 if(attribute_contains($x->getAttribute('class'),'fn'))
63 $ret['fn'] = $x->textContent;
64 if(attribute_contains($x->getAttribute('class'),'photo'))
65 $ret['photo'] = $x->getAttribute('src');
66 if(attribute_contains($x->getAttribute('class'),'key'))
67 $ret['key'] = $x->textContent;
80 if(! function_exists('validate_dfrn')) {
81 function validate_dfrn($a) {
85 if(! x($a,'dfrn-request'))
87 if(! x($a,'dfrn-confirm'))
89 if(! x($a,'dfrn-notify'))
91 if(! x($a,'dfrn-poll'))
96 if(! function_exists('scrape_meta')) {
97 function scrape_meta($url) {
103 logger('scrape_meta: url=' . $url);
105 $s = fetch_url($url);
110 $headers = $a->get_curl_headers();
111 logger('scrape_meta: headers=' . $headers, LOGGER_DEBUG);
113 $lines = explode("\n",$headers);
115 foreach($lines as $line) {
116 // don't try and run feeds through the html5 parser
117 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
124 $dom = HTML5_Parser::parse($s);
129 $items = $dom->getElementsByTagName('meta');
131 // get DFRN link elements
133 foreach($items as $item) {
134 $x = $item->getAttribute('name');
135 if(substr($x,0,5) == "dfrn-")
136 $ret[$x] = $item->getAttribute('content');
143 if(! function_exists('scrape_vcard')) {
144 function scrape_vcard($url) {
150 logger('scrape_vcard: url=' . $url);
152 $s = fetch_url($url);
157 $headers = $a->get_curl_headers();
158 $lines = explode("\n",$headers);
160 foreach($lines as $line) {
161 // don't try and run feeds through the html5 parser
162 if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
167 $dom = HTML5_Parser::parse($s);
172 // Pull out hCard profile elements
174 $items = $dom->getElementsByTagName('*');
175 foreach($items as $item) {
176 if(attribute_contains($item->getAttribute('class'), 'vcard')) {
177 $level2 = $item->getElementsByTagName('*');
178 foreach($level2 as $x) {
179 if(attribute_contains($x->getAttribute('class'),'fn'))
180 $ret['fn'] = $x->textContent;
181 if((attribute_contains($x->getAttribute('class'),'photo'))
182 || (attribute_contains($x->getAttribute('class'),'avatar')))
183 $ret['photo'] = $x->getAttribute('src');
184 if((attribute_contains($x->getAttribute('class'),'nickname'))
185 || (attribute_contains($x->getAttribute('class'),'uid')))
186 $ret['nick'] = $x->textContent;
195 if(! function_exists('scrape_feed')) {
196 function scrape_feed($url) {
201 $s = fetch_url($url);
206 $headers = $a->get_curl_headers();
207 logger('scrape_feed: headers=' . $headers, LOGGER_DEBUG);
209 $lines = explode("\n",$headers);
211 foreach($lines as $line) {
212 if(stristr($line,'content-type:')) {
213 if(stristr($line,'application/atom+xml') || stristr($s,'<feed')) {
214 $ret['feed_atom'] = $url;
217 if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
218 $ret['feed_rss'] = $url;
225 $dom = HTML5_Parser::parse($s);
231 $items = $dom->getElementsByTagName('img');
233 // get img elements (twitter)
236 foreach($items as $item) {
237 $x = $item->getAttribute('id');
238 if($x === 'profile-image') {
239 $ret['photo'] = $item->getAttribute('src');
245 $head = $dom->getElementsByTagName('base');
247 foreach($head as $head0) {
248 $basename = $head0->getAttribute('href');
253 $basename = substr($url,0,strrpos($url,'/')) . '/';
255 $items = $dom->getElementsByTagName('link');
257 // get Atom/RSS link elements, take the first one of either.
260 foreach($items as $item) {
261 $x = $item->getAttribute('rel');
262 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
263 if(! x($ret,'feed_atom'))
264 $ret['feed_atom'] = $item->getAttribute('href');
266 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
267 if(! x($ret,'feed_rss'))
268 $ret['feed_rss'] = $item->getAttribute('href');
273 // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
275 if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
276 $ret['feed_atom'] = $basename . $ret['feed_atom'];
277 if(x($ret,'feed_rss') && (! strstr($ret['feed_rss'],'://')))
278 $ret['feed_rss'] = $basename . $ret['feed_rss'];
284 function probe_url($url) {
285 require_once('include/email.php');
293 $email_conversant = false;
299 logger('probe_url: found lrdd links: ' . print_r($links,true), LOGGER_DATA);
300 foreach($links as $link) {
301 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
302 $dfrn = unamp($link['@attributes']['href']);
303 if($link['@attributes']['rel'] === 'salmon')
304 $notify = unamp($link['@attributes']['href']);
305 if($link['@attributes']['rel'] === NAMESPACE_FEED)
306 $poll = unamp($link['@attributes']['href']);
307 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
308 $hcard = unamp($link['@attributes']['href']);
309 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
310 $profile = unamp($link['@attributes']['href']);
311 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location')
315 // Status.Net can have more than one profile URL. We need to match the profile URL
316 // to a contact on incoming messages to prevent spam, and we won't know which one
317 // to match. So in case of two, one of them is stored as an alias. Only store URL's
318 // and not webfinger user@host aliases. If they've got more than two non-email style
319 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
320 // otherwise we're screwed.
322 foreach($links as $link) {
323 if($link['@attributes']['rel'] === 'alias') {
324 if(strpos($link['@attributes']['href'],'@') === false) {
325 if(isset($profile)) {
326 if($link['@attributes']['href'] !== $profile)
327 $alias = unamp($link['@attributes']['href']);
330 $profile = unamp($link['@attributes']['href']);
340 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
341 $x = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1",
344 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d AND `server` != '' LIMIT 1",
347 if(count($x) && count($r)) {
348 $mailbox = construct_mailbox_name($r[0]);
350 openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
351 $mbox = email_connect($mailbox,$r[0]['user'],$password);
355 $msgs = email_poll($mbox,$orig_url);
358 $network = NETWORK_MAIL;
359 $name = substr($url,0,strpos($url,'@'));
360 $profile = 'http://' . substr($url,strpos($url,'@')+1);
361 // fix nick character range
362 $vcard = array('fn' => $name, 'nick' => $name, 'photo' => gravatar_img($url));
363 $notify = 'smtp ' . random_string();
364 $poll = 'email ' . random_string();
366 $x = email_msg_meta($mbox,$msgs[0]);
367 if(stristr($x->from,$orig_url))
368 $adr = imap_rfc822_parse_adrlist($x->from,'');
369 elseif(stristr($x->to,$orig_url))
370 $adr = imap_rfc822_parse_adrlist($x->to,'');
371 if(isset($adr) && strlen($adr[0]->personal))
372 $vcard['fn'] = notags($adr[0]->personal);
381 $ret = scrape_dfrn($dfrn);
382 if(is_array($ret) && x($ret,'dfrn-request')) {
383 $network = NETWORK_DFRN;
384 $request = $ret['dfrn-request'];
385 $confirm = $ret['dfrn-confirm'];
386 $notify = $ret['dfrn-notify'];
387 $poll = $ret['dfrn-poll'];
391 if($network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
392 $network = NETWORK_OSTATUS;
396 $vcard = scrape_vcard($hcard);
398 // Google doesn't use absolute url in profile photos
400 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
401 $h = @parse_url($hcard);
403 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
406 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
418 $vcard['fn'] = $vcard['nick'];
420 if((! isset($vcard)) && (! $poll)) {
422 $ret = scrape_feed($url);
423 logger('probe_url: scrape_feed returns: ' . print_r($ret,true), LOGGER_DATA);
424 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
425 $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
428 $vcard['photo'] = $ret['photo'];
429 require_once('simplepie/simplepie.inc');
430 $feed = new SimplePie();
431 $xml = fetch_url($poll);
433 $feed->set_raw_data($xml);
437 if(! x($vcard,'photo'))
438 $vcard['photo'] = $feed->get_image_url();
439 $author = $feed->get_author();
441 $vcard['fn'] = unxmlify(trim($author->get_name()));
443 $vcard['fn'] = trim(unxmlify($author->get_email()));
444 if(strpos($vcard['fn'],'@') !== false)
445 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
446 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
447 if(strpos($vcard['nick'],' '))
448 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
449 $email = unxmlify($author->get_email());
452 $item = $feed->get_item(0);
454 $author = $item->get_author();
456 $vcard['fn'] = trim(unxmlify($author->get_name()));
458 $vcard['fn'] = trim(unxmlify($author->get_email()));
459 if(strpos($vcard['fn'],'@') !== false)
460 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
461 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
462 if(strpos($vcard['nick'],' '))
463 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
464 $email = unxmlify($author->get_email());
466 if(! $vcard['photo']) {
467 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
468 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
469 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
473 if((! $vcard['photo']) && strlen($email))
474 $vcard['photo'] = gravatar_img($email);
475 if($poll === $profile)
476 $lnk = $feed->get_permalink();
477 if(isset($lnk) && strlen($lnk))
479 if(! (x($vcard,'fn')))
480 $vcard['fn'] = notags($feed->get_title());
481 if(! (x($vcard,'fn')))
482 $vcard['fn'] = notags($feed->get_description());
489 if(! x($vcard,'photo')) {
491 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
493 $vcard['fn'] = notags($vcard['fn']);
494 $vcard['nick'] = notags($vcard['nick']);
497 $result['name'] = $vcard['fn'];
498 $result['nick'] = $vcard['nick'];
499 $result['url'] = $profile;
500 $result['addr'] = $addr;
501 $result['notify'] = $notify;
502 $result['poll'] = $poll;
503 $result['request'] = $request;
504 $result['confirm'] = $confirm;
505 $result['photo'] = $vcard['photo'];
506 $result['priority'] = $priority;
507 $result['network'] = $network;
508 $result['alias'] = $alias;
510 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);