From 0429a4e4296ef0a21804c546b7a3cf483e9c0a46 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 8 Jan 2023 02:04:25 +0100 Subject: [PATCH 1/9] Fix loading empty node.config.php --- src/Core/Config/Util/ConfigFileManager.php | 11 +++++++++-- .../Config/Cache/ConfigFileManagerTest.php | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Core/Config/Util/ConfigFileManager.php b/src/Core/Config/Util/ConfigFileManager.php index f3627cf477..fff240bb30 100644 --- a/src/Core/Config/Util/ConfigFileManager.php +++ b/src/Core/Config/Util/ConfigFileManager.php @@ -177,7 +177,7 @@ class ConfigFileManager { $filename = $this->configDir . '/' . self::CONFIG_DATA_FILE; - if (file_exists($filename)) { + if (file_exists($filename) && (filesize($filename) > 0)) { // The fallback empty return content $content = 'get('system', 'default_timezone')); } + + /** + * Test for empty node.config.php + */ + public function testEmptyFile() + { + $this->delConfigFile('node.config.php'); + + vfsStream::newFile('node.config.php') + ->at($this->root->getChild('config')) + ->setContent(''); + + $configFileManager = (new Config())->createConfigFileManager($this->root->url()); + $configCache = new Cache(); + + $configFileManager->setupCache($configCache); + + self::assertEquals(1,1); + } } From 2293ff6206b114be1bddbc789b843aa49b338ae7 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 8 Jan 2023 02:28:27 +0100 Subject: [PATCH 2/9] Add test for Addon failures --- src/Core/Config/Util/ConfigFileManager.php | 4 ++- src/Core/Config/ValueObject/Cache.php | 2 +- tests/src/Core/Config/Cache/CacheTest.php | 39 +++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Core/Config/Util/ConfigFileManager.php b/src/Core/Config/Util/ConfigFileManager.php index fff240bb30..011dbf0e48 100644 --- a/src/Core/Config/Util/ConfigFileManager.php +++ b/src/Core/Config/Util/ConfigFileManager.php @@ -194,11 +194,13 @@ class ConfigFileManager try { if (flock($configStream, LOCK_SH)) { + clearstatcache(true, $filename); + if (($filesize = filesize($filename)) === 0) { return; } - $content = fread($configStream, filesize($filename)); + $content = fread($configStream, $filesize); if (!$content) { throw new ConfigFileException(sprintf('Couldn\'t read file %s', $filename)); } diff --git a/src/Core/Config/ValueObject/Cache.php b/src/Core/Config/ValueObject/Cache.php index 2082511641..7492d28734 100644 --- a/src/Core/Config/ValueObject/Cache.php +++ b/src/Core/Config/ValueObject/Cache.php @@ -283,7 +283,7 @@ class Cache $keys = array_keys($config[$category]); foreach ($keys as $key) { - if (!isset($this->config[$category][$key])) { + if (!key_exists($key, $this->config[$category] ?? [])) { $return[$category][$key] = $config[$category][$key]; } } diff --git a/tests/src/Core/Config/Cache/CacheTest.php b/tests/src/Core/Config/Cache/CacheTest.php index dc9b62dc00..e14b7998c5 100644 --- a/tests/src/Core/Config/Cache/CacheTest.php +++ b/tests/src/Core/Config/Cache/CacheTest.php @@ -40,6 +40,7 @@ class CacheTest extends MockedTest 'int' => 235, 'dec' => 2.456, 'array' => ['1', 2, '3', true, false], + 'null' => null, ], 'config' => [ 'a' => 'value', @@ -503,6 +504,24 @@ class CacheTest extends MockedTest ], ], ], + /** @see https://github.com/friendica/friendica/issues/12486#issuecomment-1374609349 */ + 'test_with_null' => [ + 'data' => [ + 'test_with_null' => null, + 'config' => [ + 'register_policy' => 2, + 'register_text' => '', + 'sitename' => 'Friendica Social Network23', + 'hostname' => 'friendica.local', + 'private_addons' => false, + ], + 'system' => [ + 'dbclean_expire_conversation' => 90, + ], + ], + 'cat' => 'test_with_null', + 'assertion' => null, + ], ]; } @@ -511,10 +530,28 @@ class CacheTest extends MockedTest * * @dataProvider dataTestCat */ - public function testGetCategory(array $data, string $category, array $assertion) + public function testGetCategory($data, string $category, $assertion) { $cache = new Cache($data); self::assertEquals($assertion, $cache->get($category)); } + + /** + * Test that the cache can get merged with different categories + * + * @dataProvider dataTestCat + */ + public function testCatMerge($data, string $category) + { + $cache = new Cache($data); + + $newCache = $cache->merge(new Cache([ + $category => [ + 'new_key' => 'new_value', + ], + ])); + + self::assertEquals('new_value', $newCache->get($category, 'new_key')); + } } From 5ea50a9e81a7f363040d7c86a4a41b8a1f8f7758 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 8 Jan 2023 02:28:47 +0100 Subject: [PATCH 3/9] Fix "null" addon list --- src/Core/Config/ValueObject/Cache.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Core/Config/ValueObject/Cache.php b/src/Core/Config/ValueObject/Cache.php index 7492d28734..61a65c3e70 100644 --- a/src/Core/Config/ValueObject/Cache.php +++ b/src/Core/Config/ValueObject/Cache.php @@ -311,6 +311,11 @@ class Cache if (is_array($cache->config[$category])) { $keys = array_keys($cache->config[$category]); + if (is_null($newConfig[$category] ?? null)) { + $newConfig[$category] = []; + $newSource[$category] = []; + } + foreach ($keys as $key) { $newConfig[$category][$key] = $cache->config[$category][$key]; $newSource[$category][$key] = $cache->source[$category][$key]; From 979672a3c1c6aacb91f005e5912763ad55d08dd1 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 8 Jan 2023 02:33:50 +0100 Subject: [PATCH 4/9] Filter disabled addons with "null" --- src/Core/Addon.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Core/Addon.php b/src/Core/Addon.php index 8d1a866915..06f6a12967 100644 --- a/src/Core/Addon.php +++ b/src/Core/Addon.php @@ -84,7 +84,7 @@ class Addon public static function getAdminList(): array { $addons_admin = []; - $addons = DI::config()->get('addons') ?? []; + $addons = array_filter(DI::config()->get('addons') ?? []); ksort($addons); foreach ($addons as $name => $data) { @@ -117,7 +117,7 @@ class Addon */ public static function loadAddons() { - self::$addons = array_keys(DI::config()->get('addons') ?? []); + self::$addons = array_keys(array_filter(DI::config()->get('addons') ?? [])); } /** @@ -192,7 +192,7 @@ class Addon */ public static function reload() { - $addons = DI::config()->get('addons') ?? []; + $addons = array_filter(DI::config()->get('addons') ?? []); foreach ($addons as $name => $data) { $addonname = Strings::sanitizeFilePathItem(trim($name)); @@ -315,7 +315,7 @@ class Addon public static function getVisibleList(): array { $visible_addons = []; - $addons = DI::config()->get('addons') ?? []; + $addons = array_filter(DI::config()->get('addons') ?? []); foreach ($addons as $name => $data) { $visible_addons[] = $name; From 8ad94fef9bdbae4a328d5b30b094158485618cc8 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 7 Jan 2023 22:37:58 -0500 Subject: [PATCH 5/9] Change "thumbnail" for default Friendica banner image in Api\Mastodon\Instance --- src/Object/Api/Mastodon/Instance.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Object/Api/Mastodon/Instance.php b/src/Object/Api/Mastodon/Instance.php index fe1a9b3fef..14914cd7fc 100644 --- a/src/Object/Api/Mastodon/Instance.php +++ b/src/Object/Api/Mastodon/Instance.php @@ -54,7 +54,7 @@ class Instance extends BaseDataTransferObject protected $urls; /** @var Stats */ protected $stats; - /** @var string|null */ + /** @var string|null This is meant as a server banner, default Mastodon "thumbnail" is 1600×620px */ protected $thumbnail = null; /** @var array */ protected $languages; @@ -91,7 +91,7 @@ class Instance extends BaseDataTransferObject $this->version = '2.8.0 (compatible; Friendica ' . App::VERSION . ')'; $this->urls = null; // Not supported $this->stats = new Stats($config, $database); - $this->thumbnail = $baseUrl->get() . ($config->get('system', 'shortcut_icon') ?? 'images/friendica-32.png'); + $this->thumbnail = $baseUrl->get() . 'images/friendica-banner.jpg'; $this->languages = [$config->get('system', 'language')]; $this->max_toot_chars = (int)$config->get('config', 'api_import_size', $config->get('config', 'max_import_size')); $this->registrations = ($register_policy != Register::CLOSED); From 97642e770b0260bc5d7e067e40e818ccdeff6141 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 7 Jan 2023 22:47:52 -0500 Subject: [PATCH 6/9] Treat unsupported API call as HTTP error --- src/Module/Api/ApiResponse.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Module/Api/ApiResponse.php b/src/Module/Api/ApiResponse.php index 2d54bc1765..b5b2a4717c 100644 --- a/src/Module/Api/ApiResponse.php +++ b/src/Module/Api/ApiResponse.php @@ -276,9 +276,8 @@ class ApiResponse extends Response 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '', 'request' => $request, ]); - $error = $this->l10n->t('API endpoint %s %s is not implemented', strtoupper($method), $path); - $error_description = $this->l10n->t('The API endpoint is currently not implemented but might be in the future.'); + $error = $this->l10n->t('API endpoint %s %s is not implemented but might be in the future.', strtoupper($method), $path); - $this->exit('error', ['error' => ['error' => $error, 'error_description' => $error_description]]); + $this->error(501, 'Not Implemented', $error); } } From 6c379610dc0c9ca0af3174b21e88d35246443ae3 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 7 Jan 2023 22:48:31 -0500 Subject: [PATCH 7/9] Update main translation file after removing a string --- view/lang/C/messages.po | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/view/lang/C/messages.po b/view/lang/C/messages.po index ab61019233..4bd45b35b6 100644 --- a/view/lang/C/messages.po +++ b/view/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 2023.03-dev\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2023-01-07 17:15+0000\n" +"POT-Creation-Date: 2023-01-07 22:48-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -18,34 +18,34 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" -#: mod/item.php:101 mod/item.php:105 mod/item.php:178 mod/item.php:182 +#: mod/item.php:101 mod/item.php:104 mod/item.php:170 mod/item.php:173 #: src/Content/Item.php:860 msgid "Unable to locate original post." msgstr "" -#: mod/item.php:139 +#: mod/item.php:138 msgid "Post updated." msgstr "" -#: mod/item.php:213 mod/item.php:218 +#: mod/item.php:201 mod/item.php:205 msgid "Item wasn't stored." msgstr "" -#: mod/item.php:228 +#: mod/item.php:215 msgid "Item couldn't be fetched." msgstr "" -#: mod/item.php:273 mod/item.php:278 +#: mod/item.php:261 mod/item.php:265 msgid "Empty post discarded." msgstr "" -#: mod/item.php:414 src/Module/Admin/Themes/Details.php:39 +#: mod/item.php:401 src/Module/Admin/Themes/Details.php:39 #: src/Module/Admin/Themes/Index.php:59 src/Module/Debug/ItemBody.php:42 #: src/Module/Debug/ItemBody.php:57 src/Module/Item/Feed.php:80 msgid "Item not found." msgstr "" -#: mod/item.php:438 mod/message.php:69 mod/message.php:114 mod/notes.php:44 +#: mod/item.php:425 mod/message.php:69 mod/message.php:114 mod/notes.php:44 #: mod/photos.php:158 mod/photos.php:675 src/Model/Event.php:522 #: src/Module/Attach.php:55 src/Module/BaseApi.php:95 #: src/Module/BaseNotifications.php:98 src/Module/BaseSettings.php:52 @@ -5220,12 +5220,7 @@ msgstr "" #: src/Module/Api/ApiResponse.php:279 #, php-format -msgid "API endpoint %s %s is not implemented" -msgstr "" - -#: src/Module/Api/ApiResponse.php:280 -msgid "" -"The API endpoint is currently not implemented but might be in the future." +msgid "API endpoint %s %s is not implemented but might be in the future." msgstr "" #: src/Module/Api/Mastodon/Apps.php:64 @@ -8136,17 +8131,17 @@ msgstr "" msgid "The Photo is not available." msgstr "" -#: src/Module/Photo.php:142 +#: src/Module/Photo.php:154 #, php-format msgid "The Photo with id %s is not available." msgstr "" -#: src/Module/Photo.php:179 +#: src/Module/Photo.php:191 #, php-format msgid "Invalid external resource with url %s." msgstr "" -#: src/Module/Photo.php:181 +#: src/Module/Photo.php:193 #, php-format msgid "Invalid photo with id %s." msgstr "" From 85470147351db813d387d701724e2a3559bbcb63 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 8 Jan 2023 02:36:26 -0500 Subject: [PATCH 8/9] Update API unsupported test --- tests/src/Module/Api/ApiResponseTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Module/Api/ApiResponseTest.php b/tests/src/Module/Api/ApiResponseTest.php index 81636fe105..2defaf995c 100644 --- a/tests/src/Module/Api/ApiResponseTest.php +++ b/tests/src/Module/Api/ApiResponseTest.php @@ -130,7 +130,7 @@ class ApiResponseTest extends MockedTest $response = new ApiResponse($l10n, $args, new NullLogger(), $baseUrl, $twitterUser); $response->unsupported(); - self::assertEquals('{"error":"API endpoint %s %s is not implemented","error_description":"The API endpoint is currently not implemented but might be in the future."}', $response->getContent()); + self::assertEquals('{"error":"API endpoint %s %s is not implemented but might be in the future.","code":"501 Not Implemented","request":""}', $response->getContent()); } /** From 1ef9768b98be372531c9fce1adf118617cabd253 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 8 Jan 2023 13:00:58 +0100 Subject: [PATCH 9/9] Apply suggestions from code review Co-authored-by: Philipp --- src/Module/OStatus/PubSub.php | 2 +- src/Module/OStatus/PubSubHubBub.php | 2 +- src/Module/OStatus/Salmon.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Module/OStatus/PubSub.php b/src/Module/OStatus/PubSub.php index c4614855ca..5fbc529836 100644 --- a/src/Module/OStatus/PubSub.php +++ b/src/Module/OStatus/PubSub.php @@ -58,7 +58,7 @@ class PubSub extends \Friendica\BaseModule $this->logger->info('Feed arrived.', ['from' => $this->request->getRemoteAddress(), 'for' => $this->args->getCommand(), 'user-agent' => $this->server['HTTP_USER_AGENT']]); $this->logger->debug('Data stream.', ['xml' => $xml]); - $this->logger->debug('Gut request data.', ['request' => $request]); + $this->logger->debug('Got request data.', ['request' => $request]); $nickname = $this->parameters['nickname'] ?? ''; $contact_id = $this->parameters['cid'] ?? 0; diff --git a/src/Module/OStatus/PubSubHubBub.php b/src/Module/OStatus/PubSubHubBub.php index de2104b212..caaa31f98b 100644 --- a/src/Module/OStatus/PubSubHubBub.php +++ b/src/Module/OStatus/PubSubHubBub.php @@ -71,7 +71,7 @@ class PubSubHubBub extends \Friendica\BaseModule throw new HTTPException\ForbiddenException(); } - $this->logger->debug('Gut request data.', ['request' => $request]); + $this->logger->debug('Got request data.', ['request' => $request]); // Subscription request from subscriber // https://pubsubhubbub.github.io/PubSubHubbub/pubsubhubbub-core-0.4.html#rfc.section.5.1 diff --git a/src/Module/OStatus/Salmon.php b/src/Module/OStatus/Salmon.php index 64dcec22cf..889d852d96 100644 --- a/src/Module/OStatus/Salmon.php +++ b/src/Module/OStatus/Salmon.php @@ -65,7 +65,7 @@ class Salmon extends \Friendica\BaseModule protected function post(array $request = []) { $xml = Network::postdata(); - $this->logger->debug('Gut request data.', ['request' => $request]); + $this->logger->debug('Got request data.', ['request' => $request]); $nickname = $this->parameters['nickname'] ?? ''; if (empty($nickname)) {