From 07cae83e15f0487be4eb6c2e4b1024a0bf54d5af Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 11 Oct 2018 09:53:14 -0400 Subject: [PATCH] Default to secure (https) urls --- app/appConf/base_config.php | 3 ++- app/config/config.toml.example | 12 +++++++++++- src/Types/Config.php | 1 + src/UrlGenerator.php | 11 +++++------ tests/UrlGeneratorTest.php | 4 ++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/appConf/base_config.php b/app/appConf/base_config.php index d347280e..18a96c66 100644 --- a/app/appConf/base_config.php +++ b/app/appConf/base_config.php @@ -30,7 +30,7 @@ return array_merge($tomlConfig, [ 'asset_dir' => "{$ROOT_DIR}/public", 'base_config_dir' => __DIR__, 'config_dir' => "{$APP_DIR}/config", - + // No config defaults 'kitsu_username' => 'timw4mail', 'whose_list' => 'Someone', @@ -38,6 +38,7 @@ return array_merge($tomlConfig, [ 'connection' => [], 'driver' => 'null', ], + 'secure_urls' => TRUE, // Routing defaults 'asset_path' => '/public', diff --git a/app/config/config.toml.example b/app/config/config.toml.example index 6a166d95..b185050b 100644 --- a/app/config/config.toml.example +++ b/app/config/config.toml.example @@ -23,4 +23,14 @@ default_list = "anime" # anime or manga # Default pages for anime/manga default_anime_list_path = "watching" # watching|plan_to_watch|on_hold|dropped|completed|all -default_manga_list_path = "reading" # reading|plan_to_read|on_hold|dropped|completed|all \ No newline at end of file +default_manga_list_path = "reading" # reading|plan_to_read|on_hold|dropped|completed|all + +################################################################################ +# Not on Settings Page +# +# These settings are not available to change on the settings page +################################################################################ + +# Use HTTPs for URLs +# It is not recommended to change this setting +secure_urls = true diff --git a/src/Types/Config.php b/src/Types/Config.php index 3ac54f7d..ca031217 100644 --- a/src/Types/Config.php +++ b/src/Types/Config.php @@ -29,6 +29,7 @@ class Config extends AbstractType { public $default_manga_list_path; public $default_view_type; public $kitsu_username; + public $secure_urls = TRUE; public $show_anime_collection; public $show_manga_collection; public $whose_list; diff --git a/src/UrlGenerator.php b/src/UrlGenerator.php index f3d8e39e..3a1b6193 100644 --- a/src/UrlGenerator.php +++ b/src/UrlGenerator.php @@ -34,19 +34,20 @@ class UrlGenerator extends RoutingBase { * Constructor * * @param ContainerInterface $container - * @throws \Aviat\Ion\Di\ContainerException - * @throws \Aviat\Ion\Di\NotFoundException + * @throws \Aviat\Ion\Di\Exception\ContainerException + * @throws \Aviat\Ion\Di\Exception\NotFoundException */ public function __construct(ContainerInterface $container) { parent::__construct($container); + $this->host = $container->get('request')->getServerParams()['HTTP_HOST']; } /** * Get the base url for css/js/images * - * @param string[] ...$args + * @param string ...$args * @return string */ public function assetUrl(string ...$args): string @@ -88,9 +89,7 @@ class UrlGenerator extends RoutingBase { } $path = implode('/', $path_segments); - $isHttps = $_SERVER['SERVER_PORT'] === '443' || isset($_SERVER['HTTPS']); - - $scheme = ($isHttps) ? 'https:' : 'http:'; + $scheme = $this->config->get('secure_urls') !== FALSE ? 'https:' : 'http:'; return "{$scheme}//{$this->host}/{$path}"; } diff --git a/tests/UrlGeneratorTest.php b/tests/UrlGeneratorTest.php index 721e1dda..4f3322ab 100644 --- a/tests/UrlGeneratorTest.php +++ b/tests/UrlGeneratorTest.php @@ -28,13 +28,13 @@ class UrlGeneratorTest extends AnimeClientTestCase { 'args' => [ 'images' ], - 'expected' => 'http://localhost/assets/images', + 'expected' => 'https://localhost/assets/images', ], 'multiple arguments' => [ 'args' => [ 'images', 'anime', 'foo.png' ], - 'expected' => 'http://localhost/assets/images/anime/foo.png' + 'expected' => 'https://localhost/assets/images/anime/foo.png' ] ]; }