Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions system/HTTP/UserAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions tests/system/HTTP/UserAgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ 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();
}

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
Expand All @@ -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());
}
Expand Down Expand Up @@ -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);
}
Expand Down
22 changes: 1 addition & 21 deletions utils/phpstan-baseline/codeigniter.superglobalAccess.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 77 errors
# total 68 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 1 addition & 16 deletions utils/phpstan-baseline/codeigniter.superglobalAccessAssign.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 463 errors
# total 459 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions utils/phpstan-baseline/empty.notAllowed.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 239 errors
# total 237 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 2788 errors
# total 2773 errors

includes:
- argument.type.neon
Expand Down
Loading