Skip to content

Commit

Permalink
api/index.php/mod: filter in db instead of php, add /loader endpoint,…
Browse files Browse the repository at this point in the history
… index.php: new description/titles/labels, index.php/modloaders: dynamic/js ui, filter out beta neoforge, add filesize to loaders, loader filenames reflect the type
  • Loading branch information
cowpod committed Nov 15, 2024
1 parent e262c7e commit 66d76c8
Show file tree
Hide file tree
Showing 5 changed files with 410 additions and 233 deletions.
45 changes: 40 additions & 5 deletions api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,52 @@
$mcversion=$_GET['mcversion'];
}
if (!empty($loadertype) && !empty($mcversion)) {
$modq = $db->query("SELECT * FROM mods WHERE loadertype='{$loadertype}' AND ('{$mcversion}' LIKE mcversion || '%' OR mcversion LIKE '{$mcversion}' || '%')");
$modq = $db->query("SELECT * FROM mods WHERE type='mod' AND loadertype='{$loadertype}' AND ('{$mcversion}' LIKE mcversion || '%' OR mcversion LIKE '{$mcversion}' || '%')");
} elseif (!empty($loadertype)) {
$modq = $db->query("SELECT * FROM mods WHERE loadertype='{$loadertype}'");
$modq = $db->query("SELECT * FROM mods WHERE type='mod' AND loadertype='{$loadertype}'");
} elseif (!empty($mcversion)) {
$modq = $db->query("SELECT * FROM mods WHERE '{$mcversion}' LIKE mcversion || '%'");
$modq = $db->query("SELECT * FROM mods WHERE type='mod' AND '{$mcversion}' LIKE mcversion || '%'");
} else {
$modq = $db->query("SELECT * FROM mods");
$modq = $db->query("SELECT * FROM mods WHERE type='mod'");
}
if ($modq) {
foreach ($modq as $mod) {
$filesize = isset($mod['filesize']) ? $mod['filesize'] : 0;
$modentry = [
'id'=>$mod['id'],
'pretty_name'=>$mod['pretty_name'],
'name'=>$mod['name'],
'version'=>$mod['version'],
'mcversion'=>$mod['mcversion'],
'md5'=>$mod['md5'],
'url'=>$mod['url'],
'filesize'=>$filesize
];

array_push($modslist, $modentry);
}
}
echo json_encode($modslist, JSON_UNESCAPED_SLASHES);
}
else if (preg_match("/api\/loader$/", $url)) {
$modslist=[];
if (!empty($_GET['loadertype']) && ctype_alnum($_GET['loadertype'])) {
$loadertype=$_GET['loadertype'];
}
if (!empty($_GET['mcversion']) && preg_match('/^[a-zA-Z0-9\-\.]+$/', $_GET['mcversion'])) {
$mcversion=$_GET['mcversion'];
}
if (!empty($loadertype) && !empty($mcversion)) {
$modq = $db->query("SELECT * FROM mods WHERE type='forge' AND loadertype='{$loadertype}' AND ('{$mcversion}' LIKE mcversion || '%' OR mcversion LIKE '{$mcversion}' || '%')");
} elseif (!empty($loadertype)) {
$modq = $db->query("SELECT * FROM mods WHERE type='forge' AND loadertype='{$loadertype}'");
} elseif (!empty($mcversion)) {
$modq = $db->query("SELECT * FROM mods WHERE type='forge' AND '{$mcversion}' LIKE mcversion || '%'");
} else {
$modq = $db->query("SELECT * FROM mods WHERE type='forge'");
}
if ($modq) {
foreach ($modq as $mod) {
if ($mod['type']!='mod') continue;
$filesize = isset($mod['filesize']) ? $mod['filesize'] : 0;
$modentry = [
'id'=>$mod['id'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
exit();
}

global $db;
require_once("db.php");
$db=new Db;
if (!isset($db)){
$db=new Db;
$db->connect();
}

if (file_put_contents("../forges/modpack-".$version."/version.json", file_get_contents("https://meta.fabricmc.net/v2/versions/loader/".$mcversion."/".urlencode($version)."/profile/json"))) {
$zip = new ZipArchive();
Expand All @@ -36,21 +40,34 @@
unlink("../forges/modpack-".$version."/version.json");
rmdir("../forges/modpack-".$version);
$md5 = md5_file("../forges/fabric-".$version.".zip");
$file_size=filesize("../forges/fabric-".$version.".zip");
$url = "http://".$config['host'].$config['dir']."forges/fabric-".urlencode($version).".zip";

$db->connect();
$res = $db->execute("INSERT INTO `mods` (`name`,`pretty_name`,`md5`,`url`,`link`,`author`,`description`,`version`,`mcversion`,`filename`,`type`,`loadertype`) VALUES ('fabric','Fabric (alpha)','".$md5."','".$url."','https://fabricmc.net/','FabricMC Team', 'Fabric is a lightweight, experimental modding toolchain for Minecraft.', '".$version."','".$mcversion."','fabric-".$version.".zip','forge', 'fabric')");
$db->disconnect();

$res = $db->execute("INSERT INTO `mods` (`name`,`pretty_name`,`md5`,`url`,`link`,`author`,`description`,`version`,`mcversion`,`filename`,`filesize`,`type`,`loadertype`) VALUES (
'fabric',
'Fabric (alpha)',
'{$md5}',
'{$url}',
'https://fabricmc.net/',
'FabricMC Team',
'Fabric is a lightweight, experimental modding toolchain for Minecraft.',
'{$version}',
'{$mcversion}',
'fabric-{$version}.zip',
'{$file_size}',
'forge',
'fabric'
)");
if ($res) {
echo '{"status":"succ","message":"Mod has been saved."}';
echo '{"status":"succ","message":"Loader has been saved.", "id": '.$db->insert_id().'}';
} else {
echo '{"status":"error","message":"Mod could not be added to database"}';
echo '{"status":"error","message":"Loader could not be added to database"}';
}

} else {
echo '{"status":"error","message":"File download failed."}';
unlink("../forges/modpack-".$version."/version.json");
rmdir("../forges/modpack-".$version);
}

$db->disconnect();
exit();
21 changes: 9 additions & 12 deletions functions/add-modloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

if (file_put_contents("../forges/modpack-".$version."/modpack.jar", file_get_contents($download_link))) {
$zip = new ZipArchive();
if ($zip->open("../forges/forge-".$version.".zip", ZIPARCHIVE::CREATE) !== true) {
if ($zip->open("../forges/".$type."-".$version.".zip", ZIPARCHIVE::CREATE) !== true) {
echo '{"status":"error","message":"Could not open archive"}';
exit();
}
Expand All @@ -60,10 +60,9 @@
$zip->close();
unlink("../forges/modpack-".$version."/modpack.jar");
rmdir("../forges/modpack-".$version);
$md5 = md5_file("../forges/forge-".$version.".zip");
$filesize=filesize("../forges/forge-".$version.".zip");
$url = "http://".$config['host'].$config['dir']."forges/forge-".$version.".zip";

$md5 = md5_file("../forges/".$type."-".$version.".zip");
$file_size = filesize("../forges/".$type."-".$version.".zip");
$url = "http://".$config['host'].$config['dir']."forges/".$type."-".$version.".zip";
$res = $db->execute("
INSERT INTO `mods` (`name`,`pretty_name`,`md5`,`url`,`link`,`author`,`description`,`version`,`mcversion`,`filename`,`filesize`,`type`,`loadertype`)
VALUES (
Expand All @@ -76,23 +75,21 @@
'".$type_descriptions[$type]."',
'".$version."',
'".$mcversion."',
'forge-".$version.".zip',
".$filesize.",
'".$type."-".$version.".zip',
".$file_size.",
'forge',
'".$type."'
)
");
$id=$db->insert_id();

if ($res) {
echo '{"status":"succ","message":"Mod has been saved.", "id":"'.$id.'"}';
echo '{"status":"succ","message":"Loader has been saved.", "id":"'.$db->insert_id().'"}';
} else {
echo '{"status":"error","message":"Mod could not be added to database"}';
echo '{"status":"error","message":"Loader could not be added to database"}';
}
} else {
echo '{"status":"error","message":"File download failed."}';
unlink("../forges/modpack-".$version."/modpack.jar");
rmdir("../forges/modpack-".$version);
}

$db->disconnect();
exit();
Loading

0 comments on commit 66d76c8

Please sign in to comment.