Browscap.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /**
  22. * Zend_Http_UserAgent_Features_Adapter_Interface
  23. */
  24. require_once 'Zend/Http/UserAgent/Features/Adapter.php';
  25. /**
  26. * Features adapter utilizing PHP's native browscap support
  27. *
  28. * Requires that you have a PHP-compatible version of the browscap.ini, per the
  29. * instructions at http://php.net/get_browser
  30. *
  31. * @package Zend_Http
  32. * @subpackage UserAgent
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Http_UserAgent_Features_Adapter_Browscap
  37. implements Zend_Http_UserAgent_Features_Adapter
  38. {
  39. /**
  40. * Constructor
  41. *
  42. * Validate that we have browscap support available.
  43. *
  44. * @throws Zend_Http_UserAgent_Features_Exception
  45. */
  46. public function __construct()
  47. {
  48. $browscap = ini_get('browscap');
  49. if (empty($browscap) || !file_exists($browscap)) {
  50. require_once 'Zend/Http/UserAgent/Features/Exception.php';
  51. throw new Zend_Http_UserAgent_Features_Exception(sprintf(
  52. '%s requires a browscap entry in php.ini pointing to a valid browscap.ini; none present',
  53. __CLASS__
  54. ));
  55. }
  56. }
  57. /**
  58. * Get features from request
  59. *
  60. * @param array $request $_SERVER variable
  61. * @param array $config ignored; included only to satisfy parent class
  62. * @return array
  63. */
  64. public static function getFromRequest($request, array $config)
  65. {
  66. $browscap = get_browser($request['http_user_agent'], true);
  67. $features = array();
  68. if (is_array($browscap)) {
  69. foreach ($browscap as $key => $value) {
  70. // For a few keys, we need to munge a bit for the device object
  71. switch ($key) {
  72. case 'browser':
  73. $features['mobile_browser'] = $value;
  74. break;
  75. case 'version':
  76. $features['mobile_browser_version'] = $value;
  77. break;
  78. case 'platform':
  79. $features['device_os'] = $value;
  80. break;
  81. default:
  82. $features[$key] = $value;
  83. break;
  84. }
  85. }
  86. }
  87. return $features;
  88. }
  89. }