<?php
ob_start();

/************************************************************************/
/* PROGRAMMER     : SMA                                                */
/* SCRIPT NAME    : fraudsettings_manage.php                           */
/* DESCRIPTION    : Fraud Settings Manage and Validate                 */
/************************************************************************/

// -----------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------
require_once '../includes/constants.php';
require_once '../includes/functions.php';
require_once '../includes/function1.php';
require_once '../includes/session.php';
require_once '../includes/allstripslashes.php';

// -----------------------------------------------------------------------
// Instantiate database connection
// -----------------------------------------------------------------------
$partners = new partners();
$partners->connection($host, $user, $pass, $db);
$commonobj = new common();

// -----------------------------------------------------------------------
// Get and sanitize POST data
// -----------------------------------------------------------------------
$recentclick   = isset($_POST['chk_click']) ? 1 : 0;
$clickseconds  = trim($_POST['txt_clickseconds'] ?? '');
$clickaction   = trim($_POST['cmb_clickaction'] ?? '');
$recentsale    = isset($_POST['chk_sale']) ? 1 : 0;
$saleseconds   = trim($_POST['txt_saleseconds'] ?? '');
$saleaction    = trim($_POST['cmb_saleaction'] ?? '');
$loginretry    = trim($_POST['txt_login_retry'] ?? '');
$logindelay    = trim($_POST['txt_login_delay'] ?? '');
$declinesale   = isset($_POST['chk_samesale']) ? 1 : 0;

// For redirect fallback
$returnstring = http_build_query([
    'recentclick' => $recentclick,
    'clickseconds' => $clickseconds,
    'clickaction' => $clickaction,
    'recentsale' => $recentsale,
    'saleseconds' => $saleseconds,
    'saleaction' => $saleaction,
    'loginretry' => $loginretry,
    'logindelay' => $logindelay,
    'declinesale' => $declinesale
]);

// -----------------------------------------------------------------------
// Validate mandatory fields
// -----------------------------------------------------------------------
$mandatory_fields = [$loginretry, $logindelay];
if ($recentclick) $mandatory_fields[] = $clickseconds;
if ($recentsale) $mandatory_fields[] = $saleseconds;

if ($commonobj->nullvalidation(implode('~*', $mandatory_fields))) {
    header("Location: index.php?Act=fraudsettings&msg=Don't leave fields blank&$returnstring");
    exit;
}

// -----------------------------------------------------------------------
// Validate numeric values
// -----------------------------------------------------------------------
$numeric_fields = [$recentclick, $clickseconds, $recentsale, $saleseconds, $loginretry, $logindelay];
foreach ($numeric_fields as $field) {
    if (!is_numeric($field)) {
        header("Location: index.php?Act=fraudsettings&msg=Invalid Entry!!&$returnstring");
        exit;
    }
}

// -----------------------------------------------------------------------
// Update values in constants.php
// -----------------------------------------------------------------------
$filename = '../includes/constants.php';
if (!is_readable($filename) || !is_writable($filename)) {
    header("Location: index.php?Act=fraudsettings&msg=Unable to access configuration file&$returnstring");
    exit;
}

$contents = file_get_contents($filename);
if ($contents === false) {
    header("Location: index.php?Act=fraudsettings&msg=Error reading configuration file&$returnstring");
    exit;
}

// Replace constants
$replacements = [
    'fraudsettings_login_retry'        => $loginretry,
    'fraudsettings_login_delay'        => $logindelay,
    'fraudsettings_recentclick'        => $recentclick,
    'fraudsettings_clickseconds'       => $clickseconds,
    'fraudsettings_clickaction'        => $clickaction,
    'fraudsettings_recentsale'         => $recentsale,
    'fraudsettings_saleseconds'        => $saleseconds,
    'fraudsettings_saleaction'         => $saleaction,
    'fraudsettings_decline_recentsale' => $declinesale,
];

foreach ($replacements as $key => $value) {
    $pattern = "/define\\s*\\(\\s*['\"]" . preg_quote($key, '/') . "['\"]\\s*,\\s*['\"]?.*?['\"]?\\s*\\);/";
    $replacement = "define('$key', " . (is_numeric($value) ? $value : "'$value'") . ");";
    $contents = preg_replace($pattern, $replacement, $contents);
}

if (file_put_contents($filename, $contents) === false) {
    header("Location: index.php?Act=fraudsettings&msg=Error writing configuration file&$returnstring");
    exit;
}

// -----------------------------------------------------------------------
// Redirect with success message
// -----------------------------------------------------------------------
header("Location: index.php?Act=fraudsettings&msg=Fraud Settings Updated&$returnstring");
exit;
?>
