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

/Zend/Http/UserAgent/AbstractDevice.php

https://bitbucket.org/simukti/zf1
PHP | 995 lines | 601 code | 94 blank | 300 comment | 152 complexity | 686afe1a3949b709ea718f037adac3a6 MD5 | raw file
  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-2012 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-2012 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. if(isset($result['browser_token'])) {
  461. $result['browser_name'] = $result['browser_token'];
  462. }
  463. $result['browser_version'] = '??';
  464. }
  465. } elseif ($product == 'mozilla' && $result['browser_version'] < 5.0) {
  466. // handles the real Mozilla (or old Netscape if version < 5.0)
  467. $result['browser_name'] = 'Netscape';
  468. }
  469. /** windows */
  470. if ($result['browser_name'] == 'MSIE') {
  471. $result['browser_engine'] = 'MSIE';
  472. $result['browser_name'] = 'Internet Explorer';
  473. }
  474. if (isset($result['device_os_token'])) {
  475. if (strpos($result['device_os_token'], 'Win') !== false) {
  476. $windows = array(
  477. 'Windows NT 6.1' => 'Windows 7',
  478. 'Windows NT 6.0' => 'Windows Vista',
  479. 'Windows NT 5.2' => 'Windows Server 2003',
  480. 'Windows NT 5.1' => 'Windows XP',
  481. 'Windows NT 5.01' => 'Windows 2000 SP1',
  482. 'Windows NT 5.0' => 'Windows 2000',
  483. 'Windows NT 4.0' => 'Microsoft Windows NT 4.0',
  484. 'WinNT' => 'Microsoft Windows NT 4.0',
  485. 'Windows 98; Win 9x 4.90' => 'Windows Me',
  486. 'Windows 98' => 'Windows 98',
  487. 'Win98' => 'Windows 98',
  488. 'Windows 95' => 'Windows 95',
  489. 'Win95' => 'Windows 95',
  490. 'Windows CE' => 'Windows CE',
  491. );
  492. if (isset($windows[$result['device_os_token']])) {
  493. $result['device_os_name'] = $windows[$result['device_os_token']];
  494. } else {
  495. $result['device_os_name'] = $result['device_os_token'];
  496. }
  497. }
  498. }
  499. // iphone
  500. $apple_device = array(
  501. 'iPhone',
  502. 'iPod',
  503. 'iPad',
  504. );
  505. if (isset($result['compatibility_flag'])) {
  506. if (in_array($result['compatibility_flag'], $apple_device)) {
  507. $result['device'] = strtolower($result['compatibility_flag']);
  508. $result['device_os_token'] = 'iPhone OS';
  509. if (isset($comment[3])) {
  510. $result['browser_language'] = trim($comment[3]);
  511. }
  512. if (isset($result['others']['detail'][1])) {
  513. $result['browser_version'] = $result['others']['detail'][1][2];
  514. } elseif (isset($result['others']['detail']) && count($result['others']['detail'])) {
  515. $result['browser_version'] = $result['others']['detail'][0][2];
  516. }
  517. if (!empty($result['others']['detail'][2])) {
  518. $result['firmware'] = $result['others']['detail'][2][2];
  519. }
  520. if (!empty($result['others']['detail'][3])) {
  521. $result['browser_name'] = $result['others']['detail'][3][1];
  522. $result['browser_build'] = $result['others']['detail'][3][2];
  523. }
  524. }
  525. }
  526. // Safari
  527. if (isset($result['others'])) {
  528. if ($result['others']['detail'][0][1] == 'AppleWebKit') {
  529. $result['browser_engine'] = 'AppleWebKit';
  530. if (isset($result['others']['detail'][1]) && $result['others']['detail'][1][1] == 'Version') {
  531. $result['browser_version'] = $result['others']['detail'][1][2];
  532. } else {
  533. $result['browser_version'] = $result['others']['detail'][count($result['others']['detail']) - 1][2];
  534. }
  535. if (isset($comment[3])) {
  536. $result['browser_language'] = trim($comment[3]);
  537. }
  538. $last = $result['others']['detail'][count($result['others']['detail']) - 1][1];
  539. if (empty($result['others']['detail'][2][1]) || $result['others']['detail'][2][1] == 'Safari') {
  540. if (isset($result['others']['detail'][1])) {
  541. $result['browser_name'] = ($result['others']['detail'][1][1] && $result['others']['detail'][1][1] != 'Version' ? $result['others']['detail'][1][1] : 'Safari');
  542. $result['browser_version'] = ($result['others']['detail'][1][2] ? $result['others']['detail'][1][2] : $result['others']['detail'][0][2]);
  543. } else {
  544. $result['browser_name'] = ($result['others']['detail'][0][1] && $result['others']['detail'][0][1] != 'Version' ? $result['others']['detail'][0][1] : 'Safari');
  545. $result['browser_version'] = $result['others']['detail'][0][2];
  546. }
  547. } else {
  548. $result['browser_name'] = $result['others']['detail'][2][1];
  549. $result['browser_version'] = $result['others']['detail'][2][2];
  550. // mobile version
  551. if ($result['browser_name'] == 'Mobile') {
  552. $result['browser_name'] = 'Safari ' . $result['browser_name'];
  553. if ($result['others']['detail'][1][1] == 'Version') {
  554. $result['browser_version'] = $result['others']['detail'][1][2];
  555. }
  556. }
  557. }
  558. // For Safari < 2.2, AppleWebKit version gives the Safari version
  559. if (strpos($result['browser_version'], '.') > 2 || (int) $result['browser_version'] > 20) {
  560. $temp = explode('.', $result['browser_version']);
  561. $build = (int) $temp[0];
  562. $awkVersion = array(
  563. 48 => '0.8',
  564. 73 => '0.9',
  565. 85 => '1.0',
  566. 103 => '1.1',
  567. 124 => '1.2',
  568. 300 => '1.3',
  569. 400 => '2.0',
  570. );
  571. foreach ($awkVersion as $k => $v) {
  572. if ($build >= $k) {
  573. $result['browser_version'] = $v;
  574. }
  575. }
  576. }
  577. }
  578. // Gecko (Firefox or compatible)
  579. if ($result['others']['detail'][0][1] == 'Gecko') {
  580. $searchRV = true;
  581. if (!empty($result['others']['detail'][1][1]) && !empty($result['others']['detail'][count($result['others']['detail']) - 1][2]) || strpos(strtolower($result['others']['full']), 'opera') !== false) {
  582. $searchRV = false;
  583. $result['browser_engine'] = $result['others']['detail'][0][1];
  584. // the name of the application is at the end indepenently
  585. // of quantity of information in $result['others']['detail']
  586. $last = count($result['others']['detail']) - 1;
  587. // exception : if the version of the last information is
  588. // empty we take the previous one
  589. if (empty($result['others']['detail'][$last][2])) {
  590. $last --;
  591. }
  592. // exception : if the last one is 'Red Hat' or 'Debian' =>
  593. // use rv: to find browser_version */
  594. if (in_array($result['others']['detail'][$last][1], array(
  595. 'Debian',
  596. 'Hat',
  597. ))) {
  598. $searchRV = true;
  599. }
  600. $result['browser_name'] = $result['others']['detail'][$last][1];
  601. $result['browser_version'] = $result['others']['detail'][$last][2];
  602. if (isset($comment[4])) {
  603. $result['browser_build'] = trim($comment[4]);
  604. }
  605. if (isset($comment[3])) {
  606. $result['browser_language'] = trim($comment[3]);
  607. }
  608. // Netscape
  609. if ($result['browser_name'] == 'Navigator' || $result['browser_name'] == 'Netscape6') {
  610. $result['browser_name'] = 'Netscape';
  611. }
  612. }
  613. if ($searchRV) {
  614. // Mozilla alone : the version is identified by rv:
  615. $result['browser_name'] = 'Mozilla';
  616. if (isset($result['comment']['detail'])) {
  617. foreach ($result['comment']['detail'] as $rv) {
  618. if (strpos($rv, 'rv:') !== false) {
  619. $result['browser_version'] = trim(str_replace('rv:', '', $rv));
  620. }
  621. }
  622. }
  623. }
  624. }
  625. // Netscape
  626. if ($result['others']['detail'][0][1] == 'Netscape') {
  627. $result['browser_name'] = 'Netscape';
  628. $result['browser_version'] = $result['others']['detail'][0][2];
  629. }
  630. // Opera
  631. // Opera: engine Presto
  632. if ($result['others']['detail'][0][1] == 'Presto') {
  633. $result['browser_engine'] = 'Presto';
  634. if (!empty($result['others']['detail'][1][2])) {
  635. $result['browser_version'] = $result['others']['detail'][1][2];
  636. }
  637. }
  638. // UA ends with 'Opera X.XX' or 'Opera/X.XX'
  639. if ($result['others']['detail'][0][1] == 'Opera') {
  640. $result['browser_name'] = $result['others']['detail'][0][1];
  641. // Opera X.XX
  642. if (isset($result['others']['detail'][1][1])) {
  643. $result['browser_version'] = $result['others']['detail'][1][1];
  644. // Opera/X.XX
  645. } elseif (isset($result['others']['detail'][0][2])) {
  646. $result['browser_version'] = $result['others']['detail'][0][2];
  647. }
  648. }
  649. // Opera Mini
  650. if (isset($result["browser_token"])) {
  651. if (strpos($result["browser_token"], 'Opera Mini') !== false) {
  652. $result['browser_name'] = 'Opera Mini';
  653. }
  654. }
  655. // Symbian
  656. if ($result['others']['detail'][0][1] == 'SymbianOS') {
  657. $result['device_os_token'] = 'SymbianOS';
  658. }
  659. }
  660. // UA ends with 'Opera X.XX'
  661. if (isset($result['browser_name']) && isset($result['browser_engine'])) {
  662. if ($result['browser_name'] == 'Opera' && $result['browser_engine'] == 'Gecko' && empty($result['browser_version'])) {
  663. $result['browser_version'] = $result['others']['detail'][count($result['others']['detail']) - 1][1];
  664. }
  665. }
  666. // cleanup
  667. if (isset($result['browser_version']) && isset($result['browser_build'])) {
  668. if ($result['browser_version'] == $result['browser_build']) {
  669. unset($result['browser_build']);
  670. }
  671. }
  672. // compatibility
  673. $compatibility['AppleWebKit'] = 'Safari';
  674. $compatibility['Gecko'] = 'Firefox';
  675. $compatibility['MSIE'] = 'Internet Explorer';
  676. $compatibility['Presto'] = 'Opera';
  677. if (!empty($result['browser_engine'])) {
  678. if (isset($compatibility[$result['browser_engine']])) {
  679. $result['browser_compatibility'] = $compatibility[$result['browser_engine']];
  680. }
  681. }
  682. ksort($result);
  683. return $result;
  684. }
  685. /**
  686. * Loads the Features Adapter if it's defined in the $config array
  687. * Otherwise, nothing is done
  688. *
  689. * @param string $browserType Browser type
  690. * @return array
  691. */
  692. protected function _loadFeaturesAdapter()
  693. {
  694. $config = $this->_config;
  695. $browserType = $this->getType();
  696. if (!isset($config[$browserType]) || !isset($config[$browserType]['features'])) {
  697. return array();
  698. }
  699. $config = $config[$browserType]['features'];
  700. if (empty($config['classname'])) {
  701. require_once 'Zend/Http/UserAgent/Exception.php';
  702. throw new Zend_Http_UserAgent_Exception('The ' . $this->getType() . ' features adapter must have a "classname" config parameter defined');
  703. }
  704. $className = $config['classname'];
  705. if (!class_exists($className)) {
  706. if (isset($config['path'])) {
  707. $path = $config['path'];
  708. } else {
  709. require_once 'Zend/Http/UserAgent/Exception.php';
  710. throw new Zend_Http_UserAgent_Exception('The ' . $this->getType() . ' features adapter must have a "path" config parameter defined');
  711. }
  712. if (false === include_once ($path)) {
  713. require_once 'Zend/Http/UserAgent/Exception.php';
  714. throw new Zend_Http_UserAgent_Exception('The ' . $this->getType() . ' features adapter path that does not exist');
  715. }
  716. }
  717. return call_user_func(array($className, 'getFromRequest'), $this->_server, $this->_config);
  718. }
  719. /**
  720. * Retrieve image format support
  721. *
  722. * @return array
  723. */
  724. public function getImageFormatSupport()
  725. {
  726. return $this->_images;
  727. }
  728. /**
  729. * Get maximum image height supported by this device
  730. *
  731. * @return int
  732. */
  733. public function getMaxImageHeight()
  734. {
  735. return null;
  736. }
  737. /**
  738. * Get maximum image width supported by this device
  739. *
  740. * @return int
  741. */
  742. public function getMaxImageWidth()
  743. {
  744. return null;
  745. }
  746. /**
  747. * Get physical screen height of this device
  748. *
  749. * @return int
  750. */
  751. public function getPhysicalScreenHeight()
  752. {
  753. return null;
  754. }
  755. /**
  756. * Get physical screen width of this device
  757. *
  758. * @return int
  759. */
  760. public function getPhysicalScreenWidth()
  761. {
  762. return null;
  763. }
  764. /**
  765. * Get preferred markup type
  766. *
  767. * @return string
  768. */
  769. public function getPreferredMarkup()
  770. {
  771. return 'xhtml';
  772. }
  773. /**
  774. * Get supported X/HTML version
  775. *
  776. * @return int
  777. */
  778. public function getXhtmlSupportLevel()
  779. {
  780. return 4;
  781. }
  782. /**
  783. * Does the device support Flash?
  784. *
  785. * @return bool
  786. */
  787. public function hasFlashSupport()
  788. {
  789. return true;
  790. }
  791. /**
  792. * Does the device support PDF?
  793. *
  794. * @return bool
  795. */
  796. public function hasPdfSupport()
  797. {
  798. return true;
  799. }
  800. /**
  801. * Does the device have a phone number associated with it?
  802. *
  803. * @return bool
  804. */
  805. public function hasPhoneNumber()
  806. {
  807. return false;
  808. }
  809. /**
  810. * Does the device support HTTPS?
  811. *
  812. * @return bool
  813. */
  814. public function httpsSupport()
  815. {
  816. return true;
  817. }
  818. /**
  819. * Get the browser type
  820. *
  821. * @return string
  822. */
  823. public function getBrowser()
  824. {
  825. return $this->_browser;
  826. }
  827. /**
  828. * Get the browser version
  829. *
  830. * @return string
  831. */
  832. public function getBrowserVersion()
  833. {
  834. return $this->_browserVersion;
  835. }
  836. /**
  837. * Get the user agent string
  838. *
  839. * @return string
  840. */
  841. public function getUserAgent()
  842. {
  843. return $this->_userAgent;
  844. }
  845. /**
  846. * @return the $_images
  847. */
  848. public function getImages()
  849. {
  850. return $this->_images;
  851. }
  852. /**
  853. * @param string $browser
  854. */
  855. public function setBrowser($browser)
  856. {
  857. $this->_browser = $browser;
  858. }
  859. /**
  860. * @param string $browserVersion
  861. */
  862. public function setBrowserVersion($browserVersion)
  863. {
  864. $this->_browserVersion = $browserVersion;
  865. }
  866. /**
  867. * @param string $userAgent
  868. */
  869. public function setUserAgent($userAgent)
  870. {
  871. $this->_userAgent = $userAgent;
  872. return $this;
  873. }
  874. /**
  875. * @param array $_images
  876. */
  877. public function setImages($_images)
  878. {
  879. $this->_images = $_images;
  880. }
  881. /**
  882. * Match a user agent string against a list of signatures
  883. *
  884. * @param string $userAgent
  885. * @param array $signatures
  886. * @return bool
  887. */
  888. protected static function _matchAgentAgainstSignatures($userAgent, $signatures)
  889. {
  890. $userAgent = strtolower($userAgent);
  891. foreach ($signatures as $signature) {
  892. if (!empty($signature)) {
  893. if (strpos($userAgent, $signature) !== false) {
  894. // Browser signature was found in user agent string
  895. return true;
  896. }
  897. }
  898. }
  899. return false;
  900. }
  901. }