gen_config.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. $from_env[$target_key] = trim($val, '\'"');
  13. };
  14. $merged_config = array_merge($from_config, $from_env);
  15. function type_to_string($n) {
  16. $type = gettype($n);
  17. if ($type === "NULL") {
  18. return "null";
  19. }
  20. if ($type === "boolean") {
  21. return $n ? 'true' : 'false';
  22. }
  23. if ($type === "string") {
  24. if(is_numeric($n)) {
  25. return $n;
  26. }
  27. return "\"$n\"";
  28. }
  29. if ($type === "array") {
  30. return json_encode($n, JSON_UNESCAPED_SLASHES);
  31. }
  32. return $n;
  33. }
  34. function detect_captcha_dirs() {
  35. $captcha_dir = "/var/www/html/4get/data/captcha/";
  36. $categories = (array_map(function ($n) {
  37. return explode("/", $n)[7];
  38. }, glob($captcha_dir . "*")));
  39. $result = array_map(function($category) {
  40. return [$category, count(glob("/var/www/html/4get/data/captcha/" . $category . "/*" ))];
  41. }, $categories);
  42. return $result;
  43. }
  44. $special_keys = ["PROTO", "CAPTCHA_DATASET"];
  45. $output = "<?php\n // This file was generated by docker/gen_config.php\n";
  46. $output = $output . "class config {\n";
  47. foreach(($merged_config) as $key => $val){
  48. if(!in_array($key, $special_keys)) {
  49. // conversion between arrays and comma separated env value.
  50. // If original type of field is array and there is a type mismatch such as when a comma separted string is passed,
  51. // then split on comma if string and not numeric
  52. if(gettype($from_config[$key]) != gettype($val) && !is_numeric($val)) {
  53. $data = gettype($val) === "string" ? explode(",", $val) : $val;
  54. $output = $output . "\tconst " . $key . " = " . type_to_string($data) . ";\n";
  55. } else {
  56. $output = $output . "\tconst " . $key . " = " . type_to_string($val) . ";\n";
  57. }
  58. continue;
  59. }
  60. if($key === "CAPTCHA_DATASET") {
  61. $output = $output . "\tconst " . $key . " = " . type_to_string(detect_captcha_dirs()) . ";\n";
  62. }
  63. }
  64. $output = $output . "}\n";
  65. $output = $output . "?>";
  66. file_put_contents("./data/config.php", $output);
  67. ?>