From cc47cabd9ef69df02c64bdd4cc5fca950cd7c68d Mon Sep 17 00:00:00 2001 From: Synchro Date: Tue, 4 Jun 2013 10:54:59 +0200 Subject: [PATCH] Update version to 0.52 Add composer.json Update readme PSR-2 cleanup --- README.md | 11 +++++-- UAS/Parser.php | 78 +++++++++++++++++++++++++++++++------------------- composer.json | 27 +++++++++++++++++ 3 files changed, 84 insertions(+), 32 deletions(-) create mode 100644 composer.json diff --git a/README.md b/README.md index 68198d6..5eaced2 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,14 @@ A User Agent String parser for PHP [![Build Status](https://travis-ci.org/Synchro/UASparser.png)](https://travis-ci.org/Synchro/UASparser) -This is a parser for the user agent strings presented by HTTP clients. This code is based on the libraries available from http://user-agent-string.info +This is a parser and classifier for user agent strings presented by HTTP clients. + +This code is based on the libraries by Jaroslav Mallat available from http://user-agent-string.info/ Licensed under the LGPL, see license.txt for details. -This version amended by Marcus Bointon: +This version improved by [Marcus Bointon](https://github.com/Synchro): +- [Maintained on GitHub](https://github.com/Synchro/UASparser) - Creates a UAS namespace - Adds unit tests - Adds Travis config @@ -18,4 +21,6 @@ This version amended by Marcus Bointon: - Uses the system temp dir for default cache location - Cleans up phpdocs - Reformats code in PSR-2 style -- Fixes poor code in the example script \ No newline at end of file +- Fixes poor code in the example script +- Improves error handling and debugging, adds variable timeouts +- Adds support for gzip compression of database downloads diff --git a/UAS/Parser.php b/UAS/Parser.php index 7b6941a..9ac9d93 100755 --- a/UAS/Parser.php +++ b/UAS/Parser.php @@ -8,7 +8,7 @@ * @copyright Copyright (c) 2010 Alex Stanev (http://stanev.org) * @copyright Copyright (c) 2012 Martin van Wingerden (http://www.copernica.com) * @author Marcus Bointon (https://github.com/Synchro) - * @version 0.51 + * @version 0.52 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @link http://user-agent-string.info/download/UASparser */ @@ -61,7 +61,8 @@ public function __construct($cacheDirectory = null, $updateInterval = null, $deb * Output a time-stamped debug message if debugging is enabled * @param string $msg */ - protected function debug($msg) { + protected function debug($msg) + { if ($this->debug) { echo gmdate('Y-m-d H:i:s') . "\t$msg\n"; } @@ -348,9 +349,9 @@ public function DownloadData($force = false) } else { $this->debug('Data file hash mismatch.'); } - } else { - $this->debug('Failed to fetch hash file.'); - } + } else { + $this->debug('Failed to fetch hash file.'); + } } else { $this->debug('Failed to fetch data file.'); } @@ -383,53 +384,72 @@ private function get_contents($url, $timeout = 300) $starttime = microtime(true); // use fopen if (ini_get('allow_url_fopen')) { - $fp = @fopen($url, 'rb', false, stream_context_create(array( - 'http' => array( - 'timeout' => $timeout, - 'header' => "Accept-Encoding: gzip\r\n" - )))); + $fp = @fopen( + $url, + 'rb', + false, + stream_context_create( + array( + 'http' => array( + 'timeout' => $timeout, + 'header' => "Accept-Encoding: gzip\r\n" + ) + ) + ) + ); if (is_resource($fp)) { $data = stream_get_contents($fp); $res = stream_get_meta_data($fp); if (array_key_exists('wrapper_data', $res)) { - foreach($res['wrapper_data'] as $d) { - if ($d == 'Content-Encoding: gzip') { //Data was compressed - $data = gzinflate(substr($data, 10, -8)); //Uncompress data - $this->debug('Successfully uncompressed data'); - break; + foreach ($res['wrapper_data'] as $d) { + if ($d == 'Content-Encoding: gzip') { //Data was compressed + $data = gzinflate(substr($data, 10, -8)); //Uncompress data + $this->debug('Successfully uncompressed data'); + break; + } } - } } fclose($fp); if (empty($data)) { if ($this->debug) { if ($res['timed_out']) { - $this->debug('Fetching URL failed due to timeout: '.$url); + $this->debug('Fetching URL failed due to timeout: ' . $url); } else { - $this->debug('Fetching URL failed: '.$url); + $this->debug('Fetching URL failed: ' . $url); } } $data = ''; } else { - $this->debug('Fetching URL with fopen succeeded: '.$url.'. '.strlen($data).' bytes in '.(microtime(true) - $starttime).' sec.'); + $this->debug( + 'Fetching URL with fopen succeeded: ' . $url . '. ' . strlen($data) . ' bytes in ' . (microtime( + true + ) - $starttime) . ' sec.' + ); } } else { - $this->debug('Opening URL failed: '.$url); + $this->debug('Opening URL failed: ' . $url); } } // fall back to curl elseif (function_exists('curl_init')) { $ch = curl_init($url); - curl_setopt_array($ch, array( - CURLOPT_TIMEOUT => $timeout, - CURLOPT_CONNECTTIMEOUT => $timeout, - CURLOPT_RETURNTRANSFER => true, - CURLOPT_ENCODING => 'gzip' - )); + curl_setopt_array( + $ch, + array( + CURLOPT_TIMEOUT => $timeout, + CURLOPT_CONNECTTIMEOUT => $timeout, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => 'gzip' + ) + ); $data = curl_exec($ch); if ($data !== false and curl_errno($ch) == 0) { - $this->debug('Fetching URL with curl succeeded: '.$url.'. '.strlen($data).' bytes in '.(microtime(true) - $starttime).' sec.'); + $this->debug( + 'Fetching URL with curl succeeded: ' . $url . '. ' . strlen($data) . ' bytes in ' . (microtime( + true + ) - $starttime) . ' sec.' + ); } else { - $this->debug('Opening URL with curl failed: '.$url.' '.curl_error($ch)); + $this->debug('Opening URL with curl failed: ' . $url . ' ' . curl_error($ch)); $data = ''; } curl_close($ch); @@ -446,7 +466,7 @@ private function get_contents($url, $timeout = 300) */ public function SetCacheDir($cache_dir) { - $this->debug('Setting cache dir to '.$cache_dir); + $this->debug('Setting cache dir to ' . $cache_dir); // The directory does not exist at this moment, try to make it if (!file_exists($cache_dir)) { @mkdir($cache_dir, 0777, true); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..fb247f5 --- /dev/null +++ b/composer.json @@ -0,0 +1,27 @@ +{ + "name": "synchro/uasparser", + "type": "library", + "license": "LGPL-2.1", + "description": "UASparser is a PHP parser and classifier for user agent strings presented by HTTP clients using databases from http://user-agent-string.info/.", + "keywords": ["http","parser","user-agent"], + "homepage": "https://github.com/Synchro/UASparser", + "authors": [ + { + "name": "Jaroslav Mallat", + "homepage": "http://user-agent-string.info/", + "role": "developer" + }, + { + "name": "Marcus Bointon", + "email": "marcus@synchromedia.co.uk", + "homepage": "https://github.com/Synchro", + "role": "developer" + } + ], + "require": { + "php": ">=5.0.0" + }, + "require-dev": { + "phpunit/phpunit": "*" + } +} \ No newline at end of file