2024-01-09 07:14:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
include "/var/www/html/4get/data/config.php";
|
|
|
|
|
|
|
|
$refl = new ReflectionClass('config');
|
2024-04-19 20:47:02 +00:00
|
|
|
$from_config = ($refl->getConstants());
|
|
|
|
$from_env = array();
|
2024-01-09 07:14:27 +00:00
|
|
|
|
|
|
|
$env = getenv();
|
|
|
|
$fourget_env = array_filter($env, function($v, $k) {
|
|
|
|
return str_starts_with($k, "FOURGET");
|
|
|
|
}, ARRAY_FILTER_USE_BOTH);
|
|
|
|
|
|
|
|
foreach($fourget_env as $key => $val) {
|
|
|
|
$target_key = preg_replace('/^FOURGET_/', '', $key);
|
2024-04-19 20:47:02 +00:00
|
|
|
$from_env[$target_key] = trim($val, '\'"');
|
2024-01-09 07:14:27 +00:00
|
|
|
};
|
|
|
|
|
2024-04-19 20:47:02 +00:00
|
|
|
$merged_config = array_merge($from_config, $from_env);
|
|
|
|
|
2024-01-09 07:14:27 +00:00
|
|
|
function type_to_string($n) {
|
|
|
|
$type = gettype($n);
|
|
|
|
if ($type === "NULL") {
|
|
|
|
return "null";
|
|
|
|
}
|
|
|
|
if ($type === "boolean") {
|
|
|
|
return $n ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
if ($type === "string") {
|
2024-02-17 22:51:05 +00:00
|
|
|
if(is_numeric($n)) {
|
|
|
|
return $n;
|
|
|
|
}
|
2024-01-09 07:14:27 +00:00
|
|
|
return "\"$n\"";
|
|
|
|
}
|
|
|
|
if ($type === "array") {
|
2024-02-17 21:33:06 +00:00
|
|
|
return json_encode($n, JSON_UNESCAPED_SLASHES);
|
2024-01-09 07:14:27 +00:00
|
|
|
}
|
|
|
|
return $n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function detect_captcha_dirs() {
|
|
|
|
$captcha_dir = "/var/www/html/4get/data/captcha/";
|
|
|
|
$categories = (array_map(function ($n) {
|
|
|
|
return explode("/", $n)[7];
|
|
|
|
}, glob($captcha_dir . "*")));
|
|
|
|
|
|
|
|
|
|
|
|
$result = array_map(function($category) {
|
|
|
|
return [$category, count(glob("/var/www/html/4get/data/captcha/" . $category . "/*" ))];
|
|
|
|
}, $categories);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-19 20:47:02 +00:00
|
|
|
$special_keys = ["PROTO", "CAPTCHA_DATASET"];
|
2024-01-09 07:14:27 +00:00
|
|
|
|
|
|
|
$output = "<?php\n // This file was generated by docker/gen_config.php\n";
|
|
|
|
|
|
|
|
$output = $output . "class config {\n";
|
2024-04-19 20:47:02 +00:00
|
|
|
foreach(($merged_config) as $key => $val){
|
2024-01-09 07:14:27 +00:00
|
|
|
if(!in_array($key, $special_keys)) {
|
2024-04-24 23:11:49 +00:00
|
|
|
$stored_value = $val;
|
2024-04-19 20:47:02 +00:00
|
|
|
// conversion between arrays and comma separated env value.
|
2024-04-24 23:11:49 +00:00
|
|
|
// Handle case when original type of field is array and there is a type mismatch when a comma separted string is passed,
|
|
|
|
// then split on comma if string (and not numeric, boolean, null, etc)
|
|
|
|
//
|
|
|
|
// except in the case where the inital value in default config is null. Assuming null
|
|
|
|
// in default config will be never be assigned an array
|
|
|
|
|
|
|
|
if(gettype($from_config[$key]) != gettype($val) && !is_numeric($val) && !is_null($from_config[$key])) {
|
|
|
|
$stored_value = explode(",", $val);
|
|
|
|
}
|
|
|
|
$output = $output . "\tconst " . $key . " = " . type_to_string($stored_value) . ";\n";
|
2024-04-19 20:47:02 +00:00
|
|
|
|
|
|
|
continue;
|
2024-01-09 07:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-17 23:10:52 +00:00
|
|
|
if($key === "CAPTCHA_DATASET") {
|
|
|
|
$output = $output . "\tconst " . $key . " = " . type_to_string(detect_captcha_dirs()) . ";\n";
|
|
|
|
}
|
2024-01-09 07:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$output = $output . "}\n";
|
|
|
|
$output = $output . "?>";
|
|
|
|
|
|
|
|
file_put_contents("./data/config.php", $output);
|
|
|
|
?>
|