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:
20: 21: 22: 23: 24:
25: public static function formatDate($time)
26: {
27: $time = DateTime::from($time);
28: $time->setTimezone(new \DateTimeZone('GMT'));
29: return $time->format('D, d M Y H:i:s \G\M\T');
30: }
31:
32:
33: 34: 35: 36:
37: public static function ipMatch($ip, $mask)
38: {
39: list($mask, $size) = explode('/', $mask . '/');
40: $tmp = function ($n) { return sprintf('%032b', $n); };
41: $ip = implode('', array_map($tmp, unpack('N*', inet_pton($ip))));
42: $mask = implode('', array_map($tmp, unpack('N*', inet_pton($mask))));
43: $max = strlen($ip);
44: if (!$max || $max !== strlen($mask) || $size < 0 || $size > $max) {
45: return FALSE;
46: }
47: return strncmp($ip, $mask, $size === '' ? $max : $size) === 0;
48: }
49:
50:
51: 52: 53: 54: 55:
56: public static function removeDuplicateCookies()
57: {
58: if (headers_sent($file, $line) || ini_get('suhosin.cookie.encrypt')) {
59: return;
60: }
61:
62: $flatten = array();
63: foreach (headers_list() as $header) {
64: if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
65: $flatten[$m[0]] = $header;
66: header_remove('Set-Cookie');
67: }
68: }
69: foreach (array_values($flatten) as $key => $header) {
70: header($header, $key === 0);
71: }
72: }
73:
74:
75: 76: 77:
78: public static function stripSlashes($arr, $onlyKeys = FALSE)
79: {
80: $res = array();
81: foreach ($arr as $k => $v) {
82: $res[stripslashes($k)] = is_array($v)
83: ? self::stripSlashes($v, $onlyKeys)
84: : ($onlyKeys ? $v : stripslashes($v));
85: }
86: return $res;
87: }
88:
89: }
90: