1
0
Fork 0
mirror of synced 2024-06-29 11:40:45 +12:00
appwrite/src/Appwrite/Detector/Detector.php

113 lines
3.1 KiB
PHP
Raw Normal View History

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
{
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',
'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;
}
/**
* 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
}