1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Http;
9:
10: use Nette;
11: use Nette\Utils\DateTime;
12:
13:
14: 15: 16:
17: class Helpers
18: {
19: use Nette\StaticClass;
20:
21: 22: 23: 24: 25:
26: public static function formatDate($time)
27: {
28: $time = DateTime::from($time);
29: $time->setTimezone(new \DateTimeZone('GMT'));
30: return $time->format('D, d M Y H:i:s \G\M\T');
31: }
32:
33:
34: 35: 36: 37:
38: public static function ipMatch($ip, $mask)
39: {
40: list($mask, $size) = explode('/', $mask . '/');
41: $tmp = function ($n) { return sprintf('%032b', $n); };
42: $ip = implode('', array_map($tmp, unpack('N*', inet_pton($ip))));
43: $mask = implode('', array_map($tmp, unpack('N*', inet_pton($mask))));
44: $max = strlen($ip);
45: if (!$max || $max !== strlen($mask) || (int) $size < 0 || (int) $size > $max) {
46: return false;
47: }
48: return strncmp($ip, $mask, $size === '' ? $max : (int) $size) === 0;
49: }
50:
51:
52: 53: 54: 55: 56:
57: public static function removeDuplicateCookies()
58: {
59: if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
60: return;
61: }
62:
63: $flatten = [];
64: foreach (headers_list() as $header) {
65: if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
66: $flatten[$m[0]] = $header;
67: header_remove('Set-Cookie');
68: }
69: }
70: foreach (array_values($flatten) as $key => $header) {
71: header($header, $key === 0);
72: }
73: }
74: }
75: