Skip to content

Commit 6e90509

Browse files
authored
Merge pull request #1 from ornicar/master
Latest updates from Ornicar
2 parents 1381e16 + 91f648f commit 6e90509

File tree

9 files changed

+121
-36
lines changed

9 files changed

+121
-36
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
nbproject
2+
3+
/.idea

CHANGELOG

Lines changed: 0 additions & 3 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# CHANGELOG
2+
3+
### 1.0.1 (????-??-??)
4+
5+
* f1b82e5 - [Composer] Add branch alias support
6+
7+
### 1.0.0 (2013-08-09)
8+
9+
* Add a version following semver spec.
10+
11+
### v1.0 (2010-04-13)
12+
13+
* Create initial 1.0 version

README.markdown renamed to README.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ Uses a simple and fast algorithm to recognize major browsers.
55

66
## Overview
77

8-
$userAgent = new phpUserAgent();
8+
``` php
9+
$userAgent = new phpUserAgent();
910

10-
$userAgent->getBrowserName() // firefox
11-
$userAgent->getBrowserVersion() // 3.6
12-
$userAgent->getOperatingSystem() // linux
13-
$userAgent->getEngine() // gecko
11+
$userAgent->getBrowserName() // firefox
12+
$userAgent->getBrowserVersion() // 3.6
13+
$userAgent->getOperatingSystem() // linux
14+
$userAgent->getEngine() // gecko
15+
```
1416

1517
### Why you should use it
1618

@@ -26,17 +28,20 @@ It is performant as well, since it doesn't do any iteration or recursion.
2628

2729
## Usage
2830

29-
// include the class
30-
require_once '/path/to/php-user-agent/phpUserAgent.php';
31+
``` php
32+
// include classes or rely on Composer autoloader
33+
require_once '/path/to/php-user-agent/phpUserAgent.php';
34+
require_once '/path/to/php-user-agent/phpUserAgentStringParser.php';
3135

32-
// Create a user agent
33-
$userAgent = new phpUserAgent();
36+
// Create a user agent
37+
$userAgent = new phpUserAgent();
3438

35-
// Interrogate the user agent
36-
$userAgent->getBrowserName() // firefox
37-
$userAgent->getBrowserVersion() // 3.6
38-
$userAgent->getOperatingSystem() // linux
39-
$userAgent->getEngine() // gecko
39+
// Interrogate the user agent
40+
$userAgent->getBrowserName() // firefox
41+
$userAgent->getBrowserVersion() // 3.6
42+
$userAgent->getOperatingSystem() // linux
43+
$userAgent->getEngine() // gecko
44+
```
4045

4146
## Advanced
4247

@@ -45,34 +50,40 @@ It is performant as well, since it doesn't do any iteration or recursion.
4550
When you create a phpUserAgent object, the current user agent string is used.
4651
You can specify another user agent string:
4752

48-
// use another user agent string
49-
$userAgent = new phpUserAgent('msnbot/2.0b (+http://search.msn.com/msnbot.htm)');
50-
$userAgent->getBrowserName() // msnbot
53+
``` php
54+
// use another user agent string
55+
$userAgent = new phpUserAgent('msnbot/2.0b (+http://search.msn.com/msnbot.htm)');
56+
$userAgent->getBrowserName() // msnbot
5157

52-
// use current user agent string
53-
$userAgent = new phpUserAgent($_SERVER['HTTP_USER_AGENT');
54-
// this is equivalent to:
55-
$userAgent = new phpUserAgent();
58+
// use current user agent string
59+
$userAgent = new phpUserAgent($_SERVER['HTTP_USER_AGENT');
60+
// this is equivalent to:
61+
$userAgent = new phpUserAgent();
62+
```
5663

5764
### Custom parser class
5865

5966
By default, phpUserAgentStringParser is used to analyse the user agent string.
6067
You can replace the parser instance and customize it to match your needs:
6168

62-
// create a custom user agent string parser
63-
class myUserAgentStringParser extends phpUserAgentStringParser
64-
{
65-
// override methods
66-
}
69+
``` php
70+
// create a custom user agent string parser
71+
class myUserAgentStringParser extends phpUserAgentStringParser
72+
{
73+
// override methods
74+
}
6775

68-
// inject the custom parser when creating a user agent:
69-
$userAgent = new phpUserAgent(null, new myUserAgentStringParser());
76+
// inject the custom parser when creating a user agent:
77+
$userAgent = new phpUserAgent(null, new myUserAgentStringParser());
78+
```
7079

