<?php
declare(strict_types=1);

require_once __DIR__ . '/config.php';

if (!function_exists('str_starts_with')) {

function str_starts_with($haystack, $needle): bool
{
    $needle = (string) $needle;
    if ($needle === '') {
        return true;
    }

    return strncmp((string) $haystack, $needle, strlen($needle)) === 0;
}
}

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id < 1 || $id > 999999999) {
http_response_code(400);
header('Content-Type: text/plain; charset=utf-8');
echo 'Bad id';
exit;
}

function mocker_team_logo_is_binary_image(string $body): bool
{
$n = strlen($body);
if ($n < 24) {
    return false;
}
if (str_starts_with($body, "\x89PNG\r\n\x1a\n")) {
    return true;
}
if (str_starts_with($body, "\xff\xd8\xff")) {
    return true;
}
if (str_starts_with($body, 'GIF8')) {
    return true;
}
if (str_starts_with($body, 'RIFF') && $n > 12 && str_starts_with(substr($body, 8, 4), 'WEBP')) {
    return true;
}
if (str_starts_with($body, '<svg') || str_starts_with($body, "\xef\xbb\xbf<svg")) {
    return true;
}

return false;
}

function mocker_team_logo_send(string $body, string $contentType): void
{
$ct = $contentType !== '' && str_starts_with(strtolower($contentType), 'image/')
    ? $contentType
    : 'image/png';
header('Content-Type: ' . $ct);
header('Cache-Control: public, max-age=86400');
header('X-Content-Type-Options: nosniff');
echo $body;
exit;
}

function mocker_team_logo_curl_get(string $url, array $headers): array
{
if (!function_exists('curl_init')) {
    return ['code' => 0, 'body' => null, 'ctype' => ''];
}
$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_TIMEOUT => 20,
    CURLOPT_HTTPHEADER => $headers,
]);
$body = curl_exec($ch);
$code = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
$ctype = (string) curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);

return [
    'code' => $code,
    'body' => is_string($body) ? $body : null,
    'ctype' => $ctype,
];
}

$key = defined('SPORT_RAPIDAPI_KEY') ? (string) SPORT_RAPIDAPI_KEY : '';
$host = defined('SPORT_RAPIDAPI_HOST') ? trim((string) SPORT_RAPIDAPI_HOST) : '';
$tpl = defined('SPORT_TEAM_LOGO_PATH_TEMPLATE') ? (string) SPORT_TEAM_LOGO_PATH_TEMPLATE : '/api/v1/team/{id}/image';
$path = str_replace('{id}', (string) $id, $tpl);
if (!str_starts_with($path, '/')) {
$path = '/' . $path;
}

if ($key !== '' && $host !== '') {
$url = 'https://' . $host . $path;
$r = mocker_team_logo_curl_get($url, [
    'Accept: image/avif,image/webp,image/apng,image/png,image/svg+xml,image/*,*/*;q=0.8',
    'x-rapidapi-key: ' . $key,
    'x-rapidapi-host: ' . $host,
]);
if ($r['code'] === 200 && $r['body'] !== null && mocker_team_logo_is_binary_image($r['body'])) {
    mocker_team_logo_send($r['body'], $r['ctype']);
}
}

$sofa = 'https://img.sofascore.com/img/team/logos/normal/' . $id . '.png';
$r2 = mocker_team_logo_curl_get($sofa, [
'Accept: image/avif,image/webp,image/png,image/*,*/*;q=0.8',
'Referer: https://www.sofascore.com/',
'Origin: https://www.sofascore.com',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
]);
if ($r2['code'] === 200 && $r2['body'] !== null && mocker_team_logo_is_binary_image($r2['body'])) {
mocker_team_logo_send($r2['body'], $r2['ctype']);
}

$sofa2 = 'https://img.sofascore.com/api/v1/team/' . $id . '/image';
$r3 = mocker_team_logo_curl_get($sofa2, [
'Accept: image/*',
'Referer: https://www.sofascore.com/',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/122.0.0.0 Safari/537.36',
]);
if ($r3['code'] === 200 && $r3['body'] !== null && mocker_team_logo_is_binary_image($r3['body'])) {
mocker_team_logo_send($r3['body'], $r3['ctype']);
}

http_response_code(404);
header('Content-Type: image/svg+xml; charset=utf-8');
header('Cache-Control: public, max-age=300');
echo '<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="0 0 64 64"><rect fill="%23cbd5e1" width="64" height="64" rx="10"/></svg>';
