1. <?php
  2. /**
  3. * Batch Response Header Modifier (Development Only)
  4. *
  5. * This script allows for batch modification of HTTP response headers.
  6. * It's intended for development and debugging purposes only.
  7. * DO NOT USE IN PRODUCTION.
  8. */
  9. /**
  10. * Modifies response headers based on a provided array of operations.
  11. *
  12. * @param array $operations An array of header modification operations.
  13. * Each operation should be an associative array with:
  14. * - 'name': The header name (string).
  15. * - 'value': The new header value (string).
  16. * - 'action': 'set' to set the header, 'add' to add a header, or 'remove' to remove a header.
  17. * @return array An array of results, each containing the operation, status (success/error), and message.
  18. */
  19. function batchModifyHeaders(array $operations): array
  20. {
  21. $results = [];
  22. foreach ($operations as $operation) {
  23. $name = $operation['name'];
  24. $value = $operation['value'];
  25. $action = $operation['action'];
  26. // Validate input
  27. if (empty($name)) {
  28. $results[] = [
  29. 'operation' => $operation,
  30. 'status' => 'error',
  31. 'message' => 'Header name cannot be empty.'
  32. ];
  33. continue;
  34. }
  35. if (!is_string($name) || empty($value) || !is_string($value)) {
  36. $results[] = [
  37. 'operation' => $operation,
  38. 'status' => 'error',
  39. 'message' => 'Invalid header name or value. Must be non-empty strings.'
  40. ];
  41. continue;
  42. }
  43. // Perform the header modification. This is a simplified example.
  44. // In a real application, you would modify the headers before sending the response.
  45. $headers = array();
  46. if ($action === 'set') {
  47. $headers[$name] = $value;
  48. } elseif ($action === 'add') {
  49. $headers[$name] = $value;
  50. } elseif ($action === 'remove') {
  51. if (isset($headers[$name])) {
  52. unset($headers[$name]);
  53. } else {
  54. $results[] = [
  55. 'operation' => $operation,
  56. 'status' => 'error',
  57. 'message' => 'Header ' . $name . ' not found.'
  58. ];
  59. continue;
  60. }
  61. } else {
  62. $results[] = [
  63. 'operation' => $operation,
  64. 'status' => 'error',
  65. 'message' => 'Invalid action. Must be "set", "add", or "remove".'
  66. ];
  67. continue;
  68. }
  69. //Simulate setting the headers (in a real app, this would be done differently)
  70. header_set_extra_filter('no-cache'); // Example header
  71. $results[] = [
  72. 'operation' => $operation,
  73. 'status' => 'success',
  74. 'message' => 'Header ' . $name . ' ' . $action . ' successfully.'
  75. ];
  76. }
  77. return $results;
  78. }
  79. // Example Usage (Development Only!)
  80. $operations = [
  81. ['name' => 'X-Custom-Header', 'value' => 'Test Value', 'action' => 'add'],
  82. ['name' => 'Content-Type', 'value' => 'application/json', 'action' => 'set'],
  83. ['name' => 'X-Debug-Header', 'value' => 'Debug Info', 'action' => 'add'],
  84. ['name' => 'Content-Type', 'value' => 'application/xml', 'action' => 'set'], //This will fail as Content-Type is likely already set.
  85. ['name' => '', 'value' => 'Test Value', 'action' => 'add'], //Invalid name
  86. ['name' => 'X-Invalid', 'value' => '', 'action' => 'add'], //Invalid Value
  87. ['name' => 'NonExistentHeader', 'value' => 'Some Value', 'action' => 'remove'],
  88. ['name' => 'X-ExistingHeader', 'value' => 'Some Value', 'action' => 'remove'],
  89. ];
  90. $results = batchModifyHeaders($operations);
  91. foreach ($results as $result) {
  92. echo "<pre>";
  93. print_r($result);
  94. echo "</pre>";

Add your comment