| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Http; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
use Packetery\Nette\Utils\DateTime; |
| 12 |
/** |
| 13 |
* Rendering helpers for HTTP. |
| 14 |
*/ |
| 15 |
final class Helpers |
| 16 |
{ |
| 17 |
use \Packetery\Nette\StaticClass; |
| 18 |
/** @internal */ |
| 19 |
public const StrictCookieName = '_nss'; |
| 20 |
/** @deprecated */ |
| 21 |
public const STRICT_COOKIE_NAME = self::StrictCookieName; |
| 22 |
/** |
| 23 |
* Returns HTTP valid date format. |
| 24 |
* @param string|int|\DateTimeInterface $time |
| 25 |
*/ |
| 26 |
public static function formatDate($time) : string |
| 27 |
{ |
| 28 |
$time = DateTime::from($time)->setTimezone(new \DateTimeZone('GMT')); |
| 29 |
return $time->format('D, d M Y H:i:s \\G\\M\\T'); |
| 30 |
} |
| 31 |
/** |
| 32 |
* Is IP address in CIDR block? |
| 33 |
*/ |
| 34 |
public static function ipMatch(string $ip, string $mask) : bool |
| 35 |
{ |
| 36 |
[$mask, $size] = \explode('/', $mask . '/'); |
| 37 |
$tmp = function (int $n) : string { |
| 38 |
return \sprintf('%032b', $n); |
| 39 |
}; |
| 40 |
$ip = \implode('', \array_map($tmp, \unpack('N*', \inet_pton($ip)))); |
| 41 |
$mask = \implode('', \array_map($tmp, \unpack('N*', \inet_pton($mask)))); |
| 42 |
$max = \strlen($ip); |
| 43 |
if (!$max || $max !== \strlen($mask) || (int) $size < 0 || (int) $size > $max) { |
| 44 |
return \false; |
| 45 |
} |
| 46 |
return \strncmp($ip, $mask, $size === '' ? $max : (int) $size) === 0; |
| 47 |
} |
| 48 |
public static function initCookie(IRequest $request, IResponse $response) |
| 49 |
{ |
| 50 |
$response->setCookie(self::StrictCookieName, '1', 0, '/', null, null, \true, IResponse::SameSiteStrict); |
| 51 |
} |
| 52 |
} |
| 53 |
|