chore(legacy): remove exploded public_url config

Replace exploded public_url parts with validated url object.
This commit is contained in:
jo 2022-07-07 23:45:15 +02:00 committed by Kyle Robbertze
parent d323657047
commit 712ecd70b4
3 changed files with 36 additions and 25 deletions

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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'],