7180
## Run tests
7281

7382
You can run the unit tests on your server:
7483

75-
php prove.php
84+
``` bash
85+
$ php prove.php
86+
```
7687

7788
## Contribute
7889

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "ornicar/php-user-agent",
3+
"type": "library",
4+
"keywords": [ "user-agent" ],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Thibault Duplessis",
9+
"email": "thibault.duplessis@gmail.com"
10+
}
11+
],
12+
"require": {
13+
"php": ">=5.3.0"
14+
},
15+
"autoload": {
16+
"files": [
17+
"lib/phpUserAgent.php",
18+
"lib/phpUserAgentStringParser.php"
19+
]
20+
},
21+
"extra": {
22+
"branch-alias": {
23+
"dev-master": "1.0-dev"
24+
}
25+
}
26+
}

lib/phpUserAgent.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
* Tickets: http://github.com/ornicar/php-user-agent/issues
1313
*/
1414

15-
require_once(dirname(__FILE__).'/phpUserAgentStringParser.php');
16-
1715
class phpUserAgent
1816
{
1917
protected $userAgentString;

lib/phpUserAgentStringParser.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function parse($userAgentString = null)
2727
// parse quickly (with medium accuracy)
2828
$informations = $this->doParse($userAgentString);
2929

30+
3031
// run some filters to increase accuracy
3132
foreach($this->getFilters() as $filter)
3233
{
@@ -136,11 +137,12 @@ public function cleanUserAgentString($userAgentString)
136137
public function getFilters()
137138
{
138139
return array(
140+
'filterAndroid',
139141
'filterGoogleChrome',
140142
'filterSafariVersion',
141143
'filterOperaVersion',
142144
'filterYahoo',
143-
'filterMsie'
145+
'filterMsie',
144146
);
145147
}
146148

@@ -305,4 +307,15 @@ protected function filterMsie(array &$userAgent)
305307
$userAgent['engine'] = 'trident';
306308
}
307309
}
310+
311+
/**
312+
* Android has a safari like signature
313+
*/
314+
protected function filterAndroid(array &$userAgent) {
315+
if ('safari' === $userAgent['browser_name'] && strpos($userAgent['string'], 'android ')) {
316+
$userAgent['browser_name'] = 'android';
317+
$userAgent['operating_system'] = 'android';
318+
$userAgent['browser_version'] = preg_replace('|.+android ([0-9]+(?:\.[0-9]+)+).+|', '$1', $userAgent['string']);
319+
}
320+
}
308321
}

test/StringParserTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,31 @@
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', 'webkit')
81+
=> array('applewebkit', '528.18', 'iphone', 'webkit'),
82+
83+
// Motorola Xoom
84+
'Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13' =>
85+
array('android', '3.0', 'android', 'webkit'),
86+
87+
// Samsung Galaxy Tab
88+
'Mozilla/5.0 (Linux U Android 2.2 es-es GT-P1000 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' =>
89+
array('android', '2.2', 'android', 'webkit'),
90+
91+
// Google Nexus
92+
'Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' =>
93+
array('android', '2.2', 'android', 'webkit'),
94+
95+
// HTC Desire
96+
97+
'Mozilla/5.0 (Linux; U; Android 2.1-update1; de-de; HTC Desire 1.19.161.5 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17' =>
98+
array('android', '2.1', 'android', 'webkit'),
99+
100+
'Mozilla/5.0 (Linux; U; Android 2.3.6; ru-ru; GT-B5512 Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1' =>
101+
array('android', '2.3.6', 'android', 'webkit'),
102+
103+
// Nexus 7
104+
'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' =>
105+
array('android', '4.1.1', 'android', 'webkit'),
82106
);
83107

84108
$t = new lime_test(count($tests));

test/UserAgentTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_once dirname(__FILE__).'/vendor/lime.php';
44
require_once dirname(__FILE__).'/../lib/phpUserAgent.php';
5+
require_once dirname(__FILE__).'/../lib/phpUserAgentStringParser.php';
56

67
$t = new lime_test(12);
78

0 commit comments

Comments
 (0)