PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/Zend/Http/UserAgent/AbstractDevice.php

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