diff --git a/.travis.yml b/.travis.yml index d570888..11802fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: php php: - 5.5 - 5.6 + - 7.0 - hhvm before_script: diff --git a/src/BitWeb/ErrorReporting/Service/ErrorService.php b/src/BitWeb/ErrorReporting/Service/ErrorService.php index 49d22da..b74291f 100644 --- a/src/BitWeb/ErrorReporting/Service/ErrorService.php +++ b/src/BitWeb/ErrorReporting/Service/ErrorService.php @@ -197,7 +197,7 @@ public function getErrorReportMetaData() $meta = new ErrorMeta(); $meta->setIp(Ip::getClientIp()); $meta->setUserAgent((isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : null); - $meta->setUrl(((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://') . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); + $meta->setUrl($this->getUrl()); $meta->setPostData($_POST); $meta->setGetData($_GET); $meta->setSessionData(isset($_SESSION) ? $_SESSION : null); @@ -245,4 +245,20 @@ protected function composeAndSendErrorMail() $this->eventManager->trigger($this->event, null, $params); } + + /** + * @return string + */ + public function getUrl() + { + if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) { + $prefix = (array_key_exists('HTTP_X_FORWARDED_PROTO', $_SERVER) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') ? 'https://' : 'http://'; + $domain = $_SERVER['HTTP_X_FORWARDED_FOR']; + } else { + $prefix = (array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] === 'on') ? 'https://' : 'http://'; + $domain = $_SERVER['SERVER_NAME']; + } + + return $prefix . $domain . $_SERVER['REQUEST_URI']; + } } diff --git a/test/BitWebTest/ErrorReporting/Service/ErrorServiceTest.php b/test/BitWebTest/ErrorReporting/Service/ErrorServiceTest.php index cf289d5..cc937e9 100644 --- a/test/BitWebTest/ErrorReporting/Service/ErrorServiceTest.php +++ b/test/BitWebTest/ErrorReporting/Service/ErrorServiceTest.php @@ -181,4 +181,43 @@ public function testRestoreDefaultErrorHandling() $service = new ErrorService($this->configuration); $this->assertTrue($service->restoreDefaultErrorHandling()); } + + public function testGetUrlWithHttps() + { + $_SERVER['HTTPS'] = 'on'; + $_SERVER['SERVER_NAME'] = 'bitweb.ee'; + $_SERVER['REQUEST_URI'] = '/error-reporting'; + + $service = new ErrorService($this->configuration); + $this->assertEquals('https://bitweb.ee/error-reporting', $service->getUrl()); + } + + public function testGetUrlWithHttpsNotOn() + { + $_SERVER['HTTPS'] = 'off'; + $_SERVER['SERVER_NAME'] = 'bitweb.ee'; + $_SERVER['REQUEST_URI'] = '/error-reporting'; + + $service = new ErrorService($this->configuration); + $this->assertEquals('http://bitweb.ee/error-reporting', $service->getUrl()); + } + + public function testGetUrlWithHttp() + { + $_SERVER['SERVER_NAME'] = 'bitweb.ee'; + $_SERVER['REQUEST_URI'] = '/error-reporting'; + + $service = new ErrorService($this->configuration); + $this->assertEquals('http://bitweb.ee/error-reporting', $service->getUrl()); + } + + public function testGetUrlWithXForwardedFor() + { + $_SERVER['HTTP_X_FORWARDED_FOR'] = 'bitweb.ee'; + $_SERVER['SERVER_NAME'] = 'localhost'; + $_SERVER['REQUEST_URI'] = '/error-reporting'; + + $service = new ErrorService($this->configuration); + $this->assertEquals('http://bitweb.ee/error-reporting', $service->getUrl()); + } }