Skip to content

Commit e063a4a

Browse files
authored
[3.x] Fix browser and engine detection of web client (#132)
1 parent 04f08d8 commit e063a4a

File tree

2 files changed

+75
-14
lines changed

2 files changed

+75
-14
lines changed

Tests/Web/WebClientTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public static function getUserAgentData()
3030
{
3131
// Platform, Mobile, Engine, Browser, Version, User Agent
3232
return [
33+
[
34+
'',
35+
false,
36+
'',
37+
'',
38+
'',
39+
null,
40+
],
3341
[
3442
WebClient::WINDOWS,
3543
false,
@@ -150,6 +158,14 @@ public static function getUserAgentData()
150158
'54.0.2840.71',
151159
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
152160
],
161+
[
162+
WebClient::WINDOWS,
163+
false,
164+
WebClient::BLINK,
165+
WebClient::CHROME,
166+
'131.0.0.0',
167+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
168+
],
153169
[
154170
WebClient::LINUX,
155171
false,
@@ -350,6 +366,38 @@ public static function getUserAgentData()
350366
'75.0.107.0',
351367
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3738.0 Safari/537.36 Edg/75.0.107.0',
352368
],
369+
[
370+
'',
371+
false,
372+
WebClient::BLINK,
373+
WebClient::CHROME,
374+
'',
375+
'Chrome Privacy Preserving Prefetch Proxy',
376+
],
377+
[
378+
'',
379+
false,
380+
WebClient::BLINK,
381+
WebClient::CHROME,
382+
'',
383+
'Chrome',
384+
],
385+
[
386+
'',
387+
false,
388+
WebClient::WEBKIT,
389+
'',
390+
'',
391+
'AppleWebKit',
392+
],
393+
[
394+
'',
395+
false,
396+
WebClient::BLINK,
397+
'',
398+
'',
399+
'AppleWebKit/537.36',
400+
],
353401
];
354402
}
355403

src/Web/WebClient.php

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ protected function detectBrowser($userAgent)
287287
return;
288288
}
289289

290+
// Check for Google's Private Prefetch Proxy
291+
if ($userAgent === 'Chrome Privacy Preserving Prefetch Proxy') {
292+
// Private Prefetch Proxy does not provide any further details like e.g. version
293+
$this->browser = self::CHROME;
294+
295+
return;
296+
}
297+
290298
$patternBrowser = '';
291299

292300
// Attempt to detect the browser type. Obviously we are only worried about major browsers.
@@ -394,27 +402,32 @@ protected function detectEngine($userAgent)
394402
} elseif (\stripos($userAgent, 'Edg') !== false) {
395403
$this->engine = self::BLINK;
396404
} elseif (\stripos($userAgent, 'Chrome') !== false) {
397-
$result = \explode('/', \stristr($userAgent, 'Chrome'));
398-
$version = \explode(' ', $result[1]);
405+
$this->engine = self::BLINK;
399406

400-
if ($version[0] >= 28) {
401-
$this->engine = self::BLINK;
402-
} else {
403-
$this->engine = self::WEBKIT;
407+
$result = \explode('/', \stristr($userAgent, 'Chrome'));
408+
409+
if (isset($result[1])) {
410+
$version = \explode(' ', $result[1]);
411+
412+
if (version_compare($version[0], '28.0', 'lt')) {
413+
$this->engine = self::WEBKIT;
414+
}
404415
}
405416
} elseif (\stripos($userAgent, 'AppleWebKit') !== false || \stripos($userAgent, 'blackberry') !== false) {
417+
$this->engine = self::WEBKIT;
418+
406419
if (\stripos($userAgent, 'AppleWebKit') !== false) {
407-
$result = \explode('/', \stristr($userAgent, 'AppleWebKit'));
408-
$version = \explode(' ', $result[1]);
420+
$result = \explode('/', \stristr($userAgent, 'AppleWebKit'));
409421

410-
if ($version[0] === 537.36) {
411-
// AppleWebKit/537.36 is Blink engine specific, exception is Blink emulated IEMobile, Trident or Edge
412-
$this->engine = self::BLINK;
422+
if (isset($result[1])) {
423+
$version = \explode(' ', $result[1]);
424+
425+
if ($version[0] === '537.36') {
426+
// AppleWebKit/537.36 is Blink engine specific, exception is Blink emulated IEMobile, Trident or Edge
427+
$this->engine = self::BLINK;
428+
}
413429
}
414430
}
415-
416-
// Evidently blackberry uses WebKit and doesn't necessarily report it. Bad RIM.
417-
$this->engine = self::WEBKIT;
418431
} elseif (\stripos($userAgent, 'Gecko') !== false && \stripos($userAgent, 'like Gecko') === false) {
419432
// We have to check for like Gecko because some other browsers spoof Gecko.
420433
$this->engine = self::GECKO;

0 commit comments

Comments
 (0)