<?php
/**
 * Demo Oracle — DemoOracleDashTdeWalletsRepository.
 *
 * Portage de modules/oracle/lib/OracleDashTdeWalletsRepository.php : requetes de LECTURE
 * uniquement. Les methodes d'action ont ete RETIREES a la
 * source, pas desactivees :
 *
 *   createAutoLogin
 *
 * Une action absente du serveur ne se contourne pas.
 *
 * Convention conservee du module complet : classe sans etat, $conn (ressource
 * OCI8) en premier argument, retour systematique en array — vide en cas
 * d'echec OCI, jamais false, jamais d'exception. La page appelante affiche
 * l'erreur globale.
 */

if (class_exists('DemoOracleDashTdeWalletsRepository')) return;

class DemoOracleDashTdeWalletsRepository
{
    /** Wallets / keystores, une ligne par instance (RAC) et par conteneur. */
    public static function fetchWallets($conn): array
    {
        $rows = [];
        $stm = @oci_parse($conn,
            "SELECT inst_id, con_id, wrl_type, wrl_parameter, status,
                    wallet_type, wallet_order, fully_backed_up
             FROM   GV\$ENCRYPTION_WALLET
             ORDER  BY inst_id, con_id, wallet_order");
        if ($stm && @oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            while ($r = oci_fetch_assoc($stm)) $rows[] = $r;
        }
        if ($stm) oci_free_statement($stm);
        return $rows;
    }

    /** Paramètres d'initialisation liés au TDE (présents selon la version). */
    public static function fetchTdeParams($conn): array
    {
        $rows = [];
        $stm = @oci_parse($conn,
            "SELECT name, display_value AS value
             FROM   V\$PARAMETER
             WHERE  name IN ('wallet_root','tde_configuration','encrypt_new_tablespaces')
             ORDER  BY name");
        if ($stm && @oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            while ($r = oci_fetch_assoc($stm)) $rows[] = $r;
        }
        if ($stm) oci_free_statement($stm);
        return $rows;
    }

    /** Tablespaces chiffrés : nom, algorithme, statut. */
    public static function fetchEncryptedTablespaces($conn): array
    {
        $rows = [];
        $stm = @oci_parse($conn,
            "SELECT t.name AS name, e.encryptionalg AS algorithm, e.status AS status
             FROM   V\$ENCRYPTED_TABLESPACES e
             JOIN   V\$TABLESPACE t ON t.ts# = e.ts#
             ORDER  BY t.name");
        if ($stm && @oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            while ($r = oci_fetch_assoc($stm)) $rows[] = $r;
        }
        if ($stm) oci_free_statement($stm);
        return $rows;
    }

    /**
     * Résumé des clés maîtres (MEK) : nombre total + dernière activation.
     * V$ENCRYPTION_KEYS peut nécessiter un privilège élevé — tableau vide sinon.
     *
     * @return array{TOTAL?:string, LAST_ACTIVATION?:string}
     */
    public static function fetchMasterKeysSummary($conn): array
    {
        $stm = @oci_parse($conn,
            "SELECT COUNT(*) AS total,
                    TO_CHAR(MAX(activation_time), 'YYYY-MM-DD HH24:MI:SS') AS last_activation
             FROM   V\$ENCRYPTION_KEYS");
        if (!$stm || !@oci_execute($stm, OCI_NO_AUTO_COMMIT)) {
            if ($stm) oci_free_statement($stm);
            return [];
        }
        $row = oci_fetch_assoc($stm) ?: [];
        oci_free_statement($stm);
        return $row;
    }
}
