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');
244 $items = $dom->getElementsByTagName('link');
246 // get Atom/RSS link elements, take the first one of either.
249 foreach($items as $item) {
250 $x = $item->getAttribute('rel');
251 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
252 if(! x($ret,'feed_atom'))
253 $ret['feed_atom'] = $item->getAttribute('href');
255 if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
256 if(! x($ret,'feed_rss'))
257 $ret['feed_rss'] = $item->getAttribute('href');
266 function probe_url($url) {
274 $email_conversant = false;
280 foreach($links as $link) {
281 if($link['@attributes']['rel'] === NAMESPACE_DFRN)
282 $dfrn = unamp($link['@attributes']['href']);
283 if($link['@attributes']['rel'] === 'salmon')
284 $notify = unamp($link['@attributes']['href']);
285 if($link['@attributes']['rel'] === NAMESPACE_FEED)
286 $poll = unamp($link['@attributes']['href']);
287 if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard')
288 $hcard = unamp($link['@attributes']['href']);
289 if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
290 $profile = unamp($link['@attributes']['href']);
291 if($link['@attributes']['rel'] === 'http://joindiaspora.com/seed_location')
295 // Status.Net can have more than one profile URL. We need to match the profile URL
296 // to a contact on incoming messages to prevent spam, and we won't know which one
297 // to match. So in case of two, one of them is stored as an alias. Only store URL's
298 // and not webfinger user@host aliases. If they've got more than two non-email style
299 // aliases, let's hope we're lucky and get one that matches the feed author-uri because
300 // otherwise we're screwed.
302 foreach($links as $link) {
303 if($link['@attributes']['rel'] === 'alias') {
304 if(strpos($link['@attributes']['href'],'@') === false) {
305 if(isset($profile)) {
306 if($link['@attributes']['href'] !== $profile)
307 $alias = unamp($link['@attributes']['href']);
310 $profile = unamp($link['@attributes']['href']);
316 if((strpos($orig_url,'@')) && validate_email($orig_url)) {
317 $email_conversant = true;
323 $ret = scrape_dfrn($dfrn);
324 if(is_array($ret) && x($ret,'dfrn-request')) {
325 $network = NETWORK_DFRN;
326 $request = $ret['dfrn-request'];
327 $confirm = $ret['dfrn-confirm'];
328 $notify = $ret['dfrn-notify'];
329 $poll = $ret['dfrn-poll'];
333 if($network !== NETWORK_DFRN) {
334 $network = NETWORK_OSTATUS;
338 $vcard = scrape_vcard($hcard);
340 // Google doesn't use absolute url in profile photos
342 if((x($vcard,'photo')) && substr($vcard['photo'],0,1) == '/') {
343 $h = @parse_url($hcard);
345 $vcard['photo'] = $h['scheme'] . '://' . $h['host'] . $vcard['photo'];
348 logger('probe_url: scrape_vcard: ' . print_r($vcard,true), LOGGER_DATA);
360 $vcard['fn'] = $vcard['nick'];
362 if((! isset($vcard)) && (! $poll)) {
364 $ret = scrape_feed($url);
365 logger('probe_url: scrape_feed returns: ' . print_r($ret,true), LOGGER_DATA);
366 if(count($ret) && ($ret['feed_atom'] || $ret['feed_rss'])) {
367 $poll = ((x($ret,'feed_atom')) ? unamp($ret['feed_atom']) : unamp($ret['feed_rss']));
370 $vcard['photo'] = $ret['photo'];
371 require_once('simplepie/simplepie.inc');
372 $feed = new SimplePie();
373 $xml = fetch_url($poll);
375 $feed->set_raw_data($xml);
379 if(! x($vcard,'photo'))
380 $vcard['photo'] = $feed->get_image_url();
381 $author = $feed->get_author();
383 $vcard['fn'] = unxmlify(trim($author->get_name()));
385 $vcard['fn'] = trim(unxmlify($author->get_email()));
386 if(strpos($vcard['fn'],'@') !== false)
387 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
388 $vcard['nick'] = strtolower(notags(unxmlify($vcard['fn'])));
389 if(strpos($vcard['nick'],' '))
390 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
391 $email = unxmlify($author->get_email());
394 $item = $feed->get_item(0);
396 $author = $item->get_author();
398 $vcard['fn'] = trim(unxmlify($author->get_name()));
400 $vcard['fn'] = trim(unxmlify($author->get_email()));
401 if(strpos($vcard['fn'],'@') !== false)
402 $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
403 $vcard['nick'] = strtolower(unxmlify($vcard['fn']));
404 if(strpos($vcard['nick'],' '))
405 $vcard['nick'] = trim(substr($vcard['nick'],0,strpos($vcard['nick'],' ')));
406 $email = unxmlify($author->get_email());
408 if(! $vcard['photo']) {
409 $rawmedia = $item->get_item_tags('http://search.yahoo.com/mrss/','thumbnail');
410 if($rawmedia && $rawmedia[0]['attribs']['']['url'])
411 $vcard['photo'] = unxmlify($rawmedia[0]['attribs']['']['url']);
415 if((! $vcard['photo']) && strlen($email))
416 $vcard['photo'] = gravatar_img($email);
417 if($poll === $profile)
418 $lnk = $feed->get_permalink();
419 if(isset($lnk) && strlen($lnk))
421 if(! (x($vcard,'fn')))
422 $vcard['fn'] = notags($feed->get_title());
423 if(! (x($vcard,'fn')))
424 $vcard['fn'] = notags($feed->get_description());
431 if(! x($vcard,'photo')) {
433 $vcard['photo'] = $a->get_baseurl() . '/images/default-profile.jpg' ;
435 $vcard['fn'] = notags($vcard['fn']);
436 $vcard['nick'] = notags($vcard['nick']);
438 $result['name'] = $vcard['fn'];
439 $result['nick'] = $vcard['nick'];
440 $result['url'] = $profile;
441 $result['notify'] = $notify;
442 $result['poll'] = $poll;
443 $result['request'] = $request;
444 $result['confirm'] = $confirm;
445 $result['photo'] = $vcard['photo'];
446 $result['priority'] = $priority;
447 $result['network'] = $network;
448 $result['alias'] = $alias;
450 logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);