From 3d863d634a0ae44a274df6710551edd5e185551c Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Sun, 12 Dec 2021 21:55:38 +0400 Subject: [PATCH] feat: use Appwrite\Detector in users.php --- app/controllers/api/users.php | 49 +++++++++++------------------- src/Appwrite/Detector/Detector.php | 12 ++++++++ 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index a5d412d5b..229336fb9 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -15,7 +15,7 @@ use Utopia\Audit\Audit; use Utopia\Database\Document; use Utopia\Database\Exception\Duplicate; use Utopia\Database\Validator\UID; -use DeviceDetector\DeviceDetector; +use Appwrite\Detector\Detector; use Appwrite\Database\Validator\CustomId; use Utopia\Config\Config; use Utopia\Database\Database; @@ -306,42 +306,29 @@ App::get('/v1/users/:userId/logs') foreach ($logs as $i => &$log) { $log['userAgent'] = (!empty($log['userAgent'])) ? $log['userAgent'] : 'UNKNOWN'; - $dd = new DeviceDetector($log['userAgent']); + $detector = new Detector($log['userAgent']); + $detector->skipBotDetection(); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then) - $dd->skipBotDetection(); // OPTIONAL: If called, bot detection will completely be skipped (bots will be detected as regular devices then) - - $dd->parse(); - - $os = $dd->getOs(); - $osCode = (isset($os['short_name'])) ? $os['short_name'] : ''; - $osName = (isset($os['name'])) ? $os['name'] : ''; - $osVersion = (isset($os['version'])) ? $os['version'] : ''; - - $client = $dd->getClient(); - $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'] : ''; + $os = $detector->getOS(); + $client = $detector->getClient(); + $device = $detector->getDevice(); $output[$i] = new Document([ 'event' => $log['event'], 'ip' => $log['ip'], 'time' => $log['time'], - - 'osCode' => $osCode, - 'osName' => $osName, - 'osVersion' => $osVersion, - 'clientType' => $clientType, - 'clientCode' => $clientCode, - 'clientName' => $clientName, - 'clientVersion' => $clientVersion, - 'clientEngine' => $clientEngine, - 'clientEngineVersion' => $clientEngineVersion, - 'deviceName' => $dd->getDeviceName(), - 'deviceBrand' => $dd->getBrandName(), - 'deviceModel' => $dd->getModel(), + 'osCode' => $os['osCode'], + 'osName' => $os['osName'], + 'osVersion' => $os['osVersion'], + 'clientType' => $client['clientType'], + 'clientCode' => $client['clientCode'], + 'clientName' => $client['clientName'], + 'clientVersion' => $client['clientVersion'], + 'clientEngine' => $client['clientEngine'], + 'clientEngineVersion' => $client['clientEngineVersion'], + 'deviceName' => $device['deviceName'], + 'deviceBrand' => $device['deviceBrand'], + 'deviceModel' => $device['deviceModel'], ]); $record = $geodb->get($log['ip']); diff --git a/src/Appwrite/Detector/Detector.php b/src/Appwrite/Detector/Detector.php index 95daaba59..3e1aa0f3d 100644 --- a/src/Appwrite/Detector/Detector.php +++ b/src/Appwrite/Detector/Detector.php @@ -97,4 +97,16 @@ class Detector 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); + } }