Skip to content

Instantly share code, notes, and snippets.

@shulard
Last active October 14, 2016 15:36
Show Gist options
  • Save shulard/1fc5735332c40a33682e0ac170d28667 to your computer and use it in GitHub Desktop.
Save shulard/1fc5735332c40a33682e0ac170d28667 to your computer and use it in GitHub Desktop.
Melody script which help to identify who is current UserAgent in a webserver visit : http://melody.sensiolabs.org/
/vendor/
/ua-detector.phar
composer.lock
{
"directories": ["vendor"],
"chmod": "0755",
"main": "ua-detector.php",
"output": "ua-detector.phar",
"stub": true
}
{
"name": "bee4/ua-detector",
"type": "script",
"require": {
"bee4/useragent-classifier": "^1.0"
},
"authors": [
{
"name": "Stéphane HULARD",
"email": "s.hulard@chstudio.fr"
}
]
}
#!/usr/bin/env php
<?php
require_once 'vendor/autoload.php';
use Bee4\UserAgent\Classifier\Detector;
$regex = '/^(?P<ip>\S+) (?P<domain>\S+) (.+) \[(?P<date>[^\[]+)\] "(((?P<method>\S+) (?P<parent>.*?) (?P<version>\S+))|[^"]+)" (?P<status>\S+) (?P<size>\S+) "(?P<referer>((?:[^\\\\]*?(?:(\\x+|\\\\+|\\\"|\\\'))?)*?))" "(?P<useragent>.*)"/';
if ($argc === 2) {
$regex = $argv[1];
}
try {
$input = fgets(STDIN);
if (1 !== preg_match($regex, $input, $infos)) {
throw new Exception('Regex doesn\'t match the given line !');
}
$bot = Detector::whoIs($infos['useragent']);
echo $bot->getBot().'::'.$bot->getName();
} catch(\Exception $error) {
exit(2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment