76 lines
2.9 KiB
PHP
Executable File
76 lines
2.9 KiB
PHP
Executable File
<?php
|
|
function load($class) { include __DIR__."/../util/".$class.".php"; }
|
|
function sendJSON($code, $message) {
|
|
header("Content-Type: ".MimeTypes::getType("json"));
|
|
echo json_encode(array("code" => $code, "message" => $message));
|
|
exit(0);
|
|
}
|
|
spl_autoload_register('load');
|
|
DB::init("localhost", "nutzername", "passwort", "datenbank");
|
|
$media_dir = "/mnt/cdn/";
|
|
|
|
function checkAPIKey(): string {
|
|
if (isset($_SERVER['HTTP_API_KEY'])) {
|
|
if (DB::get()->query("SELECT null FROM api_keys WHERE apikey=?", $_SERVER['HTTP_API_KEY'])->numRows() != 1)
|
|
sendJSON(502, "You need to provide an valid api key.");
|
|
} else sendJSON(502, "You need to provide an valid api key.");
|
|
return $_SERVER['HTTP_API_KEY'];
|
|
}
|
|
|
|
Router::add("/upload", function () use ($media_dir) {
|
|
$key = checkAPIKey();
|
|
if(!empty($_FILES['asset'])) {
|
|
$fileID = substr(number_format(time() * mt_rand(),0,'',''),0,16);
|
|
$path = $media_dir.$fileID;
|
|
$asset = $_FILES['asset'];
|
|
$fileName = $asset['name'];
|
|
|
|
if(move_uploaded_file($_FILES['asset']['tmp_name'], $path)) {
|
|
$split = explode(".", $fileName);
|
|
$fileEnding = substr($split[count($split)-1], -5);
|
|
|
|
DB::get()->query("INSERT INTO media (assetID, assetEnding, assetOwner, assetName, assetDescription) VALUES (?, ?, ?, ?, ?)",
|
|
$fileID, $fileEnding, $key, $fileName, "test");
|
|
sendJSON(1, "https://cdn.sheepstar.xyz/".$fileID.".".$fileEnding);
|
|
} else {
|
|
sendJSON(500, "File upload failed");
|
|
}
|
|
}
|
|
}, "post");
|
|
|
|
Router::add("/delete", function () use ($media_dir) {
|
|
checkAPIKey();
|
|
parse_str(file_get_contents("php://input"),$post_vars);
|
|
if (isset($post_vars['assetID'])) {
|
|
$assetID = $post_vars['assetID'];
|
|
$query = DB::get()->query("SELECT null FROM media WHERE assetID=?", $assetID)->numRows();
|
|
if ($query) {
|
|
unlink($media_dir.$assetID);
|
|
DB::get()->query("DELETE FROM media WHERE assetID=?", $assetID);
|
|
sendJSON(1, "Resource deleted.");
|
|
} else sendJSON(404, "Resource not found.");
|
|
} else sendJSON(405, "Please provide an assetID.");
|
|
}, "delete");
|
|
|
|
Router::add("^/[0-9]+\.[a-zA-Z1-9]+$", function () use ($media_dir) {
|
|
|
|
$parsed_url = parse_url($_SERVER['REQUEST_URI']);
|
|
$url = str_replace("/", "", $parsed_url['path']);
|
|
$split = explode(".", $url);
|
|
$assetID = $split[0];
|
|
$assetEnding = $split[count($split)-1];
|
|
$query = DB::get()->query("SELECT null FROM media WHERE assetID=? AND assetEnding=?", $assetID, $assetEnding)->numRows();
|
|
|
|
if ($query == 1) {
|
|
header("Content-Type: " . MimeTypes::getFromString($url));
|
|
echo file_get_contents($media_dir.$assetID);
|
|
} else sendJSON(404, "Resource not found.");
|
|
|
|
});
|
|
|
|
Router::add(".+", function () {
|
|
sendJSON(402, "Please use the correct URL format");
|
|
});
|
|
|
|
Router::run();
|