You are here

function _mobile_tools_site_detection in Mobile Tools 6

Same name and namespace in other branches
  1. 5 mobile_tools.module \_mobile_tools_site_detection()
  2. 6.3 mobile_tools.module \_mobile_tools_site_detection()
1 call to _mobile_tools_site_detection()
mobile_tools_is_mobile_site in ./mobile_tools.module
Detection of the site type . the values comes out the configuration form.

File

./mobile_tools.module, line 393
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;

  // 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 robuts
  $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';
  }
}