Skip to content

Commit 7661279

Browse files
committed
Add support for browser engine (like gecko, webkit, trident or presto)
1 parent b180517 commit 7661279

File tree

4 files changed

+107
-28
lines changed

4 files changed

+107
-28
lines changed

lib/phpUserAgent.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class phpUserAgent
2020
protected $browserName;
2121
protected $browserVersion;
2222
protected $operatingSystem;
23+
protected $engine;
2324

2425
public function __construct($userAgentString = null, phpUserAgentStringParser $userAgentStringParser = null)
2526
{
@@ -86,6 +87,26 @@ public function setOperatingSystem($operatingSystem)
8687
$this->operatingSystem = $operatingSystem;
8788
}
8889

90+
/**
91+
* Get the engine name
92+
*
93+
* @return string the engine name
94+
*/
95+
public function getEngine()
96+
{
97+
return $this->engine;
98+
}
99+
100+
/**
101+
* Set the engine name
102+
*
103+
* @param string $operatingSystem the engine name
104+
*/
105+
public function setEngine($engine)
106+
{
107+
$this->engine = $engine;
108+
}
109+
89110
/**
90111
* Get the user agent string
91112
*
@@ -170,5 +191,6 @@ public function fromArray(array $data)
170191
$this->setBrowserName($data['browser_name']);
171192
$this->setBrowserVersion($data['browser_version']);
172193
$this->setOperatingSystem($data['operating_system']);
194+
$this->setEngine($data['engine']);
173195
}
174-
}
196+
}

lib/phpUserAgentStringParser.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ protected function doParse($userAgentString)
4848
'string' => $this->cleanUserAgentString($userAgentString),
4949
'browser_name' => null,
5050
'browser_version' => null,
51-
'operating_system' => null
51+
'operating_system' => null,
52+
'engine' => null
5253
);
5354

5455
if(empty($userAgent['string']))
@@ -90,6 +91,17 @@ protected function doParse($userAgentString)
9091
}
9192
}
9293

94+
// Find engine
95+
$pattern = '#'.join('|', $this->getKnownEngines()).'#';
96+
97+
if (preg_match($pattern, $userAgent['string'], $match))
98+
{
99+
if (isset($match[0]))
100+
{
101+
$userAgent['engine'] = $match[0];
102+
}
103+
}
104+
93105
return $userAgent;
94106
}
95107

@@ -110,6 +122,9 @@ public function cleanUserAgentString($userAgentString)
110122
// replace operating system names with their aliases
111123
$userAgentString = strtr($userAgentString, $this->getKnownOperatingSystemAliases());
112124

125+
// replace engine names with their aliases
126+
$userAgentString = strtr($userAgentString, $this->getKnownEngineAliases());
127+
113128
return $userAgentString;
114129
}
115130

@@ -124,7 +139,8 @@ public function getFilters()
124139
'filterGoogleChrome',
125140
'filterSafariVersion',
126141
'filterOperaVersion',
127-
'filterYahoo'
142+
'filterYahoo',
143+
'filterMsie'
128144
);
129145
}
130146

@@ -205,6 +221,31 @@ protected function getKnownOperatingSystemAliases()
205221
return array();
206222
}
207223

224+
/**
225+
* Get known engines
226+
*
227+
* @return array the engines
228+
*/
229+
protected function getKnownEngines()
230+
{
231+
return array(
232+
'gecko',
233+
'webkit',
234+
'trident',
235+
'presto'
236+
);
237+
}
238+
239+
/**
240+
* Get known engines aliases
241+
*
242+
* @return array the engines aliases
243+
*/
244+
protected function getKnownEngineAliases()
245+
{
246+
return array();
247+
}
248+
208249
/**
209250
* Filters
210251
*/
@@ -253,4 +294,15 @@ protected function filterYahoo(array &$userAgent)
253294
$userAgent['browser_name'] = 'yahoobot';
254295
}
255296
}
256-
}
297+
298+
/**
299+
* MSIE does not always declare its engine
300+
*/
301+
protected function filterMsie(array &$userAgent)
302+
{
303+
if ('msie' === $userAgent['browser_name'] && empty($userAgent['engine']))
304+
{
305+
$userAgent['engine'] = 'trident';
306+
}
307+
}
308+
}

test/StringParserTest.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,79 @@
66
$tests = array(
77
// Namoroka Ubuntu
88
'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2pre) Gecko/20100116 Ubuntu/9.10 (karmic) Namoroka/3.6pre'
9-
=> array('firefox', '3.6', 'linux'),
9+
=> array('firefox', '3.6', 'linux', 'gecko'),
1010

1111
// Namoroka Mac
1212
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2) Gecko/20100105 Firefox/3.6'
13-
=> array('firefox', '3.6', 'macintosh'),
13+
=> array('firefox', '3.6', 'macintosh', 'gecko'),
1414

1515
// Chrome Mac
1616
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; fr-fr) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10'
17-
=> array('chrome', '4.0', 'macintosh'),
17+
=> array('chrome', '4.0', 'macintosh', 'webkit'),
1818

1919
//Safari Mac
2020
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; fr-fr) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10'
21-
=> array('safari', '4.0', 'macintosh'),
21+
=> array('safari', '4.0', 'macintosh', 'webkit'),
2222

2323
// Opera 9 Windows
2424
'Opera/9.61 (Windows NT 6.0; U; en) Presto/2.1.1'
25-
=> array('opera', '9.61', 'windows'),
25+
=> array('opera', '9.61', 'windows', 'presto'),
2626

