51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
require_once __DIR__ . '/glpi_api.php';
|
|
|
|
$user = checkAuth($pdo);
|
|
if (!$user) {
|
|
http_response_code(401);
|
|
exit;
|
|
}
|
|
|
|
$kid = isset($_GET['kid']) ? (int)$_GET['kid'] : 0;
|
|
$docId = isset($_GET['doc']) ? (int)$_GET['doc'] : 0;
|
|
|
|
if (!$kid || !$docId) {
|
|
http_response_code(400);
|
|
exit;
|
|
}
|
|
|
|
$article = getLocalKnowledgeArticle($pdo, $kid);
|
|
if (!$article) {
|
|
http_response_code(404);
|
|
exit;
|
|
}
|
|
|
|
$docs = json_decode($article['documents'], true);
|
|
$targetDoc = null;
|
|
foreach ($docs as $d) {
|
|
if ($d['id'] == $docId) {
|
|
$targetDoc = $d;
|
|
break;
|
|
}
|
|
}
|
|
if (!$targetDoc) {
|
|
http_response_code(403);
|
|
exit;
|
|
}
|
|
|
|
$glpi = new GlpiApi(GLPI_API_URL, GLPI_APP_TOKEN, GLPI_USERNAME, GLPI_PASSWORD);
|
|
$filePath = lazyLoadDocument($pdo, $kid, $docId, $targetDoc['name'], $targetDoc['mime'], $glpi);
|
|
if (!$filePath || !file_exists($filePath)) {
|
|
http_response_code(404);
|
|
exit;
|
|
}
|
|
|
|
$mime = mime_content_type($filePath);
|
|
header('Content-Type: ' . $mime);
|
|
header('Content-Length: ' . filesize($filePath));
|
|
header('Content-Disposition: inline; filename="' . $targetDoc['name'] . '"');
|
|
header('Cache-Control: public, max-age=86400');
|
|
readfile($filePath);
|
|
exit; |