Skip to content

Commit 7c8ce9d

Browse files
committed
Fix PHP interception handling of duplicate config keys (extension=)
1 parent ab9179b commit 7c8ce9d

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

src/interceptors/terminal-wrappers/build-php-config.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,60 @@
1010
they'll automatically trust the certificate.
1111
*/
1212

13-
// PHP has ini reading built in, but not writing.
14-
// From https://gist.github.com/edvardHua/a9830a68ae68f57cd892a8a2903a1fb4
13+
// Like the built-in parse_ini_file, but handles duplicate keys
14+
// correctly (instead of overwriting them).
15+
function read_ini_file($file){
16+
$arr = array();
17+
$handle = fopen($file, "r");
18+
$currentSection = $arr;
19+
20+
while (($line = fgets($handle)) !== false) {
21+
$parsed = parse_ini_string($line, true);
22+
23+
if (empty($parsed)) {
24+
continue;
25+
}
26+
27+
if ($line[0] == '[') {
28+
# Start of a new section
29+
$sectionName = key($parsed);
30+
$arr[$sectionName] = array();
31+
$currentSection = &$arr[$sectionName];
32+
} else {
33+
# key=value - insert into the current section
34+
$key = key($parsed);
35+
$value = $parsed[$key];
36+
37+
if (isset($currentSection[$key])) {
38+
# Duplicate: turn the value into an array
39+
if (!is_array($currentSection[$key])) {
40+
$tmp = $currentSection[$key];
41+
$currentSection[$key] = array($tmp);
42+
}
43+
$currentSection[$key][] = $value;
44+
} else {
45+
# New value: add it bare
46+
$currentSection[$key] = $value;
47+
var_dump($currentSection);
48+
}
49+
}
50+
}
51+
fclose($handle);
52+
53+
return $arr;
54+
}
55+
56+
// PHP has no ini writing methods available: build one.
57+
// Based on https://gist.github.com/edvardHua/a9830a68ae68f57cd892a8a2903a1fb4
1558
function write_ini_file($assoc_arr, $path) {
1659
$content = "";
1760
foreach ($assoc_arr as $key => $elem) {
1861
$content .= "[" . $key . "]\n";
1962
foreach ($elem as $key2 => $elem2) {
2063
if (is_array($elem2)) {
2164
for ($i = 0; $i < count($elem2); $i++) {
22-
$content .= $key2 . "[] = \"" . $elem2[$i] . "\"\n";
65+
// php.ini allows (requires, for extensions) duplicate keys
66+
$content .= $key2 . " = \"" . $elem2[$i] . "\"\n";
2367
}
2468
} else if ($elem2 == "") {
2569
$content .= $key2 . " = \n";
@@ -41,7 +85,7 @@ function write_ini_file($assoc_arr, $path) {
4185
// Get the contents of the current config file
4286
$phpIniLocation = php_ini_loaded_file();
4387
if ($phpIniLocation) {
44-
$phpIniContents = parse_ini_file($phpIniLocation, true);
88+
$phpIniContents = read_ini_file($phpIniLocation);
4589
} else {
4690
$phpIniContents = array();
4791
}

0 commit comments

Comments
 (0)