-if(! function_exists('scrape_dfrn')) {
-function scrape_dfrn($url, $dont_probe = false) {
-
- $a = get_app();
-
- $ret = array();
-
- logger('scrape_dfrn: url=' . $url);
-
- // Try to fetch the data from noscrape. This is faster than parsing the HTML
- $noscrape = str_replace("/hcard/", "/noscrape/", $url);
- $noscrapejson = fetch_url($noscrape);
- $noscrapedata = array();
- if ($noscrapejson) {
- $noscrapedata = json_decode($noscrapejson, true);
-
- if (is_array($noscrapedata)) {
- if ($noscrapedata["nick"] != "")
- return($noscrapedata);
- else
- unset($noscrapedata["nick"]);
- } else
- $noscrapedata = array();
- }
-
- $s = fetch_url($url);
-
- if (!$s)
- return $ret;
-
- if (!$dont_probe) {
- $probe = probe_url($url);
-
- if (isset($probe["addr"]))
- $ret["addr"] = $probe["addr"];
- }
-
- $headers = $a->get_curl_headers();
- logger('scrape_dfrn: headers=' . $headers, LOGGER_DEBUG);
-
-
- $lines = explode("\n",$headers);
- if(count($lines)) {
- foreach($lines as $line) {
- // don't try and run feeds through the html5 parser
- if(stristr($line,'content-type:') && ((stristr($line,'application/atom+xml')) || (stristr($line,'application/rss+xml'))))
- return ret;
- }
- }
-
- try {
- $dom = HTML5_Parser::parse($s);
- } catch (DOMException $e) {
- logger('scrape_dfrn: parse error: ' . $e);
- }
-
- if(! $dom)
- return $ret;
-
- $items = $dom->getElementsByTagName('link');
-
- // get DFRN link elements
-
- foreach($items as $item) {
- $x = $item->getAttribute('rel');
- if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml'))
- $ret['feed_atom'] = $item->getAttribute('href');
- if(substr($x,0,5) == "dfrn-") {
- $ret[$x] = $item->getAttribute('href');
- }
- if($x === 'lrdd') {
- $decoded = urldecode($item->getAttribute('href'));
- if(preg_match('/acct:([^@]*)@/',$decoded,$matches))
- $ret['nick'] = $matches[1];
- }
- }
-
- // Pull out hCard profile elements
-
- $largest_photo = 0;
-
- $items = $dom->getElementsByTagName('*');
- foreach($items as $item) {
- if(attribute_contains($item->getAttribute('class'), 'vcard')) {
- $level2 = $item->getElementsByTagName('*');
- foreach($level2 as $x) {
- if(attribute_contains($x->getAttribute('class'),'uid'))
- $ret['guid'] = $x->textContent;
- if(attribute_contains($x->getAttribute('class'),'nickname'))
- $ret['nickname'] = $x->textContent;
- if(attribute_contains($x->getAttribute('class'),'fn'))
- $ret['fn'] = $x->textContent;
- if(attribute_contains($x->getAttribute('class'),'searchable'))
- $ret['searchable'] = $x->textContent;
- if(attribute_contains($x->getAttribute('class'),'key'))
- $ret['key'] = $x->textContent;
- if(attribute_contains($x->getAttribute('class'),'url'))
- $ret['url'] = $x->textContent;
- if((attribute_contains($x->getAttribute('class'),'photo'))
- || (attribute_contains($x->getAttribute('class'),'avatar'))) {
- $size = intval($x->getAttribute('width'));
- // dfrn prefers 175, so if we find this, we set largest_size so it can't be topped.
- if(($size > $largest_photo) || ($size == 175) || (! $largest_photo)) {
- $ret['photo'] = $x->getAttribute('src');
- $largest_photo = (($size == 175) ? 9999 : $size);
- }
- }
- }
- }
- }
- return array_merge($ret, $noscrapedata);
-}}
-
-
-
-
-
-
-if(! function_exists('validate_dfrn')) {
-function validate_dfrn($a) {
- $errors = 0;
- if(! x($a,'key'))
- $errors ++;
- if(! x($a,'dfrn-request'))
- $errors ++;
- if(! x($a,'dfrn-confirm'))
- $errors ++;
- if(! x($a,'dfrn-notify'))
- $errors ++;
- if(! x($a,'dfrn-poll'))
- $errors ++;
- return $errors;
-}}
-