gen_config.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. $stored_value = $val;
  50. // conversion between arrays and comma separated env value.
  51. // Handle case when original type of field is array and there is a type mismatch when a comma separted string is passed,
  52. // then split on comma if string (and not numeric, boolean, null, etc)
  53. //
  54. // except in the case where the inital value in default config is null or boolean. Assuming null and boolean
  55. // in default config will be never be assigned an array
  56. if(gettype($from_config[$key]) != gettype($val) && !is_numeric($val) && !is_null($from_config[$key]) && gettype($from_config[$key]) != "boolean") {
  57. $stored_value = explode(",", $val);
  58. }
  59. $output = $output . "\tconst " . $key . " = " . type_to_string($stored_value) . ";\n";
  60. continue;
  61. }
  62. if($key === "CAPTCHA_DATASET") {
  63. $output = $output . "\tconst " . $key . " = " . type_to_string(detect_captcha_dirs()) . ";\n";
  64. }
  65. }
  66. $output = $output . "}\n";
  67. $output = $output . "?>";
  68. file_put_contents("./data/config.php", $output);
  69. ?>