]> git.mxchange.org Git - friendica.git/commitdiff
Store the "authredirect" path of a server
authorMichael <heluecht@pirati.ca>
Sun, 26 May 2024 00:07:06 +0000 (00:07 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 26 May 2024 00:07:06 +0000 (00:07 +0000)
database.sql
doc/database/db_gserver.md
src/Model/GServer.php
static/dbstructure.config.php

index 6d529e8fb42f7c2ef5c67edf3e3dc1f852841a56..0c96eadb0b748f2cd660250800fe7b13be4e3a76 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2024.06-dev (Yellow Archangel)
--- DB_UPDATE_VERSION 1561
+-- DB_UPDATE_VERSION 1562
 -- ------------------------------------------
 
 
@@ -24,6 +24,7 @@ CREATE TABLE IF NOT EXISTS `gserver` (
        `directory-type` tinyint DEFAULT 0 COMMENT 'Type of directory service (Poco, Mastodon)',
        `poco` varbinary(383) NOT NULL DEFAULT '' COMMENT '',
        `openwebauth` varbinary(383) COMMENT 'Path to the OpenWebAuth endpoint',
+       `authredirect` varbinary(383) COMMENT 'Path to the authRedirect endpoint',
        `noscrape` varbinary(383) NOT NULL DEFAULT '' COMMENT '',
        `network` char(4) NOT NULL DEFAULT '' COMMENT '',
        `protocol` tinyint unsigned COMMENT 'The protocol of the server',
index 43cf4cb89aa4792a5f1190509ca6a03f98b559b9..238f053d5decbad7308e42e35a24167f4a8ca853 100644 (file)
@@ -24,6 +24,7 @@ Fields
 | directory-type        | Type of directory service (Poco, Mastodon)         | tinyint          | YES  |     | 0                   |                |
 | poco                  |                                                    | varbinary(383)   | NO   |     |                     |                |
 | openwebauth           | Path to the OpenWebAuth endpoint                   | varbinary(383)   | YES  |     | NULL                |                |
+| authredirect          | Path to the authRedirect endpoint                  | varbinary(383)   | YES  |     | NULL                |                |
 | noscrape              |                                                    | varbinary(383)   | NO   |     |                     |                |
 | network               |                                                    | char(4)          | NO   |     |                     |                |
 | protocol              | The protocol of the server                         | tinyint unsigned | YES  |     | NULL                |                |
index 8eb49ad7ddab50c4907b9e65a8a0f205e7e84591..c09aed60bfd9f940e319a843c0b0030cd63d6682 100644 (file)
@@ -578,7 +578,7 @@ class GServer
                        // We only follow redirects when the path stays the same or the target url has no path.
                        // Some systems have got redirects on their landing page to a single account page. This check handles it.
                        if (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) && (parse_url($url, PHP_URL_PATH) == parse_url($valid_url, PHP_URL_PATH))) ||
-                               (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) || (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH))) && empty(parse_url($valid_url, PHP_URL_PATH)))) {
+                       (((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) || (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH))) && empty(parse_url($valid_url, PHP_URL_PATH)))) {
                                Logger::debug('Found redirect. Mark old entry as failure', ['old' => $url, 'new' => $valid_url]);
                                self::setFailureByUrl($url);
                                if (!self::getID($valid_url, true) && !Network::isUrlBlocked($valid_url)) {
@@ -588,7 +588,7 @@ class GServer
                        }
 
                        if ((parse_url($url, PHP_URL_HOST) != parse_url($valid_url, PHP_URL_HOST)) && (parse_url($url, PHP_URL_PATH) != parse_url($valid_url, PHP_URL_PATH)) &&
-                               (parse_url($url, PHP_URL_PATH) == '')) {
+                       (parse_url($url, PHP_URL_PATH) == '')) {
                                Logger::debug('Found redirect. Mark old entry as failure and redirect to the basepath.', ['old' => $url, 'new' => $valid_url]);
                                $parts = (array)parse_url($valid_url);
                                unset($parts['path']);
@@ -606,7 +606,7 @@ class GServer
                if ((parse_url($url, PHP_URL_HOST) == parse_url($valid_url, PHP_URL_HOST)) &&
                        (parse_url($url, PHP_URL_PATH) == parse_url($valid_url, PHP_URL_PATH)) &&
                        (parse_url($url, PHP_URL_SCHEME) != parse_url($valid_url, PHP_URL_SCHEME))) {
-                       $url = $valid_url;
+                               $url = $valid_url;
                }
 
                $in_webroot = empty(parse_url($url, PHP_URL_PATH));
@@ -737,6 +737,10 @@ class GServer
                        }
                }
 
