<?php
$path = "../../";
include("../../map.php");

function settings_post_value($key, $default = "") {
    if (!isset($_POST[$key])) {
        return $default;
    }

    $value = $_POST[$key];
    if (is_array($value)) {
        return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

    return trim(str_replace("\0", "", (string)$value));
}

function settings_escape($conn, $value) {
    return mysqli_real_escape_string($conn, (string)$value);
}

function settings_safe_table($name, $fallback = "lang1") {
    $name = (string)$name;
    return preg_match('/^[a-zA-Z0-9_]+$/', $name) ? $name : $fallback;
}

function settings_collect_prefixed($prefix) {
    $result = array();
    foreach ($_POST as $key => $value) {
        $key = (string)$key;
        if ($key === "" || strpos($key, $prefix) !== 0) {
            continue;
        }
        $result[substr($key, 1)] = is_array($value) ? $value : trim(str_replace("\0", "", (string)$value));
    }

    return $result;
}

function settings_collect_general() {
    $result = array();
    foreach ($_POST as $key => $value) {
        $key = (string)$key;
        if ($key === "") {
            continue;
        }

        $prefix = $key[0];
        if ($prefix === "t" || $prefix === "n" || $prefix === "i" || $prefix === "x" || $prefix === "y" || $prefix === "j") {
            continue;
        }

        $result[$key] = is_array($value) ? $value : trim(str_replace("\0", "", (string)$value));
    }

    return $result;
}

function settings_json_encode($value, $numeric = false) {
    $flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
    if ($numeric) {
        $flags |= JSON_NUMERIC_CHECK;
    }

    return json_encode($value, $flags);
}

function settings_update_row($conn, $table, array $data) {
    if (!$data) {
        return true;
    }

    $table = settings_safe_table($table);
    $sets = array();
    foreach ($data as $column => $value) {
        if (!preg_match('/^[a-zA-Z0-9_]+$/', (string)$column)) {
            continue;
        }
        $sets[] = "`" . $column . "`='" . settings_escape($conn, $value) . "'";
    }

    if (!$sets) {
        return true;
    }

    $query = "update `" . $table . "` set " . implode(", ", $sets) . " where ne='ayarlar'";
    return mysqli_query($conn, $query);
}

function settings_language_tables() {
    $tables = array();
    $langs = explode(",", settings_post_value("m1"));
    $index = 0;

    foreach ($langs as $lang) {
        $lang = trim($lang);
        if ($lang === "") {
            continue;
        }
        $index++;
        $tables[] = "lang" . $index;
    }

    if (!$tables) {
        $tables[] = settings_safe_table(isset($_SESSION["lang"]) ? $_SESSION["lang"] : "lang1");
    }

    return array_values(array_unique($tables));
}

function settings_response($html) {
    echo $html;
}

if (isset($_GET["durum"]) && $_GET["durum"] === "dsifre") {
    $oldPass = v3e(settings_post_value("epass"));
    $newPass = v3e(settings_post_value("pass"));
    $gkod = v3e(settings_post_value("gkod"));
    mysqli_query($conn, "update genel set t2='" . $newPass . "', t3='" . $gkod . "' where ne='asifre' and t1='YWRtaW4=' and t2='" . $oldPass . "'");
    settings_response("<i class=\"fa fa-check\" style=\"font-size:24px; color:#1f4a04\"></i> " . $sdil[212]);
} elseif (isset($_GET["durum"]) && $_GET["durum"] === "kaydet") {
    if (empty($_POST)) {
        exit();
    }

    $firmArray = settings_collect_prefixed("x");
    $socialArray = settings_collect_prefixed("y");
    $tboxArray = settings_collect_prefixed("j");
    $allArray = settings_collect_general();

    if (!$allArray) {
        exit();
    }

    $exchangeArray = array(
        "d" => date("d"),
        "m" => date("m"),
        "y" => date("Y"),
        "h" => date("H"),
        "usd" => settings_post_value("zusd"),
        "eur" => settings_post_value("zeur"),
        "gbp" => settings_post_value("zgbp"),
        "grgold" => settings_post_value("zgr"),
        "btc" => settings_post_value("zbtc")
    );

    $rexchange = settings_json_encode($exchangeArray, true);
    $rall = settings_json_encode($allArray);
    $rtbox = settings_json_encode($tboxArray);
    $rfirm = settings_json_encode($firmArray);
    $rsocial = settings_json_encode($socialArray);

    $currentLangTable = settings_safe_table(isset($_SESSION["lang"]) ? $_SESSION["lang"] : "lang1");
    $languageTables = settings_language_tables();

    $seoData = array(
        "n1" => settings_post_value("n1"),
        "n2" => settings_post_value("n2"),
        "n4" => settings_post_value("n4")
    );

    $currentLangIData = array();
    for ($i = 2; $i <= 30; $i++) {
        $currentLangIData["i" . $i] = settings_post_value("i" . $i);
    }

    $currentLangTData = array();
    for ($i = 1; $i <= 20; $i++) {
        $currentLangTData["t" . $i] = settings_post_value("t" . $i);
    }

    $currentLangNData = array(
        "n3" => settings_post_value("n3"),
        "n9" => settings_post_value("n9"),
        "n10" => settings_post_value("n10"),
        "n11" => $rfirm,
        "n14" => settings_post_value("n14"),
        "n15" => settings_post_value("n15"),
        "n16" => $rsocial,
        "n19" => $rtbox,
        "n20" => settings_post_value("n20"),
        "t28" => settings_post_value("t28")
    );

    $sharedLangData = array(
        "n5" => settings_post_value("n5"),
        "n6" => settings_post_value("n6"),
        "n7" => settings_post_value("n7"),
        "n8" => settings_post_value("n8"),
        "n12" => settings_post_value("n12"),
        "n13" => settings_post_value("n13"),
        "n14" => settings_post_value("n14"),
        "n17" => settings_post_value("n17"),
        "n18" => $rall,
        "t21" => settings_post_value("t21"),
        "t22" => settings_post_value("t22"),
        "t23" => settings_post_value("t23"),
        "t24" => settings_post_value("t24"),
        "t25" => settings_post_value("t25"),
        "t26" => settings_post_value("t26"),
        "t28" => settings_post_value("t28"),
        "t36" => settings_post_value("t36"),
        "t37" => settings_post_value("t37"),
        "t38" => settings_post_value("t38"),
        "t40" => $rexchange,
        "t41" => settings_post_value("t41"),
        "t42" => settings_post_value("t42"),
        "t43" => settings_post_value("t43"),
        "t46" => settings_post_value("t46"),
        "i2" => settings_post_value("i2")
    );

    $hasError = false;
    if (function_exists("mysqli_begin_transaction")) {
        mysqli_begin_transaction($conn);
    }

    if (settings_post_value("s8") !== "") {
        foreach ($languageTables as $langTable) {
            if (!settings_update_row($conn, $langTable, $seoData)) {
                $hasError = true;
                break;
            }
        }
    } else {
        $hasError = !settings_update_row($conn, $currentLangTable, $seoData);
    }

    if (!$hasError) {
        $hasError = !settings_update_row($conn, $currentLangTable, $currentLangIData);
    }
    if (!$hasError) {
        $hasError = !settings_update_row($conn, $currentLangTable, $currentLangTData);
    }
    if (!$hasError) {
        $hasError = !settings_update_row($conn, $currentLangTable, $currentLangNData);
    }

    if (!$hasError) {
        foreach ($languageTables as $langTable) {
            if (!settings_update_row($conn, $langTable, $sharedLangData)) {
                $hasError = true;
                break;
            }
        }
    }

    if (function_exists("mysqli_commit") && function_exists("mysqli_rollback")) {
        if ($hasError) {
            mysqli_rollback($conn);
        } else {
            mysqli_commit($conn);
        }
    }

    if ($hasError) {
        settings_response("<i class=\"fa fa-times\" style=\"font-size:24px; color:#a61f2d\"></i> Kayit sirasinda hata olustu.");
    } else {
        settings_response("<i class=\"fa fa-check\" style=\"font-size:24px; color:#1f4a04\"></i> " . $sdil[212]);
    }
}

include("../public/log.php");
include("../public/set_script.php");
?>
