@@ -11,151 +11,151 @@ |
||
11 | 11 | */ |
12 | 12 | |
13 | 13 | class Hash { |
14 | - use Module; |
|
14 | + use Module; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Create ah hash for payload |
|
18 | - * @param mixed $payload The payload string/object/array |
|
19 | - * @param integer $method The hashing method, default is "md5" |
|
20 | - * @return string The hash string |
|
21 | - */ |
|
22 | - public static function make($payload, $method = 'md5') { |
|
23 | - return hash($method, serialize($payload)); |
|
24 | - } |
|
16 | + /** |
|
17 | + * Create ah hash for payload |
|
18 | + * @param mixed $payload The payload string/object/array |
|
19 | + * @param integer $method The hashing method, default is "md5" |
|
20 | + * @return string The hash string |
|
21 | + */ |
|
22 | + public static function make($payload, $method = 'md5') { |
|
23 | + return hash($method, serialize($payload)); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Verify if given payload matches hash |
|
28 | - * @param mixed $payload The payload string/object/array |
|
29 | - * @param string $hash The hash string |
|
30 | - * @param integer $method The hashing method |
|
31 | - * @return bool Returns `true` if payload matches hash |
|
32 | - */ |
|
33 | - public static function verify($payload, $hash, $method = 'md5') { |
|
34 | - return static::make($payload, $method) == $hash; |
|
35 | - } |
|
26 | + /** |
|
27 | + * Verify if given payload matches hash |
|
28 | + * @param mixed $payload The payload string/object/array |
|
29 | + * @param string $hash The hash string |
|
30 | + * @param integer $method The hashing method |
|
31 | + * @return bool Returns `true` if payload matches hash |
|
32 | + */ |
|
33 | + public static function verify($payload, $hash, $method = 'md5') { |
|
34 | + return static::make($payload, $method) == $hash; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * List registered hashing algorithms |
|
39 | - * |
|
40 | - * @method methods |
|
41 | - * |
|
42 | - * @return array Array containing the list of supported hashing algorithms. |
|
43 | - */ |
|
44 | - public static function methods() { |
|
45 | - return hash_algos(); |
|
46 | - } |
|
37 | + /** |
|
38 | + * List registered hashing algorithms |
|
39 | + * |
|
40 | + * @method methods |
|
41 | + * |
|
42 | + * @return array Array containing the list of supported hashing algorithms. |
|
43 | + */ |
|
44 | + public static function methods() { |
|
45 | + return hash_algos(); |
|
46 | + } |
|
47 | 47 | |
48 | - /** |
|
49 | - * Check if an alghoritm is registered in current PHP |
|
50 | - * |
|
51 | - * @method can |
|
52 | - * |
|
53 | - * @param string $algo The hashing algorithm name |
|
54 | - * |
|
55 | - * @return bool |
|
56 | - */ |
|
57 | - public static function can($algo) { |
|
58 | - return in_array($algo, hash_algos()); |
|
59 | - } |
|
48 | + /** |
|
49 | + * Check if an alghoritm is registered in current PHP |
|
50 | + * |
|
51 | + * @method can |
|
52 | + * |
|
53 | + * @param string $algo The hashing algorithm name |
|
54 | + * |
|
55 | + * @return bool |
|
56 | + */ |
|
57 | + public static function can($algo) { |
|
58 | + return in_array($algo, hash_algos()); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Static magic for creating hashes with a specified algorithm. |
|
63 | - * |
|
64 | - * See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms |
|
65 | - */ |
|
66 | - public static function __callStatic($method, $params) { |
|
67 | - return self::make(current($params), $method); |
|
68 | - } |
|
61 | + /** |
|
62 | + * Static magic for creating hashes with a specified algorithm. |
|
63 | + * |
|
64 | + * See [hash-algos](http://php.net/manual/it/function.hash-algos.php) for a list of algorithms |
|
65 | + */ |
|
66 | + public static function __callStatic($method, $params) { |
|
67 | + return self::make(current($params), $method); |
|
68 | + } |
|
69 | 69 | |
70 | - public static function uuid($type = 4, $namespace = '', $name = '') { |
|
71 | - switch ($type) { |
|
72 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
73 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
74 | - return false; |
|
75 | - } |
|
70 | + public static function uuid($type = 4, $namespace = '', $name = '') { |
|
71 | + switch ($type) { |
|
72 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
73 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
74 | + return false; |
|
75 | + } |
|
76 | 76 | |
77 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
78 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
79 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
80 | - } |
|
77 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
78 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
79 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
80 | + } |
|
81 | 81 | |
82 | - $hash = md5($nstr . $name); |
|
83 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
84 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
85 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
86 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
87 | - substr($hash, 20, 12)); |
|
88 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
89 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
90 | - return false; |
|
91 | - } |
|
82 | + $hash = md5($nstr . $name); |
|
83 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
84 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
85 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
86 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
87 | + substr($hash, 20, 12)); |
|
88 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
89 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
90 | + return false; |
|
91 | + } |
|
92 | 92 | |
93 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
94 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
95 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
96 | - } |
|
93 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
94 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
95 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
96 | + } |
|
97 | 97 | |
98 | - $hash = sha1($nstr . $name); |
|
99 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
100 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
101 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
102 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
103 | - substr($hash, 20, 12)); |
|
104 | - default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
105 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
106 | - mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
107 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
108 | - } |
|
109 | - } |
|
98 | + $hash = sha1($nstr . $name); |
|
99 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
100 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
101 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
102 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
103 | + substr($hash, 20, 12)); |
|
104 | + default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
105 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
106 | + mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
107 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
108 | + } |
|
109 | + } |
|
110 | 110 | |
111 | - public static function murmurhash3_int($key, $seed = 0) { |
|
112 | - $key = (string) $key; |
|
113 | - $klen = strlen($key); |
|
114 | - $h1 = $seed; |
|
115 | - for ($i = 0, $bytes = $klen - ($remainder = $klen & 3); $i < $bytes;) { |
|
116 | - $k1 = ((ord($key[$i]) & 0xff)) |
|
117 | - | ((ord($key[++$i]) & 0xff) << 8) |
|
118 | - | ((ord($key[++$i]) & 0xff) << 16) |
|
119 | - | ((ord($key[++$i]) & 0xff) << 24); |
|
120 | - ++$i; |
|
121 | - $k1 = (((($k1 & 0xffff) * 0xcc9e2d51) |
|
122 | - + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) * 0xcc9e2d51) & 0xffff) << 16))) |
|
123 | - & 0xffffffff; |
|
124 | - $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
125 | - $k1 = (((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
126 | - * 0x1b873593) & 0xffff) << 16))) & 0xffffffff; |
|
127 | - $h1 ^= $k1; |
|
128 | - $h1 = $h1 << 13 | ($h1 >= 0 ? $h1 >> 19 : (($h1 & 0x7fffffff) >> 19) | 0x1000); |
|
129 | - $h1b = (((($h1 & 0xffff) * 5) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 5) |
|
130 | - & 0xffff) << 16))) & 0xffffffff; |
|
131 | - $h1 = ((($h1b & 0xffff) + 0x6b64) + ((((($h1b >= 0 ? $h1b >> 16 : (($h1b & 0x7fffffff) >> 16) | 0x8000)) |
|
132 | - + 0xe654) & 0xffff) << 16)); |
|
133 | - } |
|
134 | - $k1 = 0; |
|
135 | - switch ($remainder) { |
|
136 | - case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
|
137 | - case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
|
138 | - case 1:$k1 ^= (ord($key[$i]) & 0xff); |
|
139 | - $k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
140 | - * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
|
141 | - $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
142 | - $k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
143 | - * 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
|
144 | - $h1 ^= $k1; |
|
145 | - } |
|
146 | - $h1 ^= $klen; |
|
147 | - $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
|
148 | - $h1 = ((($h1 & 0xffff) * 0x85ebca6b) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
|
149 | - * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff; |
|
150 | - $h1 ^= ($h1 >= 0 ? $h1 >> 13 : (($h1 & 0x7fffffff) >> 13) | 0x40000); |
|
151 | - $h1 = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
|
152 | - * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; |
|
153 | - $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
|
154 | - return $h1; |
|
155 | - } |
|
111 | + public static function murmurhash3_int($key, $seed = 0) { |
|
112 | + $key = (string) $key; |
|
113 | + $klen = strlen($key); |
|
114 | + $h1 = $seed; |
|
115 | + for ($i = 0, $bytes = $klen - ($remainder = $klen & 3); $i < $bytes;) { |
|
116 | + $k1 = ((ord($key[$i]) & 0xff)) |
|
117 | + | ((ord($key[++$i]) & 0xff) << 8) |
|
118 | + | ((ord($key[++$i]) & 0xff) << 16) |
|
119 | + | ((ord($key[++$i]) & 0xff) << 24); |
|
120 | + ++$i; |
|
121 | + $k1 = (((($k1 & 0xffff) * 0xcc9e2d51) |
|
122 | + + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) * 0xcc9e2d51) & 0xffff) << 16))) |
|
123 | + & 0xffffffff; |
|
124 | + $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
125 | + $k1 = (((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
126 | + * 0x1b873593) & 0xffff) << 16))) & 0xffffffff; |
|
127 | + $h1 ^= $k1; |
|
128 | + $h1 = $h1 << 13 | ($h1 >= 0 ? $h1 >> 19 : (($h1 & 0x7fffffff) >> 19) | 0x1000); |
|
129 | + $h1b = (((($h1 & 0xffff) * 5) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) * 5) |
|
130 | + & 0xffff) << 16))) & 0xffffffff; |
|
131 | + $h1 = ((($h1b & 0xffff) + 0x6b64) + ((((($h1b >= 0 ? $h1b >> 16 : (($h1b & 0x7fffffff) >> 16) | 0x8000)) |
|
132 | + + 0xe654) & 0xffff) << 16)); |
|
133 | + } |
|
134 | + $k1 = 0; |
|
135 | + switch ($remainder) { |
|
136 | + case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
|
137 | + case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
|
138 | + case 1:$k1 ^= (ord($key[$i]) & 0xff); |
|
139 | + $k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
140 | + * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
|
141 | + $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
142 | + $k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
143 | + * 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
|
144 | + $h1 ^= $k1; |
|
145 | + } |
|
146 | + $h1 ^= $klen; |
|
147 | + $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
|
148 | + $h1 = ((($h1 & 0xffff) * 0x85ebca6b) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
|
149 | + * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff; |
|
150 | + $h1 ^= ($h1 >= 0 ? $h1 >> 13 : (($h1 & 0x7fffffff) >> 13) | 0x40000); |
|
151 | + $h1 = (((($h1 & 0xffff) * 0xc2b2ae35) + ((((($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000)) |
|
152 | + * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff; |
|
153 | + $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
|
154 | + return $h1; |
|
155 | + } |
|
156 | 156 | |
157 | - public static function murmurhash3($key, $seed = 0) { |
|
158 | - return base_convert(static::murmurhash3_int($key, $seed), 10, 32); |
|
159 | - } |
|
157 | + public static function murmurhash3($key, $seed = 0) { |
|
158 | + return base_convert(static::murmurhash3_int($key, $seed), 10, 32); |
|
159 | + } |
|
160 | 160 | |
161 | 161 | } |
@@ -69,42 +69,42 @@ discard block |
||
69 | 69 | |
70 | 70 | public static function uuid($type = 4, $namespace = '', $name = '') { |
71 | 71 | switch ($type) { |
72 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
73 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
74 | - return false; |
|
75 | - } |
|
72 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
73 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
74 | + return false; |
|
75 | + } |
|
76 | 76 | |
77 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
78 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
79 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
80 | - } |
|
77 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
78 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
79 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
80 | + } |
|
81 | 81 | |
82 | - $hash = md5($nstr . $name); |
|
83 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
84 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
85 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
86 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
87 | - substr($hash, 20, 12)); |
|
88 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
89 | - '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
90 | - return false; |
|
91 | - } |
|
82 | + $hash = md5($nstr . $name); |
|
83 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
84 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
85 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
|
86 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
87 | + substr($hash, 20, 12)); |
|
88 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
89 | + '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
|
90 | + return false; |
|
91 | + } |
|
92 | 92 | |
93 | - $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
94 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
95 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
96 | - } |
|
93 | + $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
|
94 | + $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
95 | + $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
96 | + } |
|
97 | 97 | |
98 | - $hash = sha1($nstr . $name); |
|
99 | - return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
100 | - substr($hash, 0, 8), substr($hash, 8, 4), |
|
101 | - (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
102 | - (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
103 | - substr($hash, 20, 12)); |
|
104 | - default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
105 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
106 | - mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
107 | - mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
98 | + $hash = sha1($nstr . $name); |
|
99 | + return sprintf('%08s-%04s-%04x-%04x-%12s', |
|
100 | + substr($hash, 0, 8), substr($hash, 8, 4), |
|
101 | + (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
|
102 | + (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
|
103 | + substr($hash, 20, 12)); |
|
104 | + default:case 4:return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', |
|
105 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff), |
|
106 | + mt_rand(0, 0x0fff) | 0x4000, mt_rand(0, 0x3fff) | 0x8000, |
|
107 | + mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)); |
|
108 | 108 | } |
109 | 109 | } |
110 | 110 | |
@@ -133,15 +133,15 @@ discard block |
||
133 | 133 | } |
134 | 134 | $k1 = 0; |
135 | 135 | switch ($remainder) { |
136 | - case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
|
137 | - case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
|
138 | - case 1:$k1 ^= (ord($key[$i]) & 0xff); |
|
139 | - $k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
140 | - * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
|
141 | - $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
142 | - $k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
143 | - * 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
|
144 | - $h1 ^= $k1; |
|
136 | + case 3:$k1 ^= (ord($key[$i + 2]) & 0xff) << 16; |
|
137 | + case 2:$k1 ^= (ord($key[$i + 1]) & 0xff) << 8; |
|
138 | + case 1:$k1 ^= (ord($key[$i]) & 0xff); |
|
139 | + $k1 = ((($k1 & 0xffff) * 0xcc9e2d51) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
140 | + * 0xcc9e2d51) & 0xffff) << 16)) & 0xffffffff; |
|
141 | + $k1 = $k1 << 15 | ($k1 >= 0 ? $k1 >> 17 : (($k1 & 0x7fffffff) >> 17) | 0x4000); |
|
142 | + $k1 = ((($k1 & 0xffff) * 0x1b873593) + ((((($k1 >= 0 ? $k1 >> 16 : (($k1 & 0x7fffffff) >> 16) | 0x8000)) |
|
143 | + * 0x1b873593) & 0xffff) << 16)) & 0xffffffff; |
|
144 | + $h1 ^= $k1; |
|
145 | 145 | } |
146 | 146 | $h1 ^= $klen; |
147 | 147 | $h1 ^= ($h1 >= 0 ? $h1 >> 16 : (($h1 & 0x7fffffff) >> 16) | 0x8000); |
@@ -69,33 +69,33 @@ discard block |
||
69 | 69 | |
70 | 70 | public static function uuid($type = 4, $namespace = '', $name = '') { |
71 | 71 | switch ($type) { |
72 | - case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
72 | + case 3:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. |
|
73 | 73 | '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
74 | 74 | return false; |
75 | 75 | } |
76 | 76 | |
77 | 77 | $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
78 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
79 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
78 | + $nstr = '';for ($i = 0;$i < strlen($nhex);$i += 2) { |
|
79 | + $nstr .= chr(hexdec($nhex[$i].$nhex[$i + 1])); |
|
80 | 80 | } |
81 | 81 | |
82 | - $hash = md5($nstr . $name); |
|
82 | + $hash = md5($nstr.$name); |
|
83 | 83 | return sprintf('%08s-%04s-%04x-%04x-%12s', |
84 | 84 | substr($hash, 0, 8), substr($hash, 8, 4), |
85 | 85 | (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000, |
86 | 86 | (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000, |
87 | 87 | substr($hash, 20, 12)); |
88 | - case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?' . |
|
88 | + case 5:if (preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'. |
|
89 | 89 | '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/Si', $namespace) !== 1) { |
90 | 90 | return false; |
91 | 91 | } |
92 | 92 | |
93 | 93 | $nhex = str_replace(array('-', '{', '}'), '', $namespace); |
94 | - $nstr = '';for ($i = 0; $i < strlen($nhex); $i += 2) { |
|
95 | - $nstr .= chr(hexdec($nhex[$i] . $nhex[$i + 1])); |
|
94 | + $nstr = '';for ($i = 0;$i < strlen($nhex);$i += 2) { |
|
95 | + $nstr .= chr(hexdec($nhex[$i].$nhex[$i + 1])); |
|
96 | 96 | } |
97 | 97 | |
98 | - $hash = sha1($nstr . $name); |
|
98 | + $hash = sha1($nstr.$name); |
|
99 | 99 | return sprintf('%08s-%04s-%04x-%04x-%12s', |
100 | 100 | substr($hash, 0, 8), substr($hash, 8, 4), |
101 | 101 | (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000, |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | $key = (string) $key; |
113 | 113 | $klen = strlen($key); |
114 | 114 | $h1 = $seed; |
115 | - for ($i = 0, $bytes = $klen - ($remainder = $klen & 3); $i < $bytes;) { |
|
115 | + for ($i = 0, $bytes = $klen - ($remainder = $klen & 3);$i < $bytes;) { |
|
116 | 116 | $k1 = ((ord($key[$i]) & 0xff)) |
117 | 117 | | ((ord($key[++$i]) & 0xff) << 8) |
118 | 118 | | ((ord($key[++$i]) & 0xff) << 16) |
@@ -19,50 +19,50 @@ |
||
19 | 19 | $fileCache; |
20 | 20 | |
21 | 21 | public function __construct(array $options = []) { |
22 | - $this->path = empty($options['root'])?(tempnam(sys_get_temp_dir(), 'CFZ_').'.zip'):rtrim($options['root']); |
|
22 | + $this->path = empty($options['root']) ? (tempnam(sys_get_temp_dir(), 'CFZ_').'.zip') : rtrim($options['root']); |
|
23 | 23 | $this->zipfile = new \ZipArchive(); |
24 | - if ( !$this->zipfile->open($this->path, \ZipArchive::CREATE) ){ |
|
24 | + if (!$this->zipfile->open($this->path, \ZipArchive::CREATE)) { |
|
25 | 25 | throw new \Exception("File::ZIP Cannot open or create ".$this->path); |
26 | 26 | } |
27 | 27 | } |
28 | 28 | |
29 | - public function exists($path){ |
|
29 | + public function exists($path) { |
|
30 | 30 | return false !== $this->zipfile->locateName($path); |
31 | 31 | } |
32 | 32 | |
33 | - public function read($path){ |
|
33 | + public function read($path) { |
|
34 | 34 | if (isset($this->fileCache[$path])) return $this->fileCache[$path]; |
35 | 35 | return $this->exists($path) ? $this->zipfile->getFromName($path) : false; |
36 | 36 | } |
37 | 37 | |
38 | - public function write($path, $data){ |
|
38 | + public function write($path, $data) { |
|
39 | 39 | // This is needed because we cant write and read from the same archive. |
40 | 40 | $this->fileCache[$path] = $data; |
41 | 41 | return $this->zipfile->addFromString($path, $data); |
42 | 42 | } |
43 | 43 | |
44 | - public function append($path, $data){ |
|
45 | - return $this->write($path, ($this->read($path) ?: '') . $data); |
|
44 | + public function append($path, $data) { |
|
45 | + return $this->write($path, ($this->read($path) ?: '').$data); |
|
46 | 46 | } |
47 | 47 | |
48 | - public function delete($path){ |
|
48 | + public function delete($path) { |
|
49 | 49 | return $this->exists($path) ? $this->zipfile->deleteName($path) : false; |
50 | 50 | } |
51 | 51 | |
52 | - public function move($old, $new){ |
|
52 | + public function move($old, $new) { |
|
53 | 53 | // Atomic rename |
54 | 54 | // This is needed because we cant write and read from the same archive. |
55 | - return $this->write($new,$this->read($old)) && $this->delete($old); |
|
55 | + return $this->write($new, $this->read($old)) && $this->delete($old); |
|
56 | 56 | // return $this->zipfile->renameName($old, $new); |
57 | 57 | } |
58 | 58 | |
59 | - public function search($pattern, $recursive=true){ |
|
59 | + public function search($pattern, $recursive = true) { |
|
60 | 60 | $results = []; |
61 | - $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
|
61 | + $rx_pattern = '('.strtr($pattern, ['.'=>'\.', '*'=>'.*', '?'=>'.']).')Ai'; |
|
62 | 62 | |
63 | - for( $i = 0, $c = $this->zipfile->numFiles; $i < $c; $i++ ){ |
|
64 | - $stat = $this->zipfile->statIndex( $i ); |
|
65 | - if (preg_match($rx_pattern,$stat['name'])) $results[] = $stat['name']; |
|
63 | + for ($i = 0, $c = $this->zipfile->numFiles;$i < $c;$i++) { |
|
64 | + $stat = $this->zipfile->statIndex($i); |
|
65 | + if (preg_match($rx_pattern, $stat['name'])) $results[] = $stat['name']; |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | return $results; |
@@ -21,48 +21,52 @@ |
||
21 | 21 | public function __construct(array $options = []) { |
22 | 22 | $this->path = empty($options['root'])?(tempnam(sys_get_temp_dir(), 'CFZ_').'.zip'):rtrim($options['root']); |
23 | 23 | $this->zipfile = new \ZipArchive(); |
24 | - if ( !$this->zipfile->open($this->path, \ZipArchive::CREATE) ){ |
|
24 | + if ( !$this->zipfile->open($this->path, \ZipArchive::CREATE) ) { |
|
25 | 25 | throw new \Exception("File::ZIP Cannot open or create ".$this->path); |
26 | 26 | } |
27 | 27 | } |
28 | 28 | |
29 | - public function exists($path){ |
|
29 | + public function exists($path) { |
|
30 | 30 | return false !== $this->zipfile->locateName($path); |
31 | 31 | } |
32 | 32 | |
33 | - public function read($path){ |
|
34 | - if (isset($this->fileCache[$path])) return $this->fileCache[$path]; |
|
33 | + public function read($path) { |
|
34 | + if (isset($this->fileCache[$path])) { |
|
35 | + return $this->fileCache[$path]; |
|
36 | + } |
|
35 | 37 | return $this->exists($path) ? $this->zipfile->getFromName($path) : false; |
36 | 38 | } |
37 | 39 | |
38 | - public function write($path, $data){ |
|
40 | + public function write($path, $data) { |
|
39 | 41 | // This is needed because we cant write and read from the same archive. |
40 | 42 | $this->fileCache[$path] = $data; |
41 | 43 | return $this->zipfile->addFromString($path, $data); |
42 | 44 | } |
43 | 45 | |
44 | - public function append($path, $data){ |
|
46 | + public function append($path, $data) { |
|
45 | 47 | return $this->write($path, ($this->read($path) ?: '') . $data); |
46 | 48 | } |
47 | 49 | |
48 | - public function delete($path){ |
|
50 | + public function delete($path) { |
|
49 | 51 | return $this->exists($path) ? $this->zipfile->deleteName($path) : false; |
50 | 52 | } |
51 | 53 | |
52 | - public function move($old, $new){ |
|
54 | + public function move($old, $new) { |
|
53 | 55 | // Atomic rename |
54 | 56 | // This is needed because we cant write and read from the same archive. |
55 | 57 | return $this->write($new,$this->read($old)) && $this->delete($old); |
56 | 58 | // return $this->zipfile->renameName($old, $new); |
57 | 59 | } |
58 | 60 | |
59 | - public function search($pattern, $recursive=true){ |
|
61 | + public function search($pattern, $recursive=true) { |
|
60 | 62 | $results = []; |
61 | 63 | $rx_pattern = '('.strtr($pattern,['.'=>'\.','*'=>'.*','?'=>'.']).')Ai'; |
62 | 64 | |
63 | - for( $i = 0, $c = $this->zipfile->numFiles; $i < $c; $i++ ){ |
|
65 | + for( $i = 0, $c = $this->zipfile->numFiles; $i < $c; $i++ ) { |
|
64 | 66 | $stat = $this->zipfile->statIndex( $i ); |
65 | - if (preg_match($rx_pattern,$stat['name'])) $results[] = $stat['name']; |
|
67 | + if (preg_match($rx_pattern,$stat['name'])) { |
|
68 | + $results[] = $stat['name']; |
|
69 | + } |
|
66 | 70 | } |
67 | 71 | |
68 | 72 | return $results; |
@@ -13,9 +13,9 @@ |
||
13 | 13 | namespace View; |
14 | 14 | |
15 | 15 | interface Adapter { |
16 | - public function __construct($path=null, $options=[]); |
|
17 | - public function render($template,$data=[]); |
|
16 | + public function __construct($path = null, $options = []); |
|
17 | + public function render($template, $data = []); |
|
18 | 18 | public static function exists($path); |
19 | - public static function addGlobal($key,$val); |
|
19 | + public static function addGlobal($key, $val); |
|
20 | 20 | public static function addGlobals(array $defs); |
21 | 21 | } |