114 lines
2.8 KiB
PHP
114 lines
2.8 KiB
PHP
<?php
|
|
include_once '../controller/DatabaseController.php';
|
|
include_once '../controller/DataController.php';
|
|
|
|
header("Content-Type: application/json");
|
|
header("Access-Control-Allow-Origin: *");
|
|
header("Access-Control-Allow-Headers: Authorization, Function, Content-Type");
|
|
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
|
|
|
|
$validTokens = [
|
|
"15a437d128c918633e57dc7d18118a073b807644f21731ed5a7ebcf3eca9e242",
|
|
];
|
|
$baseUploadDir = __DIR__ . "/../services/uploads/";
|
|
|
|
$headers = getallheaders();
|
|
$token = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
|
|
|
|
$token = trim($token);
|
|
|
|
if (!in_array($token, $validTokens, true)) {
|
|
http_response_code(401);
|
|
echo json_encode([
|
|
"error" => "Unauthorized",
|
|
"received_token" => $token
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
if (!isset($_FILES['files'])) {
|
|
http_response_code(400);
|
|
echo json_encode(["error" => "No files uploaded"]);
|
|
exit;
|
|
}
|
|
|
|
$folderName = null;
|
|
$readmeContent = null;
|
|
|
|
// Prüfen ob readme.md hochgeladen wurde
|
|
foreach ($_FILES['files']['name'] as $key => $name) {
|
|
if (strtolower($name) === "readme.md") {
|
|
$tmpName = $_FILES['files']['tmp_name'][$key];
|
|
$readmeContent = file_get_contents($tmpName);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($readmeContent) {
|
|
// Erste Markdown-Überschrift extrahieren (# Titel)
|
|
if (preg_match('/^#\s*(.+)$/m', $readmeContent, $matches)) {
|
|
$rawName = $matches[1];
|
|
|
|
// Slug erzeugen (ordner-tauglich machen)
|
|
$folderName = strtolower($rawName);
|
|
$folderName = preg_replace("/[^a-z0-9]+/", "-", $folderName);
|
|
$folderName = trim($folderName, "-");
|
|
}
|
|
}
|
|
|
|
// Fallback falls keine readme oder kein Titel
|
|
if (!$folderName) {
|
|
$folderName = uniqid("upload_", true);
|
|
}
|
|
$uploadDir = $baseUploadDir . $folderName . "/";
|
|
|
|
if (!mkdir($uploadDir, 0755, true)) {
|
|
http_response_code(500);
|
|
echo json_encode(["error" => "Failed to create directory"]);
|
|
exit;
|
|
}
|
|
|
|
$uploadedFiles = [];
|
|
|
|
foreach ($_FILES['files']['tmp_name'] as $key => $tmpName) {
|
|
|
|
if ($_FILES['files']['error'][$key] !== UPLOAD_ERR_OK) {
|
|
continue;
|
|
}
|
|
|
|
$originalName = basename($_FILES['files']['name'][$key]);
|
|
|
|
$safeName = preg_replace("/[^a-zA-Z0-9._-]/", "", $originalName);
|
|
|
|
$finfo = new finfo(FILEINFO_MIME_TYPE);
|
|
$mime = $finfo->file($tmpName);
|
|
|
|
if ($_FILES['files']['size'][$key] > 25 * 1024 * 1024) {
|
|
continue;
|
|
}
|
|
|
|
$targetPath = $uploadDir . $safeName;
|
|
|
|
if (move_uploaded_file($tmpName, $targetPath)) {
|
|
$uploadedFiles[] = $safeName;
|
|
}
|
|
}
|
|
|
|
$json = json_encode([
|
|
"success" => true,
|
|
"folder" => $folderName,
|
|
"uploaded_files" => $uploadedFiles
|
|
]);
|
|
|
|
DatabaseInsert(
|
|
"API",
|
|
"CLIENT_IP, TOKEN, API, FUNKTION, JSON",
|
|
(new getSpecificData())->getClientIP(),
|
|
$token,
|
|
(new getSpecificData())->getRequestURL(),
|
|
"Upload",
|
|
$json
|
|
);
|
|
|
|
echo $json;
|