]> git.mxchange.org Git - friendica.git/blobdiff - include/Scrape.php
OStatus: The gcontact table will now be updated from the conversation as well
[friendica.git] / include / Scrape.php
index ce18bb10333f5c5dddf573c609263512716a28e3..bc6aebbdcfdee7b388c57f6905e2f008dc258647 100644 (file)
@@ -4,7 +4,7 @@ require_once('library/HTML5/Parser.php');
 require_once('include/crypto.php');
 
 if(! function_exists('scrape_dfrn')) {
-function scrape_dfrn($url) {
+function scrape_dfrn($url, $dont_probe = false) {
 
        $a = get_app();
 
@@ -12,11 +12,30 @@ function scrape_dfrn($url) {
 
        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);
+       }
+
        $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);
 
@@ -84,8 +103,7 @@ function scrape_dfrn($url) {
                        }
                }
        }
-
-       return $ret;
+       return array_merge($ret, $noscrapedata);
 }}
 
 
@@ -228,7 +246,9 @@ function scrape_feed($url) {
        $a = get_app();
 
        $ret = array();
-       $s = fetch_url($url);
+       $cookiejar = tempnam(get_temppath(), 'cookiejar-scrape-feed-');
+       $s = fetch_url($url, false, $redirects, 0, Null, $cookiejar);
+       unlink($cookiejar);
 
        $headers = $a->get_curl_headers();
        $code = $a->get_curl_code();
@@ -249,7 +269,7 @@ function scrape_feed($url) {
                                        $ret['feed_atom'] = $url;
                                        return $ret;
                                }
-                               if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
+                               if(stristr($line,'application/rss+xml') || stristr($s,'<rss')) {
                                        $ret['feed_rss'] = $url;
                                        return $ret;
                                }
@@ -262,47 +282,40 @@ function scrape_feed($url) {
                }
        }
 
-       try {
-               $dom = HTML5_Parser::parse($s);
-       } catch (DOMException $e) {
-               logger('scrape_feed: parse error: ' . $e);
-       }
+       $basename = implode('/', array_slice(explode('/',$url),0,3)) . '/';
 
-       if(! $dom) {
-               logger('scrape_feed: failed to parse.');
-               return $ret;
-       }
+       $doc = new DOMDocument();
+       @$doc->loadHTML($s);
+       $xpath = new DomXPath($doc);
 
