]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Merge pull request #10115 from tobiasd/20210402-credits
[friendica.git] / src / Model / GServer.php
index c035abc73fb711aaeb2f0c03c1f1bd5d4946d6f3..6a1a363e34f96580d991d74b6a8294e2d63715fd 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,6 +23,7 @@ namespace Friendica\Model;
 
 use DOMDocument;
 use DOMXPath;
+use Exception;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
@@ -64,12 +65,29 @@ class GServer
        const DETECT_SITEINFO_JSON = 15; // Newer Hubzilla
        const DETECT_MASTODON_API = 16;
        const DETECT_STATUS_PHP = 17; // Nextcloud
+       const DETECT_V1_CONFIG = 18;
 
        // Standardized endpoints
        const DETECT_STATISTICS_JSON = 100;
        const DETECT_NODEINFO_1 = 101;
        const DETECT_NODEINFO_2 = 102;
 
+       /**
+        * Check for the existance of a server and adds it in the background if not existant
+        *
+        * @param string $url
+        * @param boolean $only_nodeinfo
+        * @return void
+        */
+       public static function add(string $url, bool $only_nodeinfo = false)
+       {
+               if (self::getID($url, false)) {
+                       return;
+               }
+
+               Worker::add(PRIORITY_LOW, 'UpdateGServer', $url, $only_nodeinfo);
+       }
+
        /**
         * Get the ID for the given server URL
         *
@@ -173,61 +191,6 @@ class GServer
                return DateTimeFormat::utc('now +3 month');
        }
 
-       /**
-        * 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.
         *
@@ -241,7 +204,6 @@ class GServer
        public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false)
        {
                $server_url = self::cleanURL($server_url);
-
                if ($server_url == '') {
                        return false;
                }
@@ -254,24 +216,11 @@ class GServer
                                DBA::update('gserver', $fields, $condition);
                        }
 
-                       $last_contact = $gserver['last_contact'];
-                       $last_failure = $gserver['last_failure'];
-
-                       // See discussion under https://forum.friendi.ca/display/0b6b25a8135aabc37a5a0f5684081633
-                       // It can happen that a zero date is in the database, but storing it again is forbidden.
-                       if ($last_contact < DBA::NULL_DATETIME) {
-                               $last_contact = DBA::NULL_DATETIME;
-                       }
-
-                       if ($last_failure < DBA::NULL_DATETIME) {
-                               $last_failure = DBA::NULL_DATETIME;
-                       }
-
-                       if (!$force && !self::updateNeeded($gserver['created'], '', $last_failure, $last_contact)) {
+                       if (!$force && (strtotime($gserver['next_contact']) > time())) {
                                Logger::info('No update needed', ['server' => $server_url]);
-                               return ($last_contact >= $last_failure);
+                               return (!$gserver['failed']);
                        }
-                       Logger::info('Server is outdated. Start discovery.', ['Server' => $server_url, 'Force' => $force, 'Created' => $gserver['created'], 'Failure' => $last_failure, 'Contact' => $last_contact]);
+                       Logger::info('Server is outdated. Start discovery.', ['Server' => $server_url, 'Force' => $force]);
                } else {
                        Logger::info('Server is unknown. Start discovery.', ['Server' => $server_url]);
                }
@@ -371,6 +320,17 @@ class GServer
                        return false;
                }
 
+               // On a redirect follow the new host but mark the old one as failure
+               if ($curlResult->isSuccess() && (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) {
+                       $curlResult = DI::httpRequest()->get($url, ['timeout' => $xrd_timeout]);
+                       if (parse_url($url, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST)) {
+                               Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]);
+                               self::setFailure($url);
+                               self::detect($curlResult->getRedirectUrl(), $network, $only_nodeinfo);
+                               return false;
+                       }
+               }
+
                $nodeinfo = self::fetchNodeinfo($url, $curlResult);
                if ($only_nodeinfo && empty($nodeinfo)) {
                        Logger::info('Invalid nodeinfo in nodeinfo-mode, server is marked as failure', ['url' => $url]);
@@ -400,6 +360,13 @@ class GServer
 
                                $curlResult = DI::httpRequest()->get($baseurl, ['timeout' => $xrd_timeout]);
                                if ($curlResult->isSuccess()) {
+                                       if ((parse_url($baseurl, PHP_URL_HOST) != parse_url($curlResult->getRedirectUrl(), PHP_URL_HOST))) {
+                                               Logger::info('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $curlResult->getRedirectUrl()]);
+                                               self::setFailure($url);
+                                               self::detect($curlResult->getRedirectUrl(), $network, $only_nodeinfo);
+                                               return false;
+                                       }
+
                                        $basedata = self::analyseRootHeader($curlResult, $basedata);
                                        $basedata = self::analyseRootBody($curlResult, $basedata, $baseurl);
                                }
@@ -461,6 +428,10 @@ class GServer
                                $serverdata = self::detectHubzilla($url, $serverdata);
                        }
 
+                       if (empty($serverdata['network']) || in_array($serverdata['detection-method'], [self::DETECT_MANUAL, self::DETECT_BODY])) {
+                               $serverdata = self::detectPeertube($url, $serverdata);
+                       }
+
                        if (empty($serverdata['network'])) {
                                $serverdata = self::detectNextcloud($url, $serverdata);
                        }
@@ -501,7 +472,7 @@ class GServer
                }
 
                if ($serverdata['network'] == Protocol::PHANTOM) {
-                       $serverdata['registered-users'] = $registeredUsers;
+                       $serverdata['registered-users'] = max($registeredUsers, 1);
                        $serverdata = self::detectNetworkViaContacts($url, $serverdata);
                }
 
@@ -531,7 +502,7 @@ class GServer
                if (!empty($serverdata['network']) && !empty($id) && ($serverdata['network'] != Protocol::PHANTOM)) {
                        $apcontacts = DBA::count('apcontact', ['gsid' => $id]);
                        $contacts = DBA::count('contact', ['uid' => 0, 'gsid' => $id]);
-                       $max_users = max($apcontacts, $contacts, $registeredUsers);
+                       $max_users = max($apcontacts, $contacts, $registeredUsers, 1);
                        if ($max_users > $registeredUsers) {
                                Logger::info('Update registered users', ['id' => $id, 'url' => $serverdata['nurl'], 'registered-users' => $max_users]);
                                DBA::update('gserver', ['registered-users' => $max_users], ['id' => $id]);
@@ -797,7 +768,7 @@ class GServer
                }
 
                if (!empty($nodeinfo['usage']['users']['total'])) {
-                       $server['registered-users'] = $nodeinfo['usage']['users']['total'];
+                       $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
                }
 
                if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) {
@@ -873,7 +844,7 @@ class GServer
                }
 
                if (!empty($nodeinfo['usage']['users']['total'])) {
-                       $server['registered-users'] = $nodeinfo['usage']['users']['total'];
+                       $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1);
                }
 
                if (!empty($nodeinfo['protocols'])) {
@@ -950,7 +921,7 @@ class GServer
                }
 
                if (!empty($data['channels_total'])) {
-                       $serverdata['registered-users'] = $data['channels_total'];
+                       $serverdata['registered-users'] = max($data['channels_total'], 1);
                }
 
                if (!empty($data['register_policy'])) {
@@ -1054,7 +1025,7 @@ class GServer
                        }
                }
 
-               $serverdata['registered-users'] = max($serverdata['registered-users'], count($contacts));
+               $serverdata['registered-users'] = max($serverdata['registered-users'], count($contacts), 1);
 
                return $serverdata;
        }
@@ -1085,7 +1056,7 @@ class GServer
 
                if (!empty($data['totalResults'])) {
                        $registeredUsers = $serverdata['registered-users'] ?? 0;
-                       $serverdata['registered-users'] = max($data['totalResults'], $registeredUsers);
+                       $serverdata['registered-users'] = max($data['totalResults'], $registeredUsers, 1);
                        $serverdata['directory-type'] = self::DT_POCO;
                        $serverdata['poco'] = $url . '/poco';
                }
@@ -1120,6 +1091,54 @@ class GServer
                return $serverdata;
        }
 
+       /**
+        * Detects Peertube via their known endpoint
+        *
+        * @param string $url        URL of the given server
+        * @param array  $serverdata array with server data
+        *
+        * @return array server data
+        */
+       private static function detectPeertube(string $url, array $serverdata)
+       {
+               $curlResult = DI::httpRequest()->get($url . '/api/v1/config');
+
+               if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
+                       return $serverdata;
+               }
+
+               $data = json_decode($curlResult->getBody(), true);
+               if (empty($data)) {
+                       return $serverdata;
+               }
+
+               if (!empty($data['instance']) && !empty($data['serverVersion'])) {
+                       $serverdata['platform'] = 'peertube';
+                       $serverdata['version'] = $data['serverVersion'];
+                       $serverdata['network'] = Protocol::ACTIVITYPUB;
+
+                       if (!empty($data['instance']['name'])) {
+                               $serverdata['site_name'] = $data['instance']['name'];
+                       }
+
+                       if (!empty($data['instance']['shortDescription'])) {
+                               $serverdata['info'] = $data['instance']['shortDescription'];
+                       }
+
+                       if (!empty($data['signup'])) {
+                               if (!empty($data['signup']['allowed'])) {
+                                       $serverdata['register_policy'] = Register::OPEN;
+                               }
+                       }
+
+                       if (in_array($serverdata['detection-method'], [self::DETECT_HEADER, self::DETECT_BODY, self::DETECT_MANUAL])) {
+                               $serverdata['detection-method'] = self::DETECT_V1_CONFIG;
+                       }
+               }
+
+               return $serverdata;
+       }
+
        /**
         * Detects the version number of a given server when it was a NextCloud installation
         *
@@ -1199,7 +1218,7 @@ class GServer
                }
 
                if (!empty($data['stats']['user_count'])) {
-                       $serverdata['registered-users'] = $data['stats']['user_count'];
+                       $serverdata['registered-users'] = max($data['stats']['user_count'], 1);
                }
 
                if (!empty($serverdata['version']) && preg_match('/.*?\(compatible;\s(.*)\s(.*)\)/ism', $serverdata['version'], $matches)) {
@@ -1456,6 +1475,10 @@ class GServer
         */
        private static function analyseRootBody($curlResult, array $serverdata, string $url)
        {
+               if (empty($curlResult->getBody())) {
+                       return $serverdata;
+               }
+
                $doc = new DOMDocument();
                @$doc->loadHTML($curlResult->getBody());
                $xpath = new DOMXPath($doc);
@@ -1501,7 +1524,7 @@ class GServer
 
                                if (count($version_part) == 2) {
                                        if (in_array($version_part[0], ['WordPress'])) {
-                                               $serverdata['platform'] = strtolower($version_part[0]);
+                                               $serverdata['platform'] = 'wordpress';
                                                $serverdata['version'] = $version_part[1];
 
                                                // We still do need a reliable test if some AP plugin is activated
@@ -1690,7 +1713,7 @@ class GServer
                                if (!empty($data['data']['nodes'])) {
                                        foreach ($data['data']['nodes'] as $server) {
                                                // Using "only_nodeinfo" since servers that are listed on that page should always have it.
-                                               Worker::add(PRIORITY_LOW, 'UpdateGServer', 'https://' . $server['host'], true);
+                                               self::add('https://' . $server['host'], true);
                                        }
                                }
                        }
@@ -1709,11 +1732,93 @@ class GServer
 
                                foreach ($servers['instances'] as $server) {
                                        $url = (is_null($server['https_score']) ? 'http' : 'https') . '://' . $server['name'];
-                                       Worker::add(PRIORITY_LOW, 'UpdateGServer', $url);
+                                       self::add($url);
                                }
                        }
                }
 
                DI::config()->set('poco', 'last_federation_discovery', time());
        }
