Created
March 3, 2024 03:15
-
-
Save corarona/b752fc4b15d3bbb75e10400dac3f41c4 to your computer and use it in GitHub Desktop.
Dead simple cheatdb replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Minimum viable cheatdb replacement. | |
// Needs a directory of clientmods at ./packages | |
// Run with php -S 127.0.0.1:8080 simple_cheatdb.php | |
// Set dragonfire contentdb_url setting to http://127.0.0.1:8080 | |
// Needs php-zip (e.g. apt install php8.2-zip in debian) | |
$packs = scandir("./packages"); | |
error_log($_SERVER['REQUEST_URI']); | |
$rq = explode("/", $_SERVER['REQUEST_URI']); | |
$out = ''; | |
error_log(count($rq)); | |
function zip_folder($rootPath, $output_zip_file) { | |
$rootPath = rtrim($rootPath, '\\/'); | |
$rootPath = realpath($rootPath); | |
$zip = new ZipArchive(); | |
$zip->open($output_zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE); | |
$files = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($rootPath), | |
RecursiveIteratorIterator::LEAVES_ONLY | |
); | |
foreach ($files as $file) | |
{ | |
if (!$file->isDir()) | |
{ | |
$filePath = $file->getRealPath(); | |
$relativePath = substr($filePath, strlen($rootPath) + 1); | |
if(substr($relativePath, 0, 4) == ".git") continue; | |
$zip->addFile($filePath, $relativePath); | |
} | |
} | |
$zip->close(); | |
} | |
if(count($rq) <= 4) { | |
error_log("LIST"); | |
$chdb = array(); | |
foreach($packs as $file) { | |
if($file == "." or $file == ".." or $file == "index.php") continue; | |
$r = array(); | |
$r["name"] = $file; | |
$r["title"] = $file; | |
$r["author"] = "lol"; | |
$r["type"] = "mod"; | |
$r["release"] = 1; | |
$chdb[] = $r; | |
} | |
$out=json_encode($chdb); | |
} elseif(count($rq) == 6) { | |
error_log("PACK"); | |
$r = array(); | |
$r["author"] = $rq[3]; | |
$r["title"] = $rq[4]; | |
$r["name"] = $rq[4]; | |
$r["release"] = 1; | |
$r["provides"] = array($rq[4]); | |
$out=json_encode($r); | |
} elseif(count($rq) >= 6 and $rq[5] == "dependencies") { | |
error_log("DEP"); | |
$out='{ "'.$rq[3].'/'.$rq[4].'":[]}'; | |
} elseif(count($rq) >= 6 and $rq[6] == "download") { | |
error_log("DL"); | |
$dir="./packages/".$rq[3]; | |
error_log($dir); | |
$filename = "./pack.zip"; | |
zip_folder($dir, $filename); | |
header("Content-type: application/zip"); | |
header("Content-Disposition: attachment; filename=".$rq[3].".zip"); | |
header("Content-length: " . filesize($filename)); | |
header("Pragma: no-cache"); | |
header("Expires: 0"); | |
readfile("$filename"); | |
die(); | |
} | |
die($out); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment