PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / deps / nette / http / src / Http / Helpers.php

Helpers.php in Packeta trunk, at deps/nette/http/src/Http/Helpers.php

53 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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