2023-07-21 11:33:27 +00:00
|
|
|
<?php
|
|
|
|
namespace Misuzu;
|
|
|
|
|
2023-07-21 12:47:56 +00:00
|
|
|
use stdClass;
|
2023-07-22 16:37:57 +00:00
|
|
|
use JsonSerializable;
|
2023-07-21 12:47:56 +00:00
|
|
|
use RuntimeException;
|
2023-07-21 11:33:27 +00:00
|
|
|
use Stringable;
|
|
|
|
use DeviceDetector\ClientHints;
|
|
|
|
use DeviceDetector\DeviceDetector;
|
|
|
|
|
2023-07-22 16:37:57 +00:00
|
|
|
class ClientInfo implements Stringable, JsonSerializable {
|
2023-07-21 12:47:56 +00:00
|
|
|
private const SERIALIZE_VERSION = 1;
|
2023-07-21 11:33:27 +00:00
|
|
|
|
2023-07-21 12:47:56 +00:00
|
|
|
public function __construct(
|
|
|
|
private array|bool|null $botInfo,
|
|
|
|
private ?array $clientInfo,
|
|
|
|
private ?array $osInfo,
|
|
|
|
private string $brandName,
|
|
|
|
private string $modelName
|
|
|
|
) {}
|
2023-07-21 11:33:27 +00:00
|
|
|
|
|
|
|
public function __toString(): string {
|
2023-07-21 12:47:56 +00:00
|
|
|
if($this->botInfo === true || is_array($this->botInfo)) {
|
|
|
|
if($this->botInfo === true)
|
|
|
|
return 'a bot';
|
|
|
|
return $this->botInfo['name'] ?? 'an unknown bot';
|
2023-07-21 11:33:27 +00:00
|
|
|
}
|
|
|
|
|
2023-07-21 12:47:56 +00:00
|
|
|
if(empty($this->clientInfo['name']))
|
2023-07-21 11:33:27 +00:00
|
|
|
return 'an unknown browser';
|
|
|
|
|
2023-07-21 12:47:56 +00:00
|
|
|
$string = $this->clientInfo['name'];
|
|
|
|
if(!empty($this->clientInfo['version']))
|
|
|
|
$string .= ' ' . $this->clientInfo['version'];
|
2023-07-21 11:33:27 +00:00
|
|
|
|
2023-07-21 12:47:56 +00:00
|
|
|
$hasOsInfo = !empty($this->osInfo['name']);
|
|
|
|
$hasModelName = !empty($this->modelName);
|
2023-07-21 11:33:27 +00:00
|
|
|
|
|
|
|
if($hasOsInfo || $hasModelName)
|
|
|
|
$string .= ' on ';
|
|
|
|
|
|
|
|
if($hasModelName) {
|
2023-07-21 12:47:56 +00:00
|
|
|
$deviceName = trim($this->brandName . ' ' . $this->modelName);
|
2023-07-21 11:33:27 +00:00
|
|
|
// most naive check in the world but it works well enough for this lol
|
|
|
|
$firstCharIsVowel = in_array(strtolower($deviceName[0]), ['a', 'i', 'u', 'e', 'o']);
|
|
|
|
$string .= ($firstCharIsVowel ? 'an' : 'a') . ' ' . $deviceName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($hasOsInfo) {
|
|
|
|
if($hasModelName)
|
|
|
|
$string .= ' running ';
|
|
|
|
|
2023-07-21 12:47:56 +00:00
|
|
|
$string .= $this->osInfo['name'];
|
|
|
|
if(!empty($this->osInfo['version']))
|
|
|
|
$string .= ' ' . $this->osInfo['version'];
|
|
|
|
if(!empty($this->osInfo['platform']))
|
|
|
|
$string .= ' (' . $this->osInfo['platform'] . ')';
|
2023-07-21 11:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
2023-07-21 12:47:56 +00:00
|
|
|
public function encode(): string {
|
2023-07-22 16:37:57 +00:00
|
|
|
return json_encode($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function jsonSerialize(): mixed {
|
2023-07-21 12:47:56 +00:00
|
|
|
$data = new stdClass;
|
|
|
|
$data->version = self::SERIALIZE_VERSION;
|
|
|
|
|
|
|
|
if($this->botInfo === true || is_array($this->botInfo))
|
|
|
|
$data->bot = $this->botInfo;
|
|
|
|
if($this->clientInfo !== null)
|
|
|
|
$data->client = $this->clientInfo;
|
|
|
|
if($this->osInfo !== null)
|
|
|
|
$data->os = $this->osInfo;
|
|
|
|
if($this->brandName !== '')
|
|
|
|
$data->vendor = $this->brandName;
|
|
|
|
if($this->modelName !== '')
|
|
|
|
$data->model = $this->modelName;
|
|
|
|
|
2023-07-22 16:37:57 +00:00
|
|
|
return $data;
|
2023-07-21 12:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function decode(string $encoded): self {
|
|
|
|
$data = json_decode($encoded, true);
|
|
|
|
$version = $data['version'] ?? 0;
|
|
|
|
if($version < 0 || $version > self::SERIALIZE_VERSION)
|
|
|
|
throw new RuntimeException('$data does not contain a valid version argument');
|
|
|
|
|
2023-07-21 19:38:54 +00:00
|
|
|
return new ClientInfo(
|
2023-07-21 12:47:56 +00:00
|
|
|
$data['bot'] ?? null,
|
|
|
|
$data['client'] ?? null,
|
|
|
|
$data['os'] ?? null,
|
|
|
|
$data['vendor'] ?? '',
|
|
|
|
$data['model'] ?? ''
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-21 11:33:27 +00:00
|
|
|
public static function parse(array|string $serverVarsOrUserAgent): self {
|
2023-07-21 12:47:56 +00:00
|
|
|
static $dd = null;
|
|
|
|
$dd ??= new DeviceDetector();
|
|
|
|
|
2023-07-21 11:33:27 +00:00
|
|
|
if(is_string($serverVarsOrUserAgent)) {
|
2023-07-21 12:47:56 +00:00
|
|
|
$dd->setUserAgent($serverVarsOrUserAgent);
|
2023-07-21 11:33:27 +00:00
|
|
|
} else {
|
2023-07-21 12:47:56 +00:00
|
|
|
$dd->setUserAgent(
|
|
|
|
array_key_exists('HTTP_USER_AGENT', $serverVarsOrUserAgent)
|
|
|
|
? $serverVarsOrUserAgent['HTTP_USER_AGENT'] : ''
|
|
|
|
);
|
|
|
|
$dd->setClientHints(ClientHints::factory($serverVarsOrUserAgent));
|
2023-07-21 11:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$dd->parse();
|
|
|
|
|
2023-07-21 19:38:54 +00:00
|
|
|
return new ClientInfo(
|
2023-07-21 12:47:56 +00:00
|
|
|
$dd->getBot(),
|
|
|
|
$dd->getClient(),
|
|
|
|
$dd->getOs(),
|
|
|
|
$dd->getBrandName(),
|
|
|
|
$dd->getModel()
|
|
|
|
);
|
2023-07-21 11:33:27 +00:00
|
|
|
}
|
2023-07-22 16:37:57 +00:00
|
|
|
|
|
|
|
public static function fromRequest(): self {
|
|
|
|
return self::parse($_SERVER);
|
|
|
|
}
|
2023-07-21 11:33:27 +00:00
|
|
|
}
|