diff --git a/lib/PostHog.php b/lib/PostHog.php index 899bf0d..a50f94c 100644 --- a/lib/PostHog.php +++ b/lib/PostHog.php @@ -28,12 +28,24 @@ public static function init( if (null === $client) { $apiKey = $apiKey ?: getenv(self::ENV_API_KEY); + $rawHost = null; if (array_key_exists("host", $options)) { - $options["host"] = self::cleanHost($options["host"]); + $rawHost = $options["host"]; + $options["host"] = self::cleanHost($rawHost); } else { $envHost = getenv(self::ENV_HOST) ?: null; if (null !== $envHost) { - $options["host"] = self::cleanHost(getenv(self::ENV_HOST)); + $rawHost = $envHost; + $options["host"] = self::cleanHost($rawHost); + } + } + + // Infer ssl from the host protocol if the user hasn't explicitly set it + if ($rawHost !== null && !array_key_exists("ssl", $options)) { + if (substr($rawHost, 0, 7) === "http://") { + $options["ssl"] = false; + } elseif (substr($rawHost, 0, 8) === "https://") { + $options["ssl"] = true; } } diff --git a/test/PostHogTest.php b/test/PostHogTest.php index 48997e8..98f6379 100644 --- a/test/PostHogTest.php +++ b/test/PostHogTest.php @@ -63,6 +63,75 @@ public function testInitWithEnvApiKey(): void putenv(PostHog::ENV_API_KEY); } + public function testInitWithHttpHostSetsSslFalse(): void + { + PostHog::init("random_key", ["host" => "http://localhost:8010"]); + + $client = PostHog::getClient(); + $ref = new \ReflectionClass($client); + $consumerProp = $ref->getProperty('consumer'); + $consumerProp->setAccessible(true); + $consumer = $consumerProp->getValue($client); + + $cRef = new \ReflectionClass($consumer); + $httpProp = $cRef->getProperty('httpClient'); + $httpProp->setAccessible(true); + $httpClient = $httpProp->getValue($consumer); + + $hRef = new \ReflectionClass($httpClient); + $sslProp = $hRef->getProperty('useSsl'); + $sslProp->setAccessible(true); + + $this->assertFalse($sslProp->getValue($httpClient), 'HttpClient should use ssl=false for http:// hosts'); + } + + public function testInitWithHttpsHostSetsSslTrue(): void + { + PostHog::init("random_key", ["host" => "https://app.posthog.com"]); + + $client = PostHog::getClient(); + $ref = new \ReflectionClass($client); + $consumerProp = $ref->getProperty('consumer'); + $consumerProp->setAccessible(true); + $consumer = $consumerProp->getValue($client); + + $cRef = new \ReflectionClass($consumer); + $httpProp = $cRef->getProperty('httpClient'); + $httpProp->setAccessible(true); + $httpClient = $httpProp->getValue($consumer); + + $hRef = new \ReflectionClass($httpClient); + $sslProp = $hRef->getProperty('useSsl'); + $sslProp->setAccessible(true); + + $this->assertTrue($sslProp->getValue($httpClient), 'HttpClient should use ssl=true for https:// hosts'); + } + + public function testInitWithEnvHttpHostSetsSslFalse(): void + { + putenv(PostHog::ENV_HOST . "=http://localhost:8010"); + PostHog::init("random_key"); + + $client = PostHog::getClient(); + $ref = new \ReflectionClass($client); + $consumerProp = $ref->getProperty('consumer'); + $consumerProp->setAccessible(true); + $consumer = $consumerProp->getValue($client); + + $cRef = new \ReflectionClass($consumer); + $httpProp = $cRef->getProperty('httpClient'); + $httpProp->setAccessible(true); + $httpClient = $httpProp->getValue($consumer); + + $hRef = new \ReflectionClass($httpClient); + $sslProp = $hRef->getProperty('useSsl'); + $sslProp->setAccessible(true); + + $this->assertFalse($sslProp->getValue($httpClient), 'HttpClient should use ssl=false for http:// env host'); + + putenv(PostHog::ENV_HOST); + } + public function testInitThrowsExceptionWithNoApiKey(): void { $this->expectException(Exception::class);