$id = absint($_GET['id'] ?? 0); check_admin_referer('sa_download_' . $id); global $wpdb; $r = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'special_auditor_files WHERE id = %d', $id)); if (!$r || !preg_match('/^[a-f0-9]{40}\.pdf$/', $r->stored_name)) wp_die('Documento não encontrado.', '', ['response'=>404]); $dir = sa_storage_status(); if (is_wp_error($dir)) wp_die('Armazenamento indisponível.'); $path = $dir . DIRECTORY_SEPARATOR . $r->stored_name; if (!is_file($path)) wp_die('Documento não encontrado.', '', ['response'=>404]); while (ob_get_level()) ob_end_clean(); nocache_headers(); header('Content-Type: application/pdf'); header('X-Content-Type-Options: nosniff'); header('Content-Disposition: attachment; filename="documento-' . $id . '.pdf"'); header('Content-Length: ' . filesize($path)); readfile($path); exit; }); // Credencial protegida em repouso: AES-256-GCM com chave derivada dos segredos do WordPress. // Se os salts do WordPress forem alterados, será necessário cadastrar a chave novamente. function sa_ai_crypto_key() { if (!function_exists('openssl_encrypt') || !function_exists('openssl_decrypt') || !defined('AUTH_KEY') || !defined('SECURE_AUTH_KEY') || !defined('LOGGED_IN_KEY') || !defined('NONCE_KEY')) return false; return hash('sha256', AUTH_KEY . SECURE_AUTH_KEY . LOGGED_IN_KEY . NONCE_KEY . 'special-auditor-openai-v1', true); } function sa_ai_encrypt($secret) { $key = sa_ai_crypto_key(); if (!$key || !in_array('aes-256-gcm', openssl_get_cipher_methods(), true)) return false; try { $iv = random_bytes(12); } catch (Exception $e) { return false; } $tag = ''; $cipher = openssl_encrypt($secret, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $iv, $tag); return $cipher === false ? false : base64_encode($iv . $tag . $cipher); } function sa_ai_secret() { $encoded = get_option('sa_ai_secret_v1', ''); if (!is_string($encoded) || $encoded === '') return ''; $raw = base64_decode($encoded, true); $key = sa_ai_crypto_key(); if (!$key || $raw === false || strlen($raw) < 29) return ''; $plain = openssl_decrypt(substr($raw, 28), 'aes-256-gcm', $key, OPENSSL_RAW_DATA, substr($raw, 0, 12), substr($raw, 12, 16)); return $plain === false ? '' : $plain; } function sa_ai_settings_page() { if (!current_user_can('manage_options')) wp_die('Acesso não autorizado.', '', ['response'=>403]); $configured = sa_ai_secret() !== ''; echo '
Status da chave: ' . ($configured ? 'Configurada' : 'Não configurada') . '
'; echo 'A chave não é exibida após o salvamento. Apenas administradores podem alterá-la. A configuração ainda não envia PDFs à OpenAI.
'; echo ''; if ($configured) { echo ''; echo ''; } echo 'Versão 1.3.0 — Credencial preservada. Extração e auditoria financeira ainda não habilitadas.