<?php
// One-off fix: the shared cPanel mail server (mail.brcomputersmw.com) serves an SSL
// certificate issued for *.web-hosting.com, not the customer's domain, causing
// "Certificate failure: Hostname mismatch" on IMAP/SMTP connect. This turns off
// TLS certificate verification for the mailbox (connection stays encrypted, but the
// server's identity is no longer checked). Run once on the live server
// (php tmp_disable_mailbox_cert_verify.php), then delete it.

require_once __DIR__ . '/classes/Database.php';
require_once __DIR__ . '/classes/MailboxService.php';

$database = new Database();
$conn = $database->getConnection();
if (!$conn) {
    fwrite(STDERR, "Database connection failed. Check config/database.php and make sure MySQL is running.\n");
    exit(1);
}

function currentMailboxSetting(PDO $conn, string $key) {
    $stmt = $conn->prepare('SELECT setting_value FROM mailbox_settings WHERE setting_key = :key');
    $stmt->execute([':key' => $key]);
    return $stmt->fetchColumn();
}

echo "Before: verify_cert = " . (currentMailboxSetting($conn, 'verify_cert') ?: '(not set, defaults to true)') . "\n";

$mailboxService = new MailboxService($conn);
$mailboxService->saveSettings(['verify_cert' => 'false'], null);

echo "After: verify_cert = " . currentMailboxSetting($conn, 'verify_cert') . "\n";
echo "Done. Reload the Mailbox Settings page and refresh the inbox.\n";
