Fix tableprefix and cart mysql

This commit is contained in:
Frank Harris 2025-11-10 07:23:01 -05:00
parent 723dd58e71
commit ba6b8d9e6b
19 changed files with 161 additions and 39 deletions

5
.cpanel.yml Normal file
View file

@ -0,0 +1,5 @@
---
deployment:
tasks:
- export DEPLOYPATH=/home/domainpl/gameservers.world/
- /bin/cp -a * $DEPLOYPATH

View file

@ -1,7 +1,7 @@
<?php
// _website/add_to_cart.php
// Handle Add to Cart posts from order.php
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
require_once(__DIR__ . '/includes/login_required.php');
require_once(__DIR__ . '/includes/log.php');

View file

@ -1,7 +1,7 @@
<?php
// Admin invoices viewer and editor
$session_name = session_name(); session_start();
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
require_once(__DIR__ . '/includes/admin_auth.php');
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

View file

@ -11,8 +11,8 @@
/* === SITE_BASE_URL is loaded from includes/config.inc.php; leave empty to use relative paths === */
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
// Include billing bootstrap (loads config and DB helper)
require_once(__DIR__ . '/bootstrap.php');
// Protect this page: require admin
require_once(__DIR__ . '/includes/admin_auth.php');
@ -331,8 +331,8 @@ document.querySelectorAll('.locs-box').forEach(function(box){
</script>
<?php
// Close database connection
mysqli_close($db);
// Close database connection safely
billing_maybe_close_db($db);
?>
</body>
</html>

View file

@ -0,0 +1,114 @@
<?php
// modules/billing/bootstrap.php
// Central bootstrap for billing website pages. Loads config, provides safe DB helper
// and ensures $table_prefix is available.
// Load configuration (includes/config.inc.php) if present
$config_path = __DIR__ . '/includes/config.inc.php';
if (file_exists($config_path)) {
require_once $config_path;
} else {
trigger_error('Billing config not found: ' . $config_path, E_USER_WARNING);
}
// Ensure $table_prefix exists (fallback to empty string)
if (!isset($table_prefix)) {
$table_prefix = '';
}
// Billing DB connection cached in $billing_db
if (!isset($billing_db)) {
$billing_db = null;
}
// Track whether bootstrap opened the connection (so callers can safely close it)
$billing_db_opened_by_bootstrap = false;
/**
* Get a mysqli connection for billing pages.
* - Reuses global $db if already created by other code.
* - Tries to open a new connection using config variables if needed.
* - Returns null on failure.
*/
function billing_get_db()
{
global $billing_db, $db, $db_host, $db_user, $db_pass, $db_name, $billing_db_opened_by_bootstrap;
if (!empty($billing_db) && ($billing_db instanceof mysqli)) {
return $billing_db;
}
if (!empty($db) && ($db instanceof mysqli)) {
$billing_db = $db;
return $billing_db;
}
// Try to connect (suppress warnings; caller may check return value)
$conn = @mysqli_connect($db_host ?? null, $db_user ?? null, $db_pass ?? null, $db_name ?? null);
if ($conn) {
// Set charset when available
if (function_exists('mysqli_set_charset')) {
@mysqli_set_charset($conn, 'utf8mb4');
}
$billing_db = $conn;
$billing_db_opened_by_bootstrap = true;
return $billing_db;
}
// Leave $billing_db as null
$billing_db = null;
return null;
}
/**
* Close DB connection only if it was opened by bootstrap. If the connection
* is shared (created by other code) this function will not close it.
*/
function billing_maybe_close_db($conn)
{
global $billing_db, $billing_db_opened_by_bootstrap;
if (!($conn instanceof mysqli)) return;
if (!empty($billing_db_opened_by_bootstrap) && $billing_db === $conn) {
@mysqli_close($conn);
$billing_db = null;
$billing_db_opened_by_bootstrap = false;
}
}
// Small helper wrappers commonly used across billing pages
if (!function_exists('esc_mysqli')) {
function esc_mysqli($db, $v)
{
if ($db instanceof mysqli) {
return $db->real_escape_string((string)$v);
}
return addslashes((string)$v);
}
}
if (!function_exists('fetch_all_assoc')) {
function fetch_all_assoc($db, $sql)
{
if (!($db instanceof mysqli)) return [];
$res = $db->query($sql);
return $res ? $res->fetch_all(MYSQLI_ASSOC) : [];
}
}
if (!function_exists('col_exists')) {
function col_exists($db, $table, $col)
{
if (!($db instanceof mysqli)) return false;
$t = $db->real_escape_string($table);
$c = $db->real_escape_string($col);
$res = $db->query("SHOW COLUMNS FROM `{$t}` LIKE '{$c}'");
return ($res && $res->num_rows > 0);
}
}
// expose a convenience variable for scripts that expect $db
// Do not overwrite an existing $db if present
if (!isset($db) || !($db instanceof mysqli)) {
$maybe = billing_get_db();
if ($maybe instanceof mysqli) {
$db = $maybe;
}
}
// End bootstrap

View file

@ -40,7 +40,7 @@ if (session_status() === PHP_SESSION_NONE) {
}
// Load configuration
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
// Check if user is logged in
$user_id = 0;
@ -231,8 +231,7 @@ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https:
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$siteBase = $protocol . $host;
// Close database connection
mysqli_close($db);
// (Do not close the shared DB connection here; menu and other includes may use it.)
?>
<!DOCTYPE html>
<html lang="en">

View file

@ -3,7 +3,7 @@
* Check {table_prefix}billing_invoices table structure
*/
require_once('../../includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
require_once('../../includes/database_mysqli.php');
$db = createDatabaseConnection($db_host, $db_user, $db_pass, $db_name, $db_port);
@ -72,5 +72,5 @@ if (mysqli_num_rows($last_result) > 0) {
echo "<p>No invoices found.</p>\n";
}
mysqli_close($db);
billing_maybe_close_db($db);
?>

View file

@ -4,7 +4,7 @@ session_name("gameservers_website");
session_start();
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
// Create database connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
@ -109,7 +109,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['request_reset'])) {
}
// Close database connection
mysqli_close($db);
billing_maybe_close_db($db);
?>
<!DOCTYPE html>
<html lang="en">

View file

@ -50,7 +50,11 @@ if ($is_logged_in) {
}
}
if ($menu_db_opened) {
mysqli_close($menu_db);
if (function_exists('billing_maybe_close_db')) {
billing_maybe_close_db($menu_db);
} else {
@mysqli_close($menu_db);
}
}
}
}

