]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Merge pull request #9 from nupplaphil/dependabot/composer/guzzlehttp/guzzle-6.5.8
[friendica.git] / src / Model / GServer.php
index a6be93ff78f44aecea01896af1b2eaf96f5f6f3c..f6c5e14af2519ae9331413e3cfc12d92986edd2e 100644 (file)
@@ -96,7 +96,7 @@ class GServer
         *
         * @param string $url
         * @param boolean $no_check Don't check if the server hadn't been found
-        * @return int gserver id
+        * @return int|null gserver id or NULL on empty URL or failed check
         */
        public static function getID(string $url, bool $no_check = false)
        {
@@ -156,7 +156,7 @@ class GServer
         *
         * @return boolean 'true' if server seems vital
         */
-       public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false)
+       public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false): bool
        {
                if ($server == '') {
                        $contact = Contact::getByURL($profile, null, ['baseurl']);
@@ -172,7 +172,7 @@ class GServer
                return self::check($server, $network, $force);
        }
 
-       public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = '')
+       public static function getNextUpdateDate(bool $success, string $created = '', string $last_contact = ''): string
        {
                // On successful contact process check again next week
                if ($success) {
@@ -231,7 +231,7 @@ class GServer
         *
         * @return boolean 'true' if server seems vital
         */
-       public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false)
+       public static function check(string $server_url, string $network = '', bool $force = false, bool $only_nodeinfo = false): bool
        {
                $server_url = self::cleanURL($server_url);
                if ($server_url == '') {
@@ -286,7 +286,7 @@ class GServer
         * @param string $url
         * @return string cleaned URL
         */
-       public static function cleanURL(string $url)
+       public static function cleanURL(string $url): string
        {
                $url = trim($url, '/');
                $url = str_replace('/index.php', '', $url);
@@ -305,7 +305,7 @@ class GServer
         * @param string $url
         * @return string base URL
         */
-       private static function getBaseURL(string $url)
+       private static function getBaseURL(string $url): string
        {
                $urlparts = parse_url(self::cleanURL($url));
                unset($urlparts['path']);
@@ -322,7 +322,7 @@ class GServer
         *
         * @return boolean 'true' if server could be detected
         */
-       public static function detect(string $url, string $network = '', bool $only_nodeinfo = false)
+       public static function detect(string $url, string $network = '', bool $only_nodeinfo = false): bool
        {
                Logger::info('Detect server type', ['server' => $url]);
                $serverdata = ['detection-method' => self::DETECT_MANUAL];
@@ -438,7 +438,7 @@ class GServer
                                }
                        }
 
-                       if (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ACTIVITYPUB)) {
+                       if (empty($nodeinfo['network']) && (empty($serverdata['network']) || ($serverdata['network'] == Protocol::ACTIVITYPUB))) {
                                $serverdata = self::detectMastodonAlikes($url, $serverdata);
                        }
 
@@ -478,7 +478,7 @@ class GServer
                                $serverdata = self::detectNextcloud($url, $serverdata);
                        }
 
-                       if (empty($serverdata['network'])) {
+                       if (empty($nodeinfo['network']) && empty($serverdata['network'])) {
                                $serverdata = self::detectGNUSocial($url, $serverdata);
                        }
 
@@ -535,7 +535,7 @@ class GServer
                $serverdata['last_contact'] = DateTimeFormat::utcNow();
                $serverdata['failed'] = false;
 
-               $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => Strings::normaliseLink($url)]);
+               $gserver = DBA::selectFirst('gserver', ['network'], ['nurl' => $serverdata['nurl']]);
                if (!DBA::isResult($gserver)) {
                        $serverdata['created'] = DateTimeFormat::utcNow();
                        $ret = DBA::insert('gserver', $serverdata);
@@ -586,6 +586,7 @@ class GServer
         * Fetch relay data from a given server url
         *
         * @param string $server_url address of the server
+        * @return void
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function discoverRelay(string $server_url)
@@ -685,7 +686,7 @@ class GServer
         *
         * @return array server data
         */
-       private static function fetchStatistics(string $url)
+       private static function fetchStatistics(string $url): array
        {
                $curlResult = DI::httpClient()->get($url . '/statistics.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
@@ -758,7 +759,7 @@ class GServer
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult)
+       private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult): array
        {
                if (!$httpResult->isSuccess()) {
                        return [];
@@ -811,7 +812,7 @@ class GServer
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function parseNodeinfo1(string $nodeinfo_url)
+       private static function parseNodeinfo1(string $nodeinfo_url): array
        {
                $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
@@ -904,7 +905,7 @@ class GServer
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function parseNodeinfo2(string $nodeinfo_url)
+       private static function parseNodeinfo2(string $nodeinfo_url): array
        {
                $curlResult = DI::httpClient()->get($nodeinfo_url, HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
@@ -917,8 +918,11 @@ class GServer
                        return [];
                }
 
-               $server = ['detection-method' => self::DETECT_NODEINFO_2,
-                       'register_policy' => Register::CLOSED];
+               $server = [
+                       'detection-method' => self::DETECT_NODEINFO_2,
+                       'register_policy' => Register::CLOSED,
+                       'platform' => 'unknown',
+               ];
 
                if (!empty($nodeinfo['openRegistrations'])) {
                        $server['register_policy'] = Register::OPEN;
@@ -934,6 +938,11 @@ class GServer
                                // 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']);
+
+                               // qoto advertises itself as Mastodon
+                               if (($server['platform'] == 'mastodon') && substr($nodeinfo['software']['version'], -5) == '-qoto') {
+                                       $server['platform'] = 'qoto';
+                               }
                        }
                }
 
@@ -996,10 +1005,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function fetchSiteinfo(string $url, array $serverdata)
+       private static function fetchSiteinfo(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/siteinfo.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
@@ -1080,10 +1088,9 @@ class GServer
         * Checks if the server contains a valid host meta file
         *
         * @param string $url URL of the given server
-        *
         * @return boolean 'true' if the server seems to be vital
         */
-       private static function validHostMeta(string $url)
+       private static function validHostMeta(string $url): bool
        {
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
                $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', HttpClientAccept::XRD_XML, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
@@ -1126,10 +1133,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectNetworkViaContacts(string $url, array $serverdata)
+       private static function detectNetworkViaContacts(string $url, array $serverdata): array
        {
                $contacts = [];
 
@@ -1171,10 +1177,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function checkPoCo(string $url, array $serverdata)
+       private static function checkPoCo(string $url, array $serverdata): array
        {
                $serverdata['poco'] = '';
 
@@ -1203,10 +1208,9 @@ class GServer
         *
         * @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)
+       public static function checkMastodonDirectory(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/api/v1/directory?limit=1', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
@@ -1233,7 +1237,7 @@ class GServer
         *
         * @return array server data
         */
-       private static function detectPeertube(string $url, array $serverdata)
+       private static function detectPeertube(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/api/v1/config', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
@@ -1277,10 +1281,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectNextcloud(string $url, array $serverdata)
+       private static function detectNextcloud(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/status.php', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
@@ -1305,7 +1308,15 @@ class GServer
                return $serverdata;
        }
 
-       private static function fetchWeeklyUsage(string $url, array $serverdata) {
+       /**
+        * Fetches weekly usage data
+        *
+        * @param string $url        URL of the given server
+        * @param array  $serverdata array with server data
+        * @return array server data
+        */
+       private static function fetchWeeklyUsage(string $url, array $serverdata): array
+       {
                $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
                        return $serverdata;
@@ -1341,10 +1352,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectFromContacts(string $url, array $serverdata)
+       private static function detectFromContacts(string $url, array $serverdata): array
        {
                $gserver = DBA::selectFirst('gserver', ['id'], ['nurl' => Strings::normaliseLink($url)]);
                if (empty($gserver)) {
@@ -1369,10 +1379,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectMastodonAlikes(string $url, array $serverdata)
+       private static function detectMastodonAlikes(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/api/v1/instance', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
@@ -1434,10 +1443,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectHubzilla(string $url, array $serverdata)
+       private static function detectHubzilla(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/api/statusnet/config.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) {
@@ -1512,10 +1520,9 @@ class GServer
         * Converts input value to a boolean value
         *
         * @param string|integer $val
-        *
         * @return boolean
         */
-       private static function toBoolean($val)
+       private static function toBoolean($val): bool
        {
                if (($val == 'true') || ($val == 1)) {
                        return true;
@@ -1531,10 +1538,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectPumpIO(string $url, array $serverdata)
+       private static function detectPumpIO(string $url, array $serverdata): array
        {
                $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta.json', HttpClientAccept::JSON);
                if (!$curlResult->isSuccess()) {
@@ -1544,7 +1550,6 @@ class GServer
                $data = json_decode($curlResult->getBody(), true);
                if (empty($data['links'])) {
                        return $serverdata;
-
                }
 
                // We are looking for some endpoints that are typical for pump.io
@@ -1581,10 +1586,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectGNUSocial(string $url, array $serverdata)
+       private static function detectGNUSocial(string $url, array $serverdata): array
        {
                // Test for GNU Social
                $curlResult = DI::httpClient()->get($url . '/api/gnusocial/version.json', HttpClientAccept::JSON);
@@ -1636,10 +1640,9 @@ class GServer
         *
         * @param string $url        URL of the given server
         * @param array  $serverdata array with server data
-        *
         * @return array server data
         */
-       private static function detectFriendica(string $url, array $serverdata)
+       private static function detectFriendica(string $url, array $serverdata): array
        {
                // There is a bug in some versions of Friendica that will return an ActivityStream actor when the content type "application/json" is requested.
                // Because of this me must not use ACCEPT_JSON here.
@@ -1712,10 +1715,9 @@ class GServer
         * @param object $curlResult result of curl execution
         * @param array  $serverdata array with server data
         * @param string $url        Server URL
-        *
         * @return array server data
         */
-       private static function analyseRootBody($curlResult, array $serverdata, string $url)
+       private static function analyseRootBody($curlResult, array $serverdata, string $url): array
        {
                if (empty($curlResult->getBody())) {
                        return $serverdata;
@@ -1854,7 +1856,7 @@ class GServer
         *
         * @return array server data
         */
-       private static function analyseRootHeader($curlResult, array $serverdata)
+       private static function analyseRootHeader($curlResult, array $serverdata): array
        {
                if ($curlResult->getHeader('server') == 'Mastodon') {
                        $serverdata['platform'] = 'mastodon';
@@ -2049,7 +2051,7 @@ class GServer
         * @return int
         * @throws Exception
         */
-       public static function getProtocol(int $gsid)
+       public static function getProtocol(int $gsid): int
        {
                if (empty($gsid)) {
                        return null;