PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/mod_roknavmenu/lib/common/RokBrowserCheck.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 196 lines | 168 code | 13 blank | 15 comment | 25 complexity | 3b709e2523118ef6342bddd7771a741e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, LGPL-3.0, LGPL-2.0, JSON
  1. <?php
  2. /**
  3. * @version 1.13 July 2, 2012
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. if (!class_exists('RokBrowserCheck')) {
  9. class RokBrowserCheck {
  10. var $_ua;
  11. var $name;
  12. var $version;
  13. var $shortversion;
  14. var $platform;
  15. var $engine;
  16. var $_checks = array();
  17. function __construct() {
  18. $this->_ua = $_SERVER['HTTP_USER_AGENT'];
  19. $this->_checkPlatform();
  20. $this->_checkBrowser();
  21. $this->_checkEngine();
  22. // add short version
  23. if ($this->version != 'unknown') $this->shortversion = substr($this->version, 0, strpos($this->version, '.'));
  24. else $this->shortversion = 'unknown';
  25. $this->_createChecks();
  26. }
  27. function _checkPlatform() {
  28. if (preg_match("/iPhone/", $this->_ua) || preg_match("/iPod/", $this->_ua)) {
  29. $this->platform = "iphone";
  30. }
  31. elseif (preg_match("/iPad/", $this->_ua)) {
  32. $this->platform = "ipad";
  33. }
  34. elseif (preg_match("/Android/", $this->_ua)) {
  35. $this->platform = "android";
  36. }
  37. elseif (preg_match("/Mobile/i", $this->_ua)) {
  38. $this->platform = "mobile";
  39. }
  40. elseif (preg_match("/win/i", $this->_ua)) {
  41. $this->platform = "win";
  42. }
  43. elseif (preg_match("/mac/i", $this->_ua)) {
  44. $this->platform = "mac";
  45. }
  46. elseif (preg_match("/linux/i", $this->_ua)) {
  47. $this->platform = "linux";
  48. } else {
  49. $this->platform = "unknown";
  50. }
  51. return $this->platform;
  52. }
  53. function _checkEngine() {
  54. switch ($this->name) {
  55. case 'ie':
  56. $this->engine = 'trident';
  57. break;
  58. case 'firefox':
  59. $this->engine = 'gecko';
  60. break;
  61. case 'android':
  62. case 'ipad':
  63. case 'iphone':
  64. case 'chrome':
  65. case 'safari':
  66. $this->engine = 'webkit';
  67. break;
  68. case 'opera':
  69. $this->engine = 'presto';
  70. break;
  71. default:
  72. $this->engine = 'unknown';
  73. break;
  74. }
  75. }
  76. function _checkBrowser() {
  77. // IE
  78. if (preg_match('/msie/i', $this->_ua) && !preg_match('/opera/i', $this->_ua)) {
  79. $result = explode(' ', stristr(str_replace(';', ' ', $this->_ua), 'msie'));
  80. $this->name = 'ie';
  81. $this->version = $result[1];
  82. }
  83. // Firefox
  84. elseif (preg_match('/Firefox/', $this->_ua)) {
  85. $result = explode('/', stristr($this->_ua, 'Firefox'));
  86. $version = explode(' ', $result[1]);
  87. $this->name = 'firefox';
  88. $this->version = $version[0];
  89. }
  90. // Chrome
  91. elseif (preg_match('/Chrome/', $this->_ua)) {
  92. $result = explode('/', stristr($this->_ua, 'Chrome'));
  93. $version = explode(' ', $result[1]);
  94. $this->name = 'chrome';
  95. $this->version = $version[0];
  96. }
  97. //Safari
  98. elseif (preg_match('/Safari/', $this->_ua) && !preg_match('/iPhone/', $this->_ua) && !preg_match('/iPod/', $this->_ua) && !preg_match('/iPad/', $this->_ua)) {
  99. $result = explode('/', stristr($this->_ua, 'Version'));
  100. $this->name = 'safari';
  101. if (isset ($result[1])) {
  102. $version = explode(' ', $result[1]);
  103. $this->version = $version[0];
  104. } else {
  105. $this->version = 'unknown';
  106. }
  107. }
  108. // Opera
  109. elseif (preg_match('/opera/i', $this->_ua)) {
  110. $result = stristr($this->_ua, 'opera');
  111. if (preg_match('/\//', $result)) {
  112. $result = explode('/', $result);
  113. $version = explode(' ', $result[1]);
  114. $this->name = 'opera';
  115. $this->version = $version[0];
  116. } else {
  117. $version = explode(' ', stristr($result, 'opera'));
  118. $this->name = 'opera';
  119. $this->version = $version[1];
  120. }
  121. }
  122. // iPhone/iPod
  123. elseif (preg_match('/iPhone/', $this->_ua) || preg_match('/iPod/', $this->_ua)) {
  124. $result = explode('/', stristr($this->_ua, 'Version'));
  125. $this->name = 'iphone';
  126. if (isset ($result[1])) {
  127. $version = explode(' ', $result[1]);
  128. $this->version = $version[0];
  129. } else {
  130. $this->version = 'unknown';
  131. }
  132. }
  133. // iPad
  134. elseif (preg_match('/iPad/', $this->_ua)) {
  135. $result = explode('/', stristr($this->_ua, 'Version'));
  136. $this->name = 'ipad';
  137. if (isset ($result[1])) {
  138. $version = explode(' ', $result[1]);
  139. $this->version = $version[0];
  140. } else {
  141. $this->version = 'unknown';
  142. }
  143. }
  144. // Android
  145. elseif (preg_match('/Android/', $this->_ua)) {
  146. $result = explode('/', stristr($this->_ua, 'Version'));
  147. $this->name = 'android';
  148. if (isset ($result[1])) {
  149. $version = explode(' ', $result[1]);
  150. $this->version = $version[0];
  151. } else {
  152. $this->version = "unknown";
  153. }
  154. } else {
  155. $this->name = "unknown";
  156. $this->version = "unknown";
  157. }
  158. }
  159. function _createChecks() {
  160. $this->_checks = array(
  161. $this->name, // browser check
  162. $this->platform, // platform check
  163. $this->engine, // render engine
  164. $this->name . '-' . $this->platform, // browser + platform check
  165. $this->name . $this->shortversion, // short browser version check
  166. $this->name . $this->version, // longbrowser version check
  167. $this->name . $this->shortversion . '-' . $this->platform, // short browser version + platform check
  168. $this->name . $this->version . '-' . $this->platform // longbrowser version + platform check
  169. );
  170. }
  171. function getChecks($file, $keep_path = false) {
  172. $checkfiles = array();
  173. $ext = substr($file, strrpos($file, '.'));
  174. $path = ($keep_path) ? dirname($file) . DS : '';
  175. $filename = basename($file, $ext);
  176. $checkfiles[] = $path . $filename . $ext;
  177. foreach ($this->_checks as $suffix) {
  178. $checkfiles[] = $path . $filename . '-' . $suffix . $ext;
  179. }
  180. return $checkfiles;
  181. }
  182. }
  183. }