Skip to content

Commit

Permalink
Merge pull request #114 from MythicalLTD/107-option-to-set-a-limit-fo…
Browse files Browse the repository at this point in the history
…r-maximum-resources-that-a-user-can-have

Option to set a limit for maximum resources that a user can have.
  • Loading branch information
NaysKutzu authored Feb 23, 2024
2 parents 0dbcd7d + 0154d02 commit d157486
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 145 deletions.
2 changes: 1 addition & 1 deletion migrate/61.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UPDATE `mythicaldash_settings` SET `version` = '3.1.4' WHERE `mythicaldash_settings`.`id` = 1;
UPDATE `mythicaldash_settings` SET `version` = '3.1.4' WHERE `mythicaldash_settings`.`id` = 1;
1 change: 1 addition & 0 deletions migrate/62.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `mythicaldash_settings` ADD `max_ram` TEXT NOT NULL DEFAULT '65536' AFTER `def_backups`, ADD `max_cpu` TEXT NOT NULL DEFAULT '1000' AFTER `max_ram`, ADD `max_disk` TEXT NOT NULL DEFAULT '122880' AFTER `max_cpu`, ADD `max_servers` TEXT NOT NULL DEFAULT '15' AFTER `max_disk`, ADD `max_ports` TEXT NOT NULL DEFAULT '15' AFTER `max_servers`, ADD `max_backups` TEXT NOT NULL DEFAULT '15' AFTER `max_ports`, ADD `max_allocations` TEXT NOT NULL DEFAULT '15' AFTER `max_backups`;
1 change: 1 addition & 0 deletions migrate/63.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `mythicaldash_settings` SET `version` = '3.2.0' WHERE `mythicaldash_settings`.`id` = 1;
1 change: 1 addition & 0 deletions migrate/64.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `mythicaldash_settings` CHANGE `max_ports` `max_dbs` TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '\'15\'';
292 changes: 166 additions & 126 deletions view/admin/settings/main.php

Large diffs are not rendered by default.

45 changes: 28 additions & 17 deletions view/admin/settings/resources.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use MythicalDash\ErrorHandler;

include (__DIR__ . '/../../requirements/page.php');
include (__DIR__ . '/../../requirements/admin.php');

try {
if (isset($_GET['update_settings'])) {
$r_coins = mysqli_real_escape_string($conn, $_GET['resources:coins']);
Expand All @@ -12,28 +14,37 @@
$r_ports = mysqli_real_escape_string($conn, $_GET['resources:ports']);
$r_databases = mysqli_real_escape_string($conn, $_GET['resources:databases']);
$r_backups = mysqli_real_escape_string($conn, $_GET['resources:backups']);

$def_max_ram = mysqli_real_escape_string($conn, $_GET['resources:maxram']);
$def_max_disk = mysqli_real_escape_string($conn, $_GET['resources:maxdisk']);
$def_max_cpu = mysqli_real_escape_string($conn, $_GET['resources:maxcpu']);
$def_max_svlimit = mysqli_real_escape_string($conn, $_GET['resources:maxsvlimit']);
$def_max_ports = mysqli_real_escape_string($conn, $_GET['resources:maxports']);
$def_max_databases = mysqli_real_escape_string($conn, $_GET['resources:maxdatabases']);
$def_max_backups = mysqli_real_escape_string($conn, $_GET['resources:maxbackups']);

$afk_coins_per_m = mysqli_real_escape_string($conn, $_GET['afk:coins:per:min']);
$eafk = mysqli_real_escape_string($conn, $_GET['resources:eafk']);
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_coins` = '" . $r_coins . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_memory` = '" . $r_ram . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_disk_space` = '" . $r_disk . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_cpu` = '" . $r_cpu . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_server_limit` = '" . $r_svlimit . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_port` = '" . $r_ports . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_db` = '" . $r_databases . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_backups` = '" . $r_backups . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `afk_coins_per_min` = '" . $afk_coins_per_m . "' WHERE `mythicaldash_settings`.`id` = 1;");
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `enable_afk` = '" . $eafk . "' WHERE `mythicaldash_settings`.`id` = 1;");
header('location: /admin/settings?s=We updated the settings inside the database');

// Update default values
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `def_coins` = '$r_coins', `def_memory` = '$r_ram', `def_disk_space` = '$r_disk', `def_cpu` = '$r_cpu', `def_server_limit` = '$r_svlimit', `def_port` = '$r_ports', `def_db` = '$r_databases', `def_backups` = '$r_backups' WHERE `id` = 1");
sleep(2);
// Update max values
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `max_ram` = '$def_max_ram', `max_disk` = '$def_max_disk', `max_cpu` = '$def_max_cpu', `max_servers` = '$def_max_svlimit', `max_allocations` = '$def_max_ports', `max_dbs` = '$def_max_databases', `max_backups` = '$def_max_backups' WHERE `id` = 1");

// Update other settings
mysqli_query($conn, "UPDATE `mythicaldash_settings` SET `afk_coins_per_min` = '$afk_coins_per_m', `enable_afk` = '$eafk' WHERE `id` = 1");

header('Location: /admin/settings?s=Settings updated successfully');
$conn->close();
die ();
die();
} else {
header('location: /admin/settings?e=Failed to update the settings inside the database');
header('Location: /admin/settings?e=Failed to update settings');
die();
}
} catch (Exception $ex) {
ErrorHandler::Critical("Failed to update settings ",$ex);
header('location: /admin/settings?e=Failed to update the settings inside the database');
die ();
ErrorHandler::Critical("Failed to update settings ", $ex);
header('Location: /admin/settings?e=Failed to update settings');
die();
}
?>
?>
43 changes: 43 additions & 0 deletions view/requirements/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,47 @@
ErrorHandler::ShowCritical($lang['pterodactyl_connection_error']);
die();
}
try {
if (
$session->getUserInfo('ram') > SettingsManager::getSetting('max_ram') ||
$session->getUserInfo('cpu') > SettingsManager::getSetting('max_cpu') ||
$session->getUserInfo('disk') > SettingsManager::getSetting('max_disk') ||
$session->getUserInfo('server_limit') > SettingsManager::getSetting('max_servers') ||
$session->getUserInfo('databases') > SettingsManager::getSetting('max_dbs') ||
$session->getUserInfo('backups') > SettingsManager::getSetting('max_backups') ||
$session->getUserInfo('ports') > SettingsManager::getSetting('max_allocations')
) {
// Determine which setting caused the condition to trigger
$reason = "";
if ($session->getUserInfo('ram') > SettingsManager::getSetting('max_ram')) {
$reason = "RAM";
$conn->query("UPDATE `mythicaldash_users` SET `ram` = '".SettingsManager::getSetting('max_ram') - 1 ."' WHERE `mythicaldash_users`.`api_key` = '".mysqli_real_escape_string($conn, $_COOKIE['token'])."';");
} elseif ($session->getUserInfo('cpu') > SettingsManager::getSetting('max_cpu')) {
$reason = "CPU";
$conn->query("UPDATE `mythicaldash_users` SET `cpu` = '". SettingsManager::getSetting('max_cpu') - 1 ."' WHERE `mythicaldash_users`.`api_key` = '".mysqli_real_escape_string($conn, $_COOKIE['token'])."';");
} elseif ($session->getUserInfo('disk') > SettingsManager::getSetting('max_disk')) {
$reason = "Disk Space";
$conn->query("UPDATE `mythicaldash_users` SET `disk` = '".SettingsManager::getSetting('max_disk') - 1 ."' WHERE `mythicaldash_users`.`api_key` = '".mysqli_real_escape_string($conn, $_COOKIE['token'])."';");
} elseif ($session->getUserInfo('server_limit') > SettingsManager::getSetting('max_servers')) {
$reason = "Server Limit";
$conn->query("UPDATE `mythicaldash_users` SET `server_limit` = '".SettingsManager::getSetting('max_servers') - 1 ."' WHERE `mythicaldash_users`.`api_key` = '".mysqli_real_escape_string($conn, $_COOKIE['token'])."';");
} elseif ($session->getUserInfo('databases') > SettingsManager::getSetting('max_dbs')) {
$reason = "Databases";
$conn->query("UPDATE `mythicaldash_users` SET `databases` = '".SettingsManager::getSetting('max_dbs')- 1 ."' WHERE `mythicaldash_users`.`api_key` = '".mysqli_real_escape_string($conn, $_COOKIE['token'])."';");
} elseif ($session->getUserInfo('backups') > SettingsManager::getSetting('max_backups')) {
$reason = "Backups";
$conn->query("UPDATE `mythicaldash_users` SET `backups` = '".SettingsManager::getSetting('max_backups')- 1 ."' WHERE `mythicaldash_users`.`api_key` = '".mysqli_real_escape_string($conn, $_COOKIE['token'])."';");
} elseif ($session->getUserInfo('ports') > SettingsManager::getSetting('max_allocations')) {
$reason = "Ports";
$conn->query("UPDATE `mythicaldash_users` SET `ports` = '".SettingsManager::getSetting('max_allocations') - 1 ."' WHERE `mythicaldash_users`.`api_key` = '".mysqli_real_escape_string($conn, $_COOKIE['token'])."';");
}
if (!empty($reason)) {
echo '<script>alert("It looks like we needed to update your resources because you exceeded the limit set for '.$reason.'.\nPlease contact your hosting provider if you wish for them to raise this limit!");</script>';
}
}
} catch (Exception $e) {
die($e);
}


?>
2 changes: 1 addition & 1 deletion view/user/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class="d-block w-px-100 h-px-100 rounded" id="uploadedAvatar" />
<div class="mb-3 col-md-6">
<label for="username" class="form-label"><?= $lang['username']?></label>
<input class="form-control" type="text" id="username"
name="username" value="<?= $session->getUserInfo("username") ?>"
name="username" id="name" value="<?= $session->getUserInfo("username") ?>"
placeholder="jhondoe" />
</div>
<div class="mb-3 col-md-6">
Expand Down

0 comments on commit d157486

Please sign in to comment.