You are here

function _mobile_tools_site_detection in Mobile Tools 6.3

Same name and namespace in other branches
  1. 5 mobile_tools.module \_mobile_tools_site_detection()
  2. 6 mobile_tools.module \_mobile_tools_site_detection()

Mobile Tools implementation to detect what site is being served now

Return value

desktop / mobile

1 call to _mobile_tools_site_detection()
mobile_tools_site_type in ./mobile_tools.module
Detection of the site type . the values comes out the configuration form.

File

./mobile_tools.module, line 498
Mobile Tools provides a range of functionality assisting in creating a mobile Drupal site . this functionality contains:

Code

function _mobile_tools_site_detection() {
  global $base_url, $mobile_tools_device;

  // Easy detection in case redireciton is disabled and device specific theme switching is enabled
  if (!variable_get('mobile_tools_redirect', 0) && variable_get('mobile-tools-theme-switch', '') == 'mobile-tools-mobile-device') {
    if ($mobile_tools_device['type'] == 'mobile') {
      return 'mobile';
    }
    else {
      return 'desktop';
    }
  }

  // Check if $_SERVER variables are set if not, just return desktop... Still unclear what best solution is:
  if (!isset($_SERVER['HTTP_HOST']) && !isset($_SERVER['SERVER_NAME'])) {
    return 'desktop';
  }
  $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];

  // first check if the url is a m.* or .mobi url. This is robust
  $server_domain_elements = explode('.', $host);
  if (count($server_domain_elements) > 0) {
    if ($server_domain_elements[0] == 'm') {

      // check for m.* domain
      return 'mobile';
    }
    if ($server_domain_elements[count($server_domain_elements) - 1] == 'mobi') {

      // check for *.mobi
      return 'mobile';
    }
  }

  // If this doesn't return an answer, we will have to do a comparison of the mobile and desktop url!
  $mobile = variable_get('mobile_tools_mobile_url', mobile_tools_create_mobile_url($base_url));
  $mobile_url = parse_url($mobile);
  $mobile_url['host'] = mobile_tools_prepare_url($mobile_url['host']);
  $desktop = variable_get('mobile_tools_desktop_url', $base_url);
  $desktop_url = parse_url($desktop);
  $desktop_url['host'] = mobile_tools_prepare_url($desktop_url['host']);
  if (!array_key_exists('path', $desktop_url)) {
    $desktop_url['path'] = '';
  }
  if (!array_key_exists('path', $mobile_url)) {
    $mobile_url['path'] = '';
  }

  // If $_SERVER['HTTP_HOST'] is not set just return desktop
  if (!isset($_SERVER['HTTP_HOST'])) {
    return 'desktop';
  }
  $server_name = mobile_tools_prepare_url($_SERVER['HTTP_HOST']);

  // Check domain first
  if ($mobile_url['host'] == $server_name && $desktop_url['host'] != $server_name) {
    return 'mobile';
  }
  elseif ($mobile_url['host'] != $server_name && $desktop_url['host'] == $server_name) {
    return 'desktop';
  }

  // find longest url
  $longest_url = strlen($mobile) > strlen($desktop) ? $mobile : $desktop;
  $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
  $uri = $protocol . $_SERVER['HTTP_HOST'] . request_uri();
  $pos = strpos($uri, preg_replace('{/$}', '', $longest_url));
  if ($pos === FALSE) {
    return $longest_url == $mobile ? 'desktop' : 'mobile';
  }
  else {
    return $longest_url == $mobile ? 'mobile' : 'desktop';
  }
}