PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/rokcommon/RokCommon/Browser.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 288 lines | 217 code | 26 blank | 45 comment | 28 complexity | 3834306b89572271814eca58063f853c MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: Browser.php 57540 2012-10-14 18:27:59Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. defined('ROKCOMMON') or die;
  9. /**
  10. * @package RokCommon
  11. */
  12. class RokCommon_Browser
  13. {
  14. /** @var string */
  15. protected $ua;
  16. /** @var string */
  17. protected $name;
  18. /** @var string */
  19. protected $version;
  20. /** @var string */
  21. protected $shortversion;
  22. /** @var string */
  23. protected $platform;
  24. /** @var string */
  25. protected $engine;
  26. /** @var array */
  27. protected $checks = array();
  28. public function __get($name)
  29. {
  30. switch ($name) {
  31. case 'checks':
  32. return null;
  33. break;
  34. default:
  35. if (property_exists($this, $name) && isset($this->$name)) {
  36. return $this->$name;
  37. } elseif (method_exists($this, 'get' . ucfirst($name))) {
  38. return call_user_func(array($this, 'get' . ucfirst($name)));
  39. } else {
  40. return null;
  41. }
  42. }
  43. }
  44. /**
  45. *
  46. */
  47. public function __construct()
  48. {
  49. $this->ua = $_SERVER['HTTP_USER_AGENT'];
  50. $this->checkPlatform();
  51. $this->checkBrowser();
  52. $this->checkEngine();
  53. // add short version
  54. if ($this->version != 'unknown') $this->shortversion = substr($this->version, 0, strpos($this->version, '.')); else $this->shortversion = 'unknown';
  55. }
  56. /**
  57. */
  58. protected function checkPlatform()
  59. {
  60. if (preg_match("/iPhone/", $this->ua) || preg_match("/iPod/", $this->ua)) {
  61. $this->platform = "iphone";
  62. } elseif (preg_match("/iPad/", $this->ua)) {
  63. $this->platform = "ipad";
  64. } elseif (preg_match("/Android/", $this->ua)) {
  65. $this->platform = "android";
  66. } elseif (preg_match("/Mobile/i", $this->ua)) {
  67. $this->platform = "mobile";
  68. } elseif (preg_match("/win/i", $this->ua)) {
  69. $this->platform = "win";
  70. } elseif (preg_match("/mac/i", $this->ua)) {
  71. $this->platform = "mac";
  72. } elseif (preg_match("/linux/i", $this->ua)) {
  73. $this->platform = "linux";
  74. } else {
  75. $this->platform = "unknown";
  76. }
  77. return $this->platform;
  78. }
  79. /**
  80. */
  81. protected function checkEngine()
  82. {
  83. switch ($this->name) {
  84. case 'ie':
  85. $this->engine = 'trident';
  86. break;
  87. case 'minefield':
  88. case 'firefox':
  89. $this->engine = 'gecko';
  90. break;
  91. case 'android':
  92. case 'ipad':
  93. case 'iphone':
  94. case 'chrome':
  95. case 'safari':
  96. $this->engine = 'webkit';
  97. break;
  98. case 'opera':
  99. $this->engine = 'presto';
  100. break;
  101. default:
  102. $this->engine = 'unknown';
  103. break;
  104. }
  105. }
  106. /**
  107. */
  108. protected function checkBrowser()
  109. {
  110. // IE
  111. if (preg_match('/msie/i', $this->ua) && !preg_match('/opera/i', $this->ua)) {
  112. $result = explode(' ', stristr(str_replace(';', ' ', $this->ua), 'msie'));
  113. $this->name = 'ie';
  114. $this->version = $result[1];
  115. } // Firefox
  116. elseif (preg_match('/Firefox/', $this->ua)) {
  117. $result = explode('/', stristr($this->ua, 'Firefox'));
  118. $version = explode(' ', $result[1]);
  119. $this->name = 'firefox';
  120. $this->version = $version[0];
  121. } // Minefield
  122. elseif (preg_match('/Minefield/', $this->ua)) {
  123. $result = explode('/', stristr($this->ua, 'Minefield'));
  124. $version = explode(' ', $result[1]);
  125. $this->name = 'minefield';
  126. $this->version = $version[0];
  127. } // Chrome
  128. elseif (preg_match('/Chrome/', $this->ua)) {
  129. $result = explode('/', stristr($this->ua, 'Chrome'));
  130. $version = explode(' ', $result[1]);
  131. $this->name = 'chrome';
  132. $this->version = $version[0];
  133. } //Safari
  134. elseif (preg_match('/Safari/', $this->ua) && !preg_match('/iPhone/', $this->ua) && !preg_match('/iPod/', $this->ua) && !preg_match('/iPad/', $this->ua)) {
  135. $result = explode('/', stristr($this->ua, 'Version'));
  136. $this->name = 'safari';
  137. if (isset ($result[1])) {
  138. $version = explode(' ', $result[1]);
  139. $this->version = $version[0];
  140. } else {
  141. $this->version = 'unknown';
  142. }
  143. } // Opera
  144. elseif (preg_match('/opera/i', $this->ua)) {
  145. $result = stristr($this->ua, 'opera');
  146. if (preg_match('/\//', $result)) {
  147. $result = explode('/', $result);
  148. $version = explode(' ', $result[1]);
  149. $this->name = 'opera';
  150. $this->version = $version[0];
  151. } else {
  152. $version = explode(' ', stristr($result, 'opera'));
  153. $this->name = 'opera';
  154. $this->version = $version[1];
  155. }
  156. } // iPhone/iPod
  157. elseif (preg_match('/iPhone/', $this->ua) || preg_match('/iPod/', $this->ua)) {
  158. $result = explode('/', stristr($this->ua, 'Version'));
  159. $this->name = 'iphone';
  160. if (isset ($result[1])) {
  161. $version = explode(' ', $result[1]);
  162. $this->version = $version[0];
  163. } else {
  164. $this->version = 'unknown';
  165. }
  166. } // iPad
  167. elseif (preg_match('/iPad/', $this->ua)) {
  168. $result = explode('/', stristr($this->ua, 'Version'));
  169. $this->name = 'ipad';
  170. if (isset ($result[1])) {
  171. $version = explode(' ', $result[1]);
  172. $this->version = $version[0];
  173. } else {
  174. $this->version = 'unknown';
  175. }
  176. } // Android
  177. elseif (preg_match('/Android/', $this->ua)) {
  178. $result = explode('/', stristr($this->ua, 'Version'));
  179. $this->name = 'android';
  180. if (isset ($result[1])) {
  181. $version = explode(' ', $result[1]);
  182. $this->version = $version[0];
  183. } else {
  184. $this->version = "unknown";
  185. }
  186. } else {
  187. $this->name = "unknown";
  188. $this->version = "unknown";
  189. }
  190. }
  191. protected function createChecks()
  192. {
  193. $this->checks = array(
  194. '', // filename
  195. '-' . $this->name, // browser check
  196. '-' . $this->platform, // platform check
  197. '-' . $this->engine, // render engine
  198. '-' . $this->name . '-' . $this->platform, // browser + platform check
  199. '-' . $this->name . $this->shortversion, // short browser version check
  200. '-' . $this->name . $this->version, // longbrowser version check
  201. '-' . $this->name . $this->shortversion . '-' . $this->platform, // short browser version + platform check
  202. '-' . $this->name . $this->version . '-' . $this->platform // longbrowser version + platform check
  203. );
  204. }
  205. public function getChecks($file, $keep_path = false)
  206. {
  207. $checkfiles = array();
  208. $ext = substr($file, strrpos($file, '.'));
  209. $path = ($keep_path) ? dirname($file) . DS : '';
  210. $filename = basename($file, $ext);
  211. foreach ($this->checks as $suffix) {
  212. $checkfiles[] = $path . $filename . $suffix . $ext;
  213. }
  214. return $checkfiles;
  215. }
  216. /**
  217. * @return string
  218. */
  219. public function getUa()
  220. {
  221. return $this->ua;
  222. }
  223. /**
  224. * @return string
  225. */
  226. public function getEngine()
  227. {
  228. return $this->engine;
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getName()
  234. {
  235. return $this->name;
  236. }
  237. /**
  238. * @return string
  239. */
  240. public function getPlatform()
  241. {
  242. return $this->platform;
  243. }
  244. /**
  245. * @return string
  246. */
  247. public function getShortversion()
  248. {
  249. return $this->shortversion;
  250. }
  251. /**
  252. * @return string
  253. */
  254. public function getVersion()
  255. {
  256. return $this->version;
  257. }
  258. public function getShortName()
  259. {
  260. return $this->name.$this->shortversion;
  261. }
  262. }