Skip to content

Commit

Permalink
Update version to 0.52
Browse files Browse the repository at this point in the history
Add composer.json
Update readme
PSR-2 cleanup
  • Loading branch information
Synchro committed Jun 4, 2013
1 parent 392f5e1 commit cc47cab
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 32 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
- Fixes poor code in the example script
- Improves error handling and debugging, adds variable timeouts
- Adds support for gzip compression of database downloads
78 changes: 49 additions & 29 deletions UAS/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"homepage": "https://github.com/Synchro",
"role": "developer"
}
],
"require": {
"php": ">=5.0.0"
},
"require-dev": {
"phpunit/phpunit": "*"
}
}

0 comments on commit cc47cab

Please sign in to comment.