You are here

context_mobile_detect.module in Context Mobile Detect 7

Same filename and directory in other branches
  1. 7.2 context_mobile_detect.module

File

context_mobile_detect.module
View source
<?php

define('MOBILE_DETECT_LIBRARY_PATH', '/sites/all/libraries/Mobile_Detect');

/**
 * Implements hook_menu().
 */
function context_mobile_detect_menu() {
  $items['admin/structure/context/settings/mobile-detect'] = array(
    'title' => 'Mobile Detect Settings',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer site configuration',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'context_mobile_detect_settings',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'context_mobile_detect.admin.inc',
    'weight' => 4,
  );
  return $items;
}

/**
 * Implements hook_context_plugins().
 */
function context_mobile_detect_context_plugins() {
  $plugins = array();
  $plugins['cmd_condition'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'context_mobile_detect') . '/plugins',
      'file' => 'context_mobile_detect_condition.inc',
      'class' => 'cmd_condition',
      'parent' => 'context_condition',
    ),
  );
  $plugins['cmd_type_condition'] = array(
    'handler' => array(
      'path' => drupal_get_path('module', 'context_mobile_detect') . '/plugins',
      'file' => 'context_mobile_detect_type_condition.inc',
      'class' => 'cmd_type_condition',
      'parent' => 'context_condition',
    ),
  );
  return $plugins;
}

/**
 * Implements hook_context_registry().
 */
function context_mobile_detect_context_registry() {
  return array(
    'conditions' => array(
      'cmd' => array(
        'title' => t('Mobile Device'),
        'plugin' => 'cmd_condition',
      ),
      'cmd_type' => array(
        'title' => t('Mobile Device Type'),
        'plugin' => 'cmd_type_condition',
      ),
    ),
  );
}

/**
 * Allow over modules to alter condition.
 */
function context_mobile_detect_context_page_condition() {

  // Detect mobile device
  if ($plugin = context_get_plugin('condition', 'cmd')) {
    $plugin
      ->execute();
  }

  // Detect mobile device type
  if ($plugin = context_get_plugin('condition', 'cmd_type')) {
    $plugin
      ->execute();
  }
}
function _context_mobile_detect_detect($setcookie = FALSE) {
  $data = array(
    'device' => 3,
    // Set default value to Desktop
    'device_type' => 0,
  );
  if (!isset($_COOKIE['device']) || !is_numeric($_COOKIE['device']) || $_COOKIE['device'] == 0) {
    require_once DRUPAL_ROOT . rtrim(variable_get('cmd_mobile_detect_library_path', MOBILE_DETECT_LIBRARY_PATH), '/') . '/Mobile_Detect.php';
    $detect = new Mobile_Detect();
    $data['device'] = $detect
      ->isMobile() ? $detect
      ->isTablet() ? 2 : 1 : 3;
    $types = _context_mobile_detect_devices_types();

    // Suppose that only one or none of functions will response TRUE
    // TODO: investigate it later and use some sort of sanitize to store data in COOKIEs
    foreach ($types as $key => $type) {
      $data['device_type'] = $detect
        ->{'is' . $type}() ? $key : 0;
      if ($data['device_type']) {
        break;
      }
    }
    if ($setcookie) {
      $params = session_get_cookie_params();
      setcookie("device", $data['device'], REQUEST_TIME + 7200, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
      setcookie("device_type", $data['device_type'], REQUEST_TIME + 7200, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
    }
  }
  else {
    $data['device'] = $_COOKIE['device'];
    $data['device_type'] = $_COOKIE['device_type'];
  }
  return $data;
}
function _context_mobile_detect_add_query_string($str) {
  if (isset($_SERVER['REQUEST_URI'])) {
    if (strpos($_SERVER['REQUEST_URI'], '?') === FALSE) {
      $_SERVER['REQUEST_URI'] .= '?' . $str;
    }
    else {
      $_SERVER['REQUEST_URI'] .= '&' . $str;
    }
  }

  // TODO: Non-apache not supported
}

/**
 * Implements hook_boot().
 */
function context_mobile_detect_boot() {
  $data = _context_mobile_detect_detect(TRUE);
  if ($data['device'] < 3 && $data['device'] > 0) {
    drupal_add_http_header('X-Mobile-Device', $data['device']);
    drupal_add_http_header('X-Mobile-Device-Type', $data['device_type']);
  }
}

/**
 * Called via settings.inc; needs to be added to settings.php manually.
 */
function context_mobile_detect_preboot() {
  $data = _context_mobile_detect_detect(FALSE);
  if ($data['device'] < 3 && $data['device'] > 0) {

    // Change page cache hash key
    _context_mobile_detect_add_query_string('device=' . $data['device'] . '&device_type=' . $data['device_type']);
  }
}

/**
 * Helper function for device types retrieving
 */
function _context_mobile_detect_devices_types() {
  $type = array();
  $data = array(
    'iPhone',
    'BlackBerry',
    'HTC',
    'Nexus',
    'DellStreak',
    'Motorola',
    'Samsung',
    'Sony',
    'Asus',
    'Palm',
    'GenericPhone',
  );
  foreach ($data as $type) {
    $types[strtolower($type)] = $type;
  }
  return $types;
}