PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Zend/Http/UserAgent/AbstractDevice.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 974 lines | 578 code | 94 blank | 302 comment | 141 complexity | 6b900aeab41e3f6c4226181fd747fea8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http
  17. * @subpackage UserAgent
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. #require_once 'Zend/Http/UserAgent/Device.php';
  22. /**
  23. * Abstract Class to define a browser device.
  24. *
  25. * @category Zend
  26. * @package Zend_Http
  27. * @subpackage UserAgent
  28. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. abstract class Zend_Http_UserAgent_AbstractDevice
  32. implements Zend_Http_UserAgent_Device
  33. {
  34. /**
  35. * Browser signature
  36. *
  37. * @var string
  38. */
  39. protected $_browser = '';
  40. /**
  41. * Browser version
  42. *
  43. * @var string
  44. */
  45. protected $_browserVersion = '';
  46. /**
  47. * Configuration
  48. *
  49. * @var array
  50. */
  51. protected $_config;
  52. /**
  53. * User Agent chain
  54. *
  55. * @var string
  56. */
  57. protected $_userAgent;
  58. /**
  59. * Server variable
  60. *
  61. * @var array
  62. */
  63. protected $_server;
  64. /**
  65. * Image types
  66. *
  67. * @var array
  68. */
  69. protected $_images = array(
  70. 'jpeg',
  71. 'gif',
  72. 'png',
  73. 'pjpeg',
  74. 'x-png',
  75. 'bmp',
  76. );
  77. /**
  78. * Browser/Device features
  79. *
  80. * @var array
  81. */
  82. protected $_aFeatures = array();
  83. /**
  84. * Browser/Device features groups
  85. *
  86. * @var array
  87. */
  88. protected $_aGroup = array();
  89. /**
  90. * Constructor
  91. *
  92. * @param null|string|array $userAgent If array, restores from serialized version
  93. * @param array $server
  94. * @param array $config
  95. * @return void
  96. */
  97. public function __construct($userAgent = null, array $server = array(), array $config = array())
  98. {
  99. if (is_array($userAgent)) {
  100. // Restoring from serialized array
  101. $this->_restoreFromArray($userAgent);
  102. } else {
  103. // Constructing new object
  104. $this->setUserAgent($userAgent);
  105. $this->_server = $server;
  106. $this->_config = $config;
  107. $this->_getDefaultFeatures();
  108. $this->_defineFeatures();
  109. }
  110. }
  111. /**
  112. * Serialize object
  113. *
  114. * @return string
  115. */
  116. public function serialize()
  117. {
  118. $spec = array(
  119. '_aFeatures' => $this->_aFeatures,
  120. '_aGroup' => $this->_aGroup,
  121. '_browser' => $this->_browser,
  122. '_browserVersion' => $this->_browserVersion,
  123. '_userAgent' => $this->_userAgent,
  124. '_images' => $this->_images,
  125. );
  126. return serialize($spec);
  127. }
  128. /**
  129. * Unserialize
  130. *
  131. * @param string $serialized
  132. * @return void
  133. */
  134. public function unserialize($serialized)
  135. {
  136. $spec = unserialize($serialized);
  137. $this->_restoreFromArray($spec);
  138. }
  139. /**
  140. * Restore object state from array
  141. *
  142. * @param array $spec
  143. * @return void
  144. */
  145. protected function _restoreFromArray(array $spec)
  146. {
  147. foreach ($spec as $key => $value) {
  148. if (property_exists($this, $key)) {
  149. $this->{$key} = $value;
  150. }
  151. }
  152. }
  153. /**
  154. * Look for features
  155. *
  156. * @return array|null
  157. */
  158. protected function _defineFeatures()
  159. {
  160. $features = $this->_loadFeaturesAdapter();
  161. if (is_array($features)) {
  162. $this->_aFeatures = array_merge($this->_aFeatures, $features);
  163. }
  164. return $this->_aFeatures;
  165. }
  166. /**
  167. * Gets the browser type identifier
  168. *
  169. * @return string
  170. */
  171. abstract public function getType();
  172. /**
  173. * Check a feature for the current browser/device.
  174. *
  175. * @param string $feature The feature to check.
  176. * @return bool
  177. */
  178. public function hasFeature($feature)
  179. {
  180. return (!empty($this->_aFeatures[$feature]));
  181. }
  182. /**
  183. * Gets the value of the current browser/device feature
  184. *
  185. * @param string $feature Feature to search
  186. * @return string|null
  187. */
  188. public function getFeature($feature)
  189. {
  190. if ($this->hasFeature($feature)) {
  191. return $this->_aFeatures[$feature];
  192. }
  193. }
  194. /**
  195. * Set a feature for the current browser/device.
  196. *
  197. * @param string $feature The feature to set.
  198. * @param string $value (option) feature value.
  199. * @param string $group (option) Group to associate with the feature
  200. * @return Zend_Http_UserAgent_AbstractDevice
  201. */
  202. public function setFeature($feature, $value = false, $group = '')
  203. {
  204. $this->_aFeatures[$feature] = $value;
  205. if (!empty($group)) {
  206. $this->setGroup($group, $feature);
  207. }
  208. return $this;
  209. }
  210. /**
  211. * Affects a feature to a group
  212. *
  213. * @param string $group Group name
  214. * @param string $feature Feature name
  215. * @return Zend_Http_UserAgent_AbstractDevice
  216. */
  217. public function setGroup($group, $feature)
  218. {
  219. if (!isset($this->_aGroup[$group])) {
  220. $this->_aGroup[$group] = array();
  221. }
  222. if (!in_array($feature, $this->_aGroup[$group])) {
  223. $this->_aGroup[$group][] = $feature;
  224. }
  225. return $this;
  226. }
  227. /**
  228. * Gets an array of features associated to a group
  229. *
  230. * @param string $group Group param
  231. * @return array
  232. */
  233. public function getGroup($group)
  234. {
  235. return $this->_aGroup[$group];
  236. }
  237. /**
  238. * Gets all the browser/device features
  239. *
  240. * @return array
  241. */
  242. public function getAllFeatures()
  243. {
  244. return $this->_aFeatures;
  245. }
  246. /**
  247. * Gets all the browser/device features' groups
  248. *
  249. * @return array
  250. */
  251. public function getAllGroups()
  252. {
  253. return $this->_aGroup;
  254. }
  255. /**
  256. * Sets all the standard features extracted from the User Agent chain and $this->_server
  257. * vars
  258. *
  259. * @return void
  260. */
  261. protected function _getDefaultFeatures()
  262. {
  263. $server = array();
  264. // gets info from user agent chain
  265. $uaExtract = $this->extractFromUserAgent($this->getUserAgent());
  266. if (is_array($uaExtract)) {
  267. foreach ($uaExtract as $key => $info) {
  268. $this->setFeature($key, $info, 'product_info');
  269. }
  270. }
  271. if (isset($uaExtract['browser_name'])) {
  272. $this->_browser = $uaExtract['browser_name'];
  273. }
  274. if (isset($uaExtract['browser_version'])) {
  275. $this->_browserVersion = $uaExtract['browser_version'];
  276. }
  277. if (isset($uaExtract['device_os'])) {
  278. $this->device_os = $uaExtract['device_os_name'];
  279. }
  280. /* browser & device info */
  281. $this->setFeature('is_wireless_device', false, 'product_info');
  282. $this->setFeature('is_mobile', false, 'product_info');
  283. $this->setFeature('is_desktop', false, 'product_info');
  284. $this->setFeature('is_tablet', false, 'product_info');
  285. $this->setFeature('is_bot', false, 'product_info');
  286. $this->setFeature('is_email', false, 'product_info');
  287. $this->setFeature('is_text', false, 'product_info');
  288. $this->setFeature('device_claims_web_support', false, 'product_info');
  289. $this->setFeature('is_' . strtolower($this->getType()), true, 'product_info');
  290. /* sets the browser name */
  291. if (isset($this->list) && empty($this->_browser)) {
  292. $lowerUserAgent = strtolower($this->getUserAgent());
  293. foreach ($this->list as $browser_signature) {
  294. if (strpos($lowerUserAgent, $browser_signature) !== false) {
  295. $this->_browser = strtolower($browser_signature);
  296. $this->setFeature('browser_name', $this->_browser, 'product_info');
  297. }
  298. }
  299. }
  300. /* sets the client IP */
  301. if (isset($this->_server['remote_addr'])) {
  302. $this->setFeature('client_ip', $this->_server['remote_addr'], 'product_info');
  303. } elseif (isset($this->_server['http_x_forwarded_for'])) {
  304. $this->setFeature('client_ip', $this->_server['http_x_forwarded_for'], 'product_info');
  305. } elseif (isset($this->_server['http_client_ip'])) {
  306. $this->setFeature('client_ip', $this->_server['http_client_ip'], 'product_info');
  307. }
  308. /* sets the server infos */
  309. if (isset($this->_server['server_software'])) {
  310. if (strpos($this->_server['server_software'], 'Apache') !== false || strpos($this->_server['server_software'], 'LiteSpeed') !== false) {
  311. $server['version'] = 1;
  312. if (strpos($this->_server['server_software'], 'Apache/2') !== false) {
  313. $server['version'] = 2;
  314. }
  315. $server['server'] = 'apache';
  316. }
  317. if (strpos($this->_server['server_software'], 'Microsoft-IIS') !== false) {
  318. $server['server'] = 'iis';
  319. }
  320. if (strpos($this->_server['server_software'], 'Unix') !== false) {
  321. $server['os'] = 'unix';
  322. if (isset($_ENV['MACHTYPE'])) {
  323. if (strpos($_ENV['MACHTYPE'], 'linux') !== false) {
  324. $server['os'] = 'linux';
  325. }
  326. }
  327. } elseif (strpos($this->_server['server_software'], 'Win') !== false) {
  328. $server['os'] = 'windows';
  329. }
  330. if (preg_match('/Apache\/([0-9\.]*)/', $this->_server['server_software'], $arr)) {
  331. if ($arr[1]) {
  332. $server['version'] = $arr[1];
  333. $server['server'] = 'apache';
  334. }
  335. }
  336. }
  337. $this->setFeature('php_version', phpversion(), 'server_info');
  338. if (isset($server['server'])) {
  339. $this->setFeature('server_os', $server['server'], 'server_info');
  340. }
  341. if (isset($server['version'])) {
  342. $this->setFeature('server_os_version', $server['version'], 'server_info');
  343. }
  344. if (isset($this->_server['http_accept'])) {
  345. $this->setFeature('server_http_accept', $this->_server['http_accept'], 'server_info');
  346. }
  347. if (isset($this->_server['http_accept_language'])) {
  348. $this->setFeature('server_http_accept_language', $this->_server['http_accept_language'], 'server_info');
  349. }
  350. if (isset($this->_server['server_addr'])) {
  351. $this->setFeature('server_ip', $this->_server['server_addr'], 'server_info');
  352. }
  353. if (isset($this->_server['server_name'])) {
  354. $this->setFeature('server_name', $this->_server['server_name'], 'server_info');
  355. }
  356. }
  357. /**
  358. * Extract and sets informations from the User Agent chain
  359. *
  360. * @param string $userAgent User Agent chain
  361. * @return array
  362. */
  363. public static function extractFromUserAgent($userAgent)
  364. {
  365. $userAgent = trim($userAgent);
  366. /**
  367. * @see http://www.texsoft.it/index.php?c=software&m=sw.php.useragent&l=it
  368. */
  369. $pattern = "(([^/\s]*)(/(\S*))?)(\s*\[[a-zA-Z][a-zA-Z]\])?\s*(\\((([^()]|(\\([^()]*\\)))*)\\))?\s*";
  370. preg_match("#^$pattern#", $userAgent, $match);
  371. $comment = array();
  372. if (isset($match[7])) {
  373. $comment = explode(';', $match[7]);
  374. }
  375. // second part if exists
  376. $end = substr($userAgent, strlen($match[0]));
  377. if (!empty($end)) {
  378. $result['others']['full'] = $end;
  379. }
  380. $match2 = array();
  381. if (isset($result['others'])) {
  382. preg_match_all('/(([^\/\s]*)(\/)?([^\/\(\)\s]*)?)(\s\((([^\)]*)*)\))?/i', $result['others']['full'], $match2);
  383. }
  384. $result['user_agent'] = trim($match[1]);
  385. $result['product_name'] = isset($match[2]) ? trim($match[2]) : '';
  386. $result['browser_name'] = $result['product_name'];
  387. if (isset($match[4]) && trim($match[4])) {
  388. $result['product_version'] = trim($match[4]);
  389. $result['browser_version'] = trim($match[4]);
  390. }
  391. if (count($comment) && !empty($comment[0])) {
  392. $result['comment']['full'] = trim($match[7]);
  393. $result['comment']['detail'] = $comment;
  394. $result['compatibility_flag'] = trim($comment[0]);
  395. if (isset($comment[1])) {
  396. $result['browser_token'] = trim($comment[1]);
  397. }
  398. if (isset($comment[2])) {
  399. $result['device_os_token'] = trim($comment[2]);
  400. }
  401. }
  402. if (empty($result['device_os_token']) && !empty($result['compatibility_flag'])) {
  403. // some browsers do not have a platform token
  404. $result['device_os_token'] = $result['compatibility_flag'];
  405. }
  406. if ($match2) {
  407. $i = 0;
  408. $max = count($match2[0]);
  409. for ($i = 0; $i < $max; $i ++) {
  410. if (!empty($match2[0][$i])) {
  411. $result['others']['detail'][] = array(
  412. $match2[0][$i],
  413. $match2[2][$i],
  414. $match2[4][$i],
  415. );
  416. }
  417. }
  418. }
  419. /** Security level */
  420. $security = array(
  421. 'N' => 'no security',
  422. 'U' => 'strong security',
  423. 'I' => 'weak security',
  424. );
  425. if (!empty($result['browser_token'])) {
  426. if (isset($security[$result['browser_token']])) {
  427. $result['security_level'] = $security[$result['browser_token']];
  428. unset($result['browser_token']);
  429. }
  430. }
  431. $product = strtolower($result['browser_name']);
  432. // Mozilla : true && false
  433. $compatibleOrIe = false;
  434. if (isset($result['compatibility_flag']) && isset($result['comment'])) {
  435. $compatibleOrIe = ($result['compatibility_flag'] == 'compatible' || strpos($result['comment']['full'], "MSIE") !== false);
  436. }
  437. if ($product == 'mozilla' && $compatibleOrIe) {
  438. if (!empty($result['browser_token'])) {
  439. // Classic Mozilla chain
  440. preg_match_all('/([^\/\s].*)(\/|\s)(.*)/i', $result['browser_token'], $real);
  441. } else {
  442. // MSIE specific chain with 'Windows' compatibility flag
  443. foreach ($result['comment']['detail'] as $v) {
  444. if (strpos($v, 'MSIE') !== false) {
  445. $real[0][1] = trim($v);
  446. $result['browser_engine'] = "MSIE";
  447. $real[1][0] = "Internet Explorer";
  448. $temp = explode(' ', trim($v));
  449. $real[3][0] = $temp[1];
  450. }
  451. if (strpos($v, 'Win') !== false) {
  452. $result['device_os_token'] = trim($v);
  453. }
  454. }
  455. }
  456. if (!empty($real[0])) {
  457. $result['browser_name'] = $real[1][0];
  458. $result['browser_version'] = $real[3][0];
  459. } else {
  460. $result['browser_name'] = $result['browser_token'];
  461. $result['browser_version'] = '??';
  462. }
  463. } elseif ($product == 'mozilla' && $result['browser_version'] < 5.0) {
  464. // handles the real Mozilla (or old Netscape if version < 5.0)
  465. $result['browser_name'] = 'Netscape';
  466. }
  467. /** windows */
  468. if ($result['browser_name'] == 'MSIE') {
  469. $result['browser_engine'] = 'MSIE';
  470. $result['browser_name'] = 'Internet Explorer';
  471. }
  472. if (isset($result['device_os_token'])) {
  473. if (strpos($result['device_os_token'], 'Win') !== false) {
  474. $windows = array(
  475. 'Windows NT 6.1' => 'Windows 7',
  476. 'Windows NT 6.0' => 'Windows Vista',
  477. 'Windows NT 5.2' => 'Windows Server 2003',
  478. 'Windows NT 5.1' => 'Windows XP',
  479. 'Windows NT 5.01' => 'Windows 2000 SP1',
  480. 'Windows NT 5.0' => 'Windows 2000',
  481. 'Windows NT 4.0' => 'Microsoft Windows NT 4.0',
  482. 'WinNT' => 'Microsoft Windows NT 4.0',
  483. 'Windows 98; Win 9x 4.90' => 'Windows Me',
  484. 'Windows 98' => 'Windows 98',
  485. 'Win98' => 'Windows 98',
  486. 'Windows 95' => 'Windows 95',
  487. 'Win95' => 'Windows 95',
  488. 'Windows CE' => 'Windows CE',
  489. );
  490. if (isset($windows[$result['device_os_token']])) {
  491. $result['device_os_name'] = $windows[$result['device_os_token']];
  492. } else {
  493. $result['device_os_name'] = $result['device_os_token'];
  494. }
  495. }
  496. }
  497. // iphone
  498. $apple_device = array(
  499. 'iPhone',
  500. 'iPod',
  501. 'iPad',
  502. );
  503. if (isset($result['compatibility_flag'])) {
  504. if (in_array($result['compatibility_flag'], $apple_device)) {
  505. $result['device'] = strtolower($result['compatibility_flag']);
  506. $result['device_os_token'] = 'iPhone OS';
  507. $result['browser_language'] = trim($comment[3]);
  508. $result['browser_version'] = $result['others']['detail'][1][2];
  509. if (!empty($result['others']['detail'][2])) {
  510. $result['firmware'] = $result['others']['detail'][2][2];
  511. }
  512. if (!empty($result['others']['detail'][3])) {
  513. $result['browser_name'] = $result['others']['detail'][3][1];
  514. $result['browser_build'] = $result['others']['detail'][3][2];
  515. }
  516. }
  517. }
  518. // Safari
  519. if (isset($result['others'])) {
  520. if ($result['others']['detail'][0][1] == 'AppleWebKit') {
  521. $result['browser_engine'] = 'AppleWebKit';
  522. if ($result['others']['detail'][1][1] == 'Version') {
  523. $result['browser_version'] = $result['others']['detail'][1][2];
  524. } else {
  525. $result['browser_version'] = $result['others']['detail'][count($result['others']['detail']) - 1][2];
  526. }
  527. if (isset($comment[3])) {
  528. $result['browser_language'] = trim($comment[3]);
  529. }
  530. $last = $result['others']['detail'][count($result['others']['detail']) - 1][1];
  531. if (empty($result['others']['detail'][2][1]) || $result['others']['detail'][2][1] == 'Safari') {
  532. $result['browser_name'] = ($result['others']['detail'][1][1] && $result['others']['detail'][1][1] != 'Version' ? $result['others']['detail'][1][1] : 'Safari');
  533. $result['browser_version'] = ($result['others']['detail'][1][2] ? $result['others']['detail'][1][2] : $result['others']['detail'][0][2]);
  534. } else {
  535. $result['browser_name'] = $result['others']['detail'][2][1];
  536. $result['browser_version'] = $result['others']['detail'][2][2];
  537. // mobile version
  538. if ($result['browser_name'] == 'Mobile') {
  539. $result['browser_name'] = 'Safari ' . $result['browser_name'];
  540. if ($result['others']['detail'][1][1] == 'Version') {
  541. $result['browser_version'] = $result['others']['detail'][1][2];
  542. }
  543. }
  544. }
  545. // For Safari < 2.2, AppleWebKit version gives the Safari version
  546. if (strpos($result['browser_version'], '.') > 2 || (int) $result['browser_version'] > 20) {
  547. $temp = explode('.', $result['browser_version']);
  548. $build = (int) $temp[0];
  549. $awkVersion = array(
  550. 48 => '0.8',
  551. 73 => '0.9',
  552. 85 => '1.0',
  553. 103 => '1.1',
  554. 124 => '1.2',
  555. 300 => '1.3',
  556. 400 => '2.0',
  557. );
  558. foreach ($awkVersion as $k => $v) {
  559. if ($build >= $k) {
  560. $result['browser_version'] = $v;
  561. }
  562. }
  563. }
  564. }
  565. // Gecko (Firefox or compatible)
  566. if ($result['others']['detail'][0][1] == 'Gecko') {
  567. $searchRV = true;
  568. if (!empty($result['others']['detail'][1][1]) && !empty($result['others']['detail'][count($result['others']['detail']) - 1][2]) || strpos(strtolower($result['others']['full']), 'opera') !== false) {
  569. $searchRV = false;
  570. $result['browser_engine'] = $result['others']['detail'][0][1];
  571. // the name of the application is at the end indepenently
  572. // of quantity of information in $result['others']['detail']
  573. $last = count($result['others']['detail']) - 1;
  574. // exception : if the version of the last information is
  575. // empty we take the previous one
  576. if (empty($result['others']['detail'][$last][2])) {
  577. $last --;
  578. }
  579. // exception : if the last one is 'Red Hat' or 'Debian' =>
  580. // use rv: to find browser_version */
  581. if (in_array($result['others']['detail'][$last][1], array(
  582. 'Debian',
  583. 'Hat',
  584. ))) {
  585. $searchRV = true;
  586. }
  587. $result['browser_name'] = $result['others']['detail'][$last][1];
  588. $result['browser_version'] = $result['others']['detail'][$last][2];
  589. if (isset($comment[4])) {
  590. $result['browser_build'] = trim($comment[4]);
  591. }
  592. $result['browser_language'] = trim($comment[3]);
  593. // Netscape
  594. if ($result['browser_name'] == 'Navigator' || $result['browser_name'] == 'Netscape6') {
  595. $result['browser_name'] = 'Netscape';
  596. }
  597. }
  598. if ($searchRV) {
  599. // Mozilla alone : the version is identified by rv:
  600. $result['browser_name'] = 'Mozilla';
  601. if (isset($result['comment']['detail'])) {
  602. foreach ($result['comment']['detail'] as $rv) {
  603. if (strpos($rv, 'rv:') !== false) {
  604. $result['browser_version'] = trim(str_replace('rv:', '', $rv));
  605. }
  606. }
  607. }
  608. }
  609. }
  610. // Netscape
  611. if ($result['others']['detail'][0][1] == 'Netscape') {
  612. $result['browser_name'] = 'Netscape';
  613. $result['browser_version'] = $result['others']['detail'][0][2];
  614. }
  615. // Opera
  616. // Opera: engine Presto
  617. if ($result['others']['detail'][0][1] == 'Presto') {
  618. $result['browser_engine'] = 'Presto';
  619. if (!empty($result['others']['detail'][1][2])) {
  620. $result['browser_version'] = $result['others']['detail'][1][2];
  621. }
  622. }
  623. // UA ends with 'Opera X.XX'
  624. if ($result['others']['detail'][0][1] == 'Opera') {
  625. $result['browser_name'] = $result['others']['detail'][0][1];
  626. $result['browser_version'] = $result['others']['detail'][1][1];
  627. }
  628. // Opera Mini
  629. if (isset($result["browser_token"])) {
  630. if (strpos($result["browser_token"], 'Opera Mini') !== false) {
  631. $result['browser_name'] = 'Opera Mini';
  632. }
  633. }
  634. // Symbian
  635. if ($result['others']['detail'][0][1] == 'SymbianOS') {
  636. $result['device_os_token'] = 'SymbianOS';
  637. }
  638. }
  639. // UA ends with 'Opera X.XX'
  640. if (isset($result['browser_name']) && isset($result['browser_engine'])) {
  641. if ($result['browser_name'] == 'Opera' && $result['browser_engine'] == 'Gecko' && empty($result['browser_version'])) {
  642. $result['browser_version'] = $result['others']['detail'][count($result['others']['detail']) - 1][1];
  643. }
  644. }
  645. // cleanup
  646. if (isset($result['browser_version']) && isset($result['browser_build'])) {
  647. if ($result['browser_version'] == $result['browser_build']) {
  648. unset($result['browser_build']);
  649. }
  650. }
  651. // compatibility
  652. $compatibility['AppleWebKit'] = 'Safari';
  653. $compatibility['Gecko'] = 'Firefox';
  654. $compatibility['MSIE'] = 'Internet Explorer';
  655. $compatibility['Presto'] = 'Opera';
  656. if (!empty($result['browser_engine'])) {
  657. if (isset($compatibility[$result['browser_engine']])) {
  658. $result['browser_compatibility'] = $compatibility[$result['browser_engine']];
  659. }
  660. }
  661. ksort($result);
  662. return $result;
  663. }
  664. /**
  665. * Loads the Features Adapter if it's defined in the $config array
  666. * Otherwise, nothing is done
  667. *
  668. * @param string $browserType Browser type
  669. * @return array
  670. */
  671. protected function _loadFeaturesAdapter()
  672. {
  673. $config = $this->_config;
  674. $browserType = $this->getType();
  675. if (!isset($config[$browserType]) || !isset($config[$browserType]['features'])) {
  676. return array();
  677. }
  678. $config = $config[$browserType]['features'];
  679. if (empty($config['classname'])) {
  680. #require_once 'Zend/Http/UserAgent/Exception.php';
  681. throw new Zend_Http_UserAgent_Exception('The ' . $this->getType() . ' features adapter must have a "classname" config parameter defined');
  682. }
  683. $className = $config['classname'];
  684. if (!class_exists($className)) {
  685. if (isset($config['path'])) {
  686. $path = $config['path'];
  687. } else {
  688. #require_once 'Zend/Http/UserAgent/Exception.php';
  689. throw new Zend_Http_UserAgent_Exception('The ' . $this->getType() . ' features adapter must have a "path" config parameter defined');
  690. }
  691. if (false === include_once ($path)) {
  692. #require_once 'Zend/Http/UserAgent/Exception.php';
  693. throw new Zend_Http_UserAgent_Exception('The ' . $this->getType() . ' features adapter path that does not exist');
  694. }
  695. }
  696. return call_user_func(array($className, 'getFromRequest'), $this->_server, $this->_config);
  697. }
  698. /**
  699. * Retrieve image format support
  700. *
  701. * @return array
  702. */
  703. public function getImageFormatSupport()
  704. {
  705. return $this->_images;
  706. }
  707. /**
  708. * Get maximum image height supported by this device
  709. *
  710. * @return int
  711. */
  712. public function getMaxImageHeight()
  713. {
  714. return null;
  715. }
  716. /**
  717. * Get maximum image width supported by this device
  718. *
  719. * @return int
  720. */
  721. public function getMaxImageWidth()
  722. {
  723. return null;
  724. }
  725. /**
  726. * Get physical screen height of this device
  727. *
  728. * @return int
  729. */
  730. public function getPhysicalScreenHeight()
  731. {
  732. return null;
  733. }
  734. /**
  735. * Get physical screen width of this device
  736. *
  737. * @return int
  738. */
  739. public function getPhysicalScreenWidth()
  740. {
  741. return null;
  742. }
  743. /**
  744. * Get preferred markup type
  745. *
  746. * @return string
  747. */
  748. public function getPreferredMarkup()
  749. {
  750. return 'xhtml';
  751. }
  752. /**
  753. * Get supported X/HTML version
  754. *
  755. * @return int
  756. */
  757. public function getXhtmlSupportLevel()
  758. {
  759. return 4;
  760. }
  761. /**
  762. * Does the device support Flash?
  763. *
  764. * @return bool
  765. */
  766. public function hasFlashSupport()
  767. {
  768. return true;
  769. }
  770. /**
  771. * Does the device support PDF?
  772. *
  773. * @return bool
  774. */
  775. public function hasPdfSupport()
  776. {
  777. return true;
  778. }
  779. /**
  780. * Does the device have a phone number associated with it?
  781. *
  782. * @return bool
  783. */
  784. public function hasPhoneNumber()
  785. {
  786. return false;
  787. }
  788. /**
  789. * Does the device support HTTPS?
  790. *
  791. * @return bool
  792. */
  793. public function httpsSupport()
  794. {
  795. return true;
  796. }
  797. /**
  798. * Get the browser type
  799. *
  800. * @return string
  801. */
  802. public function getBrowser()
  803. {
  804. return $this->_browser;
  805. }
  806. /**
  807. * Get the browser version
  808. *
  809. * @return string
  810. */
  811. public function getBrowserVersion()
  812. {
  813. return $this->_browserVersion;
  814. }
  815. /**
  816. * Get the user agent string
  817. *
  818. * @return string
  819. */
  820. public function getUserAgent()
  821. {
  822. return $this->_userAgent;
  823. }
  824. /**
  825. * @return the $_images
  826. */
  827. public function getImages()
  828. {
  829. return $this->_images;
  830. }
  831. /**
  832. * @param string $browser
  833. */
  834. public function setBrowser($browser)
  835. {
  836. $this->_browser = $browser;
  837. }
  838. /**
  839. * @param string $browserVersion
  840. */
  841. public function setBrowserVersion($browserVersion)
  842. {
  843. $this->_browserVersion = $browserVersion;
  844. }
  845. /**
  846. * @param string $userAgent
  847. */
  848. public function setUserAgent($userAgent)
  849. {
  850. $this->_userAgent = $userAgent;
  851. return $this;
  852. }
  853. /**
  854. * @param array $_images
  855. */
  856. public function setImages($_images)
  857. {
  858. $this->_images = $_images;
  859. }
  860. /**
  861. * Match a user agent string against a list of signatures
  862. *
  863. * @param string $userAgent
  864. * @param array $signatures
  865. * @return bool
  866. */
  867. protected static function _matchAgentAgainstSignatures($userAgent, $signatures)
  868. {
  869. $userAgent = strtolower($userAgent);
  870. foreach ($signatures as $signature) {
  871. if (!empty($signature)) {
  872. if (strpos($userAgent, $signature) !== false) {
  873. // Browser signature was found in user agent string
  874. return true;
  875. }
  876. }
  877. }
  878. return false;
  879. }
  880. }