'amazon.com.au', 'BE' => 'amazon.com.be', 'BR' => 'amazon.com.br', 'CA' => 'amazon.ca', 'DE' => 'amazon.de', 'ES' => 'amazon.es', 'FR' => 'amazon.fr', 'GB' => 'amazon.co.uk', 'IE' => 'amazon.ie', 'IN' => 'amazon.in', 'IT' => 'amazon.it', 'JP' => 'amazon.co.jp', 'MX' => 'amazon.com.mx', 'NL' => 'amazon.nl', 'PL' => 'amazon.pl', 'SE' => 'amazon.se', 'US' => 'amazon.com', ]; const CLOUDFLARE_NETWORKS = [ '173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', '103.31.4.0/22', '141.101.64.0/18', '108.162.192.0/18', '190.93.240.0/20', '188.114.96.0/20', '197.234.240.0/22', '198.41.128.0/17', '162.158.0.0/15', '104.16.0.0/13', '104.24.0.0/14', '172.64.0.0/13', '131.0.72.0/22', '2400:cb00::/32', '2606:4700::/32', '2803:f800::/32', '2405:b500::/32', '2405:8100::/32', '2a06:98c0::/29', '2c0f:f248::/32', ]; function normalize_asin(string $asin): ?string { return preg_match('/\A[A-Z0-9]{10}\z/i', $asin) === 1 ? strtoupper($asin) : null; } /** Return the components of an eligible product URL. */ function product_url(string $url): ?array { if (preg_match('/[\x00-\x20\x7f]/', $url) || str_contains($url, '\\')) { return null; } $parts = parse_url($url); if ($parts === false || !in_array(strtolower($parts['scheme'] ?? ''), ['http', 'https'], true) || isset($parts['user']) || isset($parts['pass']) || isset($parts['port'])) { return null; } $host = preg_replace('/^(?:www|m)\./', '', strtolower($parts['host'] ?? '')); $path = $parts['path'] ?? ''; if (!in_array($host, MARKETPLACES, true) || !preg_match('~\A/(?:[^/]+/)?dp/([a-z0-9]{10})(?:/|\z)|\A/gp/(?:product|aw/d)/([a-z0-9]{10})(?:/|\z)~i', $path, $matches) || preg_match('~(?:^|/)(?:offer-listing(?:/|$)|ref=[^/]*cx_skuctr_share|[^/]*[?&](?:smid|seller|merchant|m|me)=)~i', rawurldecode($path))) { return null; } $parameters = []; foreach (explode('&', $parts['query'] ?? '') as $parameter) { if ($parameter === '') { continue; } $fields = explode('=', $parameter, 2); $name = urldecode($fields[0]); if (preg_match('/\A(?:smid|seller|sellerid|merchant|merchantid|m|me|offerlistingid)(?:\[|\z)/i', $name) || (strcasecmp($name, 'ref') === 0 && str_starts_with(strtolower(urldecode($fields[1] ?? '')), 'cx_skuctr_share'))) { return null; } $parameters[$name] ??= $parameter; } return [ 'host' => $host, 'asin' => strtoupper(($matches[1] ?? '') ?: $matches[2]), 'query' => implode('&', $parameters), 'fragment' => $parts['fragment'] ?? null, ]; } function marketplace_url(string $original, array $product, ?string $country): string { $marketplace = MARKETPLACES[$country] ?? null; if ($marketplace === null || $marketplace === $product['host']) { return $original; } $url = 'https://www.' . $marketplace . '/dp/' . $product['asin']; if ($product['query'] !== '') { $url .= '?' . $product['query']; } if ($product['fragment'] !== null) { $url .= '#' . $product['fragment']; } return $url; } function visitor_country(string $peer, string $country): ?string { $country = strtoupper($country); if (!isset(MARKETPLACES[$country]) || !filter_var($peer, FILTER_VALIDATE_IP)) { return null; } $address = inet_pton($peer); if (str_starts_with($address, str_repeat("\0", 10) . "\xff\xff")) { $address = substr($address, 12); } foreach (CLOUDFLARE_NETWORKS as $cidr) { [$network, $prefix] = explode('/', $cidr); $network = inet_pton($network); if (strlen($address) !== strlen($network)) { continue; } $bytes = intdiv((int) $prefix, 8); $remaining = (int) $prefix % 8; if (substr($address, 0, $bytes) === substr($network, 0, $bytes) && ($remaining === 0 || ((ord($address[$bytes]) ^ ord($network[$bytes])) >> (8 - $remaining)) === 0)) { return $country; } } return null; }