You are here

MobileDeviceDetection.php in Mobile Device Detection 8.2

File

src/Object/MobileDeviceDetection.php
View source
<?php

namespace Drupal\mobile_device_detection\Object;


/**
 * MobileDeviceDetection object.
 */
class MobileDeviceDetection {

  /**
   * A default attributes instance.
   *
   * @var \Drupal\mobile_device_detection\Object\MobileDeviceDetectionAttributes
   */
  private $attributes;

  /**
   * The MobileDeviceDetectionObject mobileHeaders.
   *
   * @var array
   */
  private $mobileHeaders;

  /**
   * The MobileDeviceDetectionObject cloudHeaders.
   *
   * @var array
   */
  private $cloudHeaders;

  /**
   * The MobileDeviceDetectionObject userAgentHeaders.
   *
   * @var array
   */
  private $userAgentHeaders;

  /**
   * The MobileDeviceDetectionObject object.
   *
   * @var object
   */
  private $object;

  /**
   * The constructoror.
   */
  public function __construct($attributes) {
    $this
      ->setAttributes($attributes);
    $this
      ->init();
  }

  /**
   * {@inheritdoc}
   */
  private function init() {
    $this->object = new \stdClass();
    $this->object->type = NULL;
    $this
      ->setMobileHeaders(\Drupal::request()->server
      ->all());
    $this
      ->setCloudHeaders(\Drupal::request()->server
      ->all());
    $this
      ->setUserAgentHeaders($this
      ->getAttributes()
      ->get('user_agent_headers'));
    if ($this
      ->check('mobile')) {
      $this->object->type = 'mobile';
    }
    if ($this
      ->check('tablet')) {
      $this->object->type = 'tablet';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getObject() {
    if (isset($this->object->type)) {
      $this
        ->getOperatingSystem();
      $this
        ->getBrowser();
      return $this->object;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function isMobile() {
    return $this->object->type === 'mobile' ? TRUE : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isTablet() {
    return $this->object->type === 'tablet' ? TRUE : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  protected function setAttributes($attributes) {
    $this->attributes = $attributes;
  }

  /**
   * {@inheritdoc}
   */
  protected function getAttributes() {
    return $this->attributes;
  }

  /**
   * {@inheritdoc}
   */
  protected function setMobileHeaders($headers) {
    array_walk($headers, function (&$v, $k) {
      if (substr($k, 0, 5) === 'HTTP_') {
        $this->mobileHeaders[$k] = $v;
      }
    });
  }

  /**
   * {@inheritdoc}
   */
  protected function getMobileHeaders() {
    return $this->mobileHeaders;
  }

  /**
   * {@inheritdoc}
   */
  protected function setCloudHeaders($headers) {
    array_walk($headers, function (&$v, $k) {
      if (substr(strtolower($k), 0, 16) === 'http_cloudfront_') {
        $this->cloudHeaders[strtoupper($k)] = $v;
      }
    });
  }

  /**
   * {@inheritdoc}
   */
  protected function getCloudHeaders() {
    return $this->cloudHeaders;
  }

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

  /**
   * {@inheritdoc}
   */
  protected function getUserAgentHeaders() {
    return $this->userAgentHeaders;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  private function getOperatingSystem() {
    $this->object->OS = $this
      ->get($this
      ->getAttributes()
      ->get('operating_systems'));
    $this
      ->version($this->object->OS);
  }

  /**
   * {@inheritdoc}
   */
  private function getBrowser() {
    $this->object->browser = $this
      ->get($this
      ->getAttributes()
      ->get('browsers'));
    $this
      ->version($this->object->browser);
  }

  /**
   * {@inheritdoc}
   */
  private function get($options) {
    foreach ($options as $key => $value) {
      if (!empty($value)) {
        if ($this
          ->match($value)) {
          return $key;
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  private function match($value) {
    return (bool) preg_match(sprintf('#%s#is', $value), $this
      ->getUserAgentHeaders(), $matches);
  }

  /**
   * {@inheritdoc}
   */
  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;
      }
    }
  }

}

Classes

Namesort descending Description
MobileDeviceDetection MobileDeviceDetection object.