Permalink
Browse files

Added the switch providing the INI_SCANNER_RAW flag if the PHP versio…

…n is >= 5.3 (fixes issue 11 in the old google code tracker).
  • Loading branch information...
Jonathan Stoppani Jonathan Stoppani
Jonathan Stoppani authored and Jonathan Stoppani committed Jul 11, 2010
1 parent b174f91 commit f4ddfbdda8027fdc7fdb222186f3373f82bd97b7
Showing with 76 additions and 71 deletions.
  1. +76 −71 browscap/Browscap.php
View
@@ -19,107 +19,107 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
- * @package Browscap
- * @author Jonathan Stoppani <st.jonathan@gmail.com>
+ * @package Browscap
+ * @author Jonathan Stoppani <st.jonathan@gmail.com>
* @copyright Copyright (c) 2006-2008 Jonathan Stoppani
- * @version 0.7
- * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
- * @link http://garetjax.info/projects/browscap/
+ * @version 0.7
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @link http://garetjax.info/projects/browscap/
*/
class Browscap
{
/**
* Current version of the class.
*/
- const VERSION = '0.7';
+ const VERSION = '0.7';
/**
* Different ways to access remote and local files.
*
- * UPDATE_FOPEN: Uses the fopen url wrapper (use file_get_contents).
+ * UPDATE_FOPEN: Uses the fopen url wrapper (use file_get_contents).
* UPDATE_FSOCKOPEN: Uses the socket functions (fsockopen).
- * UPDATE_CURL: Uses the cURL extension.
- * UPDATE_LOCAL: Updates from a local file (file_get_contents).
+ * UPDATE_CURL: Uses the cURL extension.
+ * UPDATE_LOCAL: Updates from a local file (file_get_contents).
*/
- const UPDATE_FOPEN = 'URL-wrapper';
- const UPDATE_FSOCKOPEN = 'socket';
- const UPDATE_CURL = 'cURL';
- const UPDATE_LOCAL = 'local';
+ const UPDATE_FOPEN = 'URL-wrapper';
+ const UPDATE_FSOCKOPEN = 'socket';
+ const UPDATE_CURL = 'cURL';
+ const UPDATE_LOCAL = 'local';
/**
* Options for regex patterns.
*
- * REGEX_DELIMITER: Delimiter of all the regex patterns in the whole class.
- * REGEX_MODIFIERS: Regex modifiers.
+ * REGEX_DELIMITER: Delimiter of all the regex patterns in the whole class.
+ * REGEX_MODIFIERS: Regex modifiers.
*/
- const REGEX_DELIMITER = '@';
- const REGEX_MODIFIERS = 'i';
+ const REGEX_DELIMITER = '@';
+ const REGEX_MODIFIERS = 'i';
/**
* The values to quote in the ini file
*/
- const VALUES_TO_QUOTE = 'Browser|Parent';
+ const VALUES_TO_QUOTE = 'Browser|Parent';
/**
* Definitions of the function used by the uasort() function to order the
* userAgents array.
*
- * ORDER_FUNC_ARGS: Arguments that the function will take.
+ * ORDER_FUNC_ARGS: Arguments that the function will take.
* ORDER_FUNC_LOGIC: Internal logic of the function.
*/
- const ORDER_FUNC_ARGS = '$a, $b';
- const ORDER_FUNC_LOGIC = '$a=strlen($a);$b=strlen($b);return$a==$b?0:($a<$b?1:-1);';
+ const ORDER_FUNC_ARGS = '$a, $b';
+ const ORDER_FUNC_LOGIC = '$a=strlen($a);$b=strlen($b);return$a==$b?0:($a<$b?1:-1);';
/**
* The headers to be sent for checking the version and requesting the file.
*/
- const REQUEST_HEADERS = "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\nConnection: Close\r\n\r\n";
+ const REQUEST_HEADERS = "GET %s HTTP/1.0\r\nHost: %s\r\nUser-Agent: %s\r\nConnection: Close\r\n\r\n";
/**
* Options for auto update capabilities
*
- * $remoteVerUrl: The location to use to check out if a new version of the
- * browscap.ini file is available.
- * $remoteIniUrl: The location from which download the ini file.
- * The placeholder for the file should be represented by a %s.
+ * $remoteVerUrl: The location to use to check out if a new version of the
+ * browscap.ini file is available.
+ * $remoteIniUrl: The location from which download the ini file.
+ * The placeholder for the file should be represented by a %s.
* $timeout: The timeout for the requests.
* $updateInterval: The update interval in seconds.
* $errorInterval: The next update interval in seconds in case of an error.
- * $doAutoUpdate: Flag to disable the automatic interval based update.
+ * $doAutoUpdate: Flag to disable the automatic interval based update.
* $updateMethod: The method to use to update the file, has to be a value of
- * an UPDATE_* constant, null or false.
+ * an UPDATE_* constant, null or false.
*/
- public $remoteIniUrl = 'http://browsers.garykeith.com/stream.asp?BrowsCapINI';
- public $remoteVerUrl = 'http://updates.browserproject.com/version-date.asp';
- public $timeout = 5;
- public $updateInterval = 432000; // 5 days
- public $errorInterval = 7200; // 2 hours
- public $doAutoUpdate = true;
- public $updateMethod = null;
+ public $remoteIniUrl = 'http://browsers.garykeith.com/stream.asp?BrowsCapINI';
+ public $remoteVerUrl = 'http://updates.browserproject.com/version-date.asp';
+ public $timeout = 5;
+ public $updateInterval = 432000; // 5 days
+ public $errorInterval = 7200; // 2 hours
+ public $doAutoUpdate = true;
+ public $updateMethod = null;
/**
* The path of the local version of the browscap.ini file from which to
* update (to be set only if used).
*
* @var string
*/
- public $localFile = null;
+ public $localFile = null;
/**
* The useragent to include in the requests made by the class during the
* update process.
*
* @var string
*/
- public $userAgent = 'PHP Browser Capabilities Project/%v %m';
+ public $userAgent = 'PHP Browser Capabilities Project/%v %m';
/**
* Flag to enable only lowercase indexes in the result.
* The cache has to be rebuilt in order to apply this option.
*
* @var bool
*/
- public $lowercase = false;
+ public $lowercase = false;
/**
* Flag to enable/disable silent error management.
@@ -129,45 +129,45 @@ class Browscap
*
* @var bool
*/
- public $silent = false;
+ public $silent = false;
/**
* Where to store the cached PHP arrays.
*
* @var string
*/
- public $cacheFilename = 'cache.php';
+ public $cacheFilename = 'cache.php';
/**
* Where to store the downloaded ini file.
*
* @var string
*/
- public $iniFilename = 'browscap.ini';
+ public $iniFilename = 'browscap.ini';
/**
* Path to the cache directory
*
* @var string
*/
- public $cacheDir = null;
+ public $cacheDir = null;
/**
* Flag to be set to true after loading the cache
*
* @var bool
*/
- private $_cacheLoaded = false;
+ private $_cacheLoaded = false;
/**
* Where to store the value of the included PHP cache file
*
* @var array
*/
- private $_userAgents = array();
- private $_browsers = array();
- private $_patterns = array();
- private $_properties = array();
+ private $_userAgents = array();
+ private $_browsers = array();
+ private $_patterns = array();
+ private $_properties = array();
/**
* Constructor class, checks for the existence of (and loads) the cache and
@@ -203,10 +203,10 @@ public function __construct($cache_dir)
* Gets the information about the browser by User Agent
*
* @param string $user_agent the user agent string
- * @param bool $return_array whether return an array or an object
+ * @param bool $return_array whether return an array or an object
* @throws Browscap_Exception
* @return stdObject the object containing the browsers details. Array if
- * $return_array is set to true.
+ * $return_array is set to true.
*/
public function getBrowser($user_agent = null, $return_array = false)
{
@@ -265,8 +265,8 @@ public function getBrowser($user_agent = null, $return_array = false)
$browser = $value = $browser + $this->_browsers[$key];
while (array_key_exists(3, $value) && $value[3]) {
- $value = $this->_browsers[$value[3]];
- $browser += $value;
+ $value = $this->_browsers[$value[3]];
+ $browser += $value;
}
if (!empty($browser[3])) {
@@ -293,7 +293,7 @@ public function getBrowser($user_agent = null, $return_array = false)
*/
public function updateCache()
{
- $ini_path = $this->cacheDir . $this->iniFilename;
+ $ini_path = $this->cacheDir . $this->iniFilename;
$cache_path = $this->cacheDir . $this->cacheFilename;
// Choose the right url
@@ -305,7 +305,12 @@ public function updateCache()
$this->_getRemoteIniFile($url, $ini_path);
- $browsers = parse_ini_file($ini_path, true);
+ if (version_compare(PHP_VERSION, '5.3.0', '>=')){
+ $browsers = parse_ini_file($ini_path, true, INI_SCANNER_RAW);
+ } else {
+ $browsers = parse_ini_file($ini_path, true);
+ }
+
array_shift($browsers);
$this->_properties = array_keys($browsers['DefaultProperties']);
@@ -317,25 +322,25 @@ public function updateCache()
'Parent'
);
- $this->_userAgents = array_keys($browsers);
+ $this->_userAgents = array_keys($browsers);
usort(
$this->_userAgents,
create_function(self::ORDER_FUNC_ARGS, self::ORDER_FUNC_LOGIC)
);
- $user_agents_keys = array_flip($this->_userAgents);
+ $user_agents_keys = array_flip($this->_userAgents);
$properties_keys = array_flip($this->_properties);
$search = array('\*', '\?');
$replace = array('.*', '.');
foreach ($this->_userAgents as $user_agent) {
$pattern = preg_quote($user_agent, self::REGEX_DELIMITER);
- $this->_patterns[] = self::REGEX_DELIMITER
- . '^'
- . str_replace($search, $replace, $pattern)
- . '$'
- . self::REGEX_DELIMITER;
+ $this->_patterns[] = self::REGEX_DELIMITER
+ . '^'
+ . str_replace($search, $replace, $pattern)
+ . '$'
+ . self::REGEX_DELIMITER;
if (!empty($browsers[$user_agent]['Parent'])) {
$parent = $browsers[$user_agent]['Parent'];
@@ -373,7 +378,7 @@ private function _loadCache($cache_file)
{
require $cache_file;
- $this->_browsers = $browsers;
+ $this->_browsers = $browsers;
$this->_userAgents = $userAgents;
$this->_patterns = $patterns;
$this->_properties = $properties;
@@ -391,7 +396,7 @@ private function _buildCache()
$cacheTpl = "<?php\n\$properties=%s;\n\$browsers=%s;\n\$userAgents=%s;\n\$patterns=%s;\n";
$propertiesArray = $this->_array2string($this->_properties);
- $patternsArray = $this->_array2string($this->_patterns);
+ $patternsArray = $this->_array2string($this->_patterns);
$userAgentsArray = $this->_array2string($this->_userAgents);
$browsersArray = $this->_array2string($this->_browsers);
@@ -417,7 +422,7 @@ private function _getRemoteIniFile($url, $path)
{
// Check version
if (file_exists($path) && filesize($path)) {
- $local_tmstp = filemtime($path);
+ $local_tmstp = filemtime($path);
if ($this->_getUpdateMethod() == self::UPDATE_LOCAL) {
$remote_tmstp = $this->_getLocalMTime();
@@ -509,17 +514,17 @@ private function _array2string($array)
if (is_int($key)) {
$key = '';
} else if (ctype_digit((string) $key) || strpos($key, '.0')) {
- $key = intval($key) . '=>' ;
+ $key = intval($key) . '=>' ;
} else {
- $key = "'" . str_replace("'", "\'", $key) . "'=>" ;
+ $key = "'" . str_replace("'", "\'", $key) . "'=>" ;
}
if (is_array($value)) {
$value = $this->_array2string($value);
} else if (ctype_digit((string) $value)) {
- $value = intval($value);
+ $value = intval($value);
} else {
- $value = "'" . str_replace("'", "\'", $value) . "'";
+ $value = "'" . str_replace("'", "\'", $value) . "'";
}
$strings[] = $key . $value;
@@ -653,11 +658,11 @@ private function _getUserAgent()
/**
* Browscap.ini parsing class exception
*
- * @package Browscap
- * @author Jonathan Stoppani <st.jonathan@gmail.com>
+ * @package Browscap
+ * @author Jonathan Stoppani <st.jonathan@gmail.com>
* @copyright Copyright (c) 2006-2008 Jonathan Stoppani
- * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
- * @link http://garetjax.info/projects/browscap/
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @link http://garetjax.info/projects/browscap/
*/
class Browscap_Exception extends Exception
{}

0 comments on commit f4ddfbd

Please sign in to comment.