+               if (in_array($serverdata['platform'] ?? '', ['hubzilla', 'streams', 'osada', 'mistpark', 'roadhouse', 'zap'])) {
+                       $serverdata = self::getZotData($url, $serverdata);
+               }
+
                // When we hadn't been able to detect the network type, we use the hint from the parameter
                if (($serverdata['network'] == Protocol::PHANTOM) && !empty($network)) {
                        $serverdata['network'] = $network;
@@ -1623,6 +1627,80 @@ class GServer
                return $data ?? '';
        }
 
+       private static function getZotData(string $url, array $serverdata): array
+       {
+               $curlResult = DI::httpClient()->get($url, 'application/x-zot+json');
+               if (!$curlResult->isSuccess()) {
+                       return $serverdata;
+               }
+               $json = $curlResult->getBodyString();
+               $data = json_decode($json, true);
+               if (empty($data)) {
+                       return $serverdata;
+               }
+
+               if (!empty($data['site'])) {
+                       $serverdata = self::getFromZotData($data['site'], $serverdata);
+               } else {
+                       $serverdata = self::getFromZotData($data, $serverdata);
+               }
+               return $serverdata;
+       }
+
+       private static function getFromZotData(array $data, array $serverdata): array
+       {
+               if (!empty($data['version'])) {
+                       $serverdata['version'] = $data['version'];
+               }
+
+               if (!empty($data['openWebAuth'])) {
+                       $serverdata['openwebauth'] = $data['openWebAuth'];
+               }
+
+               if (!empty($data['authRedirect'])) {
+                       $serverdata['authredirect'] = $data['authRedirect'];
+               }
+
+               if (!empty($data['sitename'])) {
+                       $serverdata['site_name'] = $data['sitename'];
+               }
+
+               if (!empty($data['about'])) {
+                       $serverdata['info'] = $data['about'];
+               }
+
+               if (empty($serverdata['info']) && !empty($data['location'])) {
+                       $serverdata['info'] = $data['location'];
+               }
+
+               if (!empty($data['project']) && in_array($data['project'], ['hubzilla', 'streams', 'osada', 'mistpark', 'roadhouse', 'zap'])) {
+                       $serverdata['platform'] = $data['project'];
+               }
+
+               if (!empty($data['accounts'])) {
+                       $serverdata['registered-users'] = $data['accounts'];
+               }
+
+               if (!empty($data['register_policy'])) {
+                       switch ($data['register_policy']) {
+                               case 'open':
+                                       $serverdata['register_policy'] = Register::OPEN;
+                                       break;
+                               case 'closed':
+                                       $serverdata['register_policy'] = Register::CLOSED;
+                                       break;
+                               case 'approve':
+                                       $serverdata['register_policy'] = Register::APPROVE;
+                                       break;
+                               default:
+                                       echo $data['register_policy'] . "\n";
+                                       break;
+                       }
+               }
+
+               return $serverdata;
+       }
+
        /**
         * Checks if the server contains a valid host meta file
         *
@@ -2105,7 +2183,7 @@ class GServer
                        ($curlResult->getBodyString() != '') && (strlen($curlResult->getBodyString()) < 30)) {
 
                        // Remove junk that some GNU Social servers return
-                       $serverdata['version'] = str_replace(chr(239).chr(187).chr(191), '', $curlResult->getBodyString());
+                       $serverdata['version'] = str_replace(chr(239) . chr(187) . chr(191), '', $curlResult->getBodyString());
                        $serverdata['version'] = str_replace(["\r", "\n", "\t"], '', $serverdata['version']);
                        $serverdata['version'] = trim($serverdata['version'], '"');
 
@@ -2192,7 +2270,7 @@ class GServer
                                break;
                        default:
                                Logger::info('Register policy is invalid', ['policy' => $register_policy, 'server' => $url]);
-                               $serverdata['register_policy'] = Register::CLOSED;
+                               $serverdata['register_policy'] = Register::CLOSED;
                                break;
                }
 
index e733732f329b561cc47257306aca35d70151475f..ec17f54eb0b9448443202a84a027a8088bb2de23 100644 (file)
@@ -56,7 +56,7 @@ use Friendica\Database\DBA;
 
 // This file is required several times during the test in DbaDefinition which justifies this condition
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1561);
+       define('DB_UPDATE_VERSION', 1562);
 }
 
 return [
@@ -80,6 +80,7 @@ return [
                        "directory-type" => ["type" => "tinyint", "default" => "0", "comment" => "Type of directory service (Poco, Mastodon)"],
                        "poco" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""],
                        "openwebauth" => ["type" => "varbinary(383)", "comment" => "Path to the OpenWebAuth endpoint"],
+                       "authredirect" => ["type" => "varbinary(383)", "comment" => "Path to the authRedirect endpoint"],
                        "noscrape" => ["type" => "varbinary(383)", "not null" => "1", "default" => "", "comment" => ""],
                        "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => ""],
                        "protocol" => ["type" => "tinyint unsigned", "comment" => "The protocol of the server"],