Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/phpbrowscap/Browscap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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()
{
Expand Down
1 change: 0 additions & 1 deletion tests/compare/compareWithOriginal.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,3 @@ protected function getUserAgents()
echo number_format(count($this->user_agents)) . " possible user agents\n";
}
}