Files
Investidor-V2/tools/portal-investor-consolidation-evidence.php
T
nala 6ddafc95ba
tests / ci (push) Canceled after 0s
oportunidades 4
2026-10-10 07:03:59 +02:00

63 lines
4.6 KiB
PHP

<?php
// Read-only complement to the existing audit; never boot mutation services or export credentials/payloads.
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
require dirname(__DIR__).'/vendor/autoload.php';
$app = require dirname(__DIR__).'/bootstrap/app.php';
(new LoadEnvironmentVariables)->bootstrap($app);
(new LoadConfiguration)->bootstrap($app);
$config = $app->make('config');
$connection = $config->get('database.connections.'.$config->get('database.default'));
if (($connection['driver'] ?? '') !== 'pgsql' || ! in_array($connection['url'] ?? null, [null, ''], true)) {
throw new RuntimeException('Explicit PostgreSQL connection required for read-only evidence.');
}
$pdo = new PDO('pgsql:host='.$connection['host'].';port='.$connection['port'].';dbname='.$connection['database'], $connection['username'], $connection['password'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdo->exec('BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ READ ONLY');
try {
$identity = $pdo->query("SELECT current_database() AS database,inet_server_port() AS port,current_setting('transaction_read_only') AS read_only,version() AS version")->fetch(PDO::FETCH_ASSOC);
if ($identity['read_only'] !== 'on') {
throw new RuntimeException('Read-only transaction required.');
}
$result = ['captured_at' => gmdate('c'), 'identity' => $identity, 'postgis_version' => $pdo->query('SELECT postgis_full_version()')->fetchColumn(),
'php_version' => PHP_VERSION, 'laravel_version' => Application::VERSION,
'configuration' => ['suitecrm_enabled' => (bool) $config->get('suitecrm.enabled'), 'ai_enabled' => (bool) $config->get('investmoz_ai.enabled'),
'notifications_enabled' => (bool) $config->get('portal.notifications_enabled'), 'queue_default' => $config->get('queue.default'),
'session_driver' => $config->get('session.driver'), 'cache_default' => $config->get('cache.default'), 'business_repository' => $config->get('mne.repository')],
'tables' => [], 'active_writes' => 0];
$tables = ['users', 'investors', 'investor_accounts', 'investment_accounts', 'contacts', 'consents', 'organizations', 'memberships',
'projects', 'investment_dossiers', 'investor_journeys', 'journey_steps', 'submissions', 'desk_cases', 'desk_requests', 'desk_service_versions', 'desk_events', 'desk_documents',
'tasks', 'leads', 'interest_requests', 'investment_submissions', 'investment_company_declarations', 'existing_businesses', 'land_assistance_cases',
'simulation_form_drafts', 'simulation_draft_business_links', 'simulations', 'simulation_results', 'documents', 'questionnaire_versions', 'reference_catalog_versions', 'investment_legal_sources', 'portal_resources',
'aftercare_plans', 'constraints', 'escalations', 'geo_locations', 'geo_geometries', 'audit_events'];
foreach ($tables as $table) {
$q = $pdo->prepare("SELECT column_name,data_type,is_nullable,column_default FROM information_schema.columns WHERE table_schema='public' AND table_name=? ORDER BY ordinal_position");
$q->execute([$table]);
$columns = $q->fetchAll(PDO::FETCH_ASSOC);
if ($columns === []) {
$result['tables'][$table] = ['exists' => false];
continue;
}
$q = $pdo->prepare('SELECT conname,contype,pg_get_constraintdef(oid) AS definition FROM pg_constraint WHERE conrelid=to_regclass(?) ORDER BY conname');
$q->execute(['public.'.$table]);
$constraints = $q->fetchAll(PDO::FETCH_ASSOC);
$q = $pdo->prepare("SELECT indexname,indexdef FROM pg_indexes WHERE schemaname='public' AND tablename=? ORDER BY indexname");
$q->execute([$table]);
$result['tables'][$table] = ['exists' => true, 'rows' => (int) $pdo->query('SELECT count(*) FROM public."'.$table.'"')->fetchColumn(),
'columns' => $columns, 'constraints' => $constraints, 'indexes' => $q->fetchAll(PDO::FETCH_ASSOC)];
}
} finally {
$pdo->exec('ROLLBACK');
}
$dir = dirname(__DIR__).'/storage/app/private/investmoz-v2/portal-audit';
$file = $dir.'/investor-consolidation-runtime.json';
if (is_file($file)) {
copy($file, $dir.'/investor-consolidation-runtime-'.gmdate('Ymd_His').'-'.bin2hex(random_bytes(3)).'.json');
}
file_put_contents($file, json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
echo json_encode(['captured_at' => $result['captured_at'], 'identity' => $identity, 'configuration' => $result['configuration'],
'table_counts' => array_map(fn ($row) => $row['rows'] ?? null, $result['tables']), 'active_writes' => 0], JSON_THROW_ON_ERROR).PHP_EOL;