You are here

function mobile_switch_mobile_detect in Mobile Switch 7.2

Get extended Mobile Detect device detection results.

The returned array contains following values:

  • mobile_detect_class: boolean
  • ismobiledevice: boolean
  • istablet: boolean
  • useragent: string, the browser user agent
  • prevent_device: boolean
  • deskbrowser_usage: boolean
  • tablet_usage: boolean
  • device_type: string, the values are desktop or mobile

This function should not be used in the operating mode 'Do not use'.

Parameters

$emulator_check: Optional boolean value to use mobile browser emulators.

Return value

array The associative array contains the informations described.

See also

mobile_switch_boot()

6 calls to mobile_switch_mobile_detect()
mobile_switch_boot in ./mobile_switch.module
Implements hook_boot().
mobile_switch_browscap_get_browser in ./mobile_switch.module
Fallback function to work with previous versions.
mobile_switch_handler_filter::query in views/mobile_switch_handler_filter.inc
Add this filter to the query.
mobile_switch_init in ./mobile_switch.module
Implements hook_init().
mobile_switch_page_alter in ./mobile_switch.module
Implements hook_page_alter().

... See full list

File

./mobile_switch.module, line 591
Provides various functionalities to develop mobile ready websites.

Code

function mobile_switch_mobile_detect($emulator_check = FALSE) {
  static $browser;
  if (!isset($browser)) {
    global $msw_detect;
    _mobile_switch_mobile_detect_class_load();
    if (is_object($msw_detect)) {
      $browser['mobile_detect_class'] = TRUE;

      // Prepare the basic informations.
      $browser['ismobiledevice'] = $msw_detect
        ->isMobile() ? $msw_detect
        ->isMobile() : FALSE;
      $browser['istablet'] = $msw_detect
        ->isTablet() ? $msw_detect
        ->isTablet() : FALSE;
      $browser['useragent'] = $_SERVER['HTTP_USER_AGENT'];

      // Mobile device emulators.
      if ((bool) $emulator_check === TRUE) {
        $emulator_strings = variable_get('mobile_switch_emulator_strings', "Fennec\nAndroid\nTablet\nMobi");
        $emulator_strings = str_replace(array(
          "\r\n",
          "\n",
          "\r",
        ), '|', $emulator_strings);
        if (preg_match("/{$emulator_strings}/i", $browser['useragent'])) {
          $browser['ismobiledevice'] = TRUE;
        }
      }

      // Mobile device prevention.
      $browser['prevent_device'] = FALSE;
      $prevent_devices = (bool) variable_get('mobile_switch_prevent_devices', FALSE);
      $prevent_devices_strings = variable_get('mobile_switch_prevent_devices_strings', '');
      if ($prevent_devices === TRUE && !empty($prevent_devices_strings)) {
        $prevent_devices_strings = str_replace(array(
          "\r\n",
          "\n",
          "\r",
        ), '|', $prevent_devices_strings);
        $strings_arr = explode("|", $prevent_devices_strings);
        foreach ($strings_arr as $val) {
          $found = stristr($browser['useragent'], $val);
          if (!empty($found)) {
            $browser['prevent_device'] = TRUE;
          }
        }
      }

      // Desktopbrowser usage.
      $browser['deskbrowser_usage'] = (bool) variable_get('mobile_switch_deskbrowser', FALSE);

      // Usage on administration pages.
      $browser['tablet_usage'] = (bool) variable_get('mobile_switch_tablet_usage', TRUE);
      $browser['device_type'] = $browser['ismobiledevice'] ? MOBILE_SWITCH_DEVICE_TYPE_MOBILE : MOBILE_SWITCH_DEVICE_TYPE_DESKTOP;
    }
    else {
      $browser['mobile_detect_class'] = FALSE;
      $browser['ismobiledevice'] = FALSE;
      $browser['istablet'] = FALSE;
      $browser['prevent_device'] = FALSE;
      $browser['useragent'] = $_SERVER['HTTP_USER_AGENT'];
      $browser['deskbrowser_usage'] = FALSE;
      $browser['tablet_usage'] = FALSE;
      $browser['device_type'] = MOBILE_SWITCH_DEVICE_TYPE_DESKTOP;
      watchdog('Mobile Switch', 'The Mobile Detect class file was not found.');
    }
  }
  return $browser;
}