You are here

function is_mobile in Back To Top 8

Same name and namespace in other branches
  1. 7 back_to_top.module \is_mobile()
  2. 2.x back_to_top.module \is_mobile()

Check if mobile or touch device with PHP so javascript and css isn't included in that case.

1 call to is_mobile()
back_to_top_page_attachments in ./back_to_top.module

File

./back_to_top.module, line 106
Adds the back to top button.

Code

function is_mobile() {

  // Check for mobile device using Browscap module if it is available.
  if (\Drupal::moduleHandler()
    ->moduleExists('browscap')) {
    $browser = \Drupal::service('browscap')
      ->getBrowser();
    if (isset($browser['ismobiledevice']) && $browser['ismobiledevice'] == 1) {
      return TRUE;
    }
  }
  if (isset($_SERVER["HTTP_X_WAP_PROFILE"])) {
    return TRUE;
  }
  if (isset($_SERVER["HTTP_ACCEPT"]) && preg_match("/wap\\.|\\.wap/i", $_SERVER["HTTP_ACCEPT"])) {
    return TRUE;
  }
  if (isset($_SERVER["HTTP_USER_AGENT"])) {
    $user_agents = [
      "midp",
      "j2me",
      "iphone",
      "avantg",
      "docomo",
      "novarra",
      "palmos",
      "palmsource",
      "240x320",
      "opwv",
      "chtml",
      "pda",
      "windows\\ ce",
      "mmp\\/",
      "blackberry",
      "mib\\/",
      "symbian",
      "wireless",
      "nokia",
      "hand",
      "mobi",
      "phone",
      "cdm",
      "up\\.b",
      "audio",
      "sie\\-",
      "sec\\-",
      "samsung",
      "htc",
      "mot\\-",
      "mitsu",
      "sagem",
      "sony",
      "alcatel",
      "lg",
      "erics",
      "vx",
      "^nec",
      "philips",
      "mmm",
      "xx",
      "panasonic",
      "sharp",
      "wap",
      "sch",
      "rover",
      "pocket",
      "benq",
      "java",
      "pt",
      "pg",
      "vox",
      "amoi",
      "bird",
      "compal",
      "kg",
      "voda",
      "sany",
      "kdd",
      "dbt",
      "sendo",
      "sgh",
      "gradi",
      "jb",
      "\\d\\d\\di",
      "moto",
      "ipad",
      "android",
      "ipod",
      "webos",
    ];
    foreach ($user_agents as $user_string) {
      if (preg_match("/" . $user_string . "/i", strtolower($_SERVER["HTTP_USER_AGENT"]))) {
        return TRUE;
      }
    }
  }
  return FALSE;
}