+
+       /**
+        * Set the protocol for the given server
+        *
+        * @param int $gsid     Server id
+        * @param int $protocol Protocol id
+        * @return void 
+        * @throws Exception 
+        */
+       public static function setProtocol(int $gsid, int $protocol)
+       {
+               if (empty($gsid)) {
+                       return;
+               }
+
+               $gserver = DBA::selectFirst('gserver', ['protocol', 'url'], ['id' => $gsid]);
+               if (!DBA::isResult($gserver)) {
+                       return;
+               }
+
+               $old = $gserver['protocol'];
+
+               if (!is_null($old)) {
+                       /*
+                       The priority for the protocols is:
+                               1. ActivityPub
+                               2. DFRN via Diaspora
+                               3. Legacy DFRN
+                               4. Diaspora
+                               5. OStatus
+                       */
+
+                       // We don't need to change it when nothing is to be changed
+                       if ($old == $protocol) {
+                               return;
+                       }
+
+                       // We don't want to mark a server as OStatus when it had been marked with any other protocol before
+                       if ($protocol == Post\DeliveryData::OSTATUS) {
+                               return;
+                       }
+
+                       // If the server is marked as ActivityPub then we won't change it to anything different
+                       if ($old == Post\DeliveryData::ACTIVITYPUB) {
+                               return;
+                       }
+
+                       // Don't change it to anything lower than DFRN if the new one wasn't ActivityPub
+                       if (($old == Post\DeliveryData::DFRN) && ($protocol != Post\DeliveryData::ACTIVITYPUB)) {
+                               return;
+                       }
+
+                       // Don't change it to Diaspora when it is a legacy DFRN server
+                       if (($old == Post\DeliveryData::LEGACY_DFRN) && ($protocol == Post\DeliveryData::DIASPORA)) {
+                               return;
+                       }
+               }
+
+               Logger::info('Protocol for server', ['protocol' => $protocol, 'old' => $old, 'id' => $gsid, 'url' => $gserver['url'], 'callstack' => System::callstack(20)]);
+               DBA::update('gserver', ['protocol' => $protocol], ['id' => $gsid]);
+       }
+
+       /**
+        * Fetch the protocol of the given server
+        *
+        * @param int $gsid Server id
+        * @return int 
+        * @throws Exception 
+        */
+       public static function getProtocol(int $gsid)
+       {
+               if (empty($gsid)) {
+                       return null;
+               }
+
+               $gserver = DBA::selectFirst('gserver', ['protocol'], ['id' => $gsid]);
+               if (DBA::isResult($gserver)) {
+                       return $gserver['protocol'];
+               }
+
+               return null;
+       }
 }