Add /organic API

This commit is contained in:
2025-07-11 23:45:28 +02:00
parent 25805f0475
commit 8757940a3c
3 changed files with 230 additions and 0 deletions

74
www/organic/index.php Normal file
View File

@@ -0,0 +1,74 @@
<?php
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 !== '/' && str_ends_with($info, '/')) {
header('Status: 303');
header('Content-Length: 0');
header('Location: /organic/' . substr($info, 0, -1));
exit;
}
$parts = explode('/', $info);
if (str_starts_with($info, '/certificates/')) {
$id = $parts[2];
if (str_ends_with($id, '.txt')) {
$id = substr($id, 0, -4);
}
if (str_contains($id, '/') || !file_exists("certificates/$id.pdf")) {
header('Content-Length: 0');
header('Status: 404');
exit;
}
if (str_ends_with($parts[2], '.txt')) {
$mode = '-layout';
if (isset($_GET['raw']) && strtolower($_GET['raw']) === 'true') {
$mode = '-raw';
}
header('Content-Type: text/plain; charset=UTF-8');
system("pdftotext $mode 'certificates/$id.pdf' -");
exit;
}
if (str_ends_with($id, '.appendix')) {
header('Status: 303');
header('Location: ' . substr($id, 0, -9));;
exit;
}
$cert = shell_exec("pdftotext -raw 'certificates/$id.pdf' -");
$appendix = shell_exec("pdftotext -raw 'certificates/$id.appendix.pdf' -");
$p1 = strpos($cert, "\nI.3 ");
$p2 = strpos($cert, "\nI.4 ");
echo substr($cert, $p1 + 5, $p2 - $p1 - 5);
exit;
} else if ($info === '/authorities') {
header('Content-Length: 0');
header('Status: 501');
exit;
} else if (str_starts_with($info, '/authorities/')) {
$code = $parts[2];
header('Content-Type: text/plain; charset=UTF-8');
echo "Control Authority Code: $code\n";
exit;
} else if ($info === '/operators') {
header('Status: 501');
exit;
} else if (str_starts_with($info, '/operators/')) {
$ooid = $parts[2];
header('Content-Type: text/plain; charset=UTF-8');
echo "Organic Operator Id: $ooid\n";
exit;
}
header('Content-Length: 0');
header('Status: 404');