1
0

gen_config.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. include "/var/www/html/4get/data/config.php";
  3. $refl = new ReflectionClass('config');
  4. $from_config = ($refl->getConstants());
  5. $from_env = array();
  6. $env = getenv();
  7. $fourget_env = array_filter($env, function($v, $k) {
  8. return str_starts_with($k, "FOURGET");
  9. }, ARRAY_FILTER_USE_BOTH);
  10. foreach($fourget_env as $key => $val) {
  11. $target_key = preg_replace('/^FOURGET_/', '', $key);
  12. $val = trim($val, '\'"');
  13. $from_env[$target_key] = match(strtolower($val)) { "true" => true, "false" => false, default => $val };
  14. };
  15. $merged_config = array_merge($from_config, $from_env);
  16. function type_to_string($n) {
  17. $type = gettype($n);
  18. if ($type === "NULL") {
  19. return "null";
  20. }
  21. if ($type === "boolean") {
  22. return $n ? 'true' : 'false';
  23. }
  24. if ($type === "string") {
  25. if(is_numeric($n)) {
  26. return $n;
  27. }
  28. return "\"$n\"";
  29. }
  30. if ($type === "array") {
  31. return json_encode($n, JSON_UNESCAPED_SLASHES);
  32. }
  33. return $n;
  34. }
  35. function detect_captcha_dirs() {
  36. $captcha_dir = "/var/www/html/4get/data/captcha/";
  37. $categories = (array_map(function ($n) {
  38. return explode("/", $n)[7];
  39. }, glob($captcha_dir . "*")));
  40. $result = array_map(function($category) {
  41. return [$category, count(glob("/var/www/html/4get/data/captcha/" . $category . "/*" ))];
  42. }, $categories);
  43. return $result;
  44. }
  45. $special_keys = ["PROTO", "CAPTCHA_DATASET"];
  46. $output = "<?php\n // This file was generated by docker/gen_config.php\n";
  47. $output = $output . "class config {\n";
  48. foreach(($merged_config) as $key => $val){
  49. if(!in_array($key, $special_keys)) {
  50. $stored_value = $val;
  51. // conversion between arrays and comma separated env value.
  52. // Handle case when original type of field is array and there is a type mismatch when a comma separted string is passed,
  53. // then split on comma if string (and not numeric, boolean, null, etc)
  54. //
  55. // except in the case where the inital value in default config is null or boolean. Assuming null and boolean
  56. // in default config will be never be assigned an array
  57. if(gettype($from_config[$key]) != gettype($val) && !is_numeric($val) && !is_null($from_config[$key]) && gettype($from_config[$key]) != "boolean") {
  58. $stored_value = explode(",", $val);
  59. }
  60. $output = $output . "\tconst " . $key . " = " . type_to_string($stored_value) . ";\n";
  61. continue;
  62. }
  63. if($key === "CAPTCHA_DATASET") {
  64. $output = $output . "\tconst " . $key . " = " . type_to_string(detect_captcha_dirs()) . ";\n";
  65. }
  66. }
  67. $output = $output . "}\n";
  68. $output = $output . "?>";
  69. file_put_contents("./data/config.php", $output);
  70. ?>