]> git.mxchange.org Git - friendica.git/blobdiff - include/socgraph.php
Opps ...
[friendica.git] / include / socgraph.php
index 7acd7725fb798b778c498040797474c22f50a06c..ec7446cdc58e580d5f24c473dd1bdf308bf2ed30 100644 (file)
@@ -35,8 +35,8 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
 
        $a = get_app();
 
-       if($cid) {
-               if((! $url) || (! $uid)) {
+       if ($cid) {
+               if ((! $url) || (! $uid)) {
                        $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
                                intval($cid)
                        );
@@ -45,11 +45,11 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
                                $uid = $r[0]['uid'];
                        }
                }
-               if(! $uid)
+               if (! $uid)
                        return;
        }
 
-       if(! $url)
+       if (! $url)
                return;
 
        $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation') ;
@@ -62,18 +62,18 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
 
        logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
 
-       if(($a->get_curl_code() > 299) || (! $s))
+       if (($a->get_curl_code() > 299) || (! $s))
                return;
 
        $j = json_decode($s);
 
        logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
 
-       if(! isset($j->entry))
+       if (! isset($j->entry))
                return;
 
        $total = 0;
-       foreach($j->entry as $entry) {
+       foreach ($j->entry as $entry) {
 
                $total ++;
                $profile_url = '';
@@ -137,7 +137,7 @@ function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
                }
 
                if (isset($entry->tags)) {
-                       foreach($entry->tags as $tag) {
+                       foreach ($entry->tags as $tag) {
                                $keywords = implode(", ", $tag);
                        }
                }
@@ -340,7 +340,7 @@ function poco_check($profile_url, $name, $network, $profile_photo, $about, $loca
 
        $gcid = update_gcontact($gcontact);
 
-       if(!$gcid)
+       if (!$gcid)
                return $gcid;
 
        $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
@@ -431,7 +431,7 @@ function poco_detect_server($profile) {
        // Wild guess
        if ($server_url == "") {
                $base = preg_replace("=(https?://)(.*?)/(.*)=ism", "$1$2", $profile);
-               if (base != $profile) {
+               if ($base != $profile) {
                        $server_url = $base;
                        $network = NETWORK_PHANTOM;
                }
@@ -763,6 +763,113 @@ function poco_detect_poco_data($data) {
        return false;
 }
 
+/**
+ * @brief Detect server type by using the nodeinfo data
+ *
+ * @param string $server_url address of the server
+ * @return array Server data
+ */
+function poco_fetch_nodeinfo($server_url) {
+       $serverret = z_fetch_url($server_url."/.well-known/nodeinfo");
+       if (!$serverret["success"]) {
+               return false;
+       }
+
+       $nodeinfo = json_decode($serverret['body']);
+
+       if (!is_object($nodeinfo)) {
+               return false;
+       }
+
+       if (!is_array($nodeinfo->links)) {
+               return false;
+       }
+
+       $nodeinfo_url = '';
+
+       foreach ($nodeinfo->links AS $link) {
+               if ($link->rel == 'http://nodeinfo.diaspora.software/ns/schema/1.0') {
+                       $nodeinfo_url = $link->href;
+               }
+       }
+
+       if ($nodeinfo_url == '') {
+               return false;
+       }
+
+       $serverret = z_fetch_url($nodeinfo_url);
+       if (!$serverret["success"]) {
+               return false;
+       }
+
+       $nodeinfo = json_decode($serverret['body']);
+
+       if (!is_object($nodeinfo)) {
+               return false;
+       }
+
+       $server = array();
+
+       $server['register_policy'] = REGISTER_CLOSED;
+
+       if (is_bool($nodeinfo->openRegistrations) AND $nodeinfo->openRegistrations) {
+               $server['register_policy'] = REGISTER_OPEN;
+       }
+
+       if (is_object($nodeinfo->software)) {
+               if (isset($nodeinfo->software->name)) {
+                       $server['platform'] = $nodeinfo->software->name;
+               }
+
+               if (isset($nodeinfo->software->version)) {
+                       $server['version'] = $nodeinfo->software->version;
+                       // Version numbers on Nodeinfo are presented with additional info, e.g.:
+                       // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
+                       $server['version'] = preg_replace("=(.+)-(.{4,})=ism", "$1", $server['version']);
+               }
+       }
+
+       if (is_object($nodeinfo->metadata)) {
+               if (isset($nodeinfo->metadata->nodeName)) {
+                       $server['site_name'] = $nodeinfo->metadata->nodeName;
+               }
+       }
+
+       $diaspora = false;
+       $friendica = false;
+       $gnusocial = false;
+
+       if (is_array($nodeinfo->protocols->inbound)) {
+               foreach ($nodeinfo->protocols->inbound AS $inbound) {
+                       if ($inbound == 'diaspora') {
+                               $diaspora = true;
+                       }
+                       if ($inbound == 'friendica') {
+                               $friendica = true;
+                       }
+                       if ($inbound == 'gnusocial') {
+                               $gnusocial = true;
+                       }
+               }
+       }
+
+       if ($gnusocial) {
+               $server['network'] = NETWORK_OSTATUS;
+       }
+       if ($diaspora) {
+               $server['network'] = NETWORK_DIASPORA;
+       }
+       if ($friendica) {
+               $server['network'] = NETWORK_DFRN;
+       }
+
+       if (!$server) {
+               return false;
+       }
+
+       return $server;
+}
+
 /**
  * @brief Detect server type (Hubzilla or Friendica) via the front page body
  *
@@ -837,7 +944,7 @@ function poco_check_server($server_url, $network = "", $force = false) {
                return false;
 
        $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
-       if ($servers) {
+       if (dbm::is_result($servers)) {
 
                if ($servers[0]["created"] == "0000-00-00 00:00:00")
                        q("UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
@@ -881,21 +988,40 @@ function poco_check_server($server_url, $network = "", $force = false) {
        $orig_last_contact = $last_contact;
 
        // Check if the page is accessible via SSL.
+       $orig_server_url = $server_url;
        $server_url = str_replace("http://", "https://", $server_url);
-       $serverret = z_fetch_url($server_url."/.well-known/host-meta");
+
+       // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
+       $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
+
+       // Quit if there is a timeout.
+       // But we want to make sure to only quit if we are mostly sure that this server url fits.
+       if (dbm::is_result($servers) AND ($orig_server_url == $server_url) AND
+               ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT)) {
+               logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
+               return false;
+       }
 
        // Maybe the page is unencrypted only?
        $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
        if (!$serverret["success"] OR ($serverret["body"] == "") OR (@sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
                $server_url = str_replace("https://", "http://", $server_url);
-               $serverret = z_fetch_url($server_url."/.well-known/host-meta");
+
+               // We set the timeout to 20 seconds since this operation should be done in no time if the server was vital
+               $serverret = z_fetch_url($server_url."/.well-known/host-meta", false, $redirects, array('timeout' => 20));
+
+               // Quit if there is a timeout
+               if ($serverret['errno'] == CURLE_OPERATION_TIMEDOUT) {
+                       logger("Connection to server ".$server_url." timed out.", LOGGER_DEBUG);
+                       return false;
+               }
 
                $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
        }
 
        if (!$serverret["success"] OR ($serverret["body"] == "") OR (sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
                // Workaround for bad configured servers (known nginx problem)
-               if ($serverret["debug"]["http_code"] != "403") {
+               if (!in_array($serverret["debug"]["http_code"], array("403", "404"))) {
                        $last_failure = datetime_convert();
                        $failure = true;
                }
@@ -950,10 +1076,10 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        }
 
                        $lines = explode("\n",$serverret["header"]);
-                       if(count($lines)) {
-                               foreach($lines as $line) {
+                       if (count($lines)) {
+                               foreach ($lines as $line) {
                                        $line = trim($line);
-                                       if(stristr($line,'X-Diaspora-Version:')) {
+                                       if (stristr($line,'X-Diaspora-Version:')) {
                                                $platform = "Diaspora";
                                                $version = trim(str_replace("X-Diaspora-Version:", "", $line));
                                                $version = trim(str_replace("x-diaspora-version:", "", $version));
@@ -963,7 +1089,7 @@ function poco_check_server($server_url, $network = "", $force = false) {
                                                $last_contact = datetime_convert();
                                        }
 
-                                       if(stristr($line,'Server: Mastodon')) {
+                                       if (stristr($line,'Server: Mastodon')) {
                                                $platform = "Mastodon";
                                                $network = NETWORK_OSTATUS;
                                                // Mastodon doesn't reveal version numbers
@@ -1063,27 +1189,60 @@ function poco_check_server($server_url, $network = "", $force = false) {
                $serverret = z_fetch_url($server_url."/statistics.json");
                if ($serverret["success"]) {
                        $data = json_decode($serverret["body"]);
-                       if ($version == "")
+                       if (isset($data->version)) {
                                $version = $data->version;
+                               // Version numbers on statistics.json are presented with additional info, e.g.:
+                               // 0.6.3.0-p1702cc1c, 0.6.99.0-p1b9ab160 or 3.4.3-2-1191.
+                               $version = preg_replace("=(.+)-(.{4,})=ism", "$1", $version);
+                       }
 
                        $site_name = $data->name;
 
-                       if (isset($data->network) AND ($platform == ""))
+                       if (isset($data->network)) {
                                $platform = $data->network;
+                       }
 
-                       if ($platform == "Diaspora")
+                       if ($platform == "Diaspora") {
                                $network = NETWORK_DIASPORA;
+                       }
 
-                       if ($data->registrations_open)
+                       if ($data->registrations_open) {
                                $register_policy = REGISTER_OPEN;
-                       else
+                       } else {
                                $register_policy = REGISTER_CLOSED;
+                       }
 
                        if (isset($data->version))
                                $last_contact = datetime_convert();
                }
        }
 
+       // Query nodeinfo. Working for (at least) Diaspora and Friendica.
+       if (!$failure) {
+               $server = poco_fetch_nodeinfo($server_url);
+               if ($server) {
+                       $register_policy = $server['register_policy'];
+
+                       if (isset($server['platform'])) {
+                               $platform = $server['platform'];
+                       }
+
+                       if (isset($server['network'])) {
+                               $network = $server['network'];
+                       }
+
+                       if (isset($server['version'])) {
+                               $version = $server['version'];
+                       }
+
+                       if (isset($server['site_name'])) {
+                               $site_name = $server['site_name'];
+                       }
+
+                       $last_contact = datetime_convert();
+               }
+       }
+
        // Check for noscrape
        // Friendica servers could be detected as OStatus servers
        if (!$failure AND in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
@@ -1162,7 +1321,7 @@ function poco_check_server($server_url, $network = "", $force = false) {
                        dbesc($last_failure),
                        dbesc(normalise_link($server_url))
                );
-       } elseif (!$failure)
+       } elseif (!$failure) {
                q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`)
                                        VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
                                dbesc($server_url),
@@ -1180,7 +1339,7 @@ function poco_check_server($server_url, $network = "", $force = false) {
                                dbesc($last_failure),
                                dbesc(datetime_convert())
                );
-
+       }
        logger("End discovery for server ".$server_url, LOGGER_DEBUG);
 
        return !$failure;
@@ -1209,7 +1368,7 @@ function count_common_friends($uid,$cid) {
 
 function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
 
-       if($shuffle)
+       if ($shuffle)
                $sql_extra = " order by rand() ";
        else
                $sql_extra = " order by `gcontact`.`name` asc ";
@@ -1254,7 +1413,7 @@ function count_common_friends_zcid($uid,$zcid) {
 
 function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
 
-       if($shuffle)
+       if ($shuffle)
                $sql_extra = " order by rand() ";
        else
                $sql_extra = " order by `gcontact`.`name` asc ";
@@ -1435,18 +1594,45 @@ function update_suggestions() {
        if (dbm::is_result($r)) {
                foreach ($r as $rr) {
                        $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
-                       if(! in_array($base,$done))
+                       if (! in_array($base,$done))
                                poco_load(0,0,0,$base);
                }
        }
 }
 
+/**
+ * @brief Fetch server list from remote servers and adds them when they are new.
+ *
+ * @param string $poco URL to the POCO endpoint
+ */
+function poco_fetch_serverlist($poco) {
+       $serverret = z_fetch_url($poco."/@server");
+       if (!$serverret["success"]) {
+               return;
+       }
+       $serverlist = json_decode($serverret['body']);
+
+       if (!is_array($serverlist)) {
+               return;
+       }
+
+       foreach ($serverlist AS $server) {
+               $server_url = str_replace("/index.php", "", $server->url);
+
+               $r = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
+               if (!dbm::is_result($r)) {
+                       logger("Call server check for server ".$server_url, LOGGER_DEBUG);
+                       proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode($server_url));
+               }
+       }
+}
+
 function poco_discover_federation() {
        $last = get_config('poco','last_federation_discovery');
 
        if ($last) {
                $next = $last + (24 * 60 * 60);
-               if($next > time())
+               if ($next > time())
                        return;
        }
 
@@ -1456,24 +1642,27 @@ function poco_discover_federation() {
        if ($serverdata) {
                $servers = json_decode($serverdata);
 
-               foreach($servers->pods AS $server)
-                       poco_check_server("https://".$server->host);
+               foreach ($servers->pods AS $server) {
+                       proc_run(PRIORITY_LOW, "include/discover_poco.php", "server", base64_encode("https://".$server->host));
+               }
        }
 
-       // Currently disabled, since the service isn't available anymore.
-       // It is not removed since I hope that there will be a successor.
-       // Discover GNU Social Servers.
-       //if (!get_config('system','ostatus_disabled')) {
-       //      $serverdata = "http://gstools.org/api/get_open_instances/";
+       /*
+        * Currently disabled, since the service isn't available anymore.
+        * It is not removed since I hope that there will be a successor.
+        * Discover GNU Social Servers.
+       if (!get_config('system','ostatus_disabled')) {
+               $serverdata = "http://gstools.org/api/get_open_instances/";
 
-       //      $result = z_fetch_url($serverdata);
-       //      if ($result["success"]) {
-       //              $servers = json_decode($result["body"]);
+               $result = z_fetch_url($serverdata);
+               if ($result["success"]) {
+                       $servers = json_decode($result["body"]);
 
-       //              foreach($servers->data AS $server)
-       //                      poco_check_server($server->instance_address);
-       //      }
-       //}
+                       foreach($servers->data AS $server)
+                               poco_check_server($server->instance_address);
+               }
+       }
+       */
 
        set_config('poco','last_federation_discovery', time());
 }
@@ -1502,6 +1691,9 @@ function poco_discover($complete = false) {
                                continue;
                        }
 
+                       // Discover new servers out there
+                       poco_fetch_serverlist($server["poco"]);
+
                        // Fetch all users from the other server
                        $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
 
@@ -1559,7 +1751,7 @@ function poco_discover_server_users($data, $server) {
        foreach ($data->entry AS $entry) {
                $username = "";
                if (isset($entry->urls)) {
-                       foreach($entry->urls as $url)
+                       foreach ($entry->urls as $url)
                                if ($url->type == 'profile') {
                                        $profile_url = $url->value;
                                        $urlparts = parse_url($profile_url);
@@ -1603,7 +1795,7 @@ function poco_discover_server($data, $default_generation = 0) {
                $name = $entry->displayName;
 
                if (isset($entry->urls)) {
-                       foreach($entry->urls as $url) {
+                       foreach ($entry->urls as $url) {
                                if ($url->type == 'profile') {
                                        $profile_url = $url->value;
                                        continue;
@@ -1628,31 +1820,31 @@ function poco_discover_server($data, $default_generation = 0) {
                        $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
                }
 
-               if(isset($entry->network)) {
+               if (isset($entry->network)) {
                        $network = $entry->network;
                }
 
-               if(isset($entry->currentLocation)) {
+               if (isset($entry->currentLocation)) {
                        $location = $entry->currentLocation;
                }
 
-               if(isset($entry->aboutMe)) {
+               if (isset($entry->aboutMe)) {
                        $about = html2bbcode($entry->aboutMe);
                }
 
-               if(isset($entry->gender)) {
+               if (isset($entry->gender)) {
                        $gender = $entry->gender;
                }
 
-               if(isset($entry->generation) AND ($entry->generation > 0)) {
+               if (isset($entry->generation) AND ($entry->generation > 0)) {
                        $generation = ++$entry->generation;
                }
 
-               if(isset($entry->contactType) AND ($entry->contactType >= 0)) {
+               if (isset($entry->contactType) AND ($entry->contactType >= 0)) {
                        $contact_type = $entry->contactType;
                }
 
-               if(isset($entry->tags)) {
+               if (isset($entry->tags)) {
                        foreach ($entry->tags as $tag) {
                                $keywords = implode(", ", $tag);
                        }
@@ -2082,7 +2274,7 @@ function gs_discover() {
  * @return array List of server urls
  */
 function poco_serverlist() {
-       $r = q("SELECT `id`, `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
+       $r = q("SELECT `url`, `site_name` AS `displayName`, `network`, `platform`, `version` FROM `gserver`
                WHERE `network` IN ('%s', '%s', '%s') AND `last_contact` > `last_failure`
                ORDER BY `last_contact`
                LIMIT 1000",
@@ -2090,11 +2282,6 @@ function poco_serverlist() {
        if (!dbm::is_result($r)) {
                return false;
        }
-       $list = array();
-       foreach ($r AS $server) {
-               $server['id'] = (int)$server['id'];
-               $list[] = $server;
-       }
-       return $list;
+       return $r;
 }
 ?>