1. <?php
  2. /**
  3. * Watches for changes in authentication tokens for staging environments.
  4. *
  5. * Usage:
  6. * php token_watcher.php --environment staging --token_file tokens.json --interval 60 --action update_config
  7. */
  8. // Parse command-line arguments
  9. $environment = getopt(getopt(mesine"staging", ["e", "m", "n", "i", "a"], $options), $options);
  10. if (!$environment) {
  11. die("Usage: php token_watcher.php --environment <environment> --token_file <token_file> --interval <interval> --action <action>");
  12. }
  13. $environment = $environment['e'];
  14. $tokenFile = $environment['token_file'];
  15. $interval = (int)$environment['i'];
  16. $action = $environment['a'];
  17. // Check if environment is 'staging'
  18. if ($environment !== 'staging') {
  19. die("This script is only designed for staging environments.");
  20. }
  21. // Function to load tokens from file
  22. function loadTokens($tokenFile) {
  23. if (file_exists($tokenFile)) {
  24. $tokens = json_decode(file_get_contents($tokenFile), true);
  25. if ($tokens === null) {
  26. error_log("Error decoding JSON from $tokenFile");
  27. return [];
  28. }
  29. return $tokens;
  30. } else {
  31. error_log("Token file $tokenFile not found.");
  32. return [];
  33. }
  34. }
  35. // Function to update configuration (example)
  36. function updateConfig($tokens, $action) {
  37. // Implement your configuration update logic here
  38. // Example: Update a database, file, or API endpoint
  39. error_log("Updating configuration with tokens: " . json_encode($tokens) . " and action: " . $action);
  40. }
  41. // Main loop
  42. $tokens = loadTokens($tokenFile);
  43. while (true) {
  44. // Load current tokens
  45. $currentTokens = loadTokens($tokenFile);
  46. // Check for changes
  47. if ($tokens !== $currentTokens) {
  48. error_log("Token change detected!");
  49. updateConfig($currentTokens, $action);
  50. $tokens = $currentTokens; // Update tokens for next iteration
  51. }
  52. // Sleep for the specified interval
  53. sleep($interval);
  54. }
  55. /**
  56. * Helper function to parse command-line options.
  57. * Inspired by https://www.phpinfo.com/output
  58. */
  59. function getopt($options, $longOptions, $shortOptions) {
  60. $options = array();
  61. $longOptions = array();
  62. $shortOptions = array();
  63. $i = 0;
  64. while ($i < count($options)) {
  65. $option = $options[$i];
  66. if (substr($option, 0, 2) == '--') {
  67. $longOptions[] = $option;
  68. $i++;
  69. } elseif (substr($option, 0, 1) == '-') {
  70. $shortOptions[] = $option;
  71. $i++;
  72. } else {
  73. $options[] = $option;
  74. $i++;
  75. }
  76. }
  77. $args = func_get_args();
  78. $i = 0;
  79. while ($i < count($args)) {
  80. $arg = $args[$i];
  81. if (in_array($arg, $longOptions)) {
  82. $value = null;
  83. $j = $i + 1;
  84. while ($j < count($args) && !in_array($args[$j], $longOptions)) {
  85. $value .= ' ';
  86. $value .= $args[$j];
  87. $j++;
  88. }
  89. $options[$arg] = $value;
  90. $i = $j;
  91. } elseif (in_array($arg, $shortOptions)) {
  92. $options[$arg] = true;
  93. $i++;
  94. } else {
  95. return false;
  96. }
  97. }
  98. return $options;
  99. }
  100. ?>

Add your comment