diff --git a/system/HTTP/UserAgent.php b/system/HTTP/UserAgent.php index 5e8eece63659..ba9e544ae2f8 100644 --- a/system/HTTP/UserAgent.php +++ b/system/HTTP/UserAgent.php @@ -109,8 +109,10 @@ public function __construct(?UserAgents $config = null) { $this->config = $config ?? config(UserAgents::class); - if (isset($_SERVER['HTTP_USER_AGENT'])) { - $this->agent = trim($_SERVER['HTTP_USER_AGENT']); + $userAgent = service('superglobals')->server('HTTP_USER_AGENT'); + + if ($userAgent !== null) { + $this->agent = trim($userAgent); $this->compileData(); } } @@ -175,10 +177,11 @@ public function isMobile(?string $key = null): bool public function isReferral(): bool { if (! isset($this->referrer)) { - if (empty($_SERVER['HTTP_REFERER'])) { + $referer = service('superglobals')->server('HTTP_REFERER'); + if ($referer === null || $referer === '') { $this->referrer = false; } else { - $refererHost = @parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); + $refererHost = @parse_url($referer, PHP_URL_HOST); $ownHost = parse_url(\base_url(), PHP_URL_HOST); $this->referrer = ($refererHost && $refererHost !== $ownHost); @@ -241,7 +244,9 @@ public function getMobile(): string */ public function getReferrer(): string { - return empty($_SERVER['HTTP_REFERER']) ? '' : trim($_SERVER['HTTP_REFERER']); + $referrer = service('superglobals')->server('HTTP_REFERER'); + + return $referrer === null ? '' : trim($referrer); } /** diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 1240d0885b23..c6a693b7681c 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -760,7 +760,7 @@ public function testIsSecureForwarded(): void public function testUserAgent(): void { - $_SERVER['HTTP_USER_AGENT'] = 'Mozilla'; + service('superglobals')->setServer('HTTP_USER_AGENT', 'Mozilla'); $config = new App(); $request = $this->createRequest($config); diff --git a/tests/system/HTTP/UserAgentTest.php b/tests/system/HTTP/UserAgentTest.php index 9799a79c2851..c43c46354e87 100644 --- a/tests/system/HTTP/UserAgentTest.php +++ b/tests/system/HTTP/UserAgentTest.php @@ -31,7 +31,7 @@ protected function setUp(): void parent::setUp(); // set a baseline user agent - $_SERVER['HTTP_USER_AGENT'] = $this->_user_agent; + service('superglobals')->setServer('HTTP_USER_AGENT', $this->_user_agent); $this->agent = new UserAgent(); } @@ -39,9 +39,8 @@ protected function setUp(): void public function testMobile(): void { // Mobile Not Set - $_SERVER['HTTP_USER_AGENT'] = $this->_mobile_ua; + service('superglobals')->setServer('HTTP_USER_AGENT', $this->_mobile_ua); $this->assertFalse($this->agent->isMobile()); - unset($_SERVER['HTTP_USER_AGENT']); } public function testIsFunctions(): void @@ -55,12 +54,13 @@ public function testIsFunctions(): void public function testReferrer(): void { - $_SERVER['HTTP_REFERER'] = 'http://codeigniter.com/user_guide/'; + service('superglobals')->setServer('HTTP_REFERER', 'http://codeigniter.com/user_guide/'); + $this->assertTrue($this->agent->isReferral()); $this->assertSame('http://codeigniter.com/user_guide/', $this->agent->getReferrer()); $this->setPrivateProperty($this->agent, 'referrer', null); - unset($_SERVER['HTTP_REFERER']); + service('superglobals')->setServer('HTTP_REFERER', ''); $this->assertFalse($this->agent->isReferral()); $this->assertSame('', $this->agent->getReferrer()); } @@ -108,7 +108,7 @@ public function testParseBot(): void public function testEmptyUserAgentVariable(): void { - unset($_SERVER['HTTP_USER_AGENT']); + service('superglobals')->setServer('HTTP_USER_AGENT', ''); $agent = new UserAgent(); $this->assertEmpty((string) $agent); } diff --git a/utils/phpstan-baseline/codeigniter.superglobalAccess.neon b/utils/phpstan-baseline/codeigniter.superglobalAccess.neon index 05153d87fb35..00869afc1f4e 100644 --- a/utils/phpstan-baseline/codeigniter.superglobalAccess.neon +++ b/utils/phpstan-baseline/codeigniter.superglobalAccess.neon @@ -1,4 +1,4 @@ -# total 77 errors +# total 68 errors parameters: ignoreErrors: @@ -117,16 +117,6 @@ parameters: count: 2 path: ../../system/HTTP/Response.php - - - message: '#^Accessing offset ''HTTP_REFERER'' directly on \$_SERVER is discouraged\.$#' - count: 4 - path: ../../system/HTTP/UserAgent.php - - - - message: '#^Accessing offset ''HTTP_USER_AGENT'' directly on \$_SERVER is discouraged\.$#' - count: 2 - path: ../../system/HTTP/UserAgent.php - - message: '#^Accessing offset mixed directly on \$_GET is discouraged\.$#' count: 1 @@ -212,16 +202,6 @@ parameters: count: 1 path: ../../tests/system/HTTP/SiteURIFactoryDetectRoutePathTest.php - - - message: '#^Accessing offset ''HTTP_REFERER'' directly on \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/UserAgentTest.php - - - - message: '#^Accessing offset ''HTTP_USER_AGENT'' directly on \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/UserAgentTest.php - - message: '#^Accessing offset ''HTTP_HOST'' directly on \$_SERVER is discouraged\.$#' count: 1 diff --git a/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon b/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon index 4a3113467afe..6a5f6dccbf23 100644 --- a/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon +++ b/utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon @@ -1,4 +1,4 @@ -# total 463 errors +# total 459 errors parameters: ignoreErrors: @@ -337,11 +337,6 @@ parameters: count: 2 path: ../../tests/system/HTTP/IncomingRequestTest.php - - - message: '#^Assigning ''Mozilla'' directly on offset ''HTTP_USER_AGENT'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/IncomingRequestTest.php - - message: '#^Assigning ''fr\-FR; q\=1\.0, en; q\=0\.5'' directly on offset ''HTTP_ACCEPT_LANGUAGE'' of \$_SERVER is discouraged\.$#' count: 1 @@ -662,16 +657,6 @@ parameters: count: 3 path: ../../tests/system/HTTP/URITest.php - - - message: '#^Assigning ''http\://codeigniter\.com/user_guide/'' directly on offset ''HTTP_REFERER'' of \$_SERVER is discouraged\.$#' - count: 1 - path: ../../tests/system/HTTP/UserAgentTest.php - - - - message: '#^Assigning string directly on offset ''HTTP_USER_AGENT'' of \$_SERVER is discouraged\.$#' - count: 2 - path: ../../tests/system/HTTP/UserAgentTest.php - - message: '#^Assigning ''/'' directly on offset ''REQUEST_URI'' of \$_SERVER is discouraged\.$#' count: 1 diff --git a/utils/phpstan-baseline/empty.notAllowed.neon b/utils/phpstan-baseline/empty.notAllowed.neon index 9bdf7e5f5720..faf902d5ec10 100644 --- a/utils/phpstan-baseline/empty.notAllowed.neon +++ b/utils/phpstan-baseline/empty.notAllowed.neon @@ -1,4 +1,4 @@ -# total 239 errors +# total 237 errors parameters: ignoreErrors: @@ -242,11 +242,6 @@ parameters: count: 2 path: ../../system/HTTP/Response.php - - - message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#' - count: 2 - path: ../../system/HTTP/UserAgent.php - - message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#' count: 1 diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index e155430838ce..b37223d367be 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 2788 errors +# total 2773 errors includes: - argument.type.neon