]> git.mxchange.org Git - friendica.git/blobdiff - include/Scrape.php
probe_url: Better detection for non-standard installations of GNU Social
[friendica.git] / include / Scrape.php
index c74fd879c2b1e30243d6a8c03f52fed4fa0f93d2..90fb1a9e3dd99e992d391a6f09f1ff61618c9f9c 100644 (file)
@@ -335,7 +335,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();
@@ -364,16 +364,6 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                $network = NETWORK_TWITTER;
        }
 
-       if (strpos($url,'www.facebook.com')) {
-               $connectornetworks = true;
-               $network = NETWORK_FACEBOOK;
-       }
-
-       if (strpos($url,'alpha.app.net')) {
-               $appnet = true;
-               $network = NETWORK_APPNET;
-       }
-
        // Twitter is deactivated since twitter closed its old API
        //$twitter = ((strpos($url,'twitter.com') !== false) ? true : false);
        $lastfm  = ((strpos($url,'last.fm/user') !== false) ? true : false);
@@ -526,8 +516,8 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                                if($j) {
                                        $network = NETWORK_ZOT;
                                        $vcard   = array(
-                                               'fn'    => $j->fullname, 
-                                               'nick'  => $j->nickname, 
+                                               'fn'    => $j->fullname,
+                                               'nick'  => $j->nickname,
                                                'photo' => $j->photo
                                        );
                                        $profile  = $j->url;
@@ -569,6 +559,10 @@ function probe_url($url, $mode = PROBE_NORMAL) {
                        $network = NETWORK_DIASPORA;
                elseif($has_lrdd)
                        $network  = NETWORK_OSTATUS;
+
+               if(strpos($url,'@'))
+                       $addr = str_replace('acct:', '', $url);
+
                $priority = 0;
 
                if($hcard && ! $vcard) {
@@ -676,6 +670,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()));
@@ -687,6 +682,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);
@@ -762,6 +766,22 @@ function probe_url($url, $mode = PROBE_NORMAL) {
        if(($network === NETWORK_FEED) && ($poll) && (! x($vcard,'fn')))
                $vcard['fn'] = $url;
 
+       if (($notify != "") AND ($poll != "")) {
+               $baseurl = matching(normalise_link($notify), normalise_link($poll));
+
+               $baseurl2 = matching($baseurl, normalise_link($profile));
+               if ($baseurl2 != "")
+                       $baseurl = $baseurl2;
+       }
+
+       if (($baseurl == "") AND ($notify != ""))
+               $baseurl = matching(normalise_link($profile), normalise_link($notify));
+
+       if (($baseurl == "") AND ($poll != ""))
+               $baseurl = matching(normalise_link($profile), normalise_link($poll));
+
+       $baseurl = rtrim($baseurl, "/");
+
        $vcard['fn'] = notags($vcard['fn']);
        $vcard['nick'] = str_replace(' ','',notags($vcard['nick']));
 
@@ -780,19 +800,47 @@ function probe_url($url, $mode = PROBE_NORMAL) {
        $result['network'] = $network;
        $result['alias'] = $alias;
        $result['pubkey'] = $pubkey;
+       $result['baseurl'] = $baseurl;
 
        logger('probe_url: ' . print_r($result,true), LOGGER_DEBUG);
 
-       // Trying if it maybe a diaspora account
-       if ($result['network'] == NETWORK_FEED) {
-               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));
 
        return $result;
 }
+
+function matching($part1, $part2) {
+       $len = min(strlen($part1), strlen($part2));
+
+       $match = "";
+       $matching = true;
+       $i = 0;
+       while (($i <= $len) AND $matching) {
+               if (substr($part1, $i, 1) == substr($part2, $i, 1))
+                       $match .= substr($part1, $i, 1);
+               else
+                       $matching = false;
+
+               $i++;
+       }
+       return($match);
+}