Hello world!

This commit is contained in:
2020-07-22 12:54:04 +02:00
commit 868050bcbe
42 changed files with 3657 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
[*.php]
charset = utf-8
indent_style = tab
indent_size = 4

View File

@@ -0,0 +1,2 @@
custom: https://www.paypal.me/donatj/15
github: donatj

View File

@@ -0,0 +1,32 @@
on:
- pull_request
- push
name: CI
jobs:
run:
name: Tests
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4']
runs-on: ${{ matrix.operating-system }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: sockets, json, curl
- name: Install dependencies with composer
run: composer install
- name: Run tests
run: make test

View File

@@ -0,0 +1,5 @@
.idea
vendor/
composer.lock
coverage.xml
phpunit.xml

View File

@@ -0,0 +1,15 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$class = new ReflectionClass($argv[1]);
echo "Predefined helper constants from `{$class->getName()}`\n\n";
echo "| Constant | {$argv[2]} | \n|----------|----------| \n";
foreach( $class->getConstants() as $constant => $value ) {
echo "| `{$class->getShortName()}::{$constant}` | {$value} | \n";
}
echo "\n";

View File

@@ -0,0 +1,108 @@
<mddoc>
<docpage target="README.md" autoloader="psr0" autoloader-root="src">
<section title="PHP User Agent Parser">
<text><![CDATA[
[![Join the chat at https://gitter.im/PhpUserAgentParser/Lobby](https://badges.gitter.im/PhpUserAgentParser/Lobby.svg)](https://gitter.im/PhpUserAgentParser/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
]]></text>
<badge-poser type="version"/>
<badge-poser type="downloads"/>
<badge-poser type="license"/>
<badge-travis name="donatj/phpUserAgent"/>
<badge-github-action name="donatj/phpUserAgent" workflow="CI"/>
<section title="What It Is">
<text><![CDATA[
A simple, streamlined PHP user-agent parser!
Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
]]></text>
</section>
<section title="Upgrading to `1.*`">
<text><![CDATA[
The new `1.*` release **does not break compatibility** with `0.*` and nothing need to change to upgrade. However, the global `parse_user_agent` is now deprecated; it has been replaced with the namespaced `\donatj\UserAgent\parse_user_agent` and functions exactly the same. You can easily replace any existing call to `parse_user_agent` with `\donatj\UserAgent\parse_user_agent`
In addition, 1.x adds a convenience object wrapper you may use should you prefer. More information on this is in the Usage section below.
]]></text>
</section>
<section title="Why Use This">
<text><![CDATA[
You have your choice in user-agent parsers. This one detects **all modern browsers** in a very light, quick, understandable fashion.
It is less than 200 lines of code, and consists of just three regular expressions!
It can also correctly identify exotic versions of IE others fail on.
It offers 100% unit test coverage, is installable via Composer, and is very easy to use.
]]></text>
</section>
<section title="What It Does Not Do">
<text><![CDATA[
This is not meant as a browser "knowledge engine" but rather a simple parser. Anything not adequately provided directly by the user agent string itself will simply not be provided by this.
]]></text>
<section title="OS Versions">
<text><![CDATA[
User-agent strings **are not** a reliable source of OS Version!
- Many agents simply don't send the information.
- Others provide varying levels of accuracy.
- Parsing Windows versions alone almost nearly doubles the size of the code.
I'm much more interested in keeping this thing *tiny* and accurate than adding niché features and would rather focus on things that can be **done well**.
All that said, there is the start of a [branch to do it](https://github.com/donatj/PhpUserAgent/tree/os_version_detection) I created for a client if you want to poke it, I update it from time to time, but frankly if you need to *reliably detect OS Version*, using user-agent isn't the way to do it. I'd go with JavaScript.
]]></text>
</section>
<section title="Undetectable Browsers">
<text><![CDATA[
- **Brave** - Brave is simply not differentiable from Chrome. This was a design decision on their part.
]]></text>
</section>
</section>
<section title="Requirements">
<composer-requires/>
</section>
<section title="Installing">
<text>PHP User Agent is available through Packagist via Composer.</text>
<composer-install/>
</section>
<section title="Usage">
<text><![CDATA[
The classic procedural use is as simple as:
```php
$ua_info = parse_user_agent();
/*
array(
'platform' => '[Detected Platform]',
'browser' => '[Detected Browser]',
'version' => '[Detected Browser Version]',
);
*/
```
]]></text>
<text><![CDATA[
The new object oriented wrapper form:
```php
$parser = new UserAgentParser();
$ua = $parser->parse();
// or
$ua = $parser();
$ua->platform();
$ua->browser();
$ua->browserVersion();
```
]]></text>
</section>
<section title="Currently Detected Platforms">
<exec cmd="php .helpers/constants.php 'donatj\UserAgent\Platforms' 'Platform'"/>
</section>
<section title="Currently Detected Browsers">
<exec cmd="php .helpers/constants.php 'donatj\UserAgent\Browsers' 'Browser'"/>
</section>
<text><![CDATA[
More information is available at [Donat Studios](http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT).
]]></text>
</section>
</docpage>
</mddoc>

View File

@@ -0,0 +1,23 @@
language: php
sudo: false
dist: precise
php:
- "5.3"
- "5.4"
- "5.5"
- "5.6"
- "7.0"
- "7.1"
- "7.2"
- "7.3"
- "nightly"
- "hhvm"
matrix:
allow_failures:
- php: nightly
- php: hhvm
install: composer install
script: make test

View File

@@ -0,0 +1,18 @@
# How to Contribute
## Reporting Issues
Issues can be reported via the [Github Issues](https://github.com/donatj/PhpUserAgent/issues) page.
- **Detail is key**: If a browser is being misidentified, one or more sample user agent strings are key to getting it resolved.
- **Missing Browser**: Is it modern? What is it being misidentified as? There are a lot of dead browsers out there that there is no reason to support.
Please do not file any requests for OS version identification. It is not a desired feature.
## Pull Requests
Pull requests are truly appreciated. While I try my best to stay on top of browsers hitting the market it is still a difficult task.
- **Formatting**: Indentation **must** use tabs. Please try to match internal formatting and spacing to existing code.
- **Tests**: If you're adding support for a new browser be sure to add test user agents for if at all possible ***every platform*** the browser is available on. Untested code will take much longer to be merged.
- **Terseness**: Try to be terse. Be clever. Take up as little space as possible. The point of this project initially was to be smaller than the other guys.

View File

@@ -0,0 +1,22 @@
The MIT License
===============
Copyright (c) 2013 Jesse G. Donat
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,13 @@
.PHONY: test
test:
./vendor/bin/phpunit --coverage-text
.PHONY: generate
generate:
php bin/user_agent_sorter.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.json
php bin/constant_generator.php
.PHONY: init
init:
php bin/init_user_agent.php > tests/user_agents.tmp.json && mv tests/user_agents.tmp.json tests/user_agents.json
make generate

View File

@@ -0,0 +1,180 @@
# PHP User Agent Parser
[![Join the chat at https://gitter.im/PhpUserAgentParser/Lobby](https://badges.gitter.im/PhpUserAgentParser/Lobby.svg)](https://gitter.im/PhpUserAgentParser/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Latest Stable Version](https://poser.pugx.org/donatj/phpuseragentparser/version)](https://packagist.org/packages/donatj/phpuseragentparser)
[![Total Downloads](https://poser.pugx.org/donatj/phpuseragentparser/downloads)](https://packagist.org/packages/donatj/phpuseragentparser)
[![License](https://poser.pugx.org/donatj/phpuseragentparser/license)](https://packagist.org/packages/donatj/phpuseragentparser)
[![Build Status](https://travis-ci.org/donatj/phpUserAgent.svg?branch=master)](https://travis-ci.org/donatj/phpUserAgent)
[![Build Status](https://github.com/donatj/phpUserAgent/workflows/CI/badge.svg?)](https://github.com/donatj/phpUserAgent/actions?query=workflow%3ACI)
## What It Is
A simple, streamlined PHP user-agent parser!
Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
## Upgrading to `1.*`
The new `1.*` release **does not break compatibility** with `0.*` and nothing need to change to upgrade. However, the global `parse_user_agent` is now deprecated; it has been replaced with the namespaced `\donatj\UserAgent\parse_user_agent` and functions exactly the same. You can easily replace any existing call to `parse_user_agent` with `\donatj\UserAgent\parse_user_agent`
In addition, 1.x adds a convenience object wrapper you may use should you prefer. More information on this is in the Usage section below.
## Why Use This
You have your choice in user-agent parsers. This one detects **all modern browsers** in a very light, quick, understandable fashion.
It is less than 200 lines of code, and consists of just three regular expressions!
It can also correctly identify exotic versions of IE others fail on.
It offers 100% unit test coverage, is installable via Composer, and is very easy to use.
## What It Does Not Do
This is not meant as a browser "knowledge engine" but rather a simple parser. Anything not adequately provided directly by the user agent string itself will simply not be provided by this.
### OS Versions
User-agent strings **are not** a reliable source of OS Version!
- Many agents simply don't send the information.
- Others provide varying levels of accuracy.
- Parsing Windows versions alone almost nearly doubles the size of the code.
I'm much more interested in keeping this thing *tiny* and accurate than adding niché features and would rather focus on things that can be **done well**.
All that said, there is the start of a [branch to do it](https://github.com/donatj/PhpUserAgent/tree/os_version_detection) I created for a client if you want to poke it, I update it from time to time, but frankly if you need to *reliably detect OS Version*, using user-agent isn't the way to do it. I'd go with JavaScript.
### Undetectable Browsers
- **Brave** - Brave is simply not differentiable from Chrome. This was a design decision on their part.
## Requirements
- **php**: >=5.3.0
## Installing
PHP User Agent is available through Packagist via Composer.
Install the latest version with:
```bash
composer require 'donatj/phpuseragentparser'
```
## Usage
The classic procedural use is as simple as:
```php
$ua_info = parse_user_agent();
/*
array(
'platform' => '[Detected Platform]',
'browser' => '[Detected Browser]',
'version' => '[Detected Browser Version]',
);
*/
```
The new object oriented wrapper form:
```php
$parser = new UserAgentParser();
$ua = $parser->parse();
// or
$ua = $parser();
$ua->platform();
$ua->browser();
$ua->browserVersion();
```
## Currently Detected Platforms
Predefined helper constants from `donatj\UserAgent\Platforms`
| Constant | Platform |
|----------|----------|
| `Platforms::MACINTOSH` | Macintosh |
| `Platforms::CHROME_OS` | Chrome OS |
| `Platforms::LINUX` | Linux |
| `Platforms::WINDOWS` | Windows |
| `Platforms::ANDROID` | Android |
| `Platforms::BLACKBERRY` | BlackBerry |
| `Platforms::FREEBSD` | FreeBSD |
| `Platforms::IPAD` | iPad |
| `Platforms::IPHONE` | iPhone |
| `Platforms::IPOD` | iPod |
| `Platforms::KINDLE` | Kindle |
| `Platforms::KINDLE_FIRE` | Kindle Fire |
| `Platforms::NETBSD` | NetBSD |
| `Platforms::NEW_NINTENDO_3DS` | New Nintendo 3DS |
| `Platforms::NINTENDO_3DS` | Nintendo 3DS |
| `Platforms::NINTENDO_DS` | Nintendo DS |
| `Platforms::NINTENDO_SWITCH` | Nintendo Switch |
| `Platforms::NINTENDO_WII` | Nintendo Wii |
| `Platforms::NINTENDO_WIIU` | Nintendo WiiU |
| `Platforms::OPENBSD` | OpenBSD |
| `Platforms::PLAYBOOK` | PlayBook |
| `Platforms::PLAYSTATION_3` | PlayStation 3 |
| `Platforms::PLAYSTATION_4` | PlayStation 4 |
| `Platforms::PLAYSTATION_VITA` | PlayStation Vita |
| `Platforms::TIZEN` | Tizen |
| `Platforms::WINDOWS_PHONE` | Windows Phone |
| `Platforms::XBOX` | Xbox |
| `Platforms::XBOX_ONE` | Xbox One |
## Currently Detected Browsers
Predefined helper constants from `donatj\UserAgent\Browsers`
| Constant | Browser |
|----------|----------|
| `Browsers::ADSBOT_GOOGLE` | AdsBot-Google |
| `Browsers::ANDROID_BROWSER` | Android Browser |
| `Browsers::BAIDUSPIDER` | Baiduspider |
| `Browsers::BINGBOT` | bingbot |
| `Browsers::BLACKBERRY_BROWSER` | BlackBerry Browser |
| `Browsers::BROWSER` | Browser |
| `Browsers::BUNJALLOO` | Bunjalloo |
| `Browsers::CAMINO` | Camino |
| `Browsers::CHROME` | Chrome |
| `Browsers::CURL` | curl |
| `Browsers::EDGE` | Edge |
| `Browsers::FACEBOOKEXTERNALHIT` | facebookexternalhit |
| `Browsers::FEEDVALIDATOR` | FeedValidator |
| `Browsers::FIREFOX` | Firefox |
| `Browsers::GOOGLEBOT` | Googlebot |
| `Browsers::GOOGLEBOT_IMAGE` | Googlebot-Image |
| `Browsers::GOOGLEBOT_VIDEO` | Googlebot-Video |
| `Browsers::HEADLESSCHROME` | HeadlessChrome |
| `Browsers::IEMOBILE` | IEMobile |
| `Browsers::KINDLE` | Kindle |
| `Browsers::LYNX` | Lynx |
| `Browsers::MIDORI` | Midori |
| `Browsers::MSIE` | MSIE |
| `Browsers::MSNBOT_MEDIA` | msnbot-media |
| `Browsers::NETFRONT` | NetFront |
| `Browsers::NINTENDOBROWSER` | NintendoBrowser |
| `Browsers::OCULUSBROWSER` | OculusBrowser |
| `Browsers::OPERA` | Opera |
| `Browsers::PUFFIN` | Puffin |
| `Browsers::SAFARI` | Safari |
| `Browsers::SAMSUNGBROWSER` | SamsungBrowser |
| `Browsers::SILK` | Silk |
| `Browsers::TIZENBROWSER` | TizenBrowser |
| `Browsers::UC_BROWSER` | UC Browser |
| `Browsers::VALVE_STEAM_TENFOOT` | Valve Steam Tenfoot |
| `Browsers::VIVALDI` | Vivaldi |
| `Browsers::WGET` | Wget |
| `Browsers::WORDPRESS` | WordPress |
| `Browsers::YANDEX` | Yandex |
| `Browsers::YANDEXBOT` | YandexBot |
More information is available at [Donat Studios](http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT).

View File

@@ -0,0 +1,24 @@
<?php
require __DIR__ . '/../src/UserAgentParser.php';
$time = microtime(true);
$uas = json_decode(file_get_contents(__DIR__ . '/../tests/user_agents.json'), true);
foreach( $uas as $ua => $junk ) {
$uatime = microtime(true);
for( $i = 0; $i <= 1000; $i++ ) {
\parse_user_agent($ua);
}
echo microtime(true) - $uatime;
echo " : $ua\n";
}
echo microtime(true) - $time;
echo " : TOTAL\n";

View File

@@ -0,0 +1,77 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$jsonfile = __DIR__ . '/../tests/user_agents.json';
$uas = json_decode(
file_get_contents($jsonfile),
true
);
$platforms = array();
$browsers = array();
foreach( $uas as $key => $val ) {
$kex = strtoupper($val['browser']);
if( $kex !== '' ) {
$kex = preg_replace('/\W+/', '_', $kex);
if( !isset($browsers[$kex][$val['browser']]) ) {
$browsers[$kex][$val['browser']] = 0;
}
$browsers[$kex][$val['browser']]++;
}
$kex = strtoupper($val['platform']);
if( $kex !== '' ) {
$kex = preg_replace('/\W+/', '_', $kex);
if( !isset($platforms[$kex][$val['platform']]) ) {
$platforms[$kex][$val['platform']] = 0;
}
$platforms[$kex][$val['platform']]++;
}
}
ksort($browsers);
$file = basename(__FILE__);
$header = <<<EOT
<?php
// DO NOT EDIT THIS FILE - IT IS GENERATED BY {$file}
EOT;
foreach( $browsers as $browser ) {
if( count($browser) !== 1 ) {
echo "bad browser count\n";
die(2);
}
}
$browserBody = "{$header}namespace donatj\UserAgent;\n\ninterface Browsers {\n\n";
$maxKey = max(array_map('strlen', array_keys($browsers)));
foreach( $browsers as $const => $val ) {
$browserBody .= sprintf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true));
}
$browserBody .= "\n}\n\n";
foreach( $platforms as $platform ) {
if( count($platform) !== 1 ) {
echo "bad platform count\n";
die(2);
}
}
$platformBody = "{$header}namespace donatj\UserAgent;\n\ninterface Platforms {\n\n";
$maxKey = max(array_map('strlen', array_keys($platforms)));
foreach( $platforms as $const => $val ) {
$platformBody .= sprintf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true));
}
$platformBody .= "\n}\n\n";
file_put_contents(__DIR__ . '/../src/UserAgent/Browsers.php', $browserBody);
file_put_contents(__DIR__ . '/../src/UserAgent/Platforms.php', $platformBody);

View File

@@ -0,0 +1,13 @@
<?php
require(__DIR__ . '/../vendor/autoload.php');
$jsonfile = __DIR__ . '/../Tests/user_agents.json';
$uas = json_decode(file_get_contents($jsonfile), true);
foreach( $uas as $key => &$val ) {
$val = parse_user_agent($key);
}
echo json_encode($uas);

View File

@@ -0,0 +1,100 @@
<?php
require(__DIR__ . '/../vendor/autoload.php');
$jsonfile = __DIR__ . '/../Tests/user_agents.json';
$uas = json_decode(file_get_contents($jsonfile), true);
foreach( $uas as $key => &$val ) {
$val['key'] = $key;
}
unset($val);
uasort($uas, function ( $a, $b ) {
if( $a['platform'] === null && $b['platform'] !== null ) {
return 1;
}
if( $b['platform'] === null && $a['platform'] !== null ) {
return -1;
}
$desktop = array( 'Windows', 'Linux', 'Macintosh', 'Chrome OS' );
$ad = in_array($a['platform'], $desktop, true);
$bd = in_array($b['platform'], $desktop, true);
if( !$ad && $bd ) {
return 1;
}
if( $ad && !$bd ) {
return -1;
}
if( $ad ) {
$result = strnatcasecmp($a['browser'], $b['browser']);
if( $result == 0 ) {
$result = strnatcasecmp($a['platform'], $b['platform']);
if( $result == 0 ) {
$result = compare_version($a['version'], $b['version']);
}
}
} else {
$result = strnatcasecmp($a['platform'], $b['platform']);
if( $result == 0 ) {
$result = strnatcasecmp($a['browser'], $b['browser']);
if( $result == 0 ) {
$result = compare_version($a['version'], $b['version']);
}
}
}
if( $result == 0 ) {
$result = strnatcasecmp($a['key'], $b['key']);
}
return $result;
});
foreach( $uas as &$val ) {
unset($val['key']);
}
unset($val);
$jsonPretty = new Camspiers\JsonPretty\JsonPretty;
$json = $jsonPretty->prettify($uas) . "\n";
echo $json;
function compare_version( $a, $b ) {
$cmp_a = explode('.', $a);
$cmp_b = explode('.', $b);
$max = max(count($cmp_a), count($cmp_b));
$value = 0;
for( $i = 0; $i < $max; $i++ ) {
$aa = strtolower(isset($cmp_a[$i]) ? $cmp_a[$i] : '0');
$bb = strtolower(isset($cmp_b[$i]) ? $cmp_b[$i] : '0');
if( is_numeric($aa) && is_numeric($bb) && $aa !== $bb ) {
$value = ($aa > $bb ? 1 : -1);
break;
}
if( $cmp = strcmp($aa, $bb) ) {
$value = $cmp / abs($cmp);
break;
}
}
return $value;
}

View File

@@ -0,0 +1,38 @@
{
"name": "donatj/phpuseragentparser",
"type": "library",
"description": "Lightning fast, minimalist PHP UserAgent string parser.",
"keywords": [
"user agent",
"useragent",
"parser",
"browser",
"browser detection"
],
"homepage": "http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT",
"license": "MIT",
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"camspiers/json-pretty": "~1.0",
"phpunit/phpunit": "~4.8",
"donatj/drop": "*"
},
"authors": [
{
"name": "Jesse G. Donat",
"email": "donatj@gmail.com",
"homepage": "https://donatstudios.com",
"role": "Developer"
}
],
"autoload": {
"files": [
"src/UserAgentParser.php"
],
"psr-4": {
"donatj\\UserAgent\\": "src/UserAgent"
}
}
}

View File

@@ -0,0 +1,11 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Application">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="coverage.xml"/>
</logging>
</phpunit>

View File

@@ -0,0 +1,51 @@
<?php
// DO NOT EDIT THIS FILE - IT IS GENERATED BY constant_generator.php
namespace donatj\UserAgent;
interface Browsers {
const ADSBOT_GOOGLE = 'AdsBot-Google';
const ANDROID_BROWSER = 'Android Browser';
const BAIDUSPIDER = 'Baiduspider';
const BINGBOT = 'bingbot';
const BLACKBERRY_BROWSER = 'BlackBerry Browser';
const BROWSER = 'Browser';
const BUNJALLOO = 'Bunjalloo';
const CAMINO = 'Camino';
const CHROME = 'Chrome';
const CURL = 'curl';
const EDGE = 'Edge';
const FACEBOOKEXTERNALHIT = 'facebookexternalhit';
const FEEDVALIDATOR = 'FeedValidator';
const FIREFOX = 'Firefox';
const GOOGLEBOT = 'Googlebot';
const GOOGLEBOT_IMAGE = 'Googlebot-Image';
const GOOGLEBOT_VIDEO = 'Googlebot-Video';
const HEADLESSCHROME = 'HeadlessChrome';
const IEMOBILE = 'IEMobile';
const KINDLE = 'Kindle';
const LYNX = 'Lynx';
const MIDORI = 'Midori';
const MSIE = 'MSIE';
const MSNBOT_MEDIA = 'msnbot-media';
const NETFRONT = 'NetFront';
const NINTENDOBROWSER = 'NintendoBrowser';
const OCULUSBROWSER = 'OculusBrowser';
const OPERA = 'Opera';
const PUFFIN = 'Puffin';
const SAFARI = 'Safari';
const SAMSUNGBROWSER = 'SamsungBrowser';
const SILK = 'Silk';
const TIZENBROWSER = 'TizenBrowser';
const UC_BROWSER = 'UC Browser';
const VALVE_STEAM_TENFOOT = 'Valve Steam Tenfoot';
const VIVALDI = 'Vivaldi';
const WGET = 'Wget';
const WORDPRESS = 'WordPress';
const YANDEX = 'Yandex';
const YANDEXBOT = 'YandexBot';
}

View File

@@ -0,0 +1,39 @@
<?php
// DO NOT EDIT THIS FILE - IT IS GENERATED BY constant_generator.php
namespace donatj\UserAgent;
interface Platforms {
const MACINTOSH = 'Macintosh';
const CHROME_OS = 'Chrome OS';
const LINUX = 'Linux';
const WINDOWS = 'Windows';
const ANDROID = 'Android';
const BLACKBERRY = 'BlackBerry';
const FREEBSD = 'FreeBSD';
const IPAD = 'iPad';
const IPHONE = 'iPhone';
const IPOD = 'iPod';
const KINDLE = 'Kindle';
const KINDLE_FIRE = 'Kindle Fire';
const NETBSD = 'NetBSD';
const NEW_NINTENDO_3DS = 'New Nintendo 3DS';
const NINTENDO_3DS = 'Nintendo 3DS';
const NINTENDO_DS = 'Nintendo DS';
const NINTENDO_SWITCH = 'Nintendo Switch';
const NINTENDO_WII = 'Nintendo Wii';
const NINTENDO_WIIU = 'Nintendo WiiU';
const OPENBSD = 'OpenBSD';
const PLAYBOOK = 'PlayBook';
const PLAYSTATION_3 = 'PlayStation 3';
const PLAYSTATION_4 = 'PlayStation 4';
const PLAYSTATION_VITA = 'PlayStation Vita';
const TIZEN = 'Tizen';
const WINDOWS_PHONE = 'Windows Phone';
const XBOX = 'Xbox';
const XBOX_ONE = 'Xbox One';
}

View File

@@ -0,0 +1,57 @@
<?php
namespace donatj\UserAgent;
class UserAgent implements UserAgentInterface {
/**
* @var string|null
*/
private $platform;
/**
* @var string|null
*/
private $browser;
/**
* @var string|null
*/
private $browserVersion;
/**
* UserAgent constructor.
*
* @param string|null $platform
* @param string|null $browser
* @param string|null $browserVersion
*/
public function __construct( $platform, $browser, $browserVersion ) {
$this->platform = $platform;
$this->browser = $browser;
$this->browserVersion = $browserVersion;
}
/**
* @return string|null
* @see \donatj\UserAgent\Platforms for a list of tested platforms
*/
public function platform() {
return $this->platform;
}
/**
* @return string|null
* @see \donatj\UserAgent\Browsers for a list of tested browsers.
*/
public function browser() {
return $this->browser;
}
/**
* The version string. Formatting depends on the browser.
*
* @return string|null
*/
public function browserVersion() {
return $this->browserVersion;
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace donatj\UserAgent;
interface UserAgentInterface {
/**
* @return string|null
* @see \donatj\UserAgent\Platforms for a list of tested platforms
*/
public function platform();
/**
* @return string|null
* @see \donatj\UserAgent\Browsers for a list of tested browsers.
*/
public function browser();
/**
* The version string. Formatting depends on the browser.
*
* @return string|null
*/
public function browserVersion();
}

View File

@@ -0,0 +1,39 @@
<?php
namespace donatj\UserAgent;
class UserAgentParser {
/**
* Parses a user agent string into its important parts, provide an object
*
* @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
* @return UserAgent an object with 'browser', 'browserVersion' and 'platform' methods
* @throws \InvalidArgumentException on not having a proper user agent to parse.
* @see \donatj\UserAgent\parse_user_agent()
*
*/
public function parse( $u_agent = null ) {
$parsed = parse_user_agent($u_agent);
return new UserAgent(
$parsed[PLATFORM],
$parsed[BROWSER],
$parsed[BROWSER_VERSION]
);
}
/**
* Parses a user agent string into its important parts
*
* @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
* @return UserAgent an object with 'browser', 'browserVersion' and 'platform' methods
* @throws \InvalidArgumentException on not having a proper user agent to parse.
* @see \donatj\UserAgent\parse_user_agent()
*
*/
public function __invoke( $u_agent = null ) {
return $this->parse($u_agent);
}
}

View File

@@ -0,0 +1,210 @@
<?php
/**
* @author Jesse G. Donat <donatj@gmail.com>
*
* @link https://donatstudios.com/PHP-Parser-HTTP_USER_AGENT
* @link https://github.com/donatj/PhpUserAgent
*
* @license MIT https://github.com/donatj/PhpUserAgent/blob/master/LICENSE.md
*/
namespace {
/**
* Parses a user agent string into its important parts
*
* This method is defined for backwards comparability with the old global method.
*
* @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
* @return string[] an array with 'browser', 'version' and 'platform' keys
* @throws \InvalidArgumentException on not having a proper user agent to parse.
*
* @deprecated This exists for backwards compatibility with 0.x and will likely be removed in 2.x
* @see \donatj\UserAgent\parse_user_agent
*/
function parse_user_agent( $u_agent = null ) {
return \donatj\UserAgent\parse_user_agent($u_agent);
}
}
namespace donatj\UserAgent {
const PLATFORM = 'platform';
const BROWSER = 'browser';
const BROWSER_VERSION = 'version';
/**
* Parses a user agent string into its important parts
*
* @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
* @return string[] an array with 'browser', 'version' and 'platform' keys
* @throws \InvalidArgumentException on not having a proper user agent to parse.
*/
function parse_user_agent( $u_agent = null ) {
if( $u_agent === null && isset($_SERVER['HTTP_USER_AGENT']) ) {
$u_agent = (string)$_SERVER['HTTP_USER_AGENT'];
}
if( $u_agent === null ) {
throw new \InvalidArgumentException('parse_user_agent requires a user agent');
}
$platform = null;
$browser = null;
$version = null;
$empty = array( PLATFORM => $platform, BROWSER => $browser, BROWSER_VERSION => $version );
if( !$u_agent ) {
return $empty;
}
if( preg_match('/\((.*?)\)/m', $u_agent, $parent_matches) ) {
preg_match_all(<<<'REGEX'
/(?P<platform>BB\d+;|Android|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
, $parent_matches[1], $result);
$priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' );
$result[PLATFORM] = array_unique($result[PLATFORM]);
if( count($result[PLATFORM]) > 1 ) {
if( $keys = array_intersect($priority, $result[PLATFORM]) ) {
$platform = reset($keys);
} else {
$platform = $result[PLATFORM][0];
}
} elseif( isset($result[PLATFORM][0]) ) {
$platform = $result[PLATFORM][0];
}
}
if( $platform == 'linux-gnu' || $platform == 'X11' ) {
$platform = 'Linux';
} elseif( $platform == 'CrOS' ) {
$platform = 'Chrome OS';
}
preg_match_all(<<<'REGEX'
%(?P<browser>Camino|Kindle(\ Fire)?|Firefox|Iceweasel|IceCat|Safari|MSIE|Trident|AppleWebKit|
TizenBrowser|(?:Headless)?Chrome|YaBrowser|Vivaldi|IEMobile|Opera|OPR|Silk|Midori|Edge|Edg|CriOS|UCBrowser|Puffin|OculusBrowser|SamsungBrowser|
Baiduspider|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl|
Valve\ Steam\ Tenfoot|
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( preg_match('%^(?!Mozilla)(?P<browser>[A-Z0-9\-]+)(/(?P<version>[0-9A-Z.]+))?%ix', $u_agent, $result) ) {
return array( PLATFORM => $platform ?: null, BROWSER => $result[BROWSER], BROWSER_VERSION => empty($result[BROWSER_VERSION]) ? null : $result[BROWSER_VERSION] );
}
return $empty;
}
if( preg_match('/rv:(?P<version>[0-9A-Z.]+)/i', $u_agent, $rv_result) ) {
$rv_result = $rv_result[BROWSER_VERSION];
}
$browser = $result[BROWSER][0];
$version = $result[BROWSER_VERSION][0];
$lowerBrowser = array_map('strtolower', $result[BROWSER]);
$find = function ( $search, &$key = null, &$value = null ) use ( $lowerBrowser ) {
$search = (array)$search;
foreach( $search as $val ) {
$xkey = array_search(strtolower($val), $lowerBrowser);
if( $xkey !== false ) {
$value = $val;
$key = $xkey;
return true;
}
}
return false;
};
$findT = function ( array $search, &$key = null, &$value = null ) use ( $find ) {
$value2 = null;
if( $find(array_keys($search), $key, $value2) ) {
$value = $search[$value2];
return true;
}
return false;
};
$key = 0;
$val = '';
if( $findT(array( 'OPR' => 'Opera', 'UCBrowser' => 'UC Browser', 'YaBrowser' => 'Yandex', 'Iceweasel' => 'Firefox', 'Icecat' => 'Firefox', 'CriOS' => 'Chrome', 'Edg' => 'Edge' ), $key, $browser) ) {
$version = $result[BROWSER_VERSION][$key];
} elseif( $find('Playstation Vita', $key, $platform) ) {
$platform = 'PlayStation Vita';
$browser = 'Browser';
} elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) {
$browser = $val == 'Silk' ? 'Silk' : 'Kindle';
$platform = 'Kindle Fire';
if( !($version = $result[BROWSER_VERSION][$key]) || !is_numeric($version[0]) ) {
$version = $result[BROWSER_VERSION][array_search('Version', $result[BROWSER])];
}
} elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) {
$browser = 'NintendoBrowser';
$version = $result[BROWSER_VERSION][$key];
} elseif( $find('Kindle', $key, $platform) ) {
$browser = $result[BROWSER][$key];
$version = $result[BROWSER_VERSION][$key];
} elseif( $find('Opera', $key, $browser) ) {
$find('Version', $key);
$version = $result[BROWSER_VERSION][$key];
} elseif( $find('Puffin', $key, $browser) ) {
$version = $result[BROWSER_VERSION][$key];
if( strlen($version) > 3 ) {
$part = substr($version, -2);
if( ctype_upper($part) ) {
$version = substr($version, 0, -2);
$flags = array( 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' );
if( isset($flags[$part]) ) {
$platform = $flags[$part];
}
}
}
} elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'OculusBrowser', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) {
$version = $result[BROWSER_VERSION][$key];
} elseif( $rv_result && $find('Trident') ) {
$browser = 'MSIE';
$version = $rv_result;
} elseif( $browser == 'AppleWebKit' ) {
if( $platform == 'Android' ) {
$browser = 'Android Browser';
} elseif( strpos($platform, 'BB') === 0 ) {
$browser = 'BlackBerry Browser';
$platform = 'BlackBerry';
} elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) {
$browser = 'BlackBerry Browser';
} else {
$find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser);
}
$find('Version', $key);
$version = $result[BROWSER_VERSION][$key];
} elseif( $pKey = preg_grep('/playstation \d/i', $result[BROWSER]) ) {
$pKey = reset($pKey);
$platform = 'PlayStation ' . preg_replace('/\D/', '', $pKey);
$browser = 'NetFront';
}
return array( PLATFORM => $platform ?: null, BROWSER => $browser ?: null, BROWSER_VERSION => $version ?: null );
}
}

View File

@@ -0,0 +1,50 @@
<?php
class UserAgentParserFunctionTest extends \PHPUnit_Framework_TestCase {
/**
* @dataProvider userAgentDataProvider
*/
public function test_parse_user_agent( $string, $expected ) {
$result = parse_user_agent($string);
$this->assertSame($expected, $result, $string . " test failed!");
}
public function userAgentDataProvider() {
$out = array();
$uas = json_decode(file_get_contents(__DIR__ . '/user_agents.json'), true);
foreach( $uas as $string => $parts ) {
$out[] = array( $string, $parts );
}
return $out;
}
public function test_parse_user_agent_empty() {
$expected = array(
'platform' => null,
'browser' => null,
'version' => null,
);
$result = parse_user_agent('');
$this->assertSame($result, $expected);
$result = parse_user_agent('Mozilla (asdjkakljasdkljasdlkj) BlahBlah');
$this->assertSame($result, $expected);
}
/**
* @expectedException \InvalidArgumentException
*/
public function test_no_user_agent_exception() {
unset($_SERVER['HTTP_USER_AGENT']);
parse_user_agent();
}
public function test_global_user_agent() {
$_SERVER['HTTP_USER_AGENT'] = 'Test/1.0';
$this->assertSame(array( 'platform' => null, 'browser' => 'Test', 'version' => '1.0' ), parse_user_agent());
}
}

View File

@@ -0,0 +1,45 @@
<?php
use donatj\UserAgent\UserAgentParser;
class UserAgentParserObjectTest extends \PHPUnit_Framework_TestCase {
public function userAgentDataProvider() {
$out = array();
$uas = json_decode(file_get_contents(__DIR__ . '/user_agents.json'), true);
foreach( $uas as $string => $parts ) {
$out[] = array( $string );
}
return $out;
}
/**
* @dataProvider userAgentDataProvider
*/
public function test_parse( $string ) {
$parser = new UserAgentParser;
$result = $parser->parse($string);
$expected = parse_user_agent($string);
$this->assertSame($expected[\donatj\UserAgent\PLATFORM], $result->platform());
$this->assertSame($expected[\donatj\UserAgent\BROWSER], $result->browser());
$this->assertSame($expected[\donatj\UserAgent\BROWSER_VERSION], $result->browserVersion());
}
/**
* @dataProvider userAgentDataProvider
*/
public function test_invoke( $string ) {
$parser = new UserAgentParser;
$result = $parser($string);
$expected = parse_user_agent($string);
$this->assertSame($expected[\donatj\UserAgent\PLATFORM], $result->platform());
$this->assertSame($expected[\donatj\UserAgent\BROWSER], $result->browser());
$this->assertSame($expected[\donatj\UserAgent\BROWSER_VERSION], $result->browserVersion());
}
}

File diff suppressed because it is too large Load Diff