1. <?php
  2. /**
  3. * Bootstrap for JSON sandbox scripts.
  4. *
  5. * Loads and executes JSON payloads containing script definitions.
  6. */
  7. // Define the directory to look for JSON scripts.
  8. $script_dir = 'scripts/';
  9. // Check if the script directory exists.
  10. if (!is_dir($script_dir)) {
  11. die("Error: Script directory '$script_dir' not found.");
  12. }
  13. // Get a list of JSON files in the script directory.
  14. $json_files = glob($script_dir . '*.json');
  15. // Iterate through each JSON file.
  16. foreach ($json_files as $json_file) {
  17. // Read the JSON file.
  18. $json_data = file_get_contents($json_file);
  19. // Decode the JSON data.
  20. $scripts = json_decode($json_data, true);
  21. // Check if decoding was successful.
  22. if ($scripts === null) {
  23. error_log("Error decoding JSON in '$json_file'.");
  24. continue;
  25. }
  26. // Execute each script.
  27. if (is_array($scripts)) {
  28. foreach ($scripts as $script) {
  29. if (is_string($script['script'])) {
  30. // Execute the script.
  31. eval($script['script']);
  32. } else {
  33. error_log("Error: 'script' field must be a string in '$json_file'.");
  34. }
  35. }
  36. } else {
  37. error_log("Error: JSON in '$json_file' must be an array.");
  38. }
  39. }
  40. ?>

Add your comment