MobileDeviceDetection.php in Mobile Device Detection 8.3
File
src/Object/MobileDeviceDetection.php
View source
<?php
namespace Drupal\mobile_device_detection\Object;
use Symfony\Component\HttpFoundation\RequestStack;
class MobileDeviceDetection {
private $attributes;
public $request;
private $mobileHeaders;
private $cloudHeaders;
private $userAgentHeaders;
private $object;
public function __construct($attributes, $request) {
$this
->setAttributes($attributes);
$this
->setRequest($request);
$this
->init();
}
private function init() {
$this->object = new \stdClass();
$this->object->type = NULL;
$headers = $this
->getRequest()
->getCurrentRequest()->server
->all();
$this
->setMobileHeaders($headers);
$this
->setCloudHeaders($headers);
$this
->setUserAgentHeaders($this
->getAttributes()
->get('user_agent_headers'));
if ($this
->check('mobile')) {
$this->object->type = 'mobile';
}
if ($this
->check('tablet')) {
$this->object->type = 'tablet';
}
}
public function getObject() {
if (isset($this->object->type)) {
$this
->getOperatingSystem();
$this
->getBrowser();
return $this->object;
}
}
public function isMobile() {
return $this->object->type === 'mobile' ? TRUE : FALSE;
}
public function isTablet() {
return $this->object->type === 'tablet' ? TRUE : FALSE;
}
protected function setAttributes($attributes) {
$this->attributes = $attributes;
}
protected function getAttributes() {
return $this->attributes;
}
protected function setRequest($request) {
$this->request = $request;
}
protected function getRequest() {
return $this->request;
}
protected function setMobileHeaders($headers) {
array_walk($headers, function (&$v, $k) {
if (substr($k, 0, 5) === 'HTTP_') {
$this->mobileHeaders[$k] = $v;
}
});
}
protected function getMobileHeaders() {
return $this->mobileHeaders;
}
protected function setCloudHeaders($headers) {
array_walk($headers, function (&$v, $k) {
if (substr(strtolower($k), 0, 16) === 'http_cloudfront_') {
$this->cloudHeaders[strtoupper($k)] = $v;
}
});
}
protected function getCloudHeaders() {
return $this->cloudHeaders;
}
protected function setUserAgentHeaders($headers) {
$this->userAgentHeaders = implode(' ', array_intersect_key($this
->getMobileHeaders(), array_flip($headers)));
if (!$this->userAgentHeaders && !empty($this
->getCloudHeaders())) {
$this->userAgentHeaders = 'Amazon CloudFront';
}
}
protected function getUserAgentHeaders() {
return $this->userAgentHeaders;
}
private function check($type) {
if ($this
->getUserAgentHeaders() === 'Amazon CloudFront') {
$headers = $this
->setCloudHeaders($this
->getRequest()
->getCurrentRequest()->server
->all());
if (array_key_exists('HTTP_CLOUDFRONT_IS_MOBILE_VIEWER', $headers) && $headers['HTTP_CLOUDFRONT_IS_MOBILE_VIEWER'] === 'true') {
return TRUE;
}
}
switch ($type) {
case 'mobile':
$options = array_merge($this
->getAttributes()
->get('phone_devices'));
$headers = array_intersect_key($this
->getAttributes()
->get('mobile_headers'), $this
->getMobileHeaders());
foreach ($headers as $key => $value) {
foreach ($value as $v) {
if (strpos($this
->getMobileHeaders()[$key], $v) !== FALSE) {
return TRUE;
}
}
}
goto device_detect;
break;
case 'tablet':
$options = array_merge($this
->getAttributes()
->get('tablet_devices'));
case 'deviceDetect':
device_detect:
$device = false;
foreach ($options as $value) {
if (!empty($value)) {
if ($this
->match($value)) {
$device = TRUE;
}
}
}
if ($device) {
foreach ($this
->getAttributes()
->get('browsers') as $value) {
if (!empty($value)) {
if ($this
->match($value)) {
return TRUE;
}
}
}
}
break;
}
return FALSE;
}
private function getOperatingSystem() {
$this->object->OS = $this
->get($this
->getAttributes()
->get('operating_systems'));
$this
->version($this->object->OS);
}
private function getBrowser() {
$this->object->browser = $this
->get($this
->getAttributes()
->get('browsers'));
$this
->version($this->object->browser);
}
private function get($options) {
foreach ($options as $key => $value) {
if (!empty($value)) {
if ($this
->match($value)) {
return $key;
}
}
}
}
private function match($value) {
return (bool) preg_match(sprintf('#%s#is', $value), $this
->getUserAgentHeaders(), $matches);
}
private function version($name) {
$properties = (array) $this
->getAttributes()
->get('properties')[$name];
foreach ($properties as $value) {
$pattern = str_replace('[VER]', $this
->getAttributes()
->get('VER'), $value);
preg_match(sprintf('#%s#is', $pattern), $this
->getUserAgentHeaders(), $matches);
if (!empty($matches)) {
$this->object->{$name}[] = $matches;
}
}
}
}