« Back to History
page_catalog.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ========================================================= File: /erp/modules/dev/page_catalog.php Purpose: Minimal page meta register (catalog + optional log) How to use: require_once __DIR__ . '/modules/dev/page_catalog.php'; page_meta([...], ['log'=>true]); ========================================================= */ if (!function_exists('page_meta')) { function __pm_get_pdo() { // 1) already present? if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof PDO) return $GLOBALS['pdo']; // 2) try to include /erp/core/db.php $erpBase = dirname(__DIR__, 2); // /erp $dbphp = $erpBase . '/core/db.php'; if (is_file($dbphp)) { @require_once $dbphp; if (isset($GLOBALS['pdo']) && $GLOBALS['pdo'] instanceof PDO) return $GLOBALS['pdo']; } // 3) If mysqli exists in globals, try to build PDO from known constants/vars $cands = [ // common constants ['const' => ['DB_HOST','DB_NAME','DB_USER','DB_PASS']], // common variable names defined in db.php ['var' => ['db_host','db_name','db_user','db_pass']], ['var' => ['DBHOST','DBNAME','DBUSER','DBPASS']], ]; $host = $name = $user = $pass = null; foreach ($cands as $spec) { if (isset($spec['const'])) { [$H,$N,$U,$P] = $spec['const']; if (defined($H) && defined($N) && defined($U)) { $host = constant($H); $name = constant($N); $user = constant($U); $pass = defined($P)?constant($P):''; break; } } elseif (isset($spec['var'])) { [$H,$N,$U,$P] = $spec['var']; if (isset($GLOBALS[$H], $GLOBALS[$N], $GLOBALS[$U])) { $host = $GLOBALS[$H]; $name = $GLOBALS[$N]; $user = $GLOBALS[$U]; $pass = $GLOBALS[$P] ?? ''; break; } } } try { if ($host && $name && $user) { $dsn = "mysql:host={$host};dbname={$name};charset=utf8mb4"; $pdo = new PDO($dsn, $user, $pass, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]); $GLOBALS['pdo'] = $pdo; return $pdo; } } catch (Throwable $e) { // fallthrough } return null; // give up } function page_meta(array $meta, array $opts = []) { $pdo = __pm_get_pdo(); if (!$pdo instanceof PDO) { if (defined('PAGE_META_DEBUG') && PAGE_META_DEBUG) { echo "<!-- page_meta: no PDO -->"; } return; } // --- ensure tables exist (safe idempotent) --- $pdo->exec("CREATE TABLE IF NOT EXISTS page_catalog ( id BIGINT AUTO_INCREMENT PRIMARY KEY, company_id BIGINT NOT NULL DEFAULT 0, page_name VARCHAR(255) NOT NULL, file_path TEXT NOT NULL, functions_json MEDIUMTEXT NULL, tables_json MEDIUMTEXT NULL, settings_json MEDIUMTEXT NULL, description TEXT NULL, includes_count INT NOT NULL DEFAULT 0, user_funcs_count INT NOT NULL DEFAULT 0, updated_by BIGINT NOT NULL DEFAULT 0, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY uq_company_page (company_id, page_name) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); $pdo->exec("CREATE TABLE IF NOT EXISTS page_usage_log ( id BIGINT AUTO_INCREMENT PRIMARY KEY, company_id BIGINT NOT NULL DEFAULT 0, user_id BIGINT NOT NULL DEFAULT 0, page_name VARCHAR(255) NOT NULL, uri TEXT NOT NULL, method VARCHAR(10) NOT NULL, functions_json MEDIUMTEXT NULL, tables_json MEDIUMTEXT NULL, settings_json MEDIUMTEXT NULL, includes_count INT NOT NULL DEFAULT 0, user_funcs_count INT NOT NULL DEFAULT 0, note VARCHAR(255) NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); // Session context if (session_status() !== PHP_SESSION_ACTIVE) @session_start(); $u = $_SESSION['user'] ?? null; $company_id = (int)($u['company_id'] ?? 0); $user_id = (int)($u['id'] ?? 0); // Normalize inputs $page_name = $meta['page_name'] ?? ($_SERVER['SCRIPT_NAME'] ?? ''); $file_path = $meta['file_path'] ?? ($_SERVER['SCRIPT_FILENAME'] ?? ''); $functions = array_values(array_unique((array)($meta['functions'] ?? []))); $tables = array_values(array_unique((array)($meta['tables'] ?? []))); $settings = (array)($meta['settings'] ?? []); $desc = trim((string)($meta['description'] ?? '')); // Auto context $includes_count = count(get_included_files()); $defs = get_defined_functions(); $user_funcs_count = isset($defs['user']) ? count($defs['user']) : 0; // Upsert try { $sql = "INSERT INTO page_catalog (company_id, page_name, file_path, functions_json, tables_json, settings_json, description, includes_count, user_funcs_count, updated_by, updated_at) VALUES (:cid,:pn,:fp,:fj,:tj,:sj,:ds,:ic,:uf,:ub,NOW()) ON DUPLICATE KEY UPDATE file_path=:fp, functions_json=:fj, tables_json=:tj, settings_json=:sj, description=:ds, includes_count=:ic, user_funcs_count=:uf, updated_by=:ub, updated_at=NOW()"; $st = $pdo->prepare($sql); $st->execute([ ':cid'=>$company_id, ':pn'=>$page_name, ':fp'=>$file_path, ':fj'=>json_encode($functions, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), ':tj'=>json_encode($tables, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), ':sj'=>json_encode($settings, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), ':ds'=>$desc, ':ic'=>$includes_count, ':uf'=>$user_funcs_count, ':ub'=>$user_id, ]); } catch (Throwable $e) { if (defined('PAGE_META_DEBUG') && PAGE_META_DEBUG) { echo "<!-- page_meta upsert failed: ".htmlspecialchars($e->getMessage())." -->"; } return; } // Optional usage log if (!empty($opts['log'])) { try { $uri = $_SERVER['REQUEST_URI'] ?? ''; $method = $_SERVER['REQUEST_METHOD'] ?? ''; $sql2 = "INSERT INTO page_usage_log (company_id,user_id,page_name,uri,method,functions_json,tables_json,settings_json, includes_count,user_funcs_count,created_at) VALUES (:cid,:uid,:pn,:uri,:m,:fj,:tj,:sj,:ic,:uf,NOW())"; $st2 = $pdo->prepare($sql2); $st2->execute([ ':cid'=>$company_id, ':uid'=>$user_id, ':pn'=>$page_name, ':uri'=>$uri, ':m'=>$method, ':fj'=>json_encode($functions, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), ':tj'=>json_encode($tables, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), ':sj'=>json_encode($settings, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), ':ic'=>$includes_count, ':uf'=>$user_funcs_count ]); } catch (Throwable $e) { if (defined('PAGE_META_DEBUG') && PAGE_META_DEBUG) { echo "<!-- page_meta log failed: ".htmlspecialchars($e->getMessage())." -->"; } } } if (!headers_sent()) @header('X-Page-Meta: ok'); if (defined('PAGE_META_DEBUG') && PAGE_META_DEBUG) echo "<!-- page_meta ok -->"; } }