/email%40domain.com * * @author Dixon * @version 1.0 */ // Prevent any output buffering issues if (ob_get_level()) { ob_end_clean(); } // Retrieve the complete request URI from the server superglobal $requestUri = $_SERVER['REQUEST_URI'] ?? ''; // Parse the URL to extract only the path component, excluding any query strings // This ensures that any GET parameters don't interfere with email extraction $path = parse_url($requestUri, PHP_URL_PATH); // Remove leading and trailing forward slashes from the path // Example: '/email%40domain.com/' or '/email%40domain.com/github' $pathParts = explode('/', trim($path, '/')); // Extract email and optional parser $emailEncoded = $pathParts[0] ?? ''; $parserName = $pathParts[1] ?? null; // Decode the URL-encoded email address (e.g., %40 becomes @) // Example: 'email%40domain.com' becomes 'email@domain.com' $email = urldecode($emailEncoded); if(empty($email)) { include'docs.htm'; die(); } // Validate the extracted email address using PHP's built-in filter // This ensures the email is in a valid format before proceeding if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { // Set HTTP response code to 400 Bad Request http_response_code(400); header('Content-Type: application/json; charset=UTF-8'); echo json_encode([ 'ok' => false, 'error' => 'invalid_email', 'message' => 'Invalid email address provided in URL path.', 'hint' => 'Encode @ as %40, e.g. /you%40example.com', ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), "\n"; exit; } // Capture the current date and time when the webhook request was received // Format: YYYY-MM-DD HH:MM:SS (24-hour format) $dateReceived = date('Y-m-d H:i:s'); // Retrieve the IP address of the client making the request // Note: This may be a proxy IP if behind a load balancer or CDN $ipAddress = $_SERVER['REMOTE_ADDR'] ?? 'Unknown'; // Capture the HTTP method used for this request (GET, POST, PUT, DELETE, etc.) $requestMethod = $_SERVER['REQUEST_METHOD'] ?? 'Unknown'; // Retrieve the Content-Type header sent by the client // Use null coalescing operator to provide a default if not set $contentType = $_SERVER['CONTENT_TYPE'] ?? 'N/A'; // Retrieve all HTTP headers sent with the request // getallheaders() returns an associative array of all headers $headersArray = getallheaders(); // Initialize an empty string to store formatted headers $headersText = ''; // Iterate through each header and format it as "Key: Value" foreach ($headersArray as $key => $value) { // Concatenate each header on a new line $headersText .= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . ': ' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . "\n"; } // Read the raw request body from the input stream // php://input allows access to the raw POST data regardless of Content-Type $rawBody = file_get_contents('php://input'); // Sanitise the raw body for HTML output to prevent XSS attacks $rawBodySafe = htmlspecialchars($rawBody, ENT_QUOTES, 'UTF-8'); // Compile all PHP superglobal variables into an array for debugging purposes // This provides visibility of all data received with the request $variables = [ 'GET Variables' => $_GET, 'POST Variables' => $_POST, 'Raw Request Body' => $_REQUEST, 'Uploaded Files' => $_FILES, ]; // Convert the variables array to a human-readable string format // The second parameter (true) makes print_r return the output instead of printing it $variablesText = json_encode($variables, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); // Sanitise the variables text for HTML output $variablesTextSafe = htmlspecialchars($variablesText, ENT_QUOTES, 'UTF-8'); // Sanitise individual metadata fields for HTML output $dateReceivedSafe = htmlspecialchars($dateReceived, ENT_QUOTES, 'UTF-8'); $ipAddressSafe = htmlspecialchars($ipAddress, ENT_QUOTES, 'UTF-8'); $requestMethodSafe = htmlspecialchars($requestMethod, ENT_QUOTES, 'UTF-8'); $contentTypeSafe = htmlspecialchars($contentType, ENT_QUOTES, 'UTF-8'); // Load parser helpers require_once __DIR__ . '/parsers/helpers.php'; // Check if a parser is specified and load it $html = ''; $subject = ''; if ($parserName) { $parserFile = __DIR__ . '/parsers/' . $parserName . '.php'; if (file_exists($parserFile)) { require_once $parserFile; $parserFunction = $parserName . 'Parse'; if (function_exists($parserFunction)) { $parsed = $parserFunction($rawBody, $headersArray, [ 'date' => $dateReceived, 'ip' => $ipAddress, 'method' => $requestMethod, 'contentType' => $contentType ]); if ($parsed !== false) { $html = $parsed['html']; $subject = $parsed['subject']; } } } } // If no parser or parser failed, use default format if (empty($html)) { // Construct the HTML email body using a heredoc string for better readability // This creates a styled HTML email with all webhook details $emailTitle = "⚡ Webhook Request Received"; $emailStamp = "$dateReceivedSafe"; $body = <<
| IP Address | $ipAddressSafe |
| Method | $requestMethodSafe |
| Content-Type | $contentTypeSafe |
$headersText
$rawBodySafe
$variablesTextSafe