Skip to content
Snippets Groups Projects
Commit ba576c62 authored by Benjamin Bellamy's avatar Benjamin Bellamy :speech_balloon:
Browse files

First commit

parent e819da08
Branches
No related tags found
No related merge requests found
src/UserAgents.php
composer.phar
/vendor/
......
{
"name": "podlibre/user-agents-php",
"description": "PHP implementation for opawg/user-agents.",
"homepage": "https://github.com/podlibre/user-agents-php",
"license": "MIT",
"authors": [
{
"name": "Benjamin Bellamy",
"email": "ben@podlibre.org",
"homepage": "https://podlibre.org/"
}
],
"autoload": {
"psr-4": {
"Opawg\\UserAgent\\": "src/"
}
},
"scripts": {
"post-update-cmd": "Opawg\\UserAgent\\UserAgentsGenerate::generate"
}
}
<?php
namespace Opawg\UserAgents;
class UserAgentsGenerate {
public static function generate()
{
/*
* Load json and convert it to PHP object:
*/
$rowstr = var_export(
json_decode(
file_get_contents(
'https://raw.githubusercontent.com/opawg/user-agents/master/src/user-agents.json'
),
true
),
true
);
// autogenerate database
print <<<EOT
<?php
/* Autogenerated. Do not edit */
namespace Opawg\UserAgents;
class UserAgents {
public static function find(\$userAgent)
{
\$playerFound = null;
//Search for current HTTP_USER_AGENT:
foreach (self::\$db as \$player) {
foreach (\$player['user_agents'] as \$userAgentsRegexp) {
//Does the HTTP_USER_AGENT match this regexp:
if (preg_match("#{\$userAgentsRegexp}#", \$userAgent)) {
\$playerFound = [
'app' => isset(\$player['app']) ? \$player['app'] : null,
'device' => isset(\$player['device'])
? \$player['device']
: null,
'os' => isset(\$player['os']) ? \$player['os'] : null,
'bot' => isset(\$player['bot']) ? \$player['bot'] : 0,
];
//We found it!
break 2;
}
}
}
return \$playerFound;
}
EOT;
print " static public \$db = ";
print $rowstr;
print ";\n";
print "}\n";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment