Skip to content

Commit

Permalink
bug fixes for /build, /admin, and /api/mod
Browse files Browse the repository at this point in the history
index.php/build: properly showall versions, hide versions if already in build.
api/index.php: provide query params mcversion,loadertype
index.php/admin and page_admin.js fix set serverwide
  • Loading branch information
cowpod committed Nov 15, 2024
1 parent f3c17d3 commit 81c1400
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 48 deletions.
18 changes: 16 additions & 2 deletions api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,23 @@
}
}
}
if (preg_match("/api\/mod$/", $url)) {
if (preg_match("/api\/mod$/", $url)) {
$modslist=[];
$modq = $db->query("SELECT * FROM mods");
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 loadertype='{$loadertype}' AND ('{$mcversion}' LIKE mcversion || '%' OR mcversion LIKE '{$mcversion}' || '%')");
} elseif (!empty($loadertype)) {
$modq = $db->query("SELECT * FROM mods WHERE loadertype='{$loadertype}'");
} elseif (!empty($mcversion)) {
$modq = $db->query("SELECT * FROM mods WHERE '{$mcversion}' LIKE mcversion || '%'");
} else {
$modq = $db->query("SELECT * FROM mods");
}
if ($modq) {
foreach ($modq as $mod) {
if ($mod['type']!='mod') continue;
Expand Down
39 changes: 20 additions & 19 deletions functions/change-version.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<?php
session_start();

if (empty($_GET['id'])) {
die("New mod not specified.");
if (empty($_GET['id_new'])) {
die('{"status":"error","message":"New mod not specified."}');
}
if (empty($_GET['mod'])) {
die("Old mod not specified.");
if (empty($_GET['id_old'])) {
die('{"status":"error","message":"Old mod(s) not specified."}');
}
if (empty($_GET['bid'])) {
die("Build not specified.");
die('{"status":"error","message":"Build not specified."}');
}
if (!$_SESSION['user']||$_SESSION['user']=="") {
die("Unauthorized request or login session has expired!");
die('{"status":"error","message":"Unauthorized request or login session has expired!"}');
}
if (substr($_SESSION['perms'], 1, 1)!=="1") {
echo 'Insufficient permission!';
exit();
die('{"status":"error","message":"Insufficient permission!"}');
}

global $db;
Expand All @@ -25,17 +24,19 @@
$db->connect();
}

$modsq = $db->query("SELECT `mods` FROM `builds` WHERE `id` = ".$db->sanitize($_GET['bid'])
);
if ($modsq) {
assert(sizeof($modsq)==1);
$mods = $modsq[0];
}
$modslist = explode(',', $mods['mods']);
$nmodlist = array_diff($modslist, [$_GET['mod']]);
array_push($nmodlist, $_GET['id']);
$modslist = implode(',', $nmodlist);
if (!is_numeric($_GET['bid'])||!is_numeric($_GET['id_new'])||!is_numeric($_GET['id_old'])) {
die('{"status":"error","Bad id"}');
}

$modsq = $db->query("SELECT mods FROM builds WHERE id = ".$db->sanitize($_GET['bid']));
if ($modsq && sizeof($modsq)==1 && !empty($modsq[0]['mods'])) {
$modslist = explode(',', $modsq[0]['mods']);
unset($modslist[array_search($id_old, $modslist)]);
array_push($id_new);
} else {
die('{"status":"error","message":"Build contains no mods. You need to set the version and loader."}');
}

$db->execute("UPDATE `builds` SET `mods` = '".$modslist."' WHERE `id` = ".$db->sanitize($_GET['bid']));

exit();
die('{"status":"succ","message":"Version changed sucessfully"')
2 changes: 1 addition & 1 deletion functions/save_api_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

$config = require("config.php");

if ($_SESSION['privileged'] && isset($_GET['serverwide']) && $_GET['serverwide']==1) {
if ($_SESSION['privileged'] && isset($_POST['serverwide']) && $_POST['serverwide']==1) {
$config['api_key'] = $api_key;
file_put_contents('./config.php', '<?php return '.var_export($config, true).' ?>');
die('{"status":"succ", "message":"successfuly set server-wide api_key"}');
Expand Down
6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1409,13 +1409,13 @@ function uri($uri) {

<?php if (substr($_SESSION['perms'],1,1)=="1") { ?>
<div class="card">
<h2>Mods <font id='mods-for-version-string' style='display:none'></font></h2>
<h2>Mods <font id='mods-for-version-string'></font></h2>
<hr>
<input id="search" type="text" placeholder="Search..." class="form-control">
<br />
<div class="custom-control custom-checkbox">
<input type="checkbox" name="showall" class="custom-control-input" id="showall">
<label class="custom-control-label" for="showall">Show all</label>
<label class="custom-control-label" for="showall">All versions</label>
</div>
<br />
<table id="modstable" class="table table-striped sortable">
Expand Down Expand Up @@ -1470,7 +1470,7 @@ function uri($uri) {
<?php }
} else echo "<div class='card'><h3 class='text-info'>Select minecraft version and save before editing mods.</h3></div>"; ?>
<script>
var modslist_0 = '<?php echo sizeof($modslist)>0 ? $modslist[0] : "" ?>';
var modslist_0 = JSON.parse('<?php echo json_encode($modslist, JSON_UNESCAPED_SLASHES) ?>');
var build_id = '<?php echo $user['id'] ?>';
var mcv="<?php echo $user['minecraft'] ?>";
var type="<?php echo $user['loadertype'] ?>";
Expand Down
36 changes: 24 additions & 12 deletions resources/js/global.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
function compareVersions(version1, version2) {
const v1 = version1.split('.').map(num => parseInt(num, 10));
const v2 = version2.split('.').map(num => parseInt(num, 10));
const v1 = version1.replace(/[\'\"\[\(\]\)]/, '').split('.').map(num => parseInt(num, 10));
const v2 = version2.replace(/[\'\"\[\(\]\)]/, '').split('.').map(num => parseInt(num, 10));

// Compare each part
const length = Math.max(v1.length, v2.length);
for (let i = 0; i < length; i++) {
const val1 = v1[i] || 0; // If version 1 has fewer parts, treat as 0
const val2 = v2[i] || 0; // If version 2 has fewer parts, treat as 0
// Compare each part
const length = Math.max(v1.length, v2.length);
for (let i = 0; i < length; i++) {
// if one has a trailing zero and the other doesn't have anything
if (!v1[i] && v2[i]==0 && i==v2.length) {
return 0;
}
if (!v2[i] && v1[i]==0 && i==v1.length) {
return 0;
}
const val1 = v1[i] || 0; // If version 1 has fewer parts, treat as 0
const val2 = v2[i] || 0; // If version 2 has fewer parts, treat as 0

if (val1 < val2) return -1;
if (val1 > val2) return 1;
}
if (val1 < val2) return -1;
if (val1 > val2) return 1;
}

return 0; // Versions are equal
return 0; // Versions are equal
}

function isVersionInInterval(version, interval) {
// console.log(version,interval)
// Remove whitespace
interval = interval.replace(/\s+/g, '');

if (!interval.includes(',')) {
// /^\[?[^,]+\]?$/
let comp=compareVersions(version, interval.replace(/[\[\]\(\)]/,''))
return comp
}

// Check interval boundaries for inclusivity/exclusivity
const startInclusive = interval[0] === '[';
const endInclusive = interval[interval.length - 1] === ']';
Expand Down
1 change: 1 addition & 0 deletions resources/js/page_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ $("#save_api_key").on("click", function() {
let formData = new FormData();
let request = new XMLHttpRequest();
formData.set('api_key', $("#api_key").val());
formData.append('serverwide',1);
request.open('POST', './functions/save_api_key.php');
request.onreadystatechange = function() {
if (request.readyState == 4) {
Expand Down
41 changes: 30 additions & 11 deletions resources/js/page_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ $('#versions').change(function(){
$('#editBuild').modal('show');
});
function fnone(){
$('#versions').val(modslist_0);
$('#versions').val(modslist_0[0]); // first item in list is modloader id
$('#forgec').val('none');
};
function fchange(){
Expand All @@ -20,28 +20,39 @@ function remove_mod(id) {
if (request.readyState == 4 && request.status == 200) {
if (request.responseText=='Mod removed') {
$("#mod-"+id).remove();
var index = modslist_0.indexOf(id);
if (index!==-1) {
modslist_0.splice(index, 1);
}
}
}
}
request.send();
}
function changeversion(id, mod, name, compatible) {
function changeversion(id_new, id_old, name, compatible) {
if (!compatible) {
$("#mod-"+name).removeClass("table-warning");
$("#warn-incompatible-"+name).hide();
/*$("#bmversions-"+name).children().each(function(){
if (this.value == mod) {
if (this.value == id_old) {
this.remove();
}
});*/
}
$("#bmversions-"+name).attr("onchange","changeversion(this.value,"+id+",'"+name+"',true)");
$("#bmversions-"+name).attr("onchange","changeversion(this.value,"+id_new+",'"+name+"',true)");
$("#spinner-"+name).show();
var request = new XMLHttpRequest();
request.open("GET", "./functions/change-version.php?bid="+build_id+"&id="+id+"&mod="+mod);
request.open("GET", "./functions/change-version.php?bid="+build_id+"&id_new="+id_new+"&id_old="+id_old);
request.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
$("#spinner-"+name).hide();

var index = modslist_0.indexOf(id_old);
if (index!==-1) {
modslist_0.splice(index, 1);
}

modslist_0.push(id_new);
}
}
request.send();
Expand Down Expand Up @@ -90,6 +101,7 @@ function add(name, id, v, mcv) {
</td>
</tr>
`);
modslist_0.push(id);
} else {
// $('#btn-add-mod-'+name).html('Add to build');
}
Expand Down Expand Up @@ -138,7 +150,7 @@ function parsemods(obj) {
let filter=$("#search").val();
for (let mod of obj) {
if (filter=='' || filter==undefined || mod['pretty_name'].toLowerCase().includes(filter)||mod['name'].toLowerCase().includes(filter)) {
if (mod['mcversion']=='' || mcv==mod['mcversion'] || isVersionInInterval(`'${mcv}'`, mod['mcversion']) || showall) {
if (mod['mcversion']=='' || mcv==mod['mcversion'] || isVersionInInterval(`'${mcv}'`, mod['mcversion']) || showall && !modslist_0.includes(mod['id'])) {
add_mod_row(mod['id'], mod['pretty_name'],mod['name'],vs[mod['name']],mcv);
added_num+=1;
}
Expand Down Expand Up @@ -173,7 +185,7 @@ async function getmods() {
console.log('could not get mods from api');
reject(false)
}
request.open("GET", 'api/mod');
request.open("GET", `api/mod?loadertype=${type}&mcversion=${mcv}`);
request.send();
}
});
Expand Down Expand Up @@ -201,16 +213,21 @@ $("#search2").on('keyup',function(){
}
}
});
$('#showall').change(function() {
function showall(){
if ($('#showall').is(':checked')) {
$('#mods-for-version-string').text('');
$('#mods-for-version-string').hide();
} else {
$('#mods-for-version-string').text(' for Minecraft '+mcv);
$('#mods-for-version-string').show();
} else {
$('#mods-for-version-string').hide();
}

set_cached('showall', $('#showall').is(':checked'), -1);
getmods(mcv, type);
}

$('#showall').change(function() {
showall()
});

$(document).on('change', '.form-control', function() {
Expand All @@ -231,6 +248,8 @@ $(document).on('change', '.form-control', function() {
});

$(document).ready(function() {
$('#showall').prop('checked', get_cached('showall'));
if (get_cached('showall') && $('#showall').prop('checked', get_cached('showall'))) {
showall()
}
getmods();
});

0 comments on commit 81c1400

Please sign in to comment.