2
0
Просмотр исходного кода

Adding TeraWurfl and DeviceAtlas adapters with tests

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23122 44c647ce-9c0f-0410-b52a-842ac1e357ba
interakting 15 лет назад
Родитель
Сommit
58766fcd2d

+ 76 - 0
library/Zend/Http/UserAgent/Features/Adapter/DeviceAtlas.php

@@ -0,0 +1,76 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UserAgent
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * Zend_Http_UserAgent_Features_Adapter_Interface
+ */
+require_once 'Zend/Http/UserAgent/Features/Adapter.php';
+
+/**
+ * Features adapter build with the Tera Wurfl Api
+ * See installation instruction here : http://deviceatlas.com/licences 
+ * Download : http://deviceatlas.com/getAPI/php
+ *
+ * @package    Zend_Http
+ * @subpackage UserAgent
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Http_UserAgent_Features_Adapter_DeviceAtlas implements Zend_Http_UserAgent_Features_Adapter
+{
+
+    /**
+     * Get features from request
+     *
+     * @param  array $request $_SERVER variable
+     * @return array
+     */
+    public static function getFromRequest($request, array $config)
+    {
+        if (!isset($config['deviceatlas'])) {
+            require_once 'Zend/Http/UserAgent/Features/Exception.php';
+            throw new Zend_Http_UserAgent_Features_Exception('"DeviceAtlas" configuration is not defined');
+        }
+        
+        $config = $config['deviceatlas'];
+        
+        if (empty($config['deviceatlas_lib_dir'])) {
+            require_once 'Zend/Http/UserAgent/Features/Exception.php';
+            throw new Zend_Http_UserAgent_Features_Exception('The "deviceatlas_lib_dir" parameter is not defined');
+        }
+        
+        if (empty($config['deviceatlas_data'])) {
+            require_once 'Zend/Http/UserAgent/Features/Exception.php';
+            throw new Zend_Http_UserAgent_Features_Exception('The "deviceatlas_data" parameter is not defined');
+        }
+        
+        // Include the Device Atlas file
+        require_once ($config['deviceatlas_lib_dir'] . '/Mobi/Mtld/DA/Api.php');
+        
+        //load the device data-tree : e.g. 'json/DeviceAtlas.json
+        $tree = Mobi_Mtld_DA_Api::getTreeFromFile($config['deviceatlas_data']);
+        
+        $properties = Mobi_Mtld_DA_Api::getProperties($tree, $request['http_user_agent']);
+        
+        return $properties;
+    }
+
+}

+ 95 - 0
library/Zend/Http/UserAgent/Features/Adapter/TeraWurfl.php

@@ -0,0 +1,95 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http
+ * @subpackage UserAgent
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+/**
+ * Zend_Http_UserAgent_Features_Adapter_Interface
+ */
+require_once 'Zend/Http/UserAgent/Features/Adapter.php';
+
+/**
+ * Features adapter build with the Tera Wurfl Api
+ * See installation instruction here : http://www.tera-wurfl.com/wiki/index.php/Installation 
+ * Download : http://www.tera-wurfl.com/wiki/index.php/Downloads
+ *
+ * @package    Zend_Http
+ * @subpackage UserAgent
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Http_UserAgent_Features_Adapter_TeraWurfl implements Zend_Http_UserAgent_Features_Adapter
+{
+
+    /**
+     * Get features from request
+     *
+     * @param  array $request $_SERVER variable
+     * @return array
+     */
+    public static function getFromRequest($request, array $config)
+    {
+        if (!isset($config['terawurfl'])) {
+            require_once 'Zend/Http/UserAgent/Features/Exception.php';
+            throw new Zend_Http_UserAgent_Features_Exception('"TeraWurfl" configuration is not defined');
+        }
+        
+        $config = $config['terawurfl'];
+        
+        if (empty($config['terawurfl_lib_dir'])) {
+            require_once 'Zend/Http/UserAgent/Features/Exception.php';
+            throw new Zend_Http_UserAgent_Features_Exception('The "terawurfl_lib_dir" parameter is not defined');
+        }
+        
+        // Include the Tera-WURFL file
+        require_once ($config['terawurfl_lib_dir'] . '/TeraWurfl.php');
+        
+        // instantiate the Tera-WURFL object
+        $wurflObj = new TeraWurfl();
+        
+        // Get the capabilities of the current client.
+        $matched = $wurflObj->getDeviceCapabilitiesFromRequest(array_change_key_case($request, CASE_UPPER));
+        
+        return self::getAllCapabilities($wurflObj);
+    }
+
+    /***
+     * Builds an array with all capabilities
+     * 
+     * @param TeraWurfl $wurflObj TeraWurfl object
+     */
+    public static function getAllCapabilities(TeraWurfl $wurflObj)
+    {
+        
+        foreach ($wurflObj->capabilities as $group) {
+            if (!is_array($group)) {
+                continue;
+            }
+            while (list ($key, $value) = each($group)) {
+                if (is_bool($value)) {
+                    // to have the same type than the official WURFL API
+                    $features[$key] = ($value ? 'true' : 'false');
+                } else {
+                    $features[$key] = $value;
+                }
+            }
+        }
+        return $features;
+    }
+}

+ 68 - 0
tests/Zend/Http/UserAgent/Features/Adapter/DeviceAtlasTest.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http_UserAgent
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+require_once dirname(__FILE__) . '/../../../../../TestHelper.php';
+
+/**
+ * Zend_Http_UserAgent
+ */
+require_once 'Zend/Http/UserAgent.php';
+require_once 'Zend/Http/UserAgent/Features/Adapter/DeviceAtlas.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Http_UserAgent
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Http_UserAgent_Features_Adapter_DeviceAtlasTest extends PHPUnit_Framework_TestCase
+{
+
+    public function setUp()
+    {
+        if (!constant('TESTS_ZEND_HTTP_USERAGENT_DEVICEATLAS_LIB_DIR') || !constant('TESTS_ZEND_HTTP_USERAGENT_DEVICEATLAS_DATA_FILE')) {
+            $this->markTestSkipped('Requires Device Atlas library');
+        }
+        $this->config['deviceatlas']['deviceatlas_lib_dir'] = constant('TESTS_ZEND_HTTP_USERAGENT_DEVICEATLAS_LIB_DIR');
+        $this->config['deviceatlas']['deviceatlas_data'] = constant('TESTS_ZEND_HTTP_USERAGENT_DEVICEATLAS_DATA_FILE');
+        $this->config['mobile']['features']['path'] = 'Zend/Http/UserAgent/Features/Adapter/DeviceAtlas.php';
+        $this->config['mobile']['features']['classname'] = 'Zend_Http_UserAgent_Features_Adapter_DeviceAtlas';
+    }
+
+    public function testGetFromRequest()
+    {
+        $request['http_user_agent'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleW1ebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3';
+        $deviceAtlas = Zend_Http_UserAgent_Features_Adapter_DeviceAtlas::getFromRequest($request, $this->config);
+        $this->assertEquals(1, $deviceAtlas['touchScreen']);
+        $this->assertEquals(1, $deviceAtlas['markup.xhtmlBasic10']);
+        $this->assertEquals('iPhone', $deviceAtlas['model']);
+        $this->assertEquals('Mozilla/5.0 (iPhone; U; C', $deviceAtlas['_matched']);
+        $this->assertEquals('Apple', $deviceAtlas['vendor']);
+        
+        $request['http_user_agent'] = 'SonyEricssonK700i/R2AC SEMC-Browser/4.0.2 Profile/MIDP-2.0 Configuration/CLDC-1.1';
+        $deviceAtlas = Zend_Http_UserAgent_Features_Adapter_DeviceAtlas::getFromRequest($request, $this->config);
+        $this->assertEquals(20000, $deviceAtlas['memoryLimitMarkup']);
+        $this->assertEquals('K700i', $deviceAtlas['model']);
+        $this->assertEquals('SonyEricssonK700i', $deviceAtlas['_matched']);
+        $this->assertEquals('Sony Ericsson', $deviceAtlas['vendor']);
+    }
+}

+ 72 - 0
tests/Zend/Http/UserAgent/Features/Adapter/TeraWurflTest.php

@@ -0,0 +1,72 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Http_UserAgent
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+
+require_once dirname(__FILE__) . '/../../../../../TestHelper.php';
+
+/**
+ * Zend_Http_UserAgent
+ */
+require_once 'Zend/Http/UserAgent.php';
+require_once 'Zend/Http/UserAgent/Features/Adapter/TeraWurfl.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Http_UserAgent
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Http_UserAgent_Features_Adapter_TeraWurflTest extends PHPUnit_Framework_TestCase
+{
+
+    public function setUp()
+    {
+        if (!constant('TESTS_ZEND_HTTP_USERAGENT_TERAWURFL_LIB_DIR')) {
+            $this->markTestSkipped('Requires TERAWURFL library');
+        }
+        $this->config['terawurfl']['terawurfl_lib_dir'] = constant('TESTS_ZEND_HTTP_USERAGENT_TERAWURFL_LIB_DIR');
+        error_reporting(E_ALL ^ E_NOTICE); // TeraWurfl.php having Notice messages
+    }
+
+    public function testGetFromRequest()
+    {
+        $request['http_user_agent'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleW1ebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3';
+        $wurfl = Zend_Http_UserAgent_Features_Adapter_TeraWurfl::getFromRequest($request, $this->config);
+        $this->assertEquals('Safari', $wurfl['mobile_browser']);
+        $this->assertEquals('iPhone OS', $wurfl['device_os']);
+        $this->assertEquals('1.0', $wurfl['device_os_version']);
+        $this->assertEquals('true', $wurfl['has_qwerty_keyboard']);
+        $this->assertEquals('touchscreen', $wurfl['pointing_method']);
+        $this->assertEquals('false', $wurfl['is_tablet']);
+        $this->assertEquals('iPhone', $wurfl['model_name']);
+        $this->assertEquals('Apple', $wurfl['brand_name']);
+        
+        $request['http_user_agent'] = 'SonyEricssonK700i/R2AC SEMC-Browser/4.0.2 Profile/MIDP-2.0 Configuration/CLDC-1.1';
+        $wurfl = Zend_Http_UserAgent_Features_Adapter_TeraWurfl::getFromRequest($request, $this->config);
+        $this->assertEquals('http://wap.sonyericsson.com/UAprof/K700iR101.xml', $wurfl['uaprof']);
+        $this->assertEquals('SonyEricsson', $wurfl['brand_name']);
+        $this->assertEquals('2002_january', $wurfl['release_date']);
+        $this->assertEquals('false', $wurfl['has_qwerty_keyboard']);
+        $this->assertEquals('', $wurfl['pointing_method']);
+        $this->assertEquals('false', $wurfl['is_tablet']);
+        $this->assertEquals('K700i', $wurfl['model_name']);
+    }
+}

+ 41 - 20
tests/Zend/Http/index.php

@@ -7,8 +7,16 @@ $autoloader = Zend_Loader_Autoloader::getInstance();
 error_reporting(E_ALL);
 set_time_limit(0);
 
-$config['config']['wurflapi']['wurfl_lib_dir'] = dirname(__FILE__) . '/_files/Wurfl/1.1/';
-$config['config']['wurflapi']['wurfl_config_file'] = dirname(__FILE__) . '/_files/Wurfl/resources/wurfl-config.php';
+$config['wurflapi']['wurfl_lib_dir'] = dirname(__FILE__) . '/_files/Wurfl/1.1/';
+$config['wurflapi']['wurfl_config_file'] = dirname(__FILE__) . '/_files/Wurfl/resources/wurfl-config.php';
+$config['terawurfl']['terawurfl_lib_dir'] = dirname(__FILE__) . '/_files/TeraWurfl_2.1.3/tera-WURFL/';
+$config['deviceatlas']['deviceatlas_lib_dir'] = dirname(__FILE__) . '/_files/DA_php_1.4.1/';
+$config['deviceatlas']['deviceatlas_data'] = dirname(__FILE__) . '/_files/DA_php_1.4.1/sample/json/20101014.json';
+$config['mobile']['features']['path']      = 'Zend/Http/UserAgent/Features/Adapter/TeraWurfl.php';
+$config['mobile']['features']['classname'] = 'Zend_Http_UserAgent_Features_Adapter_TeraWurfl';
+$config['mobile']['features']['path']      = 'Zend/Http/UserAgent/Features/Adapter/DeviceAtlas.php';
+$config['mobile']['features']['classname'] = 'Zend_Http_UserAgent_Features_Adapter_DeviceAtlas';
+
 $config['server'] = $_SERVER;
 
 if (!empty($_GET['userAgent'])) {
@@ -18,7 +26,7 @@ if (!empty($_GET['userAgent'])) {
 }
 
 if (!empty($_GET['sequence'])) {
-    $config['config']['identification_sequence'] = $_GET['sequence'];
+    $config['identification_sequence'] = $_GET['sequence'];
 }
 $oUserAgent = new Zend_Http_UserAgent($config);
 
@@ -30,7 +38,7 @@ function printBrowserDetails($browser)
     $device = $browser->getDevice();
     //Zend_Debug::dump($device->getAllFeatures());
     if (isset($device)) {
-        print "<b>General informations</b>";
+        print "<fieldset><legend><b>General informations</b></legend>";
         print "<ul>";
         print "<li>Browser Type: " . $browser->getBrowserType() . "</li>";
         print "<li>Browser Name: " . $device->getFeature('browser_name') . "</li>";
@@ -39,17 +47,19 @@ function printBrowserDetails($browser)
         print "<li>Browser Engine: " . $device->getFeature('browser_engine') . "</li>";
         print "<li>Device OS Name: " . $device->getFeature('device_os_name') . "</li>";
         print "<li>Device OS token: " . $device->getFeature('device_os_token') . "</li>";
-        print "<li>Server Os: " . $device->getFeature('server_os') . "</li>";
-        print "<li>Server OS Version: " . $device->getFeature('server_os_version') . "</li>";
+        print "<li>Server OS: " . $device->getFeature('server_os') . "</li>";
+        print "<li>Server Platform: " . $device->getFeature('server_platfom') . "</li>";
+        print "<li>Server Platform Version: " . $device->getFeature('server_platfom_version') . "</li>";
         print "</ul>";
+        print '</fieldset>';
         
-        $wurfl = $device->getFeature("mobile_browser");
+        $wurfl = $device->getFeature("brand_name");
         if (!$wurfl) {
-            print "<b>no WURFL identification</b>";
+            print "<fieldset><legend><b>no WURFL identification</b></legend>";
+            print '</fieldset>';
         } else {
-            print "<b>WURFL capabilities :</b>";
+            print "<fieldset><legend><b>WURFL capabilities</b></legend>";
             print "<ul>";
-            print "<li>WURFL ID: " . (isset($device->id) ? $device->id : "") . "</li>";
             print "<li>Mobile browser: " . $device->getFeature("mobile_browser") . "</li>";
             print "<li>Mobile browser version: " . $device->getFeature("mobile_browser_version") . "</li>";
             print "<li>Device Brand Name: " . $device->getFeature("brand_name") . "</li>";
@@ -60,33 +70,44 @@ function printBrowserDetails($browser)
             print "<li>Resolution Height:" . $device->getFeature('resolution_height') . "</li>";
             print "<li>MP3:" . $device->getFeature('mp3') . "</li>";
             print "</ul>";
+            print '</fieldset>';
         }
         
-        print "<br /><br />";
-        print "<b>Full</b>";
+        print "<fieldset><legend><b>Full</b></legend>";
         Zend_Debug::dump($device->getAllFeatures());
+        print '</fieldset>';
     }
 
 }
 
+$options = array(
+    '', 
+    'mobile, text, desktop', 
+    'bot, mobile, validator, checker, console, offline, email, text', 
+    'text, bot, validator, checker, console, offline, email'
+);
 ?>
 
 <div id="content">
 
 <p><b>Query by providing the user agent:</b></p>
 <p>look at <a target="_blank"
-	href="http://www.useragentstring.com/pages/useragentstring.php">http://www.useragentstring.com/pages/useragentstring.php</a></p>
+	href="http://www.useragentstring.com/pages/useragentstring.php"
+	target="_blank">http://www.useragentstring.com/pages/useragentstring.php</a>
+or <a href="http://www.user-agents.org/" target="_blank">http://www.user-agents.org/</a></p>
 <p>For mobile, look at <a target="_blank"
-	href="http://www.mobilemultimedia.be/">http://www.mobilemultimedia.be/</a></p>
+	href="http://en.wikipedia.org/wiki/List_of_user_agents_for_mobile_phones">http://en.wikipedia.org/wiki/List_of_user_agents_for_mobile_phones</a></p>
 <fieldset>
 <form method="get">
-<div>Sequence : <select name="sequence">
-	<option value="">(standard)</option>
-	<option value="mobile, text, desktop">mobile, text, desktop</option>
-	<option value="bot, validator, checker, console, offline, email, text">bot,
-	validator, checker, console, offline, email, text</option>
+<div>Sequence : <select name="sequence" style="width: 500">
+	<?php
+foreach ($options as $option) {
+    $selected = ($option == $_GET['sequence'] ? ' selected ' : '');
+    echo '<option value="' . $option . '"' . $selected . '>' . ($option ? $option : '(standard)') . '</option>';
+}
+?>
 </select> (DON'T FORGET TO CLEAN SESSION COOKIE)<br />
-User Agent : <input type="text" name="userAgent" size="40"
+User Agent : <input type="text" name="userAgent" style="width: 700"
 	value="<?=htmlentities($_GET['userAgent'])?>" /> <br />
 <input type="submit" /></div>
 </form>