TeraWurfl.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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-2010 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-2010 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 (!isset($config['terawurfl'])) {
  46. require_once 'Zend/Http/UserAgent/Features/Exception.php';
  47. throw new Zend_Http_UserAgent_Features_Exception('"TeraWurfl" configuration is not defined');
  48. }
  49. $config = $config['terawurfl'];
  50. if (empty($config['terawurfl_lib_dir'])) {
  51. require_once 'Zend/Http/UserAgent/Features/Exception.php';
  52. throw new Zend_Http_UserAgent_Features_Exception('The "terawurfl_lib_dir" parameter is not defined');
  53. }
  54. // Include the Tera-WURFL file
  55. require_once ($config['terawurfl_lib_dir'] . '/TeraWurfl.php');
  56. // instantiate the Tera-WURFL object
  57. $wurflObj = new TeraWurfl();
  58. // Get the capabilities of the current client.
  59. $matched = $wurflObj->getDeviceCapabilitiesFromRequest(array_change_key_case($request, CASE_UPPER));
  60. return self::getAllCapabilities($wurflObj);
  61. }
  62. /***
  63. * Builds an array with all capabilities
  64. *
  65. * @param TeraWurfl $wurflObj TeraWurfl object
  66. */
  67. public static function getAllCapabilities(TeraWurfl $wurflObj)
  68. {
  69. foreach ($wurflObj->capabilities as $group) {
  70. if (!is_array($group)) {
  71. continue;
  72. }
  73. while (list ($key, $value) = each($group)) {
  74. if (is_bool($value)) {
  75. // to have the same type than the official WURFL API
  76. $features[$key] = ($value ? 'true' : 'false');
  77. } else {
  78. $features[$key] = $value;
  79. }
  80. }
  81. }
  82. return $features;
  83. }
  84. }