<?php
/**
* Batch Response Header Modifier (Development Only)
*
* This script allows for batch modification of HTTP response headers.
* It's intended for development and debugging purposes only.
* DO NOT USE IN PRODUCTION.
*/
/**
* Modifies response headers based on a provided array of operations.
*
* @param array $operations An array of header modification operations.
* Each operation should be an associative array with:
* - 'name': The header name (string).
* - 'value': The new header value (string).
* - 'action': 'set' to set the header, 'add' to add a header, or 'remove' to remove a header.
* @return array An array of results, each containing the operation, status (success/error), and message.
*/
function batchModifyHeaders(array $operations): array
{
$results = [];
foreach ($operations as $operation) {
$name = $operation['name'];
$value = $operation['value'];
$action = $operation['action'];
// Validate input
if (empty($name)) {
$results[] = [
'operation' => $operation,
'status' => 'error',
'message' => 'Header name cannot be empty.'
];
continue;
}
if (!is_string($name) || empty($value) || !is_string($value)) {
$results[] = [
'operation' => $operation,
'status' => 'error',
'message' => 'Invalid header name or value. Must be non-empty strings.'
];
continue;
}
// Perform the header modification. This is a simplified example.
// In a real application, you would modify the headers before sending the response.
$headers = array();
if ($action === 'set') {
$headers[$name] = $value;
} elseif ($action === 'add') {
$headers[$name] = $value;
} elseif ($action === 'remove') {
if (isset($headers[$name])) {
unset($headers[$name]);
} else {
$results[] = [
'operation' => $operation,
'status' => 'error',
'message' => 'Header ' . $name . ' not found.'
];
continue;
}
} else {
$results[] = [
'operation' => $operation,
'status' => 'error',
'message' => 'Invalid action. Must be "set", "add", or "remove".'
];
continue;
}
//Simulate setting the headers (in a real app, this would be done differently)
header_set_extra_filter('no-cache'); // Example header
$results[] = [
'operation' => $operation,
'status' => 'success',
'message' => 'Header ' . $name . ' ' . $action . ' successfully.'
];
}
return $results;
}
// Example Usage (Development Only!)
$operations = [
['name' => 'X-Custom-Header', 'value' => 'Test Value', 'action' => 'add'],
['name' => 'Content-Type', 'value' => 'application/json', 'action' => 'set'],
['name' => 'X-Debug-Header', 'value' => 'Debug Info', 'action' => 'add'],
['name' => 'Content-Type', 'value' => 'application/xml', 'action' => 'set'], //This will fail as Content-Type is likely already set.
['name' => '', 'value' => 'Test Value', 'action' => 'add'], //Invalid name
['name' => 'X-Invalid', 'value' => '', 'action' => 'add'], //Invalid Value
['name' => 'NonExistentHeader', 'value' => 'Some Value', 'action' => 'remove'],
['name' => 'X-ExistingHeader', 'value' => 'Some Value', 'action' => 'remove'],
];
$results = batchModifyHeaders($operations);
foreach ($results as $result) {
echo "<pre>";
print_r($result);
echo "</pre>";
Add your comment