diff --git a/upload.php b/upload.php index c406e76..e26abd5 100644 --- a/upload.php +++ b/upload.php @@ -1,7 +1,7 @@ 100000000) { // if the file is larger than 100MB - echo "The file is too large"; - die(); -} - -if (!isZipFile($map["name"])) { - echo "The file is not a zip file"; - die(); -} - -$fileHash = hash_file("sha256", $file); // the hash of the file - -$exists = false; -foreach (scandir($target_dir) as $file) { // scan the maps directory for a file with the same hash - if ($file == "." || $file == "..") { // ignore the . and .. files - continue; - } - if (hash_file("sha256", $target_dir . $file) == $fileHash) { - $mapId = pathinfo($file, PATHINFO_FILENAME); - $exists = true; - break; + $zip = new ZipArchive(); + if ($zip->open($file) === true) { + if ($zip->locateName("info.dat", ZipArchive::FL_NOCASE) === false) { + return false; + } + $zip->close(); + return true; } + return false; } -if ($exists) { // if the file already exists, redirect to the existing file +try { + $map = $_FILES["map"]; // the file to upload + + if (!isset($map)) { // if the file is not set + echo "No file was uploaded"; + die(); + } + + $mapId = generateMapId(); // the id of the map + $file = $map["tmp_name"]; // the temporary file path + $size = $map["size"]; // the size of the file + + if ($size > 100000000) { // if the file is larger than 100MB + echo "The file is too large"; + die(); + } + + if (!isValidBeatSaberMap($file)) { + echo "The file is not a valid BeatSaber map"; + die(); + } + + $fileHash = hash_file("sha256", $file); // the hash of the file + + $exists = false; + foreach (scandir($target_dir) as $scannedFile) { // scan the maps directory for a file with the same hash + if ($scannedFile == "." || $scannedFile == "..") { // ignore the . and .. files + continue; + } + if (hash_file("sha256", $target_dir . $scannedFile) == $fileHash) { + $mapId = pathinfo($scannedFile, PATHINFO_FILENAME); + $exists = true; + break; + } + } + + if ($exists) { // if the file already exists, redirect to the existing file + header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip"); + die(); + } + + $target_file = $target_dir . $mapId . ".zip"; // the output file path + if (!move_uploaded_file($file, $target_file)) { + error_log("Error: Failed to move uploaded file from $file to $target_file"); + echo "There was an error uploading the file"; + die(); + } + + error_log("Uploaded file moved from $file to $target_file"); + header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip"); +} catch (Exception $e) { + echo "There was an error uploading the file. Error: " . $e->getMessage(); die(); } - -$target_file = $target_dir . $mapId . ".zip"; -if (!move_uploaded_file($file, $target_file)) { - echo "There was an error uploading the file"; - die(); -} - -// redirect to /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip -header("Location: /?map=https://wip.fascinated.cc/maps/" . $mapId . ".zip");