-
Notifications
You must be signed in to change notification settings - Fork 0
/
confeditor.php
199 lines (183 loc) · 4.93 KB
/
confeditor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
if(count($argv) >= 2 )
{
if($argv[1] == 'process' && count($argv) >= 4 )
{
process($argv[2], $argv[3]);
}
else if($argv[1] == 'process-all' )
{
$config = parse_ini('confeditor-config.ini',false,false);
//print_r($config);
//exit();
foreach($config as $k=>$v)
{
$AddEdit = $k;
$original = $v['original'];
process($k, $original, $v);
}
}
}
function process( $editFileName, $originalFile, $config = null )
{
global $argv;
// gettings extra params
$keepComments = getSetting( $config, $argv, 'keep_comments', '-kc', false, false);
$keepEmptyLines = getSetting( $config, $argv, 'keep_empty_lines', '-kel', false, false);
$sort = getSetting( $config, $argv, 'sort', '-sort', false, false);
$debug = getSetting( $config, $argv, 'debug', '-d', false, false);
$separator = getSetting( $config, $argv, 'separator', '-sep', true, '=');
$commentRegex = getSetting( $config, $argv, 'comment_regex', '-cr', true, '-^(;|#)-');
$separator = str_replace('"', '', $separator);
$commentRegex = str_replace('"', '',$commentRegex);
//exit( '$keepComments '.$keepComments. ' $keepEmptyLines '.$keepEmptyLines. ' $sort '.$sort.' $separator '.$separator.' $commentRegex '.$commentRegex.' $debug '.$debug);
$editFile = './data/'.$editFileName;
if(!file_exists($editFile))
die( "editFile does not exist ".$editFile);
if(!file_exists($originalFile))
die( "originalFile does not exist ".$originalFile);
$to = $debug ? $originalFile.'-new.ini' : $originalFile;
backup_file($originalFile);
$ini = parse_ini($originalFile, $keepComments, $keepEmptyLines, $separator, $commentRegex );
$iniNewValues = parse_ini($editFile, false, false);
$iniEdited = edit_ini($ini, $iniNewValues, $sort);
save_ini($to, $iniEdited);
//print_r($config);
print_r($ini);
print_r($iniNewValues);
print_r($iniEdited);
//$ini2 = parse_ini($file2, true);
//print_r($ini2);
}
function getSetting( $config, $argv, $varName, $argName, $keepNextArg, $default)
{
if(!empty($config) && isset($config[$varName]))
return $config[$varName];
if(!empty($argv) && !empty($argName) )
{
if(in_array($argName, $argv) )
return $keepNextArg ? $argv[ array_search($argName, $argv) + 1 ] : true;
}
return $default;
}
function backup_file ($file)
{
//echo $file.'.bak';
if(!copy( $file, $file.'.bak'))
//if(!file_put_contents( $file.'.bak', file_get_contents( $file)))
exit("la création du backup a échoué");
}
function edit_ini ( $ini, $iniNewValues, $sort = false )
{
$ini = array_replace_recursive( $ini, $iniNewValues );
$globals = [];
$sections = [];
foreach( $ini as $k=>$v)
{
if(is_array($v)) //section
$sections[$k] = $v;
else
$globals[$k] = $v;
}
if($sort)
{
ksort($globals);
//print_r($globals);
foreach($sections as $k => $v)
{
ksort($v);
$sections[$k] = $v;
//print_r($sections[$k]);
}
}
return $globals + $sections;
}
function save_ini ( $filepath, $ini, $save = true )
{
$str = '';
foreach( $ini as $k=>$v)
{
if(is_array($v)) //section
{
$str .= "[$k]".PHP_EOL;
$str .= save_ini( '', $v, false);
}else
{
if( preg_match('#^comment#', $k ))
$str .= $v.PHP_EOL;
else if( preg_match('#^empty#', $k ))
$str .= PHP_EOL;
else
$str .= "$k = $v".PHP_EOL;
}
}
if($save)
file_put_contents($filepath, $str );
else
return $str;
}
function parse_ini ( $filepath, $keepComments = true, $keepEmptyLines = true, $separator = '=', $commentRegex = '-^(;|#)-' )
{
$ini = file( $filepath );
if ( count( $ini ) == 0 ) { return array(); }
$sections = array();
$values = array();
$globals = array();
$result = array();
$i = 0;
$j = 0;
foreach( $ini as $line ){
$line = trim( $line );
// Sections
if ( !empty($line) && $line{0} == '[' ) {
$sections[] = substr( $line, 1, -1 );
$i++;
continue;
}
// Key-value pair
if ( $line == '' )
{
if($keepEmptyLines)
{
$key = 'empty'.$j++;
$value = $line;
//echo $value;
}
else
continue;
}else if ( preg_match($commentRegex, $line) )
{
if($keepComments )
{
$key = 'comment'.$j++;
$value = $line;
//echo $value
}else
continue;
}else if( preg_match( '#'.$separator.'#', $line)){
list( $key, $value ) = explode( $separator, $line, 2 );
}
$key = trim( $key );
$value = trim( $value );
if ( $i == 0 ) {
// Array values
if ( substr( $line, -1, 2 ) == '[]' ) {
$globals[ $key ][] = $value;
} else {
$globals[ $key ] = $value;
}
} else {
// Array values
if ( substr( $line, -1, 2 ) == '[]' ) {
$values[ $i - 1 ][ $key ][] = $value;
} else {
$values[ $i - 1 ][ $key ] = $value;
}
}
}
for( $j=0; $j<$i; $j++ ) {
$result[ $sections[ $j ] ] = $values[ $j ];
}
return $globals + $result;
}
?>