BrowserDevice.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /** @package verysimple::HTTP */
  3. /**
  4. * BrowserDevice represents a device used when browsing (or executing)
  5. * the current code. This is a Singleton pattern, cannot be
  6. * instantiated directly.
  7. *
  8. * TODO: this only has minimal support and basically only supplies
  9. * information about whether the device is mobile or not, and if so
  10. * it supplies the major vendor name.
  11. *
  12. * Usage:
  13. * $browser = BrowserDevice::GetInstance();
  14. * if ($device->IsMobile()) dosomething();
  15. *
  16. * @package verysimple::HTTP
  17. * @author VerySimple Inc.
  18. * @copyright 1997-2007 VerySimple, Inc. http://www.verysimple.com
  19. * @license http://www.gnu.org/licenses/lgpl.html LGPL
  20. * @version 1.0
  21. */
  22. class BrowserDevice
  23. {
  24. /**
  25. * patters to search for devices
  26. * @var Array
  27. */
  28. static $DESKTOP_DEVICE_PATTERNS = Array(
  29. 'AdobeAIR'=>'AdobeAIR',
  30. 'chrome'=>'google',
  31. 'firefox'=>'mozilla',
  32. 'MSIE'=>'microsoft',
  33. 'safari'=>'apple'
  34. );
  35. /**
  36. * patters to search for devices
  37. * @var Array
  38. */
  39. static $MOBILE_DEVICE_PATTERNS = Array(
  40. '(ipad|ipod|iphone)'=>'apple',
  41. 'android'=>'android',
  42. 'opera mini'=>'opera',
  43. 'blackberry'=>'blackberry',
  44. '(pre\/|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)'=>'palm',
  45. '(iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile)'=>'windows',
  46. '(mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo)'=>'unknown'
  47. );
  48. private static $instance;
  49. public $UserAgent;
  50. public $IsWAP;
  51. public $IsMobile;
  52. public $IsTablet;
  53. public $IsConsole;
  54. public $Vendor;
  55. public $SupportsJS;
  56. public $SupportsCSS;
  57. public $IsNativeDevice;
  58. public $NativeClientVersion;
  59. /**
  60. * Private constructor enforces the Singleton pattern
  61. */
  62. private function __construct()
  63. {
  64. // do nothing
  65. }
  66. /**
  67. * Parse the user agent and detect the browser, populating
  68. * all internal properties accordingly
  69. */
  70. public function Detect()
  71. {
  72. // RESET DEFAULT VALUES
  73. $this->IsWAP = false;
  74. $this->IsMobile = false;
  75. $this->IsTablet = false;
  76. $this->IsConsole = false;
  77. $this->Vendor = 'unknown';
  78. $this->SupportsJS = true;
  79. $this->SupportsCSS = true;
  80. $this->UserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : "";
  81. $wap = isset($_SERVER['HTTP_X_WAP_PROFILE']) ? $_SERVER['HTTP_X_WAP_PROFILE'] : "";
  82. if (!$this->UserAgent)
  83. {
  84. $this->IsConsole = true;
  85. }
  86. else
  87. {
  88. foreach (BrowserDevice::$MOBILE_DEVICE_PATTERNS as $key => $val )
  89. {
  90. if (preg_match('/'.$key.'/i',$this->UserAgent))
  91. {
  92. $this->IsMobile = true;
  93. $this->Vendor = $val;
  94. break;
  95. }
  96. }
  97. if ($this->IsMobile == false) {
  98. foreach (BrowserDevice::$DESKTOP_DEVICE_PATTERNS as $key => $val )
  99. {
  100. if (preg_match('/'.$key.'/i',$this->UserAgent))
  101. {
  102. $this->Vendor = $val;
  103. break;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Returns an instance of a BrowserDevice populated based on the
  111. * server variables provided
  112. * @return BrowserDevice
  113. */
  114. public static function GetInstance()
  115. {
  116. if (!isset(self::$instance))
  117. {
  118. $c = __CLASS__;
  119. self::$instance = new $c;
  120. self::$instance->Detect();
  121. }
  122. return self::$instance;
  123. }
  124. }
  125. ?>