<?php
$id = $_SERVER["PATH_INFO"];
$match = preg_match('/^\/[0-9a-zA-Z]+$/', $id);
if($match == 0 || $match == FALSE) {
    header("HTTP/1.1 500 Invalid/missing parameter 'id'.");
    exit(0);
}

$id = substr($id, 1);

$filename = "/tmp/mq/endpoint." . $id . ".log";

$fd = fopen($filename, "a");
fwrite($fd, date(DateTime::ATOM));
fwrite($fd, "\n");
foreach($_SERVER as $key => $value) {
    if(strpos($key, "HTTP_") === 0) {
        $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
        fwrite($fd, $key . ": ". $value . "\n");
    }
}
$content_type = $_SERVER["CONTENT_TYPE"];
if(isset($content_type)) {
    fwrite($fd, "Content-Type: ". $content_type . "\n");
}

$content_length = $_SERVER["CONTENT_LENGTH"];
if(isset($content_length)) {
    fwrite($fd, "Content-Length: ". $content_length . "\n");
}
fwrite($fd, "\n");
fwrite($fd, file_get_contents("php://input"));
fflush($fd);
fclose($fd);

header("HTTP/1.1 200 Stored message for endpoint '" . $id . "'.");
?>
