diff --git a/src/phpbrowscap/Browscap.php b/src/phpbrowscap/Browscap.php index 5ac723fd..7ad3f2c8 100644 --- a/src/phpbrowscap/Browscap.php +++ b/src/phpbrowscap/Browscap.php @@ -884,19 +884,39 @@ protected function _getRemoteIniFile($url, $path) } } - // Get updated .ini file - $browscap = $this->_getRemoteData($url); - $browscap = explode("\n", $browscap); - $pattern = self::REGEX_DELIMITER . '(' . self::VALUES_TO_QUOTE . ')="?([^"]*)"?$' . self::REGEX_DELIMITER; - - // Ok, lets read the file - $content = ''; - foreach ($browscap as $subject) { - $subject = trim($subject); - $content .= preg_replace($pattern, '$1="$2"', $subject) . "\n"; - } - if ($url != $path) { + // Check if it's possible to write to the .ini file. + if (is_file($path)) { + if (!is_writable($path)) { + throw new Exception( + 'Could not write to "' . $path . '" (check the permissions of the current/old ini file).' + ); + } + } else { + // Test writability by creating a file only if one already doesn't exist, so we can safely delete it after the test. + $test_file = fopen($path, 'a'); + if ($test_file) { + fclose($test_file); + unlink($path); + } else { + throw new Exception( + 'Could not write to "' . $path . '" (check the permissions of the cache directory).' + ); + } + } + + // Get updated .ini file + $browscap = $this->_getRemoteData($url); + $browscap = explode("\n", $browscap); + $pattern = self::REGEX_DELIMITER . '(' . self::VALUES_TO_QUOTE . ')="?([^"]*)"?$' . self::REGEX_DELIMITER; + + // Ok, lets read the file + $content = ''; + foreach ($browscap as $subject) { + $subject = trim($subject); + $content .= preg_replace($pattern, '$1="$2"', $subject) . "\n"; + } + if (!file_put_contents($path, $content)) { throw new Exception("Could not write .ini content to $path"); } @@ -979,7 +999,7 @@ protected function _array2string($array) * Checks for the various possibilities offered by the current configuration * of PHP to retrieve external HTTP data * - * @return string|boolean the name of function to use to retrieve the file or false if no methods are available + * @return string|false the name of function to use to retrieve the file or false if no methods are available */ protected function _getUpdateMethod() { diff --git a/tests/compare/compareWithOriginal.php b/tests/compare/compareWithOriginal.php index f6b89bbf..715351df 100644 --- a/tests/compare/compareWithOriginal.php +++ b/tests/compare/compareWithOriginal.php @@ -633,4 +633,3 @@ protected function getUserAgents() echo number_format(count($this->user_agents)) . " possible user agents\n"; } } -