MobileDeviceDetectionObject.php in Mobile Device Detection 8
File
src/Object/MobileDeviceDetectionObject.php
View source
<?php
namespace Drupal\mobile_device_detection\Object;
use Drupal\mobile_device_detection\Object\MobileDeviceDetectionAttributes;
class MobileDeviceDetectionObject {
private $attributes;
private $mobile_headers;
private $cloud_headers;
private $user_agent_headers;
private $object;
public function __construct(MobileDeviceDetectionAttributes $attributes) {
$this
->setAttributes($attributes);
$this
->init();
}
private function init() {
$this
->setMobileHeaders(\Drupal::request()->server
->all());
$this
->setCloudHeaders(\Drupal::request()->server
->all());
$this
->setUserAgentHeaders($this
->getAttributes()
->get('user_agent_headers'));
$this->object->type = null;
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(MobileDeviceDetectionAttributes $attributes) {
$this->attributes = $attributes;
}
protected function getAttributes() {
return $this->attributes;
}
protected function setMobileHeaders($headers) {
array_walk($headers, function (&$v, $k) {
if (substr($k, 0, 5) === 'HTTP_') {
$this->mobile_headers[$k] = $v;
}
});
}
protected function getMobileHeaders() {
return $this->mobile_headers;
}
protected function setCloudHeaders($headers) {
array_walk($headers, function (&$v, $k) {
if (substr(strtolower($k), 0, 16) === 'http_cloudfront_') {
$this->cloud_headers[strtoupper($k)] = $v;
}
});
}
protected function getCloudHeaders() {
return $this->cloud_headers;
}
protected function setUserAgentHeaders($headers) {
$this->user_agent_headers = join(' ', array_intersect_key($this
->getMobileHeaders(), array_flip($headers)));
if (!$this->user_agent_headers && !empty($this
->getCloudHeaders())) {
$this->user_agent_headers = 'Amazon CloudFront';
}
}
protected function getUserAgentHeaders() {
return $this->user_agent_headers;
}
private function check($type) {
if ($this
->getUserAgentHeaders() === 'Amazon CloudFront') {
$headers = $this
->setCloudHeaders();
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:
foreach ($options 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;
}
}
}
}