<?php if (!defined('DASHBOARD_ROOT')) { http_response_code(403); exit; } function safeScalar(callable $fn, $default = 0) { try { return $fn(); } catch (\Throwable $e) { return $default; } } function safeFetch(callable $fn, $default = []) { try { return $fn(); } catch (\Throwable $e) { return $default; } } $stores = safeFetch(fn() => Database::fetchAll( "SELECT sd.id, sd.domain, sd.nickname, sd.is_active, sd.accent_color, sd.color_master, sd.store_type, sd.maintenance_mode, sd.proxy_enabled, (SELECT COUNT(*) FROM categories WHERE store_id = sd.id AND status_code = 6001) as cat_count, (SELECT COUNT(*) FROM products WHERE store_id = sd.id AND status = 'active') as prod_count, (SELECT COUNT(*) FROM storefront_rows WHERE store_id = sd.id AND is_active = 1) as row_count, (SELECT COUNT(*) FROM orders WHERE listing_id IN (SELECT id FROM products WHERE store_id = sd.id) AND DATE(orders.created_at) = CURDATE()) as orders_today FROM stores sd ORDER BY sd.id ASC" )); $revenueToday = (float)safeScalar(fn() => Database::scalar("SELECT COALESCE(SUM(subtotal),0) FROM orders WHERE DATE(created_at) = CURDATE() AND status_code NOT IN (2098,2099)")); $revenueWeek = (float)safeScalar(fn() => Database::scalar("SELECT COALESCE(SUM(subtotal),0) FROM orders WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND status_code NOT IN (2098,2099)")); $ordersToday = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE DATE(created_at) = CURDATE()")); $ordersWeek = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)")); $pendingOrders = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE status_code = 2001")); $paidOrders = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE status_code = 2002")); $totalProducts = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM products WHERE status = 'active'")); $totalCats = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM categories WHERE status_code = 6001")); $draftProducts = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM products WHERE status = 'draft'")); $totalUsers = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM users")); $usersToday = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM users WHERE DATE(created_at) = CURDATE()")); $openTickets = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM support_messages WHERE is_read = 0 AND is_admin = 0")); $reviewsPending= (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM customer_reviews WHERE is_approved = 0")); $fleetNodes = safeFetch(fn() => Database::fetchAll("SELECT node_id, node_role, hostname, status, last_heartbeat FROM nodes ORDER BY id ASC")); $fleetOnline = count(array_filter($fleetNodes, fn($n) => $n['status'] === 'online')); $fleetTotal = count($fleetNodes); $secToday = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM events WHERE DATE(created_at) = CURDATE()")); $blockedToday= (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM blacklist WHERE DATE(created_at) = CURDATE()")); $hpToday = (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM honeypot_hits WHERE DATE(created_at) = CURDATE()")); $recentEvents= safeFetch(fn() => Database::fetchAll("SELECT action, ip_address, created_at FROM events ORDER BY created_at DESC LIMIT 8")); $pipeline = [ 'Pending' => (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE status_code = 2001")), 'Paid' => (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE status_code = 2002")), 'Packaged' => (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE status_code = 2004")), 'Shipped' => (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE status_code = 2007")), 'Delivered'=> (int)safeScalar(fn() => Database::scalar("SELECT COUNT(*) FROM orders WHERE status_code = 2010")), ]; $pipeMax = max(max($pipeline), 1); $engineRunning = false; $engineUptime = null; $engineBlacklist = 0; $engineDdos = false; $engineWatchdog = 0; $engineDnsMode = 'normal'; try { if (!defined('C1O2N3S4T5A6N7T8')) define('C1O2N3S4T5A6N7T8', true); if (file_exists(PRIVATE_DIR . '/p1a2y3m4_bridge.php')) { require_once PRIVATE_DIR . '/p1a2y3m4_bridge.php'; $eData = p1a2y3m4_query('ping', []); $engineRunning = !empty($eData["ok"]); $nRow = Database::fetchOne("SELECT last_heartbeat, metrics, engine_version FROM nodes WHERE node_id = '669427' LIMIT 1"); $nMet = $nRow ? json_decode($nRow['metrics'] ?? '{}', true) : []; $engineUptime = $nMet['uptime'] ?? null; $engineBlacklist = (int)($nMet['blacklist_count'] ?? 0); $engineDdos = !empty($nMet['ddos_active']); $engineWatchdog = (int)($nMet['watchdog_files'] ?? 0); $engineDnsMode = $nMet['dns_failover_mode'] ?? 'normal'; } } catch (\Throwable $e) {} $engineStatus = $engineRunning ? 'running' : 'offline'; 