Skip to content

Commit a4128ed

Browse files
committed
Fixed issue in agentzero::__construct() where the returned type for version values could be an int, whereas they are stored as strings, they are now converted.
Fixed bug in `versions::get()` where if the specified version was out of range of the earliest version we have data for, and the major version was the same, it would incorrect return that it was the latest version. It now checks the string against the earliest version to see if it lower, and if so marks it as `legacy`. Updated tests.
1 parent bca8e03 commit a4128ed

4 files changed

Lines changed: 37 additions & 19 deletions

File tree

src/agentzero.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ private function __construct(string $ua, \stdClass $data) {
8989
// platform
9090
$this->kernel = $data->kernel ?? null;
9191
$this->platform = $data->platform ?? null;
92-
$this->platformversion = $data->platformversion ?? null;
92+
$this->platformversion = !empty($data->platformversion) ? \strval($data->platformversion) : null;
9393

9494
// browser
9595
$this->engine = $data->engine ?? null;
96-
$this->engineversion = $data->engineversion ?? null;
96+
$this->engineversion = !empty($data->engineversion) ? \strval($data->engineversion) : null;
9797
$this->browser = $data->browser ?? null;
98-
$this->browserversion = $data->browserversion ?? null;
98+
$this->browserversion = !empty($data->browserversion) ? \strval($data->browserversion) : null;
9999
$this->browserstatus = $data->browserstatus ?? null;
100100
$this->browserreleased = !empty($data->browserreleased) ? $data->browserreleased : null;
101101
$this->browserlatest = $data->browserlatest ?? null;
@@ -104,9 +104,9 @@ private function __construct(string $ua, \stdClass $data) {
104104
// app
105105
$this->app = $data->app ?? null;
106106
$this->appname = $data->appname ?? null;
107-
$this->appversion = $data->appversion ?? null;
107+
$this->appversion = !empty($data->appversion) ? \strval($data->appversion) : null;
108108
$this->framework = $data->framework ?? null;
109-
$this->frameworkversion = $data->frameworkversion ?? null;
109+
$this->frameworkversion = !empty($data->frameworkversion) ? \strval($data->frameworkversion) : null;
110110
$this->url = $data->url ?? null;
111111

112112
// network
@@ -251,7 +251,7 @@ public static function parse(string $ua, array $hints = [], array $config = [])
251251
// lowercase the tokens
252252
$tokenslower = [];
253253
foreach ($tokens AS $key => $item) {
254-
$tokenslower[$key] = $item;
254+
$tokenslower[$key] = \mb_strtolower($item);
255255
}
256256

257257
// extract UA info

src/helpers/versions.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ protected static function released(array $data, string $version) : ?string {
9797
return !empty($released) ? (new \DateTime(\strval($released)))->format('Y-m-d') : null;
9898
}
9999

100+
protected static function legacy(array $browsers, string $version) : bool {
101+
$earliest = \strval(\array_key_last($browsers));
102+
103+
// check if we have a version in range
104+
$parts = \explode('.', $version);
105+
foreach (\explode('.', $earliest) AS $key => $item) {
106+
if (isset($parts[$key])) {
107+
108+
// older than earlier version we have a date for
109+
if ($item > $parts[$key]) {
110+
return true;
111+
112+
// newer than the earlier version we have a date for
113+
} elseif ($item < $parts[$key]) {
114+
return false;
115+
}
116+
}
117+
}
118+
return false;
119+
}
120+
100121
public static function get(string $browser, string $version, array $config) : array {
101122
$source = $config['versionssource'];
102123
$cache = $config['versionscache'];
@@ -110,10 +131,13 @@ public static function get(string $browser, string $version, array $config) : ar
110131
// check if version is greater than latest version
111132
$major = \intval($version);
112133
$latest = \intval($data['browserlatest']);
113-
$first = \intval(\array_key_last($versions[$browser]));
134+
135+
// check if we have a version in range
136+
if (self::legacy($versions[$browser], $version)) {
137+
$data['browserstatus'] = 'legacy';
114138

115139
// version is way out of bounds (This happens sometimes, for example if the safari engine version is reported instead of the browser version)
116-
if ($latest + 3 < $major) {
140+
} elseif ($latest + 3 < $major) {
117141
return [];
118142

119143
// nightly build?
@@ -128,10 +152,6 @@ public static function get(string $browser, string $version, array $config) : ar
128152
} elseif ($latest + 1 === $major) {
129153
$data['browserstatus'] = 'beta';
130154

131-
// so old we don't have data for it
132-
} elseif ($major < $first) {
133-
$data['browserstatus'] = 'legacy';
134-
135155
// find closes match for version
136156
} else {
137157

tests/browsersTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,7 @@ public function testBrave() : void {
499499
'engine' => 'WebKit',
500500
'engineversion' => '601.1.46',
501501
'browser' => 'Brave',
502-
'browserversion' => '1.2.11',
503-
'browserreleased' => '2026-04-08'
502+
'browserversion' => '1.2.11'
504503
],
505504
'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Brave/115.0.0.0 Safari/605.1.15' => [
506505
'string' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_4_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Brave/115.0.0.0 Safari/605.1.15',
@@ -660,8 +659,7 @@ public function testKonqueror() : void {
660659
'platform' => 'Kubuntu',
661660
'browser' => 'Konqueror',
662661
'browserversion' => '3.5',
663-
'language' => 'en-US',
664-
'browserreleased' => '2007-12-04'
662+
'language' => 'en-US'
665663
],
666664
'Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20021102)' => [
667665
'string' => 'Mozilla/5.0 (compatible; Konqueror/3.1; i686 Linux; 20021102)',
@@ -672,8 +670,7 @@ public function testKonqueror() : void {
672670
'architecture' => 'x86',
673671
'bits' => 32,
674672
'browser' => 'Konqueror',
675-
'browserversion' => '3.1',
676-
'browserreleased' => '2007-12-04'
673+
'browserversion' => '3.1'
677674
]
678675
];
679676
foreach ($strings AS $ua => $item) {
@@ -1573,6 +1570,7 @@ public function testSilk() : void {
15731570
'bits' => 64,
15741571
'browser' => 'Silk',
15751572
'browserversion' => '148.0.5259.39',
1573+
'browserreleased' => '2026-04-23',
15761574
'engine' => 'Blink',
15771575
'engineversion' => '148.0.5259.39'
15781576
]

tests/clientHintsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testClientHints() : void {
2424
'bits' => 64,
2525
'kernel' => 'Windows NT',
2626
'platform' => 'Windows',
27-
'platformversion' => '19.0.0',
27+
'platformversion' => '11',
2828
'engine' => 'Blink',
2929
'engineversion' => '133.0.6943.142',
3030
'browser' => 'Chrome',

0 commit comments

Comments
 (0)