+       $base = $xpath->query("//base");
+       foreach ($base as $node) {
+               $attr = array();
 
-       $head = $dom->getElementsByTagName('base');
-       if($head) {
-               foreach($head as $head0) {
-                       $basename = $head0->getAttribute('href');
-                       break;
-               }
+               if ($node->attributes->length)
+                       foreach ($node->attributes as $attribute)
+                               $attr[$attribute->name] = $attribute->value;
+
+               if ($attr["href"] != "")
+                       $basename = $attr["href"] ;
        }
-       if(! $basename)
-               $basename = implode('/', array_slice(explode('/',$url),0,3)) . '/';
 
-       $items = $dom->getElementsByTagName('link');
+       $list = $xpath->query("//link");
+       foreach ($list as $node) {
+               $attr = array();
 
-       // get Atom/RSS link elements, take the first one of either.
+               if ($node->attributes->length)
+                       foreach ($node->attributes as $attribute)
+                               $attr[$attribute->name] = $attribute->value;
 
-       if($items) {
-               foreach($items as $item) {
-                       $x = $item->getAttribute('rel');
-                       if(($x === 'alternate') && ($item->getAttribute('type') === 'application/atom+xml')) {
-                               if(! x($ret,'feed_atom'))
-                                       $ret['feed_atom'] = $item->getAttribute('href');
-                       }
-                       if(($x === 'alternate') && ($item->getAttribute('type') === 'application/rss+xml')) {
-                               if(! x($ret,'feed_rss'))
-                                       $ret['feed_rss'] = $item->getAttribute('href');
-                       }
-               }
+               if (($attr["rel"] == "alternate") AND ($attr["type"] == "application/atom+xml"))
+                       $ret["feed_atom"] = $attr["href"];
+
+               if (($attr["rel"] == "alternate") AND ($attr["type"] == "application/rss+xml"))
+                       $ret["feed_rss"] = $attr["href"];
        }
 
-       // Drupal and perhaps others only provide relative URL's. Turn them into absolute.
+       // Drupal and perhaps others only provide relative URLs. Turn them into absolute.
 
        if(x($ret,'feed_atom') && (! strstr($ret['feed_atom'],'://')))
                $ret['feed_atom'] = $basename . $ret['feed_atom'];
@@ -327,7 +340,7 @@ function scrape_feed($url) {
  * PROBE_DIASPORA has a bias towards returning Diaspora information
  * while PROBE_NORMAL has a bias towards dfrn/zot - in the case where
  * an address (such as a Friendica address) supports more than one type
- * of network. 
+ * of network.
  *
  */
 
@@ -335,7 +348,7 @@ function scrape_feed($url) {
 define ( 'PROBE_NORMAL',   0);
 define ( 'PROBE_DIASPORA', 1);
 
-function probe_url($url, $mode = PROBE_NORMAL) {
+function probe_url($url, $mode = PROBE_NORMAL, $level = 1) {
        require_once('include/email.php');
 
        $result = array();
@@ -414,7 +427,7 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                                                $pubkey = $diaspora_key;
                                        $diaspora = true;
                                }
-                               if($link['@attributes']['rel'] === 'http://ostatus.org/schema/1.0/subscribe') {
+                               if(($link['@attributes']['rel'] === 'http://ostatus.org/schema/1.0/subscribe') AND ($mode == PROBE_NORMAL)) {
                                        $diaspora = false;
                                }
                        }
@@ -509,6 +522,7 @@ function probe_url($url, $mode = PROBE_NORMAL) {
        }
 
        if($mode == PROBE_NORMAL) {
+
                if(strlen($zot)) {
                        $s = fetch_url($zot);
                        if($s) {
@@ -528,8 +542,9 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                        }
                }
 
+
                if(strlen($dfrn)) {
-                       $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn);
+                       $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn, true);
                        if(is_array($ret) && x($ret,'dfrn-request')) {
                                $network = NETWORK_DFRN;
                                $request = $ret['dfrn-request'];
@@ -545,9 +560,23 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                }
        }
 
+       // Scrape the public key from the hcard.
+       // Diaspora will remove it from the webfinger somewhere in the future.
+       if (($hcard != "") AND ($pubkey == "")) {
+               $ret = scrape_dfrn(($hcard) ? $hcard : $dfrn, true);
+               if (isset($ret["key"])) {
+                       $hcard_key = $ret["key"];
+                       if(strstr($hcard_key,'RSA '))
+                               $pubkey = rsatopem($hcard_key);
+                       else
+                               $pubkey = $hcard_key;
+               }
+       }
        if($diaspora && $diaspora_base && $diaspora_guid) {
-               if($mode == PROBE_DIASPORA || ! $notify) {
-                       $notify = $diaspora_base . 'receive/users/' . $diaspora_guid;
+               $diaspora_notify = $diaspora_base.'receive/users/'.$diaspora_guid;
+
+               if($mode == PROBE_DIASPORA || ! $notify || ($notify == $diaspora_notify)) {
+                       $notify = $diaspora_notify;
                        $batch  = $diaspora_base . 'receive/public' ;
                }
                if(strpos($url,'@'))
@@ -557,7 +586,7 @@ function probe_url($url, $mode = PROBE_NORMAL) {
        if($network !== NETWORK_ZOT && $network !== NETWORK_DFRN && $network !== NETWORK_MAIL) {
                if($diaspora)
                        $network = NETWORK_DIASPORA;
-               elseif($has_lrdd)
+               elseif($has_lrdd AND ($notify))
                        $network  = NETWORK_OSTATUS;
 
                if(strpos($url,'@'))
@@ -634,6 +663,7 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                if($check_feed) {
 
                        $feedret = scrape_feed(($poll) ? $poll : $url);
+
                        logger('probe_url: scrape_feed ' . (($poll)? $poll : $url) . ' returns: ' . print_r($feedret,true), LOGGER_DATA);
                        if(count($feedret) && ($feedret['feed_atom'] || $feedret['feed_rss'])) {
                                $poll = ((x($feedret,'feed_atom')) ? unamp($feedret['feed_atom']) : unamp($feedret['feed_rss']));
@@ -645,7 +675,9 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                                $vcard['photo'] = $feedret['photo'];
                        require_once('library/simplepie/simplepie.inc');
                        $feed = new SimplePie();
-                       $xml = fetch_url($poll);
+                       $cookiejar = tempnam(get_temppath(), 'cookiejar-scrape-feed-');
+                       $xml = fetch_url($poll, false, $redirects, 0, Null, $cookiejar);
+                       unlink($cookiejar);
 
                        logger('probe_url: fetch feed: ' . $poll . ' returns: ' . $xml, LOGGER_DATA);
                        $a = get_app();
@@ -653,12 +685,13 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                        logger('probe_url: scrape_feed: headers: ' . $a->get_curl_headers(), LOGGER_DATA);
 
                        // Don't try and parse an empty string
-                       $feed->set_raw_data(($xml) ? $xml : '<?xml version="1.0" encoding="utf-8" ?><xml></xml>');
+                       $feed->set_raw_data(($xml) ? $xml : '<?xml version="1.0" encoding="utf-8" ?><xml></xml>');
 
                        $feed->init();
-                       if($feed->error())
+                       if($feed->error()) {
                                logger('probe_url: scrape_feed: Error parsing XML: ' . $feed->error());
-
+                               $network = NETWORK_PHANTOM;
+                       }
 
                        if(! x($vcard,'photo'))
                                $vcard['photo'] = $feed->get_image_url();
@@ -670,6 +703,7 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                                        $vcard['fn'] = trim(unxmlify($author->get_email()));
                                if(strpos($vcard['fn'],'@') !== false)
                                        $vcard['fn'] = substr($vcard['fn'],0,strpos($vcard['fn'],'@'));
+
                                $email = unxmlify($author->get_email());
                                if(! $profile && $author->get_link())
                                        $profile = trim(unxmlify($author->get_link()));
@@ -681,6 +715,15 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                                                        $vcard['photo'] = $elems['link'][0]['attribs']['']['href'];
                                        }
                                }
+                               // Fetch fullname via poco:displayName
+                               $pocotags = $feed->get_feed_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'author');
+                               if ($pocotags) {
+                                       $elems = $pocotags[0]['child']['http://portablecontacts.net/spec/1.0'];
+                                       if (isset($elems["displayName"]))
+                                               $vcard['fn'] = $elems["displayName"][0]["data"];
+                                       if (isset($elems["preferredUsername"]))
+                                               $vcard['nick'] = $elems["preferredUsername"][0]["data"];
+                               }
                        }
                        else {
                                $item = $feed->get_item(0);
@@ -712,6 +755,45 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                                }
                        }
 
+                       // Workaround for misconfigured Friendica servers
+                       if (($network == "") AND (strstr($url, "/profile/"))) {
+                               $noscrape = str_replace("/profile/", "/noscrape/", $url);
+                               $noscrapejson = fetch_url($noscrape);
+                               if ($noscrapejson) {
+
+                                       $network = NETWORK_DFRN;
+
+                                       $poco = str_replace("/profile/", "/poco/", $url);
+
+                                       $noscrapedata = json_decode($noscrapejson, true);
+
+                                       if (isset($noscrapedata["addr"]))
+                                               $addr = $noscrapedata["addr"];
+
+                                       if (isset($noscrapedata["fn"]))
+                                               $vcard["fn"] = $noscrapedata["fn"];
+
+                                       if (isset($noscrapedata["key"]))
+                                               $pubkey = $noscrapedata["key"];
+
+                                       if (isset($noscrapedata["photo"]))
+                                               $vcard["photo"] = $noscrapedata["photo"];
+
+                                       if (isset($noscrapedata["dfrn-request"]))
+                                               $request = $noscrapedata["dfrn-request"];
+
+                                       if (isset($noscrapedata["dfrn-confirm"]))
+                                               $confirm = $noscrapedata["dfrn-confirm"];
+
+                                       if (isset($noscrapedata["dfrn-notify"]))
+                                               $notify = $noscrapedata["dfrn-notify"];
+
+                                       if (isset($noscrapedata["dfrn-poll"]))
+                                               $poll = $noscrapedata["dfrn-poll"];
+
+                               }
+                       }
+
                        if((! $vcard['photo']) && strlen($email))
                                $vcard['photo'] = avatar_img($email);
                        if($poll === $profile)
@@ -757,21 +839,24 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                $vcard['fn'] = $url;
 
        if (($notify != "") AND ($poll != "")) {
-               $baseurl = matching($notify, $poll);
+               $baseurl = matching(normalise_link($notify), normalise_link($poll));
 
-               $baseurl2 = matching($baseurl, $profile);
+               $baseurl2 = matching($baseurl, normalise_link($profile));
                if ($baseurl2 != "")
                        $baseurl = $baseurl2;
        }
 
        if (($baseurl == "") AND ($notify != ""))
-               $baseurl = matching($profile, $notify);
+               $baseurl = matching(normalise_link($profile), normalise_link($notify));
 
        if (($baseurl == "") AND ($poll != ""))
-               $baseurl = matching($profile, $poll);
+               $baseurl = matching(normalise_link($profile), normalise_link($poll));
 
        $baseurl = rtrim($baseurl, "/");
 
+       if(strpos($url,'@') AND ($addr == "") AND ($network == NETWORK_DFRN))
+               $addr = str_replace('acct:', '', $url);
+
        $vcard['fn'] = notags($vcard['fn']);
        $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
 
@@ -794,16 +879,28 @@ function probe_url($url, $mode = PROBE_NORMAL) {
 
        logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);
 
-       // Trying if it maybe a diaspora account
-       if (($result['network'] == NETWORK_FEED) OR ($result['addr'] == "")) {
-               require_once('include/bbcode.php');
-               $address = GetProfileUsername($url, "", true);
-               $result2 = probe_url($address, $mode);
-               if ($result2['network'] != "")
-                       $result = $result2;
+       if ($level == 1) {
+               // Trying if it maybe a diaspora account
+               if (($result['network'] == NETWORK_FEED) OR ($result['addr'] == "")) {
+                       require_once('include/bbcode.php');
+                       $address = GetProfileUsername($url, "", true);
+                       $result2 = probe_url($address, $mode, ++$level);
+                       if ($result2['network'] != "")
+                               $result = $result2;
+               }
+
+               // Maybe it's some non standard GNU Social installation (Single user, subfolder or no uri rewrite)
+               if (($result['network'] == NETWORK_FEED) AND ($result['baseurl'] != "") AND ($result['nick'] != "")) {
+                       $addr = $result['nick'].'@'.str_replace("http://", "", $result['baseurl']);
+                       $result2 = probe_url($addr, $mode, ++$level);
+                       if (($result2['network'] != "") AND ($result2['network'] != NETWORK_FEED))
+                               $result = $result2;
+               }
        }
 
-       Cache::set("probe_url:".$mode.":".$url,serialize($result));
+       // Only store into the cache if the value seems to be valid
+       if ($result['network'] != NETWORK_PHANTOM)
+               Cache::set("probe_url:".$mode.":".$url,serialize($result), CACHE_DAY);
 
        return $result;
 }