Browscap.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  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-2012 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 implements Zend_Http_UserAgent_Features_Adapter
  37. {
  38. /**
  39. * Constructor
  40. *
  41. * Validate that we have browscap support available.
  42. *
  43. * @return void
  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. foreach ($browscap as $key => $value) {
  69. // For a few keys, we need to munge a bit for the device object
  70. switch ($key) {
  71. case 'browser':
  72. $features['mobile_browser'] = $value;
  73. break;
  74. case 'version':
  75. $features['mobile_browser_version'] = $value;
  76. break;
  77. case 'platform':
  78. $features['device_os'] = $value;
  79. break;
  80. default:
  81. $features[$key] = $value;
  82. break;
  83. }
  84. }
  85. return $features;
  86. }
  87. }