Skip to content
Open
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
48 changes: 33 additions & 15 deletions src/phpbrowscap/Browscap.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,27 +811,45 @@ protected function _getRemoteIniFile($url, $path)
}
}

// Get updated .ini file
$browscap = $this->_getRemoteData($url);
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;
$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) {
// 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