organic: Enhance external apis

This commit is contained in:
2025-12-22 16:56:23 +01:00
parent 551ddd3c11
commit 0cd3b3de8e
10 changed files with 350 additions and 110 deletions

View File

@@ -1,11 +1,15 @@
<?php
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json; charset=UTF-8");
$base = 'https://' . $_SERVER['SERVER_NAME'] . explode('?', $_SERVER['REQUEST_URI'])[0];
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
header('Status: 405');
header('Content-Length: 0');
header('Content-Length: 56');
header('Allow: GET, HEAD');
echo "{\"error\":\"method not allowed\",\"allow\":[\"GET\",\"HEAD\"]}\n";
exit;
}
@@ -22,13 +26,15 @@ if ($info !== '') {
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
if (($json = curl_exec($s)) === false) {
header('Status: 500');
header('Content-Length: 0');
header('Content-Length: 34');
echo "{\"error\":\"internal server error\"}\n";
exit;
}
$json = json_decode($json);
if ($json->{'totalCount'} !== 1) {
header('Status: 404');
header('Content-Length: 0');
header('Content-Length: 22');
echo "{\"error\":\"not found\"}\n";
exit;
}
header('Status: 303');
@@ -36,7 +42,6 @@ if ($info !== '') {
exit;
}
$refDate = $_GET['referenceDate'] ?? null;
if ($refDate !== null) {
$refDate = explode('-', $refDate);
@@ -47,7 +52,8 @@ if ($info !== '') {
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
if (($html = curl_exec($s)) === false) {
header('Status: 500');
header('Content-Length: 0');
header('Content-Length: 34');
echo "{\"error\":\"internal server error\"}\n";
exit;
}
@@ -73,7 +79,8 @@ if ($info !== '') {
if ($data['Name'] === null) {
header('Status: 404');
header('Content-Length: 0');
header('Content-Length: 22');
echo "{\"error\":\"not found\"}\n";
exit;
}
@@ -107,21 +114,21 @@ if ($info !== '') {
];
}
header("Content-Type: application/json; charset=UTF-8");
echo '{"db":' . jenc($parts[0]) .
',"id":' . jenc($parts[1]) .
",\n \"idNr\":" . jenc($data['ID-Nummer'] ?? null) .
',"name":' . jenc($data['Name']) .
',"address":' . jenc($data['Strasse'] ?? null) .
',"postalCode":' . jenc($postalCode) .
',"city":' . jenc($city) .
',"countryCode":' . jenc($data['Land'] ?? null) .
",\n \"referenceDate\":" . jenc($refDate) .
",\n \"certificates\":" . jenc($certs) .
",\n \"privateStandardApprovals\":" . jenc($labels) .
'}';
echo jenc([
'db' => $parts[0],
'id' => $parts[1],
'idNr' => $data['ID-Nummer'] ?? null,
'name' => $data['Name'],
'address' => $data['Strasse'] ?? null,
'postalCode' => $postalCode,
'city' => $city,
'countryCode' => $data['Land'] ?? null,
'url' => $base,
'originUrl' => $url,
'referenceDate' => $refDate,
'certificates' => $certs,
'privateStandardApprovals' => $labels,
]);
exit;
}
@@ -207,12 +214,12 @@ if ($url === null) {
}
if ($url === null) {
header("Status: 500");
header("Content-Length: 0");
header('Status: 500');
header('Content-Length: 34');
echo "{\"error\":\"internal server error\"}\n";
exit;
}
$sed = [
's/^.*"searchresults":\[//',
's/],.*$//',
@@ -235,8 +242,6 @@ $replace = [
];
$replaceSed = array_map(fn($v, $k): string => "s/$k/$v/", $replace, array_keys($replace));
header("Content-Type: application/json; charset=UTF-8");
echo '{"searchUrl":' . jenc($search_url) .
',"queryId":' . jenc($query_id) .
',"rawFileUrl":' . jenc($url) .
@@ -262,6 +267,7 @@ $process = proc_open(
"tail -n +$offset | " . // apply offset
($limit !== null ? " head -n $limit | " : "") . // optionally apply limit
"sed '" . implode(';', $replaceSed) . "' | " . // replace strings in json
"sed 's|{\\(\"db\":\"\\([^\"]*\\)\".*\"id\":\"\\([^\"]*\\)\".*\\)}|{\\1,\"url\":\"$base/\\2:\\3\"}|' | " . // add "url"
"sed '\$s/.$//'"], // remove last comma of last line
$fd_spec,
$pipes