2019-04-09 20:36:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DeviceDetectorNet;
|
|
|
|
|
|
|
|
require_once '../vendor/autoload.php';
|
|
|
|
|
2022-03-08 22:53:33 +01:00
|
|
|
use DeviceDetector\ClientHints;
|
2019-04-09 20:36:39 +02:00
|
|
|
use DeviceDetector\DeviceDetector;
|
2019-06-25 20:02:30 +02:00
|
|
|
use DeviceDetector\Parser\Client\Browser;
|
2020-10-27 10:45:52 +01:00
|
|
|
use DeviceDetector\Parser\Device\AbstractDeviceParser;
|
2019-06-25 20:02:30 +02:00
|
|
|
use DeviceDetector\Parser\OperatingSystem;
|
2019-08-24 19:19:05 +02:00
|
|
|
use DeviceDetector\Yaml\Symfony;
|
2022-03-08 22:53:33 +01:00
|
|
|
use Symfony\Component\Yaml\Exception\ParseException;
|
|
|
|
use Symfony\Component\Yaml\Yaml;
|
2019-04-09 20:36:39 +02:00
|
|
|
|
2020-10-27 10:45:52 +01:00
|
|
|
AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE);
|
2019-04-09 20:36:39 +02:00
|
|
|
|
|
|
|
$cacheloader = new CacheLoader();
|
|
|
|
|
2022-03-08 22:53:33 +01:00
|
|
|
class CustomClientHints extends ClientHints
|
|
|
|
{
|
|
|
|
public function toDict(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
"architecture" => $this->architecture,
|
|
|
|
"bitness" => $this->bitness,
|
|
|
|
"mobile" => $this->mobile,
|
|
|
|
"model" => $this->model,
|
|
|
|
"platform" => $this->platform,
|
|
|
|
"platformVersion" => $this->platformVersion,
|
|
|
|
"uaFullVersion" => $this->uaFullVersion,
|
|
|
|
"fullVersionList" => $this->fullVersionList,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function factory(array $headers): CustomClientHints
|
|
|
|
{
|
|
|
|
$ch = parent::factory($headers); // TODO: Change the autogenerated stub
|
|
|
|
return new self(
|
|
|
|
$ch->model,
|
|
|
|
$ch->platform,
|
|
|
|
$ch->platformVersion,
|
|
|
|
$ch->uaFullVersion,
|
|
|
|
$ch->fullVersionList,
|
|
|
|
$ch->mobile,
|
|
|
|
$ch->architecture,
|
|
|
|
$ch->bitness
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-09 20:36:39 +02:00
|
|
|
if (!empty($_GET["ua"])) {
|
|
|
|
$userAgent = $_GET["ua"];
|
|
|
|
} else {
|
|
|
|
$userAgent = $_SERVER['HTTP_USER_AGENT'];
|
|
|
|
}
|
|
|
|
|
2022-03-08 22:53:33 +01:00
|
|
|
if (!empty($_GET["ch"])) {
|
|
|
|
$ch_string = $_GET["ch"];
|
|
|
|
if (strpos($ch_string, "{") === 0) {
|
|
|
|
$data = json_decode($_GET["ch"], true);
|
|
|
|
unset($data['brands']);
|
|
|
|
$clientHints = new CustomClientHints(...$data);
|
|
|
|
$headers = NULL;
|
|
|
|
} else {
|
|
|
|
$lines = explode("\n", $_GET["ch"]);
|
|
|
|
try {
|
|
|
|
$trimmed = join("\n", array_map("trim", $lines));
|
|
|
|
$headers = Yaml::parse($trimmed);
|
|
|
|
} catch (ParseException $e) {
|
|
|
|
$headers = [];
|
|
|
|
$lines = explode("\n", $_GET["ch"]);
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
list($key, $value) = explode(":", $line, 2);
|
|
|
|
$key = trim($key);
|
|
|
|
$value = trim($value);
|
|
|
|
if (substr($value, -1) == $value[0] and strtolower($key) != "sec-ch-ua") {
|
|
|
|
$value = trim($value, substr($value, -1));
|
|
|
|
}
|
|
|
|
$headers[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$clientHints = CustomClientHints::factory($headers);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// $clientHints = CustomClientHints::factory($_SERVER);
|
|
|
|
$clientHints = new CustomClientHints();
|
|
|
|
}
|
|
|
|
|
|
|
|
//sec-ch-ua: "(Not(A:Brand";v="8", "Chromium";v="98"
|
|
|
|
//sec-ch-ua-mobile: ?0
|
|
|
|
//sec-ch-ua-platform: "Linux"
|
|
|
|
|
2019-09-04 11:08:02 +02:00
|
|
|
if (strlen($userAgent) > 1000) {
|
|
|
|
echo "The user agent has to be shorter than 1000 characters.";
|
2020-10-27 13:38:26 +01:00
|
|
|
http_response_code(400);
|
2019-09-04 11:08:02 +02:00
|
|
|
exit();
|
|
|
|
}
|
2022-03-08 22:53:33 +01:00
|
|
|
$dd = new DeviceDetector($userAgent, $clientHints);
|
2019-08-24 19:19:05 +02:00
|
|
|
$dd->setYamlParser(new Symfony());
|
2019-04-09 20:36:39 +02:00
|
|
|
|
|
|
|
$cacheloader->configureDeviceDetector($dd);
|
|
|
|
$dd->parse();
|
2019-04-10 12:12:22 +02:00
|
|
|
$icons = new IconPath($dd);
|
2019-04-09 20:36:39 +02:00
|
|
|
if ($dd->isBot()) {
|
2020-10-27 13:38:26 +01:00
|
|
|
$data = [
|
|
|
|
"isBot" => true,
|
|
|
|
"botInfo" => $dd->getBot()
|
2020-03-27 18:04:12 +01:00
|
|
|
];
|
2020-10-27 13:38:26 +01:00
|
|
|
} else {
|
|
|
|
$data = [
|
|
|
|
"isBot" => false,
|
|
|
|
"clientInfo" => $dd->getClient(),
|
|
|
|
"browserFamily" => Browser::getBrowserFamily($dd->getClient('short_name')),
|
|
|
|
"isMobileOnlyBrowser" => Browser::isMobileOnlyBrowser($dd->getClient('short_name')),
|
|
|
|
"osInfo" => $dd->getOs(),
|
|
|
|
"osFamily" => OperatingSystem::getOsFamily($dd->getOs('short_name')),
|
|
|
|
"device" => $dd->getDevice(),
|
|
|
|
"deviceName" => $dd->getDeviceName(),
|
|
|
|
"deviceBrand" => [
|
|
|
|
"name" => $dd->getBrandName(),
|
|
|
|
"short_name" => $dd->getBrand()
|
|
|
|
],
|
|
|
|
"model" => $dd->getModel(),
|
|
|
|
"icons" => [
|
|
|
|
"browser" => $icons->getBrowserLogo(),
|
|
|
|
"os" => $icons->getOsLogo(),
|
|
|
|
"device" => $icons->getDeviceTypeLogo(),
|
|
|
|
"brand" => $icons->getBrandLogo()
|
2022-03-08 22:53:33 +01:00
|
|
|
],
|
|
|
|
"clientHints" => $clientHints->toDict(),
|
|
|
|
"headers" => $headers
|
2019-04-17 21:17:42 +02:00
|
|
|
];
|
2019-04-09 20:36:39 +02:00
|
|
|
}
|
2020-10-27 13:38:26 +01:00
|
|
|
|
2019-04-09 20:36:39 +02:00
|
|
|
header("Content-Type: application/json; charset=UTF-8");
|
2020-03-27 18:04:12 +01:00
|
|
|
echo json_encode($data, JSON_FORCE_OBJECT);
|
2019-04-09 20:36:39 +02:00
|
|
|
|