<?php
error_reporting(E_ERROR | E_PARSE);
if (!class_exists('crm_hardlock')) require_once("common_cadline_libraries/crm_hardlock.class.php");

if (!crm_hardlock::isServerActive()) {
    die();
}

$function_id    = $_GET["id"];
$product_id     = $_GET["prod"];
$ver_id         = $_GET["ver"];
$country_id     = $_GET["cnt"];
$param          = $_GET["param"];
$build          = $_GET["bld"];
$test           = $_GET["test"] == 'true';

if (crm_hardlock::isPrimaryServerAlive()) {
    $notNeededFunctions = array(17, 20, 21);
    if (in_array($function_id, $notNeededFunctions))
        die();

    $param          = base64_decode($param);
    $privatekey     = Webform::GetWebformPrivateKey();
    $session        = Webform::get_valueFromStringUrl("?{$param}", "session");
    $param          = Webform::get_valueFromStringUrl("?{$param}", "param");
    $decodedSession = base64_decode($session);
    $decoded_param  = Webform::decode($privatekey, $decodedSession, $param);
    $decoded_param  = "?{$decoded_param}";

    $password = '';
    $password = Webform::get_valueFromStringUrl($decoded_param, "password");

    if ($password == '')
        $password = Webform::get_valueFromStringUrl($decoded_param, "serialNumber");

    if ($password == '')
        $password = Webform::get_valueFromStringUrl($decoded_param, "serial");

    if ($password == '')
        $password = Webform::get_valueFromStringUrl($decoded_param, "pwd");

    crm_hardlock::secondaryServerLogRefused($password, $function_id, 'A foszerver el, nem engedtuk tovabb');
    die();
}

if ($function_id == '99') {
    echo 'ok';
    return;
}

$url = "";

// Itt majd nem a CRMADATBAZIS-t hivja meg, hanem majd az ujat (szinkronizalt adatbazis), secondary_url mezobol vegye az urlt, a secondary_domain-t pedig ki kell cserelni
$query = "SELECT * FROM `" . SYNCADATBAZIS . "`.`url_switch`
    WHERE function_id = ? AND (product_id = ? OR product_id = 0) AND (country_id = ? OR country_id = 0) AND
    (ver_id <= ? OR ver_id = 0) ORDER BY country_id DESC, ver_id DESC";
Database::getInstance()->query($query, array('iiii', $function_id, $product_id, $country_id, $ver_id));
$res = Database::getInstance()->fetchNext();

if (empty($res)) {
    print "No result found.";
} else {
    $param = base64_decode($param);

    if ($test && $res['test_url'] != '') {
        $url = $res['test_url'];

        $session    = Webform::get_valueFromStringUrl("?{$param}", "session");
        $privateKey = Webform::GetWebformPrivateKey();
        $encoded    = Webform::encode($privateKey, $session, base64_encode(date('Y-m-d')));
        $encoded    = base64_encode($encoded);

        $param .= "&authCode={$encoded}";
    } else {
        if (strpos($res['secondary_url'], 'https') === false)
            return;

        $url = str_replace('secondary_domain', 'secondary.cadline.hu', $res['secondary_url']);
    }

    $url .= $param;

    if ($function_id == 8 || $function_id == 19) $url .= "&ver={$ver_id}";
    if ($function_id == 17 || $function_id == 20 || $function_id == 4 || $function_id == 24 || $function_id == 22 || $function_id == 30) $url .= "&prod={$product_id}&ver={$ver_id}&cnt={$country_id}";
    if ($function_id == 15) $url .= "&cnt={$country_id}";
    if ($function_id == 25) $url .= "&ver={$ver_id}&build={$build}";

    if ($function_id == 19) {
        $se = Webform::get_valueFromStringUrl("?{$param}", "se");
        $url .= "&se={$se}";
    }

    $url .= "&secondary=true";

    header('Location: ' . $url);
}
