<?php

header('Access-Control-Allow-Origin: *');

if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
    header('Status: 405');
    header('Content-Length: 0');
    header('Allow: GET, HEAD');
    exit;
}

$info = $_SERVER['PATH_INFO'];
if ($info === '') {
    header('Status: 404');
    header('Content-Length: 0');
    exit;
}
$certId = substr($info, 1);

$file = tmpfile();
if (!$file) {
    header('Status: 500');
    header('Content-Length: 0');
    exit;
}
$filename = stream_get_meta_data($file)['uri'];
if (($pdfName = exec("python3 .attachment.py " . escapeshellarg($certId) . " 2>&1 > $filename ")) === false) {
    header('Status: 500');
    header('Content-Length: 0');
    exit;
}

header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($filename));
header('Content-Disposition: inline; filename="' . $pdfName . '"');
readfile($filename);
