2021-02-15 06:28:54 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Detector;
|
|
|
|
|
|
|
|
use DeviceDetector\DeviceDetector;
|
|
|
|
|
|
|
|
class Detector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
protected $userAgent = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DeviceDetector
|
|
|
|
*/
|
|
|
|
protected $detctor;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $userAgent
|
|
|
|
*/
|
|
|
|
public function __construct(string $userAgent)
|
|
|
|
{
|
|
|
|
$this->userAgent = $userAgent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get OS info
|
2021-10-06 19:08:54 +13:00
|
|
|
*
|
2021-02-15 06:28:54 +13:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getOS(): array
|
|
|
|
{
|
|
|
|
$os = $this->getDetector()->getOs();
|
|
|
|
|
|
|
|
return [
|
|
|
|
'osCode' => (isset($os['short_name'])) ? $os['short_name'] : '',
|
|
|
|
'osName' => (isset($os['name'])) ? $os['name'] : '',
|
|
|
|
'osVersion' => (isset($os['version'])) ? $os['version'] : '',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get client info
|
2021-10-06 19:08:54 +13:00
|
|
|
*
|
2021-02-15 06:28:54 +13:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getClient(): array
|
|
|
|
{
|
2021-12-13 23:37:05 +13:00
|
|
|
if (strpos($this->userAgent, 'Appwrite CLI') !== false) {
|
2021-12-13 06:46:41 +13:00
|
|
|
$version = explode(' ', $this->userAgent)[0];
|
|
|
|
$version = explode('/', $version)[1];
|
|
|
|
$client = [
|
|
|
|
'type' => 'desktop',
|
2021-12-14 00:19:47 +13:00
|
|
|
'short_name' => 'cli',
|
|
|
|
'name' => 'Appwrite CLI',
|
2021-12-13 06:46:41 +13:00
|
|
|
'version' => $version
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$client = $this->getDetector()->getClient();
|
|
|
|
}
|
2021-02-15 06:28:54 +13:00
|
|
|
|
|
|
|
return [
|
|
|
|
'clientType' => (isset($client['type'])) ? $client['type'] : '',
|
|
|
|
'clientCode' => (isset($client['short_name'])) ? $client['short_name'] : '',
|
|
|
|
'clientName' => (isset($client['name'])) ? $client['name'] : '',
|
|
|
|
'clientVersion' => (isset($client['version'])) ? $client['version'] : '',
|
|
|
|
'clientEngine' => (isset($client['engine'])) ? $client['engine'] : '',
|
|
|
|
'clientEngineVersion' => (isset($client['engine_version'])) ? $client['engine_version'] : '',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get device info
|
2021-10-06 19:08:54 +13:00
|
|
|
*
|
2021-02-15 06:28:54 +13:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getDevice(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'deviceName' => $this->getDetector()->getDeviceName(),
|
|
|
|
'deviceBrand' => $this->getDetector()->getBrandName(),
|
|
|
|
'deviceModel' => $this->getDetector()->getModel(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DeviceDetector
|
|
|
|
*/
|
|
|
|
protected function getDetector(): DeviceDetector
|
|
|
|
{
|
2021-10-06 19:08:54 +13:00
|
|
|
if (!$this->detctor) {
|
2021-02-15 06:28:54 +13:00
|
|
|
$this->detctor = new DeviceDetector($this->userAgent);
|
|
|
|
$this->detctor->skipBotDetection(); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then)
|
|
|
|
$this->detctor->parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->detctor;
|
|
|
|
}
|
2021-12-13 06:55:38 +13:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether to skip bot detection.
|
|
|
|
* It is needed if we want bots to be processed as a simple clients. So we can detect if it is mobile client,
|
|
|
|
* or desktop, or enything else. By default all this information is not retrieved for the bots.
|
|
|
|
*
|
|
|
|
* @param bool $skip
|
|
|
|
*/
|
|
|
|
public function skipBotDetection(bool $skip = true): void
|
|
|
|
{
|
|
|
|
$this->getDetector()->skipBotDetection($skip);
|
|
|
|
}
|
2021-02-15 06:28:54 +13:00
|
|
|
}
|