<?php
if (isset($_POST['CRITERION_force_sid'])) {
    $_POST['force_sid'] = $_POST['CRITERION_force_sid'];
}
if (isset($_POST['CRITERION_heidelpay_input_position'])) {
    $_POST['heidelpay_input_position'] = $_POST['CRITERION_heidelpay_input_position'];
}
if (isset($_POST['CRITERION_language'])) {
    $_POST['language'] = $_POST['CRITERION_language'];
}
if (isset($_POST['CRITERION_lang'])) {
    $_POST['lang'] = $_POST['CRITERION_lang'];
}
if (isset($_POST['CRITERION_rtoken'])) {
    $_POST['rtoken'] = $_POST['CRITERION_rtoken'];
}
if (isset($_POST['CRITERION_stoken'])) {
    $_POST['stoken'] = $_POST['CRITERION_stoken'];
}
if (isset($_POST['CRITERION_shp'])) {
    $_POST['shp'] = $_POST['CRITERION_shp'];
}
if (isset($_POST['CRITERION_cutsomerip'])) {
    $_SERVER["HTTP_X_FORWARDED_FOR"] = $_POST['CRITERION_cutsomerip'];
}

/**
 * Returns shop base path.
 *
 * @return string
 */
function getShopBasePath()
{
    return realpath(dirname(__FILE__) . '/../../../../') . '/';
}

require_once getShopBasePath() . "/bootstrap.php";
ksort($_POST);
d3_cfg_mod::get('d3heidelpay')->d3getLog()->log(
    d3log::INFO,
    basename(__FILE__),
    'none',
    __LINE__,
    basename(__FILE__) . " got requested",
    var_export($_POST, true)
);

/** @var d3_hp_response $oResponse */
$oResponse = oxNew('d3_hp_response');
$sReturn   = $oResponse->init();
$urlParameter = $oResponse->d3GetHeidelpayURLParameter();
if ($sReturn !== d3_hp_response::REDIRECT && false === empty($urlParameter)) {
    $sReturn .= "&" . $urlParameter;
}

d3_cfg_mod::get('d3heidelpay')->d3getLog()->log(
    d3log::INFO,
    basename(__FILE__),
    'none',
    __LINE__,
    basename(__FILE__) . " return value",
    $sReturn . PHP_EOL . $oResponse->getRedirectUrl()
);

if ($sReturn === d3_hp_response::REDIRECT) {
    header("HTTP/1.1 200 OK");
    header("Location: " . $oResponse->getRedirectUrl());
    header("Connection: close");
} else {
    echo $sReturn;
}

oxRegistry::getConfig()->pageClose();
