]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/GServer.php
Merge pull request #10969 from MrPetovan/task/remove-private-contacts
[friendica.git] / src / Model / GServer.php
index 7c5a2b98a3fc1f0f626f6162f8cb1686bb064f30..af03041498d56cdb2ecaa36bf9afc3e5ced9a880 100644 (file)
@@ -32,8 +32,8 @@ use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Module\Register;
-use Friendica\Network\HTTPClientOptions;
-use Friendica\Network\IHTTPResult;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
+use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
 use Friendica\Protocol\Relay;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
@@ -117,6 +117,33 @@ class GServer
                return self::getID($url, true);
        }
 
+       /**
+        * Retrieves all the servers which base domain are matching the provided domain pattern
+        *
+        * The pattern is a simple fnmatch() pattern with ? for single wildcard and * for multiple wildcard
+        *
+        * @param string $pattern
+        * @return array
+        * @throws Exception
+        */
+       public static function listByDomainPattern(string $pattern): array
+       {
+               $likePattern = 'http://' . strtr($pattern, ['_' => '\_', '%' => '\%', '?' => '_', '*' => '%']);
+
+               // The SUBSTRING_INDEX returns everything before the eventual third /, which effectively trims an
+               // eventual server path and keep only the server domain which we're matching against the pattern.
+               $sql = "SELECT `gserver`.*, COUNT(*) AS `contacts`
+                       FROM `gserver`
+                       LEFT JOIN `contact` ON `gserver`.`id` = `contact`.`gsid`
+                       WHERE SUBSTRING_INDEX(`gserver`.`nurl`, '/', 3) LIKE ?
+                       AND NOT `gserver`.`failed`
+                       GROUP BY `gserver`.`id`";
+
+               $stmt = DI::dba()->p($sql, $likePattern);
+
+               return DI::dba()->toArray($stmt);
+       }
+
        /**
         * Checks if the given server is reachable
         *
@@ -315,7 +342,7 @@ class GServer
 
                // When a nodeinfo is present, we don't need to dig further
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
-               $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
+               $curlResult = DI::httpClient()->get($url . '/.well-known/nodeinfo', [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                if ($curlResult->isTimeout()) {
                        self::setFailure($url);
                        return false;
@@ -323,7 +350,7 @@ class GServer
 
                // 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::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
+                       $curlResult = DI::httpClient()->get($url, [HttpClientOptions::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);
@@ -359,7 +386,7 @@ class GServer
                                        $basedata = ['detection-method' => self::DETECT_MANUAL];
                                }
 
-                               $curlResult = DI::httpClient()->get($baseurl, [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
+                               $curlResult = DI::httpClient()->get($baseurl, [HttpClientOptions::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()]);
@@ -383,7 +410,7 @@ class GServer
                                        // When the base path doesn't seem to contain a social network we try the complete path.
                                        // Most detectable system have to be installed in the root directory.
                                        // We checked the base to avoid false positives.
-                                       $curlResult = DI::httpClient()->get($url, [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
+                                       $curlResult = DI::httpClient()->get($url, [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                                        if ($curlResult->isSuccess()) {
                                                $urldata = self::analyseRootHeader($curlResult, $serverdata);
                                                $urldata = self::analyseRootBody($curlResult, $urldata, $url);
@@ -672,13 +699,13 @@ class GServer
        /**
         * Detect server type by using the nodeinfo data
         *
-        * @param string      $url        address of the server
-        * @param IHTTPResult $httpResult
+        * @param string                  $url        address of the server
+        * @param ICanHandleHttpResponses $httpResult
         *
         * @return array Server data
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       private static function fetchNodeinfo(string $url, IHTTPResult $httpResult)
+       private static function fetchNodeinfo(string $url, ICanHandleHttpResponses $httpResult)
        {
                if (!$httpResult->isSuccess()) {
                        return [];
@@ -959,7 +986,7 @@ class GServer
        private static function validHostMeta(string $url)
        {
                $xrd_timeout = DI::config()->get('system', 'xrd_timeout');
-               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HTTPClientOptions::TIMEOUT => $xrd_timeout]);
+               $curlResult = DI::httpClient()->get($url . '/.well-known/host-meta', [HttpClientOptions::TIMEOUT => $xrd_timeout]);
                if (!$curlResult->isSuccess()) {
                        return false;
                }
@@ -1725,7 +1752,7 @@ class GServer
 
                if (!empty($accesstoken)) {
                        $api = 'https://instances.social/api/1.0/instances/list?count=0';
-                       $curlResult = DI::httpClient()->get($api, [HTTPClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
+                       $curlResult = DI::httpClient()->get($api, [HttpClientOptions::HEADERS => ['Authorization' => ['Bearer ' . $accesstoken]]]);
 
                        if ($curlResult->isSuccess()) {
                                $servers = json_decode($curlResult->getBody(), true);