« Back to History
page_usage_logger.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ========================================================================= File: /erp/modules/dev/page_usage_logger.php Purpose: Auto page usage logging (company-scoped), feature-flag gated Scope : Page-local; no global setting changes. ========================================================================= */ if (!defined('FLAG_PAGE_USAGE_LOG') || FLAG_PAGE_USAGE_LOG !== true) { return; // feature OFF → do nothing } /* ---- Safe helpers ---- */ if (!function_exists('mm_json')) { function mm_json($v) { return json_encode($v, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); } } /* ---- Resolve PDO (non-intrusive) ---- */ $__pdo = $GLOBALS['pdo'] ?? null; if (!($__pdo instanceof PDO)) { $dbfile = __DIR__ . '/../../core/db.php'; if (is_file($dbfile)) { @require_once $dbfile; } $__pdo = $GLOBALS['pdo'] ?? ($__pdo instanceof PDO ? $__pdo : null); } if (!($__pdo instanceof PDO)) { return; } // give up silently /* ---- Resolve session user / company (if auth present) ---- */ $company_id = 0; $user_id = 0; try { if (session_status() === PHP_SESSION_NONE) @session_start(); if (isset($_SESSION['user']) && is_array($_SESSION['user'])) { $company_id = (int)($_SESSION['user']['company_id'] ?? 0); $user_id = (int)($_SESSION['user']['id'] ?? 0); } } catch (\Throwable $e) {} /* ---- Capture environment ---- */ $method = $_SERVER['REQUEST_METHOD'] ?? ''; $uri = $_SERVER['REQUEST_URI'] ?? ''; $script = $_SERVER['SCRIPT_FILENAME'] ?? ($_SERVER['SCRIPT_NAME'] ?? ''); $page = str_replace($_SERVER['DOCUMENT_ROOT'] ?? '', '', $script); $ip = $_SERVER['REMOTE_ADDR'] ?? ''; $ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; /* ---- Included files + declared functions delta ---- */ $included = get_included_files(); sort($included); $decl_before = $GLOBALS['__DECLARED_FUNCS_BEFORE__'] ?? null; // if set by bootstrap $decl_all = get_defined_functions(); $decl_user = $decl_all['user'] ?? []; sort($decl_user); if (is_array($decl_before)) { // keep only new ones after bootstrap $decl_user = array_values(array_diff($decl_user, $decl_before)); } /* ---- Best-effort: SQL table name sniffing via PDO proxy (optional hook) If pdo_logger ran earlier and populated $_MM_SQL_SNIFF_TABLES, use it. -------------------------------------------------------------------------- */ $used_tables = []; if (!empty($GLOBALS['_MM_SQL_SNIFF_TABLES']) && is_array($GLOBALS['_MM_SQL_SNIFF_TABLES'])) { $used_tables = array_values(array_unique($GLOBALS['_MM_SQL_SNIFF_TABLES'])); } /* ---- Any runtime settings worth logging ---- */ $settings = [ 'flag_page_usage_log' => true, ]; /* ---- Insert ---- */ try { $sql = "INSERT INTO page_usage_log (company_id, user_id, method, uri, script, page_name, included_files, declared_functions, used_tables, settings, ip, ua) VALUES (:company_id, :user_id, :method, :uri, :script, :page_name, :included_files, :declared_functions, :used_tables, :settings, :ip, :ua)"; $st = $__pdo->prepare($sql); $st->execute([ ':company_id' => $company_id, ':user_id' => $user_id, ':method' => (string)$method, ':uri' => (string)$uri, ':script' => (string)$script, ':page_name' => (string)$page, ':included_files' => mm_json($included), ':declared_functions' => mm_json($decl_user), ':used_tables' => mm_json($used_tables), ':settings' => mm_json($settings), ':ip' => (string)$ip, ':ua' => (string)$ua, ]); } catch (\Throwable $e) { // swallow errors; never break a page due to logging }