gen_config.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. include "/var/www/html/4get/data/config.php";
  3. $refl = new ReflectionClass('config');
  4. $config = ($refl->getConstants());
  5. $env = getenv();
  6. $fourget_env = array_filter($env, function($v, $k) {
  7. return str_starts_with($k, "FOURGET");
  8. }, ARRAY_FILTER_USE_BOTH);
  9. foreach($fourget_env as $key => $val) {
  10. $target_key = preg_replace('/^FOURGET_/', '', $key);
  11. $config[$target_key] = trim($val, '\'"');
  12. };
  13. function type_to_string($n) {
  14. $type = gettype($n);
  15. if ($type === "NULL") {
  16. return "null";
  17. }
  18. if ($type === "boolean") {
  19. return $n ? 'true' : 'false';
  20. }
  21. if ($type === "string") {
  22. if(is_numeric($n)) {
  23. return $n;
  24. }
  25. return "\"$n\"";
  26. }
  27. if ($type === "array") {
  28. return json_encode($n, JSON_UNESCAPED_SLASHES);
  29. }
  30. return $n;
  31. }
  32. function detect_captcha_dirs() {
  33. $captcha_dir = "/var/www/html/4get/data/captcha/";
  34. $categories = (array_map(function ($n) {
  35. return explode("/", $n)[7];
  36. }, glob($captcha_dir . "*")));
  37. $result = array_map(function($category) {
  38. return [$category, count(glob("/var/www/html/4get/data/captcha/" . $category . "/*" ))];
  39. }, $categories);
  40. return $result;
  41. }
  42. $special_keys = ["CAPTCHA_DATASET", "INSTANCES"];
  43. $output = "<?php\n // This file was generated by docker/gen_config.php\n";
  44. $output = $output . "class config {\n";
  45. foreach(($config) as $key => $val){
  46. if(!in_array($key, $special_keys)) {
  47. $output = $output . "\tconst " . $key . " = " . type_to_string($val) . ";\n";
  48. continue;
  49. }
  50. if($key === "CAPTCHA_DATASET") {
  51. $output = $output . "\tconst " . $key . " = " . type_to_string(detect_captcha_dirs()) . ";\n";
  52. }
  53. if($key === "INSTANCES") {
  54. $output = $output . "\tconst " . $key . " = " . type_to_string(explode(',', $val)) . ";\n";
  55. }
  56. }
  57. $output = $output . "}\n";
  58. $output = $output . "?>";
  59. file_put_contents("./data/config.php", $output);
  60. ?>