PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-data/lib/is_mobile.php

https://bitbucket.org/scottywz/portal
PHP | 123 lines | 61 code | 3 blank | 59 comment | 47 complexity | ee523ef5923ad47cc66f35d1d02e0f47 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* is_mobile()
  3. * Shitty mobile device detection based on shitty user agent strings.
  4. *
  5. * Copyright (C) 2009-2012 Scott Zeid
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. * Except as contained in this notice, the name(s) of the above copyright holders
  26. * shall not be used in advertising or otherwise to promote the sale, use or
  27. * other dealings in this Software without prior written authorization.
  28. */
  29. /**
  30. * Determine whether the user agent represents a mobile device.
  31. *
  32. * The user can use the following query string parameters to override this
  33. * function's output:
  34. * * !mobile, nomobile - Cause this function to return False.
  35. * * mobile - Cause this function to return True.
  36. * * mobile=[...], device=[...] - Override the device type.
  37. *
  38. * device=[...] takes precedence over mobile=[...].
  39. *
  40. * Valid device types are firefox-tablet, firefox, chrome-tablet, chrome,
  41. * android-tablet, android, webos, tablet, unknown, apple, and apple-tablet
  42. * (listed in descending order of the author's personal preference). Android
  43. * tablets and iPads are not considered to be mobile devices, but is_mobile()
  44. * will still return a device name ending in "-tablet", as appropriate for the
  45. * device in question.
  46. *
  47. * If the user is running Firefox Mobile, the device name would be "firefox",
  48. * or "firefox-tablet" if it is a tablet. The same is true for users using
  49. * Chrome, except (obviously) "firefox" would be replaced with "chrome".
  50. * Although it has been discontinued for a while, support for the HP Touchpad
  51. * may be added in the future; its device name would be "webos-tablet".
  52. *
  53. * @param bool $return_device Return a string representing the type of device.
  54. * @param bool $use_get Allow overriding default behavior using query strings.
  55. * @return mixed If $return_device is false, returns a boolean value.
  56. */
  57. function is_mobile($return_device = False, $use_get = True) {
  58. # config
  59. $user_agent = $_SERVER["HTTP_USER_AGENT"];
  60. $nomobile = False; $forcemobile = False; $forcedevice = "";
  61. if ($use_get) {
  62. if (isset($_GET["!mobile"]) || isset($_GET["nomobile"]))
  63. $nomobile = True;
  64. elseif (isset($_GET["mobile"])) {
  65. $forcedevice = strtolower($_GET["mobile"]);
  66. if (!stristr($forcedevice, "tablet") && $forcedevice != "ipad")
  67. $forcemobile = True;
  68. if (!$forcedevice) $forcedevice = "unknown";
  69. }
  70. if (!empty($_GET["device"])) {
  71. $forcedevice = strtolower($_GET["device"]);
  72. if (!stristr($forcedevice, "tablet") && $forcedevice != "ipad") {
  73. $forcemobile = True;
  74. $nomobile = False;
  75. }
  76. }
  77. }
  78. # is mobile device?
  79. if (((
  80. (stristr($user_agent, "Android") && !stristr($user_agent, "Android 3.") &&
  81. stristr($user_agent, "Mobile")) ||
  82. stristr($user_agent, "webOS") ||
  83. ((stristr($user_agent, "Firefox") || stristr($user_agent, "Fennec")) &&
  84. stristr($user_agent, "Mobile")) ||
  85. stristr($user_agent, "iPhone") || stristr($user_agent, "iPod")
  86. ) && !stristr($user_agent, "Tablet") && $nomobile == False) ||
  87. $forcemobile == True)
  88. $mobile = True;
  89. else
  90. $mobile = False;
  91. # which mobile device
  92. $device = "unknown";
  93. if (stristr($user_agent, "Android")) {
  94. if (!stristr($user_agent, "Mobile") || stristr($user_agent, "Android 3."))
  95. $device = "android-tablet";
  96. else $device = "android";
  97. if (stristr($user_agent, "Chrome"))
  98. $device = str_replace("android", "chrome", $device);
  99. }
  100. if (stristr($user_agent, "Firefox") || stristr($user_agent, "Fennec")) {
  101. if (stristr($user_agent, "Tablet")) $device = "firefox-tablet";
  102. else $device = "firefox";
  103. }
  104. if (stristr($user_agent, "webOS")) $device = "webos";
  105. if (stristr($user_agent, "iPhone") || stristr($user_agent, "iPod"))
  106. $device = "apple";
  107. if (stristr($user_agent, "iPad")) $device = "apple-tablet";
  108. if ($forcedevice != "") $device = $forcedevice;
  109. if (stristr($forcedevice, "fennec"))
  110. $device = str_replace("fennec", "firefox", $device);
  111. if ($forcedevice == "iphone" || $forcedevice == "ipod") $device = "apple";
  112. if (stristr($forcedevice, "ipad")) $device = "apple-tablet";
  113. if (((!$mobile && !$forcemobile) || $nomobile || $forcedevice === "") &&
  114. !stristr($device, "tablet"))
  115. $device = "";
  116. # return value
  117. if ($return_device == False) return $mobile;
  118. else return $device;
  119. }
  120. ?>