<?php
// Pin the open_basedir allow-list before any code that reads /root/.my.cnf,
// shells out, or does filesystem work. CLI scripts that follow the
// `require_once('/docker/whp/web/configs/config.php')` convention get this
// for free; HTTP requests get it through auto-prepend.php (which loads the
// same file). Either path lands here at-most once thanks to require_once.
require_once('/docker/whp/web/includes/safety-net.php');

//Set INC FLAG, and should be called as a part of the prepend
define('WHP',TRUE);

// Per-install secret salt. Generated by setup.sh on fresh installs (or
// self-healed below on first load) and rotated onto existing servers by the
// rotate-nonce-salt update script. The pre-2026.06 salt was a hardcoded
// public literal; it remains in git history, which is what makes legacy
// ciphertexts recoverable during rotation.
// Everything keyed off NONCE_SALT (session nonces, persist tokens,
// backup_targets secret/password encryption, settings.json smtp/bichon
// secrets, cpanel-import temp passwords) becomes undecryptable if this file
// is lost: it lives under /docker/whp on purpose so it shares a lifecycle
// with the data it protects — keep it in any server-level backup.
$whp_salt_file = '/docker/whp/secrets/nonce-salt';
$whp_salt = @file_get_contents($whp_salt_file);
$whp_salt = ($whp_salt === false) ? '' : trim($whp_salt);
if ($whp_salt === '') {
    @mkdir('/docker/whp/secrets', 0700, true);
    // 'x' = exclusive create: exactly one concurrent request wins the race,
    // everyone (winner included) then reads the file back.
    $whp_salt_fh = @fopen($whp_salt_file, 'x');
    if ($whp_salt_fh !== false) {
        fwrite($whp_salt_fh, bin2hex(random_bytes(32)) . "\n");
        fclose($whp_salt_fh);
        @chmod($whp_salt_file, 0600);
        // Loud breadcrumb: legitimate only on a fresh install's first
        // request. On an established server a missing salt file means it
        // was lost (bad restore, errant cleanup) and every NONCE_SALT
        // ciphertext (backup targets, SMTP/Bichon secrets) just became
        // undecryptable — this log line is the connecting evidence.
        error_log('WHP config: nonce-salt file was missing — generated a new salt at '
            . $whp_salt_file . '. If this server is NOT a fresh install, restore the '
            . 'old salt file from backup or re-enter all stored credentials.');
    }
    $whp_salt = trim((string)@file_get_contents($whp_salt_file));
    if ($whp_salt === '') {
        // Unwritable filesystem — fail loudly rather than silently issuing
        // nonces/ciphertexts under an ephemeral in-memory salt. Exit
        // signals matter: crons check $? and monitors check the HTTP code.
        error_log('WHP config: cannot create or read ' . $whp_salt_file);
        if (PHP_SAPI === 'cli') {
            fwrite(STDERR, "WHP configuration error: salt file unavailable\n");
            exit(1);
        }
        http_response_code(503);
        exit('WHP configuration error: salt file unavailable');
    }
    unset($whp_salt_fh);
}
define('NONCE_SALT', $whp_salt);
unset($whp_salt, $whp_salt_file);
//Get MySQL Password from /root/.my.cnf
$root_mysql_password = shell_exec('cat /root/.my.cnf |grep password |cut -f2- -d=');
define('MYSQL_PASS',$root_mysql_password);

//Get PostgreSQL Password from /root/.pgpass if it exists
if (file_exists('/root/.pgpass')) {
    $pgpass_content = shell_exec('cat /root/.pgpass | head -1');
    if ($pgpass_content) {
        // Format: hostname:port:database:username:password
        $pgpass_parts = explode(':', trim($pgpass_content));
        if (count($pgpass_parts) >= 5) {
            define('POSTGRES_PASS', $pgpass_parts[4]);
        }
    }
}
// Fallback for environments without .pgpass
if (!defined('POSTGRES_PASS')) {
    define('POSTGRES_PASS', '');
}