-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfiger.php
352 lines (285 loc) · 12.8 KB
/
configer.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* Configer - PHP class for create form to comfortable edit php configuration files
* options of displaying it's sipmle php comments after data
* @link https://github.com/vencendor/configer The Configer GitHub project
* @author Max Uglov <[email protected]>
*/
class Configer {
public $file_name = "";
public $data = array(); // Data storage from file
public $atribute; // Atributes of display
// Minimal numbers of elements for dispaly select input
public $optForSelect = 4;
// True and false values for checkbox usage
public $trueValues = array("1", 1, "true", true, "yes");
public $falseValues = array("0", 0, "false", false, "no");
// Parse atributes of values from config file
public function getAtributes() {
$conf_str = file_get_contents($this->file_name);
foreach ($this->data as $n => $cf) {
// First level of array
if (preg_match("#" . $n . "[^\r\n]*//([^\n\r]+)[\n\r]#", $conf_str, $m)) {
$this->atribute[$n] = trim($m[1]);
}
if (is_array($cf)) {
//second level
foreach ($cf as $ns => $vs) {
if (preg_match("#" . $n . ".*" . $ns . "[^\r\n]*=>[^\r\n]*//([^\n\r]+)[\n\r]#isU", $conf_str, $m)) {
$this->atribute[$n . "~" . $ns] = trim($m[1]);
}
}
}
}
}
//Convert values to 0 or 1
public function toBool($val) {
if (in_array($val, $this->trueValues))
return 1;
if (in_array($val, $this->falseValues))
return 0;
return false;
}
// Return all boolean style values
public function booleanValues() {
return array_merge($this->trueValues, $this->falseValues);
}
// Write result to file
public function save() {
$conf_str = var_export($this->data, true);
foreach ($this->data as $n => $v) {
// Insert back values atribute first level
if (isset($this->atribute[$n])) {
$conf_str = preg_replace("#([^\n\r]*" . $n . "[^\n\r]*)[\n\r]#is", "\\1//" . $this->atribute[$n] . "\n", $conf_str);
}
if (is_array($v)) {
// Insert atributes second level
foreach ($v as $ns => $vs) {
if (isset($this->atribute[$n . "~" . $ns])) {
$conf_str = preg_replace("#([^\n\r]*" . $n . ".*" . $ns . "[^\r\n]*=>[^\r\n]*)[\n\r]#isU", "\\1//" . $this->atribute[$n . "~" . $ns] . "\n", $conf_str);
}
}
}
}
file_put_contents($this->file_name, "<?php \n return " . $conf_str . " \n ?>");
}
// Display input for value
public function renderInput($title, $name_parent, $name_var = false, $data_var = false, $options = false) {
$inputStr = "";
$input_name = "config[" . $name_parent . "]" . ($name_var !== false ? "[" . $name_var . "]" : "");
// Simple text input
if (!$options or ! in_array($data_var, $options['data'])) {
$inputStr = "<span> " . $title . " </span><input type='text' name='" . $input_name . "' value='" . $data_var . "' />";
} else {
//CHECKBOX input
if ($options['type'] === "checkbox") {
if (in_array($data_var, $this->booleanValues())) {
$inputStr = "<input type='checkbox' " . ($data_var ? "checked='checked'" : "") . " name='" . $input_name . "' /> <span> " . $title . " </span>";
} else {
$inputStr = "<span> " . $title . " </span><input type='text' name='" . $input_name . "' value='" . $data_var . "' />";
}
}
//RADIO inputs
if ($options['type'] === "radio") {
foreach ($options['data'] as $n => $v) {
$inputStr .= "<input type='radio' value='" . $v . "' " . ( $data_var == $v ? "checked='checked'" : "" ) . " name='" . $input_name . "' /> <span> " . ( isset($options['labels'][$n]) ? $options['labels'][$n] : $v ) . " </span>";
}
$inputStr = "<span> " . $title . " </span>" . $inputStr;
}
//SELECT input generate
if ($options['type'] === "select") {
foreach ($options['data'] as $n => $v) {
$inputStr .= "<option value='" . $v . "' " . ($data_var === $v ? "selected='selected'" : "") . " > " . ( isset($options['labels'][$n]) ? $options['labels'][$n] : $v ) . " </option> ";
}
$inputStr = "<span> " . $title . " </span> <select name='" . $input_name . "' > " . $inputStr . "</select>";
}
}
return $inputStr;
}
// Parsing options from values atribute
public function parseAtribute($atribute) {
$flags = array();
$flags['title'] = $atribute;
//can't delete this value
if (strpos($atribute, "[static]") !== false) {
$flags['static'] = true;
} else {
$flags['static'] = false;
}
//can add values in this sub array
if (strpos($atribute, "[dinamic]") !== false) {
$flags['dinamic'] = true;
}
//toggled block by default
if (strpos($atribute, "[hidden]") !== false) {
$flags['hidden'] = true;
} else {
$flags['hidden'] = false;
}
//parsing options that can by values for curent input
//select type of input
if (strpos($atribute, "[options") !== false) {
preg_match("#\[options\|(.+)+\]#", $atribute, $options);
$options = explode("|", $options[1]);
if (sizeof($options) == 2) {
// if have 2 options and both its from boolean type
$checkbox = true;
foreach ($options as $od) {
if (!in_array($od, $this->booleanValues(), true)) {
$checkbox = false;
}
}
if ($checkbox) {
$flags['options']['type'] = "checkbox";
} else {
$flags['options']['type'] = "radio";
}
} elseif (sizeof($options) < $this->optForSelect) {
$flags['options']['type'] = "radio";
} else {
$flags['options']['type'] = "select";
}
$flags['options']['data'] = $options;
// parse labels for values and clear data
foreach ($options as $n => $v) {
if (preg_match("#(.*)\((.*)\)#", $v, $m)) {
$flags['options']['data'][$n] = trim($m[1]);
$flags['options']['labels'][$n] = $m[2];
}
}
} else {
$flags['options'] = false;
}
// clear title of input
$flags['title'] = trim(preg_replace("#\[[^\[\]]*\]#", "", $flags['title']));
return $flags;
}
// boolean data for checkboxes converting
public function checkboxFilter($atr, $val) {
$ret = $val;
if (trim($val) === "" or ! isset($val) or trim($val) === "on") {
$opt = $this->parseAtribute($atr);
if ($opt['options']['type'] === "checkbox") {
if ($ret === "on")
$ret = true;
if ($this->toBool($ret) === $this->toBool($opt['options']['data'][0])) {
$ret = $opt['options']['data'][0];
} else {
$ret = $opt['options']['data'][1];
}
}
}
return $ret;
}
public function __construct($file_name) {
if (!is_file($file_name))
return false;
//create backup on first launch
if (!is_file($file_name . ".bak"))
copy($file_name, $file_name . ".bak");
// read data
$this->file_name = $file_name;
$this->data = require $this->file_name;
$this->getAtributes();
//process form submit
if ($_SERVER['REQUEST_METHOD'] === "POST") {
// restore default
if (isset($_POST['restore_default'])) {
unlink($file_name);
copy($file_name . ".bak", $file_name);
}
if (isset($_POST['config']) and is_array($_POST['config'])) {
//check data if its checkbox values for first and second level of values
foreach ($this->atribute as $n => $v) {
if (strpos($n, "~") !== false) {
$name = explode("~", $n);
if (!isset($_POST['config'][$name[0]][$name[1]]) or $_POST['config'][$name[0]][$name[1]] === "on") {
if (!isset($_POST['config'][$name[0]][$name[1]]))
$_POST['config'][$name[0]][$name[1]] = 0;
$_POST['config'][$name[0]][$name[1]] = $this->checkboxFilter($v, $_POST['config'][$name[0]][$name[1]]);
}
} else {
if (!isset($_POST['config'][$n]) or $_POST['config'][$n] === "on") {
if (!isset($_POST['config'][$n]))
$_POST['config'][$n] = 0;
$_POST['config'][$n] = $this->checkboxFilter($v, $_POST['config'][$n]);
}
}
}
$this->data = array_merge($this->data, $_POST['config']);
$this->save();
}
}
}
function showForm() {
?>
<script>
if (!window.jQuery) {
document.write(unescape('<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js">%3C/script%3E'));
}
</script>
<style>
legend{
cursor: pointer;
}
</style>
<form name='restore'>
<input type='hidden' name='restore_default' value='do' />
<input type='submit' value='Restore default' />
</form>
<form class="form" id='configForm' method='post'>
<?php
foreach ($this->data as $n => $d_val) {
if (isset($this->atribute[$n])) {
$flags['dinamic'] = false;
$flags = $this->parseAtribute($this->atribute[$n]);
if (is_array($d_val)) {
echo "<fieldset><legend>" . $flags['title'] . "</legend>";
echo "<div class='field' " . ($flags['hidden'] ? "style='display:none'" : "") . "> ";
foreach ($d_val as $nc => $vc) {
if (isset($this->atribute[$n . "~" . $nc])) {
$flags = array_merge($flags, $this->parseAtribute($this->atribute[$n . "~" . $nc]));
} else {
$flags['title'] = $nc;
}
echo "<div>" . $this->renderInput($flags['title'], $n, $nc, $vc, $flags['options']);
if (isset($flags['dinamic']) and $flags['dinamic'] and ! $flags['static']) {
echo "<a class='icon-remove' href='javascript:void(0)' onclick='removeOption(this)' >Del</a>";
}
echo "</div>";
if (isset($flags['dinamic']) and $flags['dinamic']) {
$flags['static'] = false;
}
}
if (isset($flags['dinamic']) and $flags['dinamic'] and ! $flags['static']) {
echo "<a class='icon-plus' href='javascript:void(0)' onclick='addOption(this,\"" . $n . "\")' >Add</a>";
}
echo "</div></fieldset>";
} else {
echo "<div>";
echo $this->renderInput($flags['title'], $n, false, $d_val, $flags['options']);
echo "</div>";
}
}
}
?>
<input type='submit' class='btn btn-info' value='Save' />
</form>
<script>
function addOption(t, name) {
var optList = $(t.parentNode).find('div');
var opt = parseInt($(optList[optList.length - 1]).find('span').html());
$("<div><span> " + (opt + 1) + " </span><input type='text' name='config[" + name + "][" + (opt + 1) + "]' value='' /><a class='icon-remove' href='javascript:void(0)' onclick='removeOption(this)' >Del</a></div>").insertBefore(t);
}
function removeOption(t) {
$(t.parentNode).find('input').attr('value', '~#deleted#~');
$(t.parentNode).remove();
}
$('legend').click(function () {
$(this).parent().find('div.field').slideToggle();
});
</script>
<?php
}
}
?>