You are here

function _context_mobile_detect_detect in Context Mobile Detect 7

Same name and namespace in other branches
  1. 7.2 context_mobile_detect.module \_context_mobile_detect_detect()
4 calls to _context_mobile_detect_detect()
cmd_condition::execute in plugins/context_mobile_detect_condition.inc
cmd_type_condition::execute in plugins/context_mobile_detect_type_condition.inc
context_mobile_detect_boot in ./context_mobile_detect.module
Implements hook_boot().
context_mobile_detect_preboot in ./context_mobile_detect.module
Called via settings.inc; needs to be added to settings.php manually.

File

./context_mobile_detect.module, line 80

Code

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;
}