From 712ecd70b46e44b3b1fa815be70d61509e39b1a3 Mon Sep 17 00:00:00 2001 From: jo Date: Thu, 7 Jul 2022 23:45:15 +0200 Subject: [PATCH] chore(legacy): remove exploded public_url config Replace exploded public_url parts with validated url object. --- legacy/application/configs/conf.php | 50 +++++++++++++-------- legacy/application/models/StoredFile.php | 4 +- legacy/application/models/StreamSetting.php | 7 ++- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/legacy/application/configs/conf.php b/legacy/application/configs/conf.php index ed55ff342..855172064 100644 --- a/legacy/application/configs/conf.php +++ b/legacy/application/configs/conf.php @@ -25,24 +25,9 @@ class Config // ////////////////////////////////////////////////////////////////////////////// $CC_CONFIG['apiKey'] = [$values['general']['api_key']]; - // Explode public_url into multiple component with possible defaults for required fields - try { - $public_url = Uri::createFromString($values['general']['public_url']); - } catch (UriException|TypeError $e) { - echo 'could not parse configuration field general.public_url: ' . $e->getMessage(); - - exit; - } - - $scheme = $public_url->getScheme() ?? 'http'; - $host = $public_url->getHost() ?? 'localhost'; - $port = $public_url->getPort() ?? ($scheme == 'https' ? 443 : 80); - $path = rtrim($public_url->getPath() ?? '', '/') . '/'; // Path requires a trailing slash - - $CC_CONFIG['protocol'] = $scheme; - $CC_CONFIG['baseUrl'] = $host; - $CC_CONFIG['basePort'] = $port; - $CC_CONFIG['baseDir'] = $path; + $public_url = self::validateUrl('general.public_url', $values['general']['public_url']); + $CC_CONFIG['public_url_raw'] = $public_url; + $CC_CONFIG['public_url'] = strval($public_url); // Allowed hosts $CC_CONFIG['allowedCorsOrigins'] = $values['general']['allowed_cors_origins'] ?? []; @@ -157,8 +142,37 @@ class Config return in_array(strtolower($value), ['yes', 'true']); } + /** + * Validate and sanitize url. + * + * @param mixed $key + * @param mixed $value + */ + public static function validateUrl($key, $value) + { + try { + $url = Uri::createFromString($value); + + return $url->withPath(rtrim($url->getPath() ?? '', '/') . '/'); + } catch (UriException|TypeError $e) { + echo "could not parse configuration field {$key}: " . $e->getMessage(); + + exit; + } + } + public static function getStoragePath() { return rtrim(self::getConfig()['storagePath'], '/') . '/'; } + + public static function getPublicUrl() + { + return self::getConfig()['public_url']; + } + + public static function getBasePath() + { + return self::getConfig()['public_url_raw']->getPath(); + } } diff --git a/legacy/application/models/StoredFile.php b/legacy/application/models/StoredFile.php index 92646d9eb..b2c51a3ac 100644 --- a/legacy/application/models/StoredFile.php +++ b/legacy/application/models/StoredFile.php @@ -510,13 +510,11 @@ SQL; */ public function getFileUrl() { - $CC_CONFIG = Config::getConfig(); - $protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https'; $serverName = $_SERVER['SERVER_NAME']; $serverPort = $_SERVER['SERVER_PORT']; - $subDir = $CC_CONFIG['baseDir']; + $subDir = Config::getBasePath(); if ($protocol === 'https' && $serverPort == 80) { $serverPort = 443; diff --git a/legacy/application/models/StreamSetting.php b/legacy/application/models/StreamSetting.php index 43c789ec4..0b828ceda 100644 --- a/legacy/application/models/StreamSetting.php +++ b/legacy/application/models/StreamSetting.php @@ -260,10 +260,9 @@ class Application_Model_StreamSetting $v = trim($v); if ($k != 'admin_pass') { self::saveStreamSetting($keyname, $v); - /* We use 'xxxxxx' as the admin password placeholder so we - * only want to save it when it is a different string - */ } elseif ($v != 'xxxxxx') { + // We use 'xxxxxx' as the admin password placeholder so we + // only want to save it when it is a different string self::saveStreamSetting($keyname, $v); } } @@ -283,7 +282,7 @@ class Application_Model_StreamSetting $config = Config::getConfig(); return [ - 'host' => $config['baseUrl'], + 'host' => $config['public_url_raw']->getHost(), 'port' => DEFAULT_ICECAST_PORT, 'output' => 'icecast', 'user' => $config['stationId'],