$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 '
Special Auditor — Integração OpenAI
';
$notice = isset($_GET['sa_ai_notice']) ? sanitize_key(wp_unslash($_GET['sa_ai_notice'])) : '';
$messages = [
'saved'=>['updated','Chave salva de forma criptografada.'],
'removed'=>['updated','Chave removida.'],
'invalid'=>['error','Informe uma chave de API válida.'],
'crypto'=>['error','Criptografia AES-256-GCM indisponível no servidor. Nada foi salvo.'],
'ok'=>['updated','Conexão com a API OpenAI confirmada.'],
'failed'=>['error','Não foi possível confirmar a conexão. Verifique a chave, permissões e conectividade do servidor.']
];
if (isset($messages[$notice])) echo '
' . esc_html($messages[$notice][1]) . '
';
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.
';
}
function sa_ai_redirect($notice) {
wp_safe_redirect(add_query_arg('sa_ai_notice', $notice, admin_url('admin.php?page=special-auditor-ai')));
exit;
}
add_action('admin_post_sa_ai_save', function () {
if (!current_user_can('manage_options')) wp_die('Acesso não autorizado.', '', ['response'=>403]);
check_admin_referer('sa_ai_save', '_sa_ai_nonce');
$secret = isset($_POST['sa_ai_key']) && is_string($_POST['sa_ai_key']) ? trim(wp_unslash($_POST['sa_ai_key'])) : '';
if (!preg_match('/^sk-[A-Za-z0-9_-]{16,}$/', $secret) || strlen($secret) > 512) sa_ai_redirect('invalid');
$encrypted = sa_ai_encrypt($secret);
if ($encrypted === false) sa_ai_redirect('crypto');
update_option('sa_ai_secret_v1', $encrypted, false);
sa_ai_redirect('saved');
});
add_action('admin_post_sa_ai_remove', function () {
if (!current_user_can('manage_options')) wp_die('Acesso não autorizado.', '', ['response'=>403]);
check_admin_referer('sa_ai_remove', '_sa_ai_nonce');
delete_option('sa_ai_secret_v1');
sa_ai_redirect('removed');
});
add_action('admin_post_sa_ai_test', function () {
if (!current_user_can('manage_options')) wp_die('Acesso não autorizado.', '', ['response'=>403]);
check_admin_referer('sa_ai_test', '_sa_ai_nonce');
$secret = sa_ai_secret();
if ($secret === '') sa_ai_redirect('failed');
$response = wp_remote_get('https://api.openai.com/v1/models', [
'timeout'=>20, 'redirection'=>0,
'headers'=>['Authorization'=>'Bearer ' . $secret, 'Accept'=>'application/json']
]);
if (is_wp_error($response)) sa_ai_redirect('failed');
sa_ai_redirect(wp_remote_retrieve_response_code($response) === 200 ? 'ok' : 'failed');
});
https://www.specialimoveis.com.br/post-sitemap.xml
2026-05-14T15:03:40+00:00
https://www.specialimoveis.com.br/page-sitemap.xml
2026-10-28T17:13:19+00:00
https://www.specialimoveis.com.br/imovel-sitemap.xml
2026-10-30T13:26:05+00:00
https://www.specialimoveis.com.br/imovel-sitemap2.xml
2026-10-28T06:27:26+00:00
https://www.specialimoveis.com.br/imovel-sitemap3.xml
2026-10-28T06:41:10+00:00
https://www.specialimoveis.com.br/imovel-sitemap4.xml
2026-10-28T06:53:52+00:00
https://www.specialimoveis.com.br/imovel-sitemap5.xml
2026-10-28T07:07:35+00:00
https://www.specialimoveis.com.br/imovel-sitemap6.xml
2026-10-28T07:21:13+00:00
https://www.specialimoveis.com.br/imovel-sitemap7.xml
2026-10-28T07:32:58+00:00
https://www.specialimoveis.com.br/imovel-sitemap8.xml
2026-10-28T07:45:41+00:00
https://www.specialimoveis.com.br/imovel-sitemap9.xml
2026-10-28T07:59:19+00:00
https://www.specialimoveis.com.br/imovel-sitemap10.xml
2026-10-30T13:26:05+00:00
https://www.specialimoveis.com.br/category-sitemap.xml
2026-05-14T15:03:40+00:00
https://www.specialimoveis.com.br/post_tag-sitemap.xml
2026-05-14T15:03:40+00:00
https://www.specialimoveis.com.br/finalidade-e-tipo-do-imovel-sitemap.xml
2026-10-28T06:07:19+00:00
https://www.specialimoveis.com.br/localizacao-sitemap.xml
2026-10-30T13:26:05+00:00
https://www.specialimoveis.com.br/author-sitemap.xml
2026-10-14T15:21:13+00:00