cai-exos-systems/daveadmin-exos-demo:api/tool-export.php
api/tool-export.php
```text
<?php
declare(strict_types=1);
require_once dirname(__DIR__) . '/auth.php';
demoRequireApiLogin();
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
http_response_code(405);
header('Content-Type: application/json; charset=UTF-8');
echo json_encode(['error' => 'Method not allowed']);
exit;
}
$id = trim((string)($_GET['id'] ?? ''));
$type = trim((string)($_GET['type'] ?? 'manifest'));
$typeMap = [
'manifest' => ['key' => 'estate_manifest', 'download' => 'estate-manifest.json'],
'tools' => ['key' => 'tool_spec', 'download' => 'tool-spec.json'],
'copilot' => ['key' => 'copilot_digest', 'download' => 'copilot-digest.json'],
'factory' => ['key' => 'factory_response', 'download' => 'factory-response.json'],
];
if ($id === '' || !isset($typeMap[$type])) {
http_response_code(400);
header('Content-Type: application/json; charset=UTF-8');
echo json_encode(['error' => 'Invalid export request']);
exit;
}
$registryPath = (string)demoEnv(
'EXOS_TOOL_REGISTRY_FILE',
demoSupportDir() . DIRECTORY_SEPARATOR . 'tool-creator-registry.json'
);
$exportDir = (string)demoEnv(
'EXOS_TOOL_EXPORT_DIR',
demoSupportDir() . DIRECTORY_SEPARATOR . 'tool-creator-exports'
);
$registry = [];
if (is_file($registryPath)) {
$decoded = json_decode((string)file_get_contents($registryPath), true);
if (is_array($decoded)) {
$registry = $decoded;
}
}
$entry = null;
foreach ($registry as $row) {
if (($row['id'] ?? '') === $id) {
$entry = $row;
break;
}
}
$key = $typeMap[$type]['key'];
$file = is_array($entry) ? (string)($entry['files'][$key] ?? '') : '';
$path = $file !== '' ? $exportDir . DIRECTORY_SEPARATOR . $file : '';
if (!is_array($entry) || $file === '' || !is_file($path)) {
http_response_code(404);
header('Content-Type: application/json; charset=UTF-8');
echo json_encode(['error' => 'Export not found']);
exit;
}
header('Content-Type: application/json; charset=UTF-8');
header('Content-Disposition: inline; filename="' . $typeMap[$type]['download'] . '"');
readfile($path);
```