10
10
they'll automatically trust the certificate.
11
11
*/
12
12
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
15
58
function write_ini_file ($ assoc_arr , $ path ) {
16
59
$ content = "" ;
17
60
foreach ($ assoc_arr as $ key => $ elem ) {
18
61
$ content .= "[ " . $ key . "] \n" ;
19
62
foreach ($ elem as $ key2 => $ elem2 ) {
20
63
if (is_array ($ elem2 )) {
21
64
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" ;
23
67
}
24
68
} else if ($ elem2 == "" ) {
25
69
$ content .= $ key2 . " = \n" ;
@@ -41,7 +85,7 @@ function write_ini_file($assoc_arr, $path) {
41
85
// Get the contents of the current config file
42
86
$ phpIniLocation = php_ini_loaded_file ();
43
87
if ($ phpIniLocation ) {
44
- $ phpIniContents = parse_ini_file ($ phpIniLocation, true );
88
+ $ phpIniContents = read_ini_file ($ phpIniLocation );
45
89
} else {
46
90
$ phpIniContents = array ();
47
91
}
0 commit comments