2727
// Opera 10 Windows
2828
'Opera/9.80 (Windows NT 5.1; U; en) Presto/2.2.15 Version/10.10'
29-
=> array('opera', '10.10', 'windows'),
29+
=> array('opera', '10.10', 'windows', 'presto'),
3030

3131
// Firefox Linux
3232
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.17) Gecko/2010010604 Linux Mint/7 (Gloria) Firefox/3.0.17'
33-
=> array('firefox', '3.0', 'linux'),
33+
=> array('firefox', '3.0', 'linux', 'gecko'),
3434

3535
// Firefox Windows
3636
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6 (.NET CLR 3.5.30729)'
37-
=> array('firefox', '3.5', 'windows'),
37+
=> array('firefox', '3.5', 'windows', 'gecko'),
3838

3939
// Firefox OSX
4040
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8'
41-
=> array('firefox', '3.5', 'macintosh'),
41+
=> array('firefox', '3.5', 'macintosh', 'gecko'),
4242

4343
// Chrome Linux
4444
'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.0.249.43 Safari/532.5'
45-
=> array('chrome', '4.0', 'linux'),
45+
=> array('chrome', '4.0', 'linux', 'webkit'),
4646

4747
// Speedy Spider
4848
'Speedy Spider (http://www.entireweb.com/about/search_tech/speedy_spider/)'
49-
=> array(null, null, null),
49+
=> array(null, null, null, null),
5050

5151
// Minefield Mac
5252
'Gecko 20100113Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.3a1pre) Gecko/20100113 Minefield/3.7a1pre'
53-
=> array('firefox', '3.7', 'macintosh'),
53+
=> array('firefox', '3.7', 'macintosh', 'gecko'),
5454

5555
// IE7 Windows
5656
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; GTB6; SLCC1; .NET CLR 2.0.50727; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 3.5.30729; InfoPath.2; .NET CLR 3.0.30729; MSOffice 12)'
57-
=> array('msie', '7.0', 'windows'),
57+
=> array('msie', '7.0', 'windows', 'trident'),
5858

5959
// IE6 Windows
6060
'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; DigExt)'
61-
=> array('msie', '6.0', 'windows'),
61+
=> array('msie', '6.0', 'windows', 'trident'),
6262

6363
// Feedfetcher Google
6464
'Feedfetcher-Google; (+http://www.google.com/feedfetcher.html; 2 subscribers; feed-id=6924676383167400434)'
65-
=> array(null, null, null),
65+
=> array(null, null, null, null),
6666

6767
// Google Bot
6868
'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
69-
=> array('googlebot', '2.1', null),
69+
=> array('googlebot', '2.1', null, null),
7070

7171
// MSN Bot
7272
'msnbot/2.0b (+http://search.msn.com/msnbot.htm)'
73-
=> array('msnbot', '2.0', null),
73+
=> array('msnbot', '2.0', null, null),
7474

7575
// Yahoo Bot
7676
'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)'
77-
=> array('yahoobot', null, null),
77+
=> array('yahoobot', null, null, null),
7878

7979
// Iphone
8080
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1_2 like Mac OS X; de-de) AppleWebKit/528.18 (KHTML, like Gecko) Mobile/7D11'
81-
=> array('applewebkit', '528.18', 'iphone')
81+
=> array('applewebkit', '528.18', 'iphone', 'webkit')
8282
);
8383

8484
$t = new lime_test(count($tests));
@@ -91,10 +91,11 @@
9191
'string' => $parser->cleanUserAgentString($userAgentString),
9292
'browser_name' => $userAgentArray[0],
9393
'browser_version' => $userAgentArray[1],
94-
'operating_system' => $userAgentArray[2]
94+
'operating_system' => $userAgentArray[2],
95+
'engine' => $userAgentArray[3]
9596
);
9697

9798
$result = $parser->parse($userAgentString);
9899

99-
$t->is_deeply($parser->parse($userAgentString), $expected, $userAgentString.' -> '.implode(', ', $result));
100-
}
100+
$t->is_deeply($result, $expected, $userAgentString.' -> '.implode(', ', $result));
101+
}

test/UserAgentTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require_once dirname(__FILE__).'/vendor/lime.php';
44
require_once dirname(__FILE__).'/../lib/phpUserAgent.php';
55

6-
$t = new lime_test(8);
6+
$t = new lime_test(10);
77

88
$userAgentString = 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2pre) Gecko/20100116 Ubuntu/9.10 (karmic) Namoroka/3.6pre';
99
$userAgent = new phpUserAgent($userAgentString);
@@ -14,6 +14,8 @@
1414

1515
$t->is($userAgent->getOperatingSystem(), 'linux', '$userAgent->getOperatingSystem() works');
1616

17+
$t->is($userAgent->getEngine(), 'gecko', '$userAgent->getEngine() works');
18+
1719
$t->is($userAgent->isUnknown(), false, 'User agent is not unknown');
1820

1921
$userAgent = new phpUserAgent('hmm...');
@@ -24,4 +26,6 @@
2426

2527
$t->is($userAgent->getOperatingSystem(), null, '$userAgent->getOperatingSystem() works');
2628

27-
$t->is($userAgent->isUnknown(), true, 'User agent is unknown');
29+
$t->is($userAgent->getEngine(), null, '$userAgent->getEngine() works');
30+
31+
$t->is($userAgent->isUnknown(), true, 'User agent is unknown');

0 commit comments

Comments
 (0)