View source
<?php
define('MOBILE_DETECT_LIBRARY_PATH', '/sites/all/libraries/Mobile_Detect');
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;
}
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;
}
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',
),
),
);
}
function context_mobile_detect_context_page_condition() {
if ($plugin = context_get_plugin('condition', 'cmd')) {
$plugin
->execute();
}
if ($plugin = context_get_plugin('condition', 'cmd_type')) {
$plugin
->execute();
}
}
function _context_mobile_detect_detect($setcookie = FALSE) {
$data = array(
'device' => 3,
'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();
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;
}
}
}
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']);
}
}
function context_mobile_detect_preboot() {
$data = _context_mobile_detect_detect(FALSE);
if ($data['device'] < 3 && $data['device'] > 0) {
_context_mobile_detect_add_query_string('device=' . $data['device'] . '&device_type=' . $data['device_type']);
}
}
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;
}