Skip to content

Commit

Permalink
Merge pull request #86 from kocsismate:patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Aug 6, 2022
2 parents bb0453d + 801e198 commit a35900b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
test:
./vendor/bin/phpunit --coverage-text

.PHONY: clean
clean:
-rm tests/user_agents.json
touch tests/user_agents.json

.PHONY: generate
generate:
php bin/user_agent_sorter.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.dist.json
Expand Down
13 changes: 9 additions & 4 deletions src/UserAgentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ function parse_user_agent( $u_agent = null ) {

if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) {
preg_match_all(<<<'REGEX'
/(?P<platform>BB\d+;|Android|Adr|Symbian|Sailfish|CrOS|Tizen|iPhone|iPad|iPod|Linux|(Open|Net|Free)BSD|Macintosh|Windows(\ Phone)?|Silk|linux-gnu|BlackBerry|PlayBook|X11|(New\ )?Nintendo\ (WiiU?|3?DS|Switch)|Xbox(\ One)?)
/(?P<platform>BB\d+;|Android|Adr|Symbian|Sailfish|CrOS|Tizen|iPhone|iPad|iPod|Linux|(?:Open|Net|Free)BSD|Macintosh|
Windows(?:\ Phone)?|Silk|linux-gnu|BlackBerry|PlayBook|X11|(?:New\ )?Nintendo\ (?:WiiU?|3?DS|Switch)|Xbox(?:\ One)?)
(?:\ [^;]*)?
(?:;|$)/imx
REGEX
Expand All @@ -88,6 +89,10 @@ function parse_user_agent( $u_agent = null ) {
$platform = 'Chrome OS';
} elseif( $platform == 'Adr' ) {
$platform = 'Android';
} elseif( $platform === null ) {
if(preg_match_all('%(?P<platform>Android)[:/ ]%ix', $u_agent, $result)) {
$platform = $result[PLATFORM][0];
}
}

preg_match_all(<<<'REGEX'
Expand All @@ -96,14 +101,14 @@ function parse_user_agent( $u_agent = null ) {
OculusBrowser|SamsungBrowser|SailfishBrowser|XiaoMi/MiuiBrowser|
Baiduspider|Applebot|Facebot|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl|
Valve\ Steam\ Tenfoot|
NintendoBrowser|PLAYSTATION\ (\d|Vita)+)
NintendoBrowser|PLAYSTATION\ (?:\d|Vita)+)
\)?;?
(?:[:/ ](?P<version>[0-9A-Z.]+)|/[A-Z]*)%ix
REGEX
, $u_agent, $result);

// If nothing matched, return null (to avoid undefined index errors)
if( !isset($result[BROWSER][0]) || !isset($result[BROWSER_VERSION][0]) ) {
if( !isset($result[BROWSER][0], $result[BROWSER_VERSION][0]) ) {
if( preg_match('%^(?!Mozilla)(?P<browser>[A-Z0-9\-]+)(/(?P<version>[0-9A-Z.]+))?%ix', $u_agent, $result) ) {
return [ PLATFORM => $platform ?: null, BROWSER => $result[BROWSER], BROWSER_VERSION => empty($result[BROWSER_VERSION]) ? null : $result[BROWSER_VERSION] ];
}
Expand Down Expand Up @@ -190,7 +195,7 @@ function parse_user_agent( $u_agent = null ) {
} elseif( $browser == 'AppleWebKit' ) {
if( $platform == 'Android' ) {
$browser = 'Android Browser';
} elseif( strpos($platform, 'BB') === 0 ) {
} elseif( strpos((string)$platform, 'BB') === 0 ) {
$browser = 'BlackBerry Browser';
$platform = 'BlackBerry';
} elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) {
Expand Down
5 changes: 5 additions & 0 deletions tests/user_agents.dist.json
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,11 @@
"browser": "Android Browser",
"version": "4.0"
},
"Lenovo-A880\/S100 Linux\/3.4.5 Android\/4.2 Release\/08.07.2013 Browser\/AppleWebKit 534.30 Profile\/ Configuration;": {
"platform": "Android",
"browser": "Android Browser",
"version": "534.30"
},
"Mozilla\/5.0 (Linux; Android 4.2.2; de-at; SAMSUNG GT-I9195\/I9195XXUAMF6 Build\/JDQ39) AppleWebKit\/535.19 (KHTML, like Gecko) Version\/1.0 Chrome\/18.0.1025.308 Mobile Safari\/535.19": {
"platform": "Android",
"browser": "Chrome",
Expand Down

0 comments on commit a35900b

Please sign in to comment.