<?php

declare(strict_types=1);

// ===== Settings per partner =====
const BASE_URL = 'https://www.example.com';
const COOKIE_DOMAIN = 'example.com';
const IDENT = 'example';
// ===== Settings per partner =====

const REQUEST_TIMEOUT_MS = 2000;
const COOKIE_URL = 'https://hit.skrz.cz/cookie';
const COOKIE_NAME = 'skrzcc3';

function redirectAndExit(string $relativeUrl): void
{
	header('Location: ' . BASE_URL . $relativeUrl);
	exit;
}

// ===== Validate inputs =====

$skrzData = $_GET['skrz'] ?? null;
$detailUrl = $_GET['detail'] ?? null;
$cookieValue = $_COOKIE[COOKIE_NAME] ?? null;

if ($detailUrl === null) {
	http_response_code(400);
	exit;
}

if ($skrzData === null) {
	redirectAndExit($detailUrl);
}

// ===== Get new cookie value from skrz.cz =====

$query = http_build_query([
	'ident' => IDENT,
	'skrz' => $skrzData,
	'current' => $cookieValue,
]);

$cookieUrl = COOKIE_URL . '?' . $query;

$request = curl_init($cookieUrl);

if ($request === false) {
	redirectAndExit($detailUrl);
}

curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_TIMEOUT_MS, REQUEST_TIMEOUT_MS);

$response = curl_exec($request);

// ===== Validate response from skrz.cz =====

if (!is_string($response)) {
	redirectAndExit($detailUrl);
}

$json = json_decode($response, true);

if (!is_array($json)) {
	redirectAndExit($detailUrl);
}

$cookieName = $json['cookieName'] ?? null;
$cookieValue = $json['value'] ?? null;
$expiration = $json['expiration'] ?? null;

if ($cookieName === null || $cookieValue === null || $expiration === null) {
	redirectAndExit($detailUrl);
}

// ===== Set new cookie and redirect to detail page =====

setcookie($cookieName, $cookieValue, $expiration, '/', COOKIE_DOMAIN, true);

redirectAndExit($detailUrl);

Skrz.cz s.r.o. (2024) Všeobecné obchodní podmínky | Pravidla pro zveřejňování nabídek