1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Http;
9:
10: use Nette,
11: Nette\Utils\DateTime;
12:
13:
14: 15: 16: 17: 18:
19: class Helpers
20: {
21:
22: 23: 24: 25: 26:
27: public static function formatDate($time)
28: {
29: $time = DateTime::from($time);
30: $time->setTimezone(new \DateTimeZone('GMT'));
31: return $time->format('D, d M Y H:i:s \G\M\T');
32: }
33:
34:
35: 36: 37: 38:
39: public static function ipMatch($ip, $mask)
40: {
41: list($mask, $size) = explode('/', $mask . '/');
42: $ipv4 = strpos($ip, '.');
43: $max = $ipv4 ? 32 : 128;
44: if (($ipv4 xor strpos($mask, '.')) || $size < 0 || $size > $max) {
45: return FALSE;
46: } elseif ($ipv4) {
47: $arr = array(ip2long($ip), ip2long($mask));
48: } else {
49: $arr = unpack('N*', inet_pton($ip) . inet_pton($mask));
50: $size = $size === '' ? 0 : $max - $size;
51: }
52: $bits = implode('', array_map(function ($n) {
53: return sprintf('%032b', $n);
54: }, $arr));
55: return substr($bits, 0, $max - $size) === substr($bits, $max, $max - $size);
56: }
57:
58:
59: 60: 61: 62: 63:
64: public static function removeDuplicateCookies()
65: {
66: if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
67: return;
68: }
69:
70: $flatten = array();
71: foreach (headers_list() as $header) {
72: if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
73: $flatten[$m[0]] = $header;
74: header_remove('Set-Cookie');
75: }
76: }
77: foreach (array_values($flatten) as $key => $header) {
78: header($header, $key === 0);
79: }
80: }
81:
82:
83: 84: 85:
86: public static function stripSlashes($arr, $onlyKeys = FALSE)
87: {
88: $res = array();
89: foreach ($arr as $k => $v) {
90: $res[stripslashes($k)] = is_array($v)
91: ? self::stripSlashes($v, $onlyKeys)
92: : ($onlyKeys ? $v : stripslashes($v));
93: }
94: return $res;
95: }
96:
97: }
98: