diff options
author | throwaway <developerthrowaway@protonmail.com> | 2024-01-08 23:14:27 -0800 |
---|---|---|
committer | throwaway <developerthrowaway@protonmail.com> | 2024-01-08 23:14:27 -0800 |
commit | 0cea51e5a3aedd9faefc6029d6d0b28fe86c3db1 (patch) | |
tree | 538cafd857e288f0f008a23fe54c06bdf99bec55 /docker | |
parent | ed5a992354fc07e8ca73ef4865f6664ff24d504c (diff) |
generate 4get config from env variables
Diffstat (limited to 'docker')
-rwxr-xr-x | docker/docker-entrypoint.sh | 4 | ||||
-rw-r--r-- | docker/gen_config.php | 74 |
2 files changed, 78 insertions, 0 deletions
diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index 89bd7cf..bbb8229 100755 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -14,6 +14,10 @@ if [ ! -f /etc/4get/certs/cert.pem ] || [ ! -f /etc/4get/certs/chain.pem ] || [ mv /etc/apache2/httpd.conf_temp /etc/apache2/httpd.conf fi + +php82 ./docker/gen_config.php + + echo "4get is running" exec httpd -DFOREGROUND diff --git a/docker/gen_config.php b/docker/gen_config.php new file mode 100644 index 0000000..71fd721 --- /dev/null +++ b/docker/gen_config.php @@ -0,0 +1,74 @@ + <?php + +include "/var/www/html/4get/data/config.php"; + +$refl = new ReflectionClass('config'); +$config = ($refl->getConstants()); + +$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); + $config[$target_key] = $val; +}; + +function type_to_string($n) { + $type = gettype($n); + if ($type === "NULL") { + return "null"; + } + if ($type === "boolean") { + return $n ? 'true' : 'false'; + } + if ($type === "string") { + return "\"$n\""; + } + if ($type === "array") { + return json_encode($n); + } + 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; +} + + +$special_keys = ["CAPTCHA_DATASET"]; + +$output = "<?php\n // This file was generated by docker/gen_config.php\n"; + +$output = $output . "class config {\n"; +foreach(($config) as $key => $val){ + if(!in_array($key, $special_keys)) { +$output = $output . "\tconst " . $key . " = " . type_to_string($val) . ";\n"; +continue; + } + + +if($key === "CAPTCHA_DATASET") { + $output = $output . "\tconst " . $key . " = " . type_to_string(detect_captcha_dirs()) . ";\n"; + } + +} + +$output = $output . "}\n"; +$output = $output . "?>"; + +file_put_contents("./data/config.php", $output); +?> |