]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Improved documentation, now checking all items
[friendica.git] / src / Model / GServer.php
index 074e1b61304540f186deb08af30417d20a4e52fb..fe5ef6d6f018c353faa3fd7262952fd8388a2478 100644 (file)
@@ -10,8 +10,10 @@ use DOMDocument;
 use DOMXPath;
 use Friendica\Core\Config;
 use Friendica\Core\Protocol;
+use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Module\Register;
+use Friendica\Network\CurlResult;
 use Friendica\Util\Network;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Strings;
@@ -26,6 +28,10 @@ use Friendica\Network\Probe;
  */
 class GServer
 {
+       // Directory types
+       const DT_NONE = 0;
+       const DT_POCO = 1;
+       const DT_MASTODON = 2;
        /**
         * Checks if the given server is reachable
         *
@@ -39,7 +45,7 @@ class GServer
        public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false)
        {
                if ($server == '') {
-                       $server = Contact::getBasepath($profile);
+                       $server = GContact::getBasepath($profile);
                }
 
                if ($server == '') {
@@ -49,6 +55,61 @@ class GServer
                return self::check($server, $network, $force);
        }
 
+       /**
+        * Decides if a server needs to be updated, based upon several date fields
+        *
+        * @param date $created      Creation date of that server entry
+        * @param date $updated      When had the server entry be updated
+        * @param date $last_failure Last failure when contacting that server
+        * @param date $last_contact Last time the server had been contacted
+        *
+        * @return boolean Does the server record needs an update?
+        */
+       public static function updateNeeded($created, $updated, $last_failure, $last_contact)
+       {
+               $now = strtotime(DateTimeFormat::utcNow());
+
+               if ($updated > $last_contact) {
+                       $contact_time = strtotime($updated);
+               } else {
+                       $contact_time = strtotime($last_contact);
+               }
+
+               $failure_time = strtotime($last_failure);
+               $created_time = strtotime($created);
+
+               // If there is no "created" time then use the current time
+               if ($created_time <= 0) {
+                       $created_time = $now;
+               }
+
+               // If the last contact was less than 24 hours then don't update
+               if (($now - $contact_time) < (60 * 60 * 24)) {
+                       return false;
+               }
+
+               // If the last failure was less than 24 hours then don't update
+               if (($now - $failure_time) < (60 * 60 * 24)) {
+                       return false;
+               }
+
+               // If the last contact was less than a week ago and the last failure is older than a week then don't update
+               //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) && ($contact_time > $failure_time))
+               //      return false;
+
+               // If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
+               if ((($now - $contact_time) > (60 * 60 * 24 * 7)) && (($now - $created_time) > (60 * 60 * 24 * 7)) && (($now - $failure_time) < (60 * 60 * 24 * 7))) {
+                       return false;
+               }
+
+               // If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
+               if ((($now - $contact_time) > (60 * 60 * 24 * 30)) && (($now - $created_time) > (60 * 60 * 24 * 30)) && (($now - $failure_time) < (60 * 60 * 24 * 30))) {
+                       return false;
+               }
+
+               return true;
+       }
+
        /**
         * Checks the state of the given server.
         *
@@ -89,7 +150,7 @@ class GServer
                                $last_failure = DBA::NULL_DATETIME;
                        }
 
-                       if (!$force && !PortableContact::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
+                       if (!$force && !self::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
                                Logger::info('No update needed', ['server' => $server_url]);
                                return ($last_contact >= $last_failure);
                        }
@@ -112,8 +173,24 @@ class GServer
         */
        public static function detect(string $url, string $network = '')
        {
+               Logger::info('Detect server type', ['server' => $url]);
                $serverdata = [];
 
+               $original_url = $url;
+
+               // Remove URL content that is not supposed to exist for a server url
+               $urlparts = parse_url($url);
+               unset($urlparts['user']);
+               unset($urlparts['pass']);
+               unset($urlparts['query']);
+               unset($urlparts['fragment']);
+               $url = Network::unparseURL($urlparts);
+
+               // If the URL missmatches, then we mark the old entry as failure
+               if ($url != $original_url) {
+                       DBA::update('gserver', ['last_failure' => DateTimeFormat::utcNow()], ['nurl' => Strings::normaliseLink($original_url)]);
+               }
+
                // When a nodeinfo is present, we don't need to dig further
                $xrd_timeout = Config::get('system', 'xrd_timeout');
                $curlResult = Network::curl($url . '/.well-known/nodeinfo', false, ['timeout' => $xrd_timeout]);
@@ -183,7 +260,10 @@ class GServer
                        $serverdata = $nodeinfo;
                }
 
+               // Detect the directory type
+               $serverdata['directory-type'] = self::DT_NONE;
                $serverdata = self::checkPoCo($url, $serverdata);
+               $serverdata = self::checkMastodonDirectory($url, $serverdata);
 
                // We can't detect the network type. Possibly it is some system that we don't know yet
                if (empty($serverdata['network'])) {
@@ -370,11 +450,12 @@ class GServer
        /**
         * Detect server type by using the nodeinfo data
         *
-        * @param string $url address of the server
+        * @param string     $url        address of the server
+        * @param CurlResult $curlResult
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function fetchNodeinfo(string $url, $curlResult)
+       private static function fetchNodeinfo(string $url, CurlResult $curlResult)
        {
                $nodeinfo = json_decode($curlResult->getBody(), true);
 
@@ -549,7 +630,7 @@ class GServer
                                $protocols[$protocol] = true;
                        }
 
-                       if (!empty($protocols['friendica'])) {
+                       if (!empty($protocols['dfrn'])) {
                                $server['network'] = Protocol::DFRN;
                        } elseif (!empty($protocols['activitypub'])) {
                                $server['network'] = Protocol::ACTIVITYPUB;
@@ -740,6 +821,8 @@ class GServer
         */
        private static function checkPoCo(string $url, array $serverdata)
        {
+               $serverdata['poco'] = '';
+
                $curlResult = Network::curl($url. '/poco');
                if (!$curlResult->isSuccess()) {
                        return $serverdata;
@@ -753,9 +836,35 @@ class GServer
                if (!empty($data['totalResults'])) {
                        $registeredUsers = $serverdata['registered-users'] ?? 0;
                        $serverdata['registered-users'] = max($data['totalResults'], $registeredUsers);
+                       $serverdata['directory-type'] = self::DT_POCO;
                        $serverdata['poco'] = $url . '/poco';
-               } else {
-                       $serverdata['poco'] = '';
+               }
+
+               return $serverdata;
+       }
+
+       /**
+        * Checks if the given server does have a Mastodon style directory endpoint.
+        *
+        * @param string $url        URL of the given server
+        * @param array  $serverdata array with server data
+        *
+        * @return array server data
+        */
+       public static function checkMastodonDirectory(string $url, array $serverdata)
+       {
+               $curlResult = Network::curl($url . '/api/v1/directory?limit=1');
+               if (!$curlResult->isSuccess()) {
+                       return $serverdata;
+               }
+
+               $data = json_decode($curlResult->getBody(), true);
+               if (empty($data)) {
+                       return $serverdata;
+               }
+
+               if (count($data) == 1) {
+                       $serverdata['directory-type'] = self::DT_MASTODON;
                }
 
                return $serverdata;
@@ -1061,12 +1170,12 @@ class GServer
                        $attr = [];
                        if ($node->attributes->length) {
                                foreach ($node->attributes as $attribute) {
-                                       $attribute->value = @trim($attribute->value);
-                                       if (empty($attribute->value)) {
+                                       $value = trim($attribute->value);
+                                       if (empty($value)) {
                                                continue;
                                        }
 
-                                       $attr[$attribute->name] = $attribute->value;
+                                       $attr[$attribute->name] = $value;
                                }
 
                                if (empty($attr['name']) || empty($attr['content'])) {
@@ -1117,12 +1226,12 @@ class GServer
                        $attr = [];
                        if ($node->attributes->length) {
                                foreach ($node->attributes as $attribute) {
-                                       $attribute->value = @trim($attribute->value);
-                                       if (empty($attribute->value)) {
+                                       $value = trim($attribute->value);
+                                       if (empty($value)) {
                                                continue;
                                        }
 
-                                       $attr[$attribute->name] = $attribute->value;
+                                       $attr[$attribute->name] = $value;
                                }
 
                                if (empty($attr['property']) || empty($attr['content'])) {
@@ -1184,4 +1293,112 @@ class GServer
                }
                return $serverdata;
        }
+
+       /**
+        * Update the user directory of a given gserver record
+        *
+        * @param array $gserver gserver record
+        */
+       public static function updateDirectory(array $gserver)
+       {
+               /// @todo Add Mastodon API directory
+
+               if (!empty($gserver['poco'])) {
+                       PortableContact::discoverSingleServer($gserver['id']);
+               }
+       }
+
+       /**
+        * Update GServer entries
+        */
+       public static function discover()
+       {
+               // Update the server list
+               self::discoverFederation();
+
+               $no_of_queries = 5;
+
+               $requery_days = intval(Config::get('system', 'poco_requery_days'));
+
+               if ($requery_days == 0) {
+                       $requery_days = 7;
+               }
+
+               $last_update = date('c', time() - (60 * 60 * 24 * $requery_days));
+
+               $gservers = DBA::p("SELECT `id`, `url`, `nurl`, `network`, `poco`
+                       FROM `gserver`
+                       WHERE `last_contact` >= `last_failure`
+                       AND `poco` != ''
+                       AND `last_poco_query` < ?
+                       ORDER BY RAND()", $last_update
+               );
+
+               while ($gserver = DBA::fetch($gservers)) {
+                       if (!GServer::check($gserver['url'], $gserver['network'])) {
+                               // The server is not reachable? Okay, then we will try it later
+                               $fields = ['last_poco_query' => DateTimeFormat::utcNow()];
+                               DBA::update('gserver', $fields, ['nurl' => $gserver['nurl']]);
+                               continue;
+                       }
+
+                       Logger::info('Update directory', ['server' => $gserver['url'], 'id' => $gserver['id']]);
+                       Worker::add(PRIORITY_LOW, 'UpdateServerDirectory', $gserver);
+
+                       if (--$no_of_queries == 0) {
+                               break;
+                       }
+               }
+
+               DBA::close($gservers);
+       }
+
+       /**
+        * Discover federated servers
+        */
+       private static function discoverFederation()
+       {
+               $last = Config::get('poco', 'last_federation_discovery');
+
+               if ($last) {
+                       $next = $last + (24 * 60 * 60);
+
+                       if ($next > time()) {
+                               return;
+                       }
+               }
+
+               // Discover federated servers
+               $curlResult = Network::fetchUrl("http://the-federation.info/pods.json");
+
+               if (!empty($curlResult)) {
+                       $servers = json_decode($curlResult, true);
+
+                       if (!empty($servers['pods'])) {
+                               foreach ($servers['pods'] as $server) {
+                                       Worker::add(PRIORITY_LOW, 'UpdateGServer', 'https://' . $server['host']);
+                               }
+                       }
+               }
+
+               // Disvover Mastodon servers
+               $accesstoken = Config::get('system', 'instances_social_key');
+
+               if (!empty($accesstoken)) {
+                       $api = 'https://instances.social/api/1.0/instances/list?count=0';
+                       $header = ['Authorization: Bearer '.$accesstoken];
+                       $curlResult = Network::curl($api, false, ['headers' => $header]);
+
+                       if ($curlResult->isSuccess()) {
+                               $servers = json_decode($curlResult->getBody(), true);
+
+                               foreach ($servers['instances'] as $server) {
+                                       $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
+                                       Worker::add(PRIORITY_LOW, 'UpdateGServer', $url);
+                               }
+                       }
+               }
+
+               Config::set('poco', 'last_federation_discovery', time());
+       }
 }