<?php declare(strict_types=1); class CredentialStore { private static $payload = null; private static function init() { if (self::$payload !== null) return; $privateDir = defined('GHOST_PRIVATE') ? GHOST_PRIVATE : dirname(__DIR__) . '/private'; $envFile = $privateDir . '/.env'; if (!file_exists($envFile)) die("System error."); $envLines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $sk = ''; foreach ($envLines as $line) { if ($line === '' || $line[0] === '#') continue; $parts = explode('=', $line, 2); if (count($parts) === 2 && trim($parts[0]) === 'sk') { $sk = trim($parts[1]); break; } } if (strlen($sk) !== 64) die("System error."); $key = hex2bin($sk); $encFile = $privateDir . '/credentials.enc'; if (!file_exists($encFile)) die("System error."); $data = file_get_contents($encFile); if (strlen($data) < 30) die("System error."); $version = ord($data[0]); if ($version === 1) { $iv = substr($data, 1, 12); $tag = substr($data, 13, 16); $ciphertext = substr($data, 29); } else { $iv = substr($data, 0, 16); $tag = substr($data, 16, 16); $ciphertext = substr($data, 32); } $json = openssl_decrypt($ciphertext, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag); if ($json === false) die("System error."); self::$payload = json_decode($json, true); if (!is_array(self::$payload)) die("System error."); } public static function get(string $key) { self::init(); return self::$payload[$key] ?? null; } } 