TeraWurfl.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 build with the Tera Wurfl Api
  27. * See installation instruction here : http://www.tera-wurfl.com/wiki/index.php/Installation
  28. * Download : http://www.tera-wurfl.com/wiki/index.php/Downloads
  29. *
  30. * @package Zend_Http
  31. * @subpackage UserAgent
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Http_UserAgent_Features_Adapter_TeraWurfl implements Zend_Http_UserAgent_Features_Adapter
  36. {
  37. /**
  38. * Get features from request
  39. *
  40. * @param array $request $_SERVER variable
  41. * @return array
  42. */
  43. public static function getFromRequest($request, array $config)
  44. {
  45. if (!class_exists('TeraWurfl')) {
  46. // If TeraWurfl class not found, see if we can load it from
  47. // configuration
  48. //
  49. if (!isset($config['terawurfl'])) {
  50. // No configuration
  51. require_once 'Zend/Http/UserAgent/Features/Exception.php';
  52. throw new Zend_Http_UserAgent_Features_Exception('"TeraWurfl" configuration is not defined');
  53. }
  54. $config = $config['terawurfl'];
  55. if (empty($config['terawurfl_lib_dir'])) {
  56. // No lib_dir given
  57. require_once 'Zend/Http/UserAgent/Features/Exception.php';
  58. throw new Zend_Http_UserAgent_Features_Exception('The "terawurfl_lib_dir" parameter is not defined');
  59. }
  60. // Include the Tera-WURFL file
  61. require_once ($config['terawurfl_lib_dir'] . '/TeraWurfl.php');
  62. }
  63. // instantiate the Tera-WURFL object
  64. $wurflObj = new TeraWurfl();
  65. // Get the capabilities of the current client.
  66. $matched = $wurflObj->getDeviceCapabilitiesFromRequest(array_change_key_case($request, CASE_UPPER));
  67. return self::getAllCapabilities($wurflObj);
  68. }
  69. /***
  70. * Builds an array with all capabilities
  71. *
  72. * @param TeraWurfl $wurflObj TeraWurfl object
  73. */
  74. public static function getAllCapabilities(TeraWurfl $wurflObj)
  75. {
  76. foreach ($wurflObj->capabilities as $group) {
  77. if (!is_array($group)) {
  78. continue;
  79. }
  80. foreach ($group as $key => $value) {
  81. if (is_bool($value)) {
  82. // to have the same type than the official WURFL API
  83. $features[$key] = ($value ? 'true' : 'false');
  84. } else {
  85. $features[$key] = $value;
  86. }
  87. }
  88. }
  89. return $features;
  90. }
  91. }