Switch to DeviceDetector library.
This commit is contained in:
parent
58b9781b20
commit
80bf66a337
|
@ -40,11 +40,11 @@
|
|||
"league/mime-type-detection": "^1.5",
|
||||
"league/plates": "^3.1",
|
||||
"lstrojny/fxmlrpc": "dev-master",
|
||||
"matomo/device-detector": "^4.0",
|
||||
"maxmind-db/reader": "~1.0",
|
||||
"mezzio/mezzio-session": "^1.3",
|
||||
"mezzio/mezzio-session-cache": "^1.4",
|
||||
"mnapoli/silly-php-di": "^1.2",
|
||||
"mobiledetect/mobiledetectlib": "^2.8",
|
||||
"monolog/monolog": "^2",
|
||||
"myclabs/deep-copy": "^1.10",
|
||||
"nesbot/carbon": "^2.36",
|
||||
|
|
|
@ -4,20 +4,20 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "e308b78d2f128f2a11789a205aefae53",
|
||||
"content-hash": "152d1c444de811053c2d92c6b87881ab",
|
||||
"packages": [
|
||||
{
|
||||
"name": "aws/aws-sdk-php",
|
||||
"version": "3.168.3",
|
||||
"version": "3.171.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/aws/aws-sdk-php.git",
|
||||
"reference": "49ef1f905388c8185012c9651b80941b8f2a218d"
|
||||
"reference": "01407effc918634048346d8bd6513b2db0d07ad8"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/49ef1f905388c8185012c9651b80941b8f2a218d",
|
||||
"reference": "49ef1f905388c8185012c9651b80941b8f2a218d",
|
||||
"url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/01407effc918634048346d8bd6513b2db0d07ad8",
|
||||
"reference": "01407effc918634048346d8bd6513b2db0d07ad8",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -92,9 +92,9 @@
|
|||
"support": {
|
||||
"forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
|
||||
"issues": "https://github.com/aws/aws-sdk-php/issues",
|
||||
"source": "https://github.com/aws/aws-sdk-php/tree/3.168.3"
|
||||
"source": "https://github.com/aws/aws-sdk-php/tree/3.171.1"
|
||||
},
|
||||
"time": "2020-12-11T19:12:18+00:00"
|
||||
"time": "2020-12-17T19:16:51+00:00"
|
||||
},
|
||||
{
|
||||
"name": "azuracast/azuraforms",
|
||||
|
@ -2377,12 +2377,12 @@
|
|||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/JamesHeinrich/getID3.git",
|
||||
"reference": "475a5291509471d6b087d62b139c60be6fbdd5d1"
|
||||
"reference": "f199994d797754cac398c35240732ba235f5c2fa"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/JamesHeinrich/getID3/zipball/475a5291509471d6b087d62b139c60be6fbdd5d1",
|
||||
"reference": "475a5291509471d6b087d62b139c60be6fbdd5d1",
|
||||
"url": "https://api.github.com/repos/JamesHeinrich/getID3/zipball/f199994d797754cac398c35240732ba235f5c2fa",
|
||||
"reference": "f199994d797754cac398c35240732ba235f5c2fa",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -2437,7 +2437,7 @@
|
|||
"issues": "https://github.com/JamesHeinrich/getID3/issues",
|
||||
"source": "https://github.com/JamesHeinrich/getID3/tree/master"
|
||||
},
|
||||
"time": "2020-12-09T16:21:59+00:00"
|
||||
"time": "2020-12-15T15:18:21+00:00"
|
||||
},
|
||||
{
|
||||
"name": "jhofm/flysystem-iterator",
|
||||
|
@ -3212,6 +3212,75 @@
|
|||
},
|
||||
"time": "2020-11-23T14:42:43+00:00"
|
||||
},
|
||||
{
|
||||
"name": "matomo/device-detector",
|
||||
"version": "4.0.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/matomo-org/device-detector.git",
|
||||
"reference": "20572bbf8607cfc2f91a11fc75e9a3c6592bdc7a"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/matomo-org/device-detector/zipball/20572bbf8607cfc2f91a11fc75e9a3c6592bdc7a",
|
||||
"reference": "20572bbf8607cfc2f91a11fc75e9a3c6592bdc7a",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"mustangostang/spyc": "*",
|
||||
"php": ">=7.2"
|
||||
},
|
||||
"replace": {
|
||||
"piwik/device-detector": "self.version"
|
||||
},
|
||||
"require-dev": {
|
||||
"matthiasmullie/scrapbook": "@stable",
|
||||
"mayflower/mo4-coding-standard": "dev-master#275cb9d",
|
||||
"phpstan/phpstan": "^0.12.52",
|
||||
"phpunit/phpunit": "^8.5.8",
|
||||
"psr/cache": "^1.0.1",
|
||||
"psr/simple-cache": "^1.0.1",
|
||||
"symfony/yaml": "^5.1.7"
|
||||
},
|
||||
"suggest": {
|
||||
"doctrine/cache": "Can directly be used for caching purpose",
|
||||
"ext-yaml": "Necessary for using the Pecl YAML parser"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"DeviceDetector\\": ""
|
||||
},
|
||||
"exclude-from-classmap": [
|
||||
"Tests/"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"LGPL-3.0-or-later"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "The Matomo Team",
|
||||
"email": "hello@matomo.org",
|
||||
"homepage": "https://matomo.org/team/"
|
||||
}
|
||||
],
|
||||
"description": "The Universal Device Detection library, that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, media players, mobile apps, feed readers, libraries, etc), operating systems, devices, brands and models.",
|
||||
"homepage": "https://matomo.org",
|
||||
"keywords": [
|
||||
"devicedetection",
|
||||
"parser",
|
||||
"useragent"
|
||||
],
|
||||
"support": {
|
||||
"forum": "http://forum.matomo.org/",
|
||||
"issues": "https://github.com/matomo-org/device-detector/issues",
|
||||
"source": "https://github.com/matomo-org/piwik",
|
||||
"wiki": "https://dev.matomo.org/"
|
||||
},
|
||||
"time": "2020-12-07T09:40:38+00:00"
|
||||
},
|
||||
{
|
||||
"name": "maxmind-db/reader",
|
||||
"version": "v1.8.0",
|
||||
|
@ -3515,62 +3584,6 @@
|
|||
},
|
||||
"time": "2018-04-05T08:53:27+00:00"
|
||||
},
|
||||
{
|
||||
"name": "mobiledetect/mobiledetectlib",
|
||||
"version": "2.8.34",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/serbanghita/Mobile-Detect.git",
|
||||
"reference": "6f8113f57a508494ca36acbcfa2dc2d923c7ed5b"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/serbanghita/Mobile-Detect/zipball/6f8113f57a508494ca36acbcfa2dc2d923c7ed5b",
|
||||
"reference": "6f8113f57a508494ca36acbcfa2dc2d923c7ed5b",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.0.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.8.35||~5.7"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"classmap": [
|
||||
"Mobile_Detect.php"
|
||||
],
|
||||
"psr-0": {
|
||||
"Detection": "namespaced/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Serban Ghita",
|
||||
"email": "serbanghita@gmail.com",
|
||||
"homepage": "http://mobiledetect.net",
|
||||
"role": "Developer"
|
||||
}
|
||||
],
|
||||
"description": "Mobile_Detect is a lightweight PHP class for detecting mobile devices. It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.",
|
||||
"homepage": "https://github.com/serbanghita/Mobile-Detect",
|
||||
"keywords": [
|
||||
"detect mobile devices",
|
||||
"mobile",
|
||||
"mobile detect",
|
||||
"mobile detector",
|
||||
"php mobile detect"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/serbanghita/Mobile-Detect/issues",
|
||||
"source": "https://github.com/serbanghita/Mobile-Detect/tree/2.8.34"
|
||||
},
|
||||
"time": "2019-09-18T18:44:20+00:00"
|
||||
},
|
||||
{
|
||||
"name": "monolog/monolog",
|
||||
"version": "2.2.0",
|
||||
|
@ -3728,6 +3741,56 @@
|
|||
},
|
||||
"time": "2020-07-31T21:01:56+00:00"
|
||||
},
|
||||
{
|
||||
"name": "mustangostang/spyc",
|
||||
"version": "0.6.3",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "git@github.com:mustangostang/spyc.git",
|
||||
"reference": "4627c838b16550b666d15aeae1e5289dd5b77da0"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/mustangostang/spyc/zipball/4627c838b16550b666d15aeae1e5289dd5b77da0",
|
||||
"reference": "4627c838b16550b666d15aeae1e5289dd5b77da0",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=5.3.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "4.3.*@dev"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "0.5.x-dev"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
"Spyc.php"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "mustangostang",
|
||||
"email": "vlad.andersen@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "A simple YAML loader/dumper class for PHP",
|
||||
"homepage": "https://github.com/mustangostang/spyc/",
|
||||
"keywords": [
|
||||
"spyc",
|
||||
"yaml",
|
||||
"yml"
|
||||
],
|
||||
"time": "2019-09-10T13:16:29+00:00"
|
||||
},
|
||||
{
|
||||
"name": "myclabs/deep-copy",
|
||||
"version": "1.10.2",
|
||||
|
@ -9866,16 +9929,16 @@
|
|||
},
|
||||
{
|
||||
"name": "phpstan/phpstan",
|
||||
"version": "0.12.62",
|
||||
"version": "0.12.63",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/phpstan/phpstan.git",
|
||||
"reference": "632393159335bbbdd7ca07d19b3ad50d76aa7fd8"
|
||||
"reference": "c97ec4754bd53099a06c24847bd2870b99966b6a"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/632393159335bbbdd7ca07d19b3ad50d76aa7fd8",
|
||||
"reference": "632393159335bbbdd7ca07d19b3ad50d76aa7fd8",
|
||||
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/c97ec4754bd53099a06c24847bd2870b99966b6a",
|
||||
"reference": "c97ec4754bd53099a06c24847bd2870b99966b6a",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -9906,7 +9969,7 @@
|
|||
"description": "PHPStan - PHP Static Analysis Tool",
|
||||
"support": {
|
||||
"issues": "https://github.com/phpstan/phpstan/issues",
|
||||
"source": "https://github.com/phpstan/phpstan/tree/0.12.62"
|
||||
"source": "https://github.com/phpstan/phpstan/tree/0.12.63"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
|
@ -9922,7 +9985,7 @@
|
|||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2020-12-13T13:59:38+00:00"
|
||||
"time": "2020-12-15T16:37:16+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phpstan/phpstan-doctrine",
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace App\Controller\Api\Stations;
|
|||
use App\Entity;
|
||||
use App\Http\Response;
|
||||
use App\Http\ServerRequest;
|
||||
use App\Service\DeviceDetector;
|
||||
use App\Service\IpGeolocation;
|
||||
use App\Utilities\Csv;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
@ -19,10 +20,13 @@ class ListenersController
|
|||
|
||||
protected IpGeolocation $geoLite;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, IpGeolocation $geoLite)
|
||||
protected DeviceDetector $deviceDetector;
|
||||
|
||||
public function __construct(EntityManagerInterface $em, IpGeolocation $geoLite, DeviceDetector $deviceDetector)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->geoLite = $geoLite;
|
||||
$this->deviceDetector = $deviceDetector;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,7 +131,6 @@ class ListenersController
|
|||
}
|
||||
}
|
||||
|
||||
$detect = new Mobile_Detect();
|
||||
$locale = $request->getAttribute('locale');
|
||||
|
||||
$format = $params['format'] ?? 'json';
|
||||
|
@ -153,7 +156,7 @@ class ListenersController
|
|||
(string)$listener['listener_ip'],
|
||||
Entity\Listener::getListenerSeconds($listener['intervals']),
|
||||
(string)$listener['listener_user_agent'],
|
||||
$detect->isMobile($listener['listener_user_agent']) ? 'true' : 'false',
|
||||
$this->isMobile($listener['listener_user_agent']) ? 'true' : 'false',
|
||||
];
|
||||
|
||||
if ('success' === $location['status']) {
|
||||
|
@ -182,7 +185,7 @@ class ListenersController
|
|||
$api = new Entity\Api\Listener();
|
||||
$api->ip = (string)$listener['listener_ip'];
|
||||
$api->user_agent = (string)$listener['listener_user_agent'];
|
||||
$api->is_mobile = $detect->isMobile($listener['listener_user_agent']);
|
||||
$api->is_mobile = $this->isMobile($listener['listener_user_agent']);
|
||||
$api->connected_on = (int)$listener['timestamp_start'];
|
||||
$api->connected_time = Entity\Listener::getListenerSeconds($listener['intervals']);
|
||||
$api->location = $this->geoLite->getLocationInfo($listener['listener_ip'], $locale);
|
||||
|
@ -192,4 +195,10 @@ class ListenersController
|
|||
|
||||
return $response->withJson($listeners);
|
||||
}
|
||||
|
||||
protected function isMobile(string $userAgent): bool
|
||||
{
|
||||
$dd = $this->deviceDetector->parse($userAgent);
|
||||
return $dd->isMobile();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ use App\Entity;
|
|||
use App\Environment;
|
||||
use App\Exception;
|
||||
use App\Radio\AutoDJ;
|
||||
use App\Service\DeviceDetector;
|
||||
use Carbon\CarbonImmutable;
|
||||
use Carbon\CarbonInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
@ -17,16 +18,20 @@ class StationRequestRepository extends Repository
|
|||
{
|
||||
protected StationMediaRepository $mediaRepo;
|
||||
|
||||
protected DeviceDetector $deviceDetector;
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
Serializer $serializer,
|
||||
Environment $environment,
|
||||
LoggerInterface $logger,
|
||||
StationMediaRepository $mediaRepo
|
||||
StationMediaRepository $mediaRepo,
|
||||
DeviceDetector $deviceDetector
|
||||
) {
|
||||
parent::__construct($em, $serializer, $environment, $logger);
|
||||
|
||||
$this->mediaRepo = $mediaRepo;
|
||||
$this->deviceDetector = $deviceDetector;
|
||||
}
|
||||
|
||||
public function submit(
|
||||
|
@ -37,7 +42,9 @@ class StationRequestRepository extends Repository
|
|||
string $userAgent
|
||||
): int {
|
||||
// Forbid web crawlers from using this feature.
|
||||
if ($this->isCrawler($userAgent)) {
|
||||
$dd = $this->deviceDetector->parse($userAgent);
|
||||
|
||||
if ($dd->isBot()) {
|
||||
throw new Exception(__('Search engine crawlers are not permitted to use this feature.'));
|
||||
}
|
||||
|
||||
|
@ -100,26 +107,6 @@ class StationRequestRepository extends Repository
|
|||
return $record->getId();
|
||||
}
|
||||
|
||||
protected function isCrawler(string $userAgent): bool
|
||||
{
|
||||
$userAgent = strtolower($userAgent);
|
||||
|
||||
// phpcs:disable Generic.Files.LineLength
|
||||
$crawlers_agents = strtolower(
|
||||
'Bloglines subscriber|Dumbot|Sosoimagespider|QihooBot|FAST-WebCrawler|Superdownloads Spiderman|LinkWalker|msnbot|ASPSeek|WebAlta Crawler|Lycos|FeedFetcher-Google|Yahoo|YoudaoBot|AdsBot-Google|Googlebot|Scooter|Gigabot|Charlotte|eStyle|AcioRobot|GeonaBot|msnbot-media|Baidu|CocoCrawler|Google|Charlotte t|Yahoo! Slurp China|Sogou web spider|YodaoBot|MSRBOT|AbachoBOT|Sogou head spider|AltaVista|IDBot|Sosospider|Yahoo! Slurp|Java VM|DotBot|LiteFinder|Yeti|Rambler|Scrubby|Baiduspider|accoona'
|
||||
);
|
||||
// phpcs:enable
|
||||
$crawlers = explode('|', $crawlers_agents);
|
||||
|
||||
foreach ($crawlers as $crawler) {
|
||||
if (strpos($userAgent, trim($crawler)) !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the song is already enqueued as a request.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use DeviceDetector\Cache\CacheInterface;
|
||||
use DeviceDetector\Cache\PSR6Bridge;
|
||||
use Psr\Cache\CacheItemPoolInterface;
|
||||
use Symfony\Component\Cache\Adapter\ProxyAdapter;
|
||||
|
||||
class DeviceDetector
|
||||
{
|
||||
protected CacheInterface $cache;
|
||||
|
||||
public function __construct(CacheItemPoolInterface $cache)
|
||||
{
|
||||
$wrappedCache = new ProxyAdapter($cache, 'device.');
|
||||
$this->cache = new PSR6Bridge($wrappedCache);
|
||||
}
|
||||
|
||||
public function parse(string $userAgent): \DeviceDetector\DeviceDetector
|
||||
{
|
||||
$dd = new \DeviceDetector\DeviceDetector($userAgent);
|
||||
$dd->setCache($this->cache);
|
||||
$dd->parse();
|
||||
|
||||
return $dd;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue