Merge pull request #118 from GameServerPanel/copilot/update-game-monitor-expiration

This commit is contained in:
Frank Harris 2026-05-05 05:19:30 -05:00 committed by GitHub
commit 7016b79805
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 74 additions and 3 deletions

View file

@ -486,4 +486,74 @@ function get_monitor_buttons($server_home, $server_xml)
}
return $buttons_html;
}
/**
* Returns an HTML-formatted expiration label for the given server home_id.
*
* Source of truth: billing_orders.end_date (DATETIME, NULL means no date set).
* The most recent billing order for the home is used (ORDER BY end_date DESC LIMIT 1).
*
* Color thresholds:
* green more than 10 days remaining (shows actual date)
* yellow 410 days remaining (shows "X days remaining")
* red 13 days remaining (shows "X days remaining")
* red less than 1 day but not yet expired (shows "Less than 1 day remaining")
* red already expired/suspended (shows "Suspended")
* red no order or NULL/invalid end_date (shows "No expiration date found")
*
* @param int $home_id The server home ID.
* @return string Safe HTML string ready for inline display.
*/
function get_server_billing_expiration_html(int $home_id): string
{
global $db;
// Query billing_orders for the most recent end_date on an active order for this home.
// Statuses 'Active' and 'Invoiced' represent live subscriptions (invoiced = awaiting renewal).
// OGP_DB_PREFIX is replaced at runtime by the panel's DB wrapper.
$rows = $db->resultQuery(
"SELECT end_date FROM OGP_DB_PREFIXbilling_orders
WHERE home_id = " . intval($home_id) . "
AND status IN ('Active','Invoiced')
ORDER BY end_date DESC
LIMIT 1"
);
if (empty($rows) || empty($rows[0]['end_date'])) {
return "<span style='color:red;'>No expiration date found</span>";
}
// Parse end_date using DateTime for PHP 8.3+ compatibility.
try {
$end_dt = new DateTime($rows[0]['end_date']);
} catch (\Exception $e) {
return "<span style='color:red;'>No expiration date found</span>";
}
$now = new DateTime();
$diff = $now->diff($end_dt);
if ($end_dt <= $now) {
// Server billing period has already passed — treat as suspended.
return "<span style='color:red;'>Suspended</span>";
}
// $diff->days is the total number of whole days between $now and $end_dt.
$days_remaining = (int)$diff->days;
$display_date = htmlentities($end_dt->format('Y-m-d H:i'), ENT_QUOTES, 'UTF-8');
if ($days_remaining > 10) {
// More than 10 days: show the actual expiration date in green.
return "<span style='color:green;'>" . $display_date . "</span>";
} elseif ($days_remaining >= 4) {
// 410 days: yellow warning (darkgoldenrod for WCAG contrast).
return "<span style='color:#B8860B;'>" . htmlentities($days_remaining, ENT_QUOTES, 'UTF-8') . " days remaining</span>";
} elseif ($days_remaining >= 1) {
// 13 days: urgent red warning.
return "<span style='color:red;'>" . htmlentities($days_remaining, ENT_QUOTES, 'UTF-8') . " days remaining</span>";
} else {
// Less than one full day but not yet expired.
return "<span style='color:red;'>Less than 1 day remaining</span>";
}
}
?>

View file

@ -351,9 +351,10 @@ echo "<table id='servermonitor' class='tablesorter' data-sortlist='[[0,0],[3,1]]
//set the display of how long server has until expired
//default is it never expires
$expiration_dates = "This Server Will NEVER Expire";
// Look up the billing expiration for this server from billing_orders.end_date.
// See get_server_billing_expiration_html() in home_handling_functions.php for
// color thresholds: green >10 days, yellow 4-10 days, red 1-3 days or expired.
$expiration_dates = get_server_billing_expiration_html((int)$server_home['home_id']);
if( !isset($server_home['mod_id']) )