View file

@ -9,8 +9,8 @@ error_reporting(E_ALL);
// We'll compute a site root below (up to /_website) and define a strict sanitizer after config is loaded
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
// Include billing bootstrap (loads database configuration)
require_once(__DIR__ . '/bootstrap.php');
require_once(__DIR__ . '/includes/log.php');
// Determine site root up to /_website so we can enforce absolute redirects within this site
@ -97,8 +97,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['login'])) {
}
}
// Close database connection
mysqli_close($db);
// Keep DB connection open for includes (menu.php may query the DB). The
// connection lifecycle is handled centrally; avoid closing here.
?>
<!DOCTYPE html>
<html lang="en">

View file

@ -28,7 +28,7 @@ if (!$is_logged_in) {
}
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
// Create database connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
@ -379,7 +379,7 @@ $status_config = [
<?php
// Close database connection
mysqli_close($db);
billing_maybe_close_db($db);
?>
</body>

View file

@ -10,8 +10,8 @@
// Require login for this page
require_once(__DIR__ . '/includes/login_required.php');
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
// Include billing bootstrap (loads config and DB helper)
require_once(__DIR__ . '/bootstrap.php');
// Create database connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
@ -136,7 +136,7 @@ $result = mysqli_query($db, $query);
<?php
// Close database connection
mysqli_close($db);
billing_maybe_close_db($db);
?>
</body>

View file

@ -23,8 +23,8 @@ This method means we can use one code block in every game page and fill in the d
// Require login for ordering
require_once(__DIR__ . '/includes/login_required.php');
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
// Include billing bootstrap (loads config and DB helper)
require_once(__DIR__ . '/bootstrap.php');
// Create database connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
@ -308,7 +308,7 @@ if ($row['price_monthly'] == 0.0) {
</div>
<?php
// Close database connection
mysqli_close($db);
billing_maybe_close_db($db);
?>
</body>
<?php include(__DIR__ . '/includes/footer.php'); ?>

View file

@ -1,7 +1,7 @@
<?php
session_name("gameservers_website");
session_start();
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
// Simple registration form (creates a user in {table_prefix}users with MD5 password)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['username']) && !empty($_POST['password'])) {

View file

@ -7,7 +7,7 @@ if (session_status() === PHP_SESSION_NONE) {
session_name("gameservers_website");
session_start();
}
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
require_once(__DIR__ . '/includes/login_required.php');
require_once(__DIR__ . '/includes/log.php');
@ -264,7 +264,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['confirm_renewal'])) {
<?php
// Close database connection
mysqli_close($db);
billing_maybe_close_db($db);
?>
</body>

View file

@ -4,7 +4,7 @@ session_name("gameservers_website");
session_start();
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
// Create database connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
@ -105,7 +105,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['reset_password']) &&
}
// Close database connection
mysqli_close($db);
billing_maybe_close_db($db);
?>
<!DOCTYPE html>
<html lang="en">

View file

@ -7,8 +7,8 @@
</head>
<body>
<?php
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
// Include billing bootstrap (loads config and DB helper)
require_once(__DIR__ . '/bootstrap.php');
// Create database connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
@ -165,7 +165,7 @@ $result = mysqli_query($db, $query);
<?php
// Close database connection
mysqli_close($db);
billing_maybe_close_db($db);
?>
</body>

View file

@ -12,7 +12,7 @@ ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
require_once(__DIR__ . '/bootstrap.php');
// Create database connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
@ -38,7 +38,7 @@ $services = $db->query($qry_services);
if (!$services) {
echo "<meta http-equiv='refresh' content='1'>";
mysqli_close($db);
billing_maybe_close_db($db);
return;
}
@ -123,7 +123,7 @@ include(__DIR__ . '/includes/menu.php');
<?php
// Close database connection
mysqli_close($db);
billing_maybe_close_db($db);
?>
<?php include(__DIR__ . '/includes/footer.php'); ?>

View file

@ -10,8 +10,8 @@
* accessible in production.
*/
// Include database configuration
require_once(__DIR__ . '/includes/config.inc.php');
// Include billing bootstrap (loads config and DB helper)
require_once(__DIR__ . '/bootstrap.php');
// Create database connection
$db = mysqli_connect($db_host, $db_user, $db_pass, $db_name);