1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20:
21: 22: 23: 24: 25: 26: 27:
28: class String {
29:
30: 31: 32: 33: 34: 35: 36:
37: function uuid() {
38: $node = env('SERVER_ADDR');
39: $pid = null;
40:
41: if (strpos($node, ':') !== false) {
42: if (substr_count($node, '::')) {
43: $node = str_replace(
44: '::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node
45: );
46: }
47: $node = explode(':', $node) ;
48: $ipv6 = '' ;
49:
50: foreach ($node as $id) {
51: $ipv6 .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
52: }
53: $node = base_convert($ipv6, 2, 10);
54:
55: if (strlen($node) < 38) {
56: $node = null;
57: } else {
58: $node = crc32($node);
59: }
60: } elseif (empty($node)) {
61: $host = env('HOSTNAME');
62:
63: if (empty($host)) {
64: $host = env('HOST');
65: }
66:
67: if (!empty($host)) {
68: $ip = gethostbyname($host);
69:
70: if ($ip === $host) {
71: $node = crc32($host);
72: } else {
73: $node = ip2long($ip);
74: }
75: }
76: } elseif ($node !== '127.0.0.1') {
77: $node = ip2long($node);
78: } else {
79: $node = null;
80: }
81:
82: if (empty($node)) {
83: $node = crc32(Configure::read('Security.salt'));
84: }
85:
86: if (function_exists('zend_thread_id')) {
87: $pid = zend_thread_id();
88: } else {
89: $pid = getmypid();
90: }
91:
92: if (!$pid || $pid > 65535) {
93: $pid = mt_rand(0, 0xfff) | 0x4000;
94: }
95:
96: list($timeMid, $timeLow) = explode(' ', microtime());
97: $uuid = sprintf(
98: "%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
99: mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node
100: );
101:
102: return $uuid;
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116:
117: function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
118: if (empty($data) || is_array($data)) {
119: return $data;
120: }
121:
122: $depth = 0;
123: $offset = 0;
124: $buffer = '';
125: $results = array();
126: $length = strlen($data);
127: $open = false;
128:
129: while ($offset <= $length) {
130: $tmpOffset = -1;
131: $offsets = array(
132: strpos($data, $separator, $offset),
133: strpos($data, $leftBound, $offset),
134: strpos($data, $rightBound, $offset)
135: );
136: for ($i = 0; $i < 3; $i++) {
137: if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
138: $tmpOffset = $offsets[$i];
139: }
140: }
141: if ($tmpOffset !== -1) {
142: $buffer .= substr($data, $offset, ($tmpOffset - $offset));
143: if ($data{$tmpOffset} == $separator && $depth == 0) {
144: $results[] = $buffer;
145: $buffer = '';
146: } else {
147: $buffer .= $data{$tmpOffset};
148: }
149: if ($leftBound != $rightBound) {
150: if ($data{$tmpOffset} == $leftBound) {
151: $depth++;
152: }
153: if ($data{$tmpOffset} == $rightBound) {
154: $depth--;
155: }
156: } else {
157: if ($data{$tmpOffset} == $leftBound) {
158: if (!$open) {
159: $depth++;
160: $open = true;
161: } else {
162: $depth--;
163: $open = false;
164: }
165: }
166: }
167: $offset = ++$tmpOffset;
168: } else {
169: $results[] = $buffer . substr($data, $offset);
170: $offset = $length + 1;
171: }
172: }
173: if (empty($results) && !empty($buffer)) {
174: $results[] = $buffer;
175: }
176:
177: if (!empty($results)) {
178: $data = array_map('trim', $results);
179: } else {
180: $data = array();
181: }
182: return $data;
183: }
184:
185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207:
208: function insert($str, $data, $options = array()) {
209: $defaults = array(
210: 'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
211: );
212: $options += $defaults;
213: $format = $options['format'];
214: $data = (array)$data;
215: if (empty($data)) {
216: return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
217: }
218:
219: if (!isset($format)) {
220: $format = sprintf(
221: '/(?<!%s)%s%%s%s/',
222: preg_quote($options['escape'], '/'),
223: str_replace('%', '%%', preg_quote($options['before'], '/')),
224: str_replace('%', '%%', preg_quote($options['after'], '/'))
225: );
226: }
227:
228: if (strpos($str, '?') !== false && is_numeric(key($data))) {
229: $offset = 0;
230: while (($pos = strpos($str, '?', $offset)) !== false) {
231: $val = array_shift($data);
232: $offset = $pos + strlen($val);
233: $str = substr_replace($str, $val, $pos, 1);
234: }
235: return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
236: } else {
237: asort($data);
238:
239: $hashKeys = array();
240: foreach ($data as $key => $value) {
241: $hashKeys[] = crc32($key);
242: }
243:
244: $tempData = array_combine(array_keys($data), array_values($hashKeys));
245: krsort($tempData);
246: foreach ($tempData as $key => $hashVal) {
247: $key = sprintf($format, preg_quote($key, '/'));
248: $str = preg_replace($key, $hashVal, $str);
249: }
250: $dataReplacements = array_combine($hashKeys, array_values($data));
251: foreach ($dataReplacements as $tmpHash => $tmpValue) {
252: $tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
253: $str = str_replace($tmpHash, $tmpValue, $str);
254: }
255: }
256:
257: if (!isset($options['format']) && isset($options['before'])) {
258: $str = str_replace($options['escape'].$options['before'], $options['before'], $str);
259: }
260: return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
261: }
262:
263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275:
276: function cleanInsert($str, $options) {
277: $clean = $options['clean'];
278: if (!$clean) {
279: return $str;
280: }
281: if ($clean === true) {
282: $clean = array('method' => 'text');
283: }
284: if (!is_array($clean)) {
285: $clean = array('method' => $options['clean']);
286: }
287: switch ($clean['method']) {
288: case 'html':
289: $clean = array_merge(array(
290: 'word' => '[\w,.]+',
291: 'andText' => true,
292: 'replacement' => '',
293: ), $clean);
294: $kleenex = sprintf(
295: '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
296: preg_quote($options['before'], '/'),
297: $clean['word'],
298: preg_quote($options['after'], '/')
299: );
300: $str = preg_replace($kleenex, $clean['replacement'], $str);
301: if ($clean['andText']) {
302: $options['clean'] = array('method' => 'text');
303: $str = String::cleanInsert($str, $options);
304: }
305: break;
306: case 'text':
307: $clean = array_merge(array(
308: 'word' => '[\w,.]+',
309: 'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
310: 'replacement' => '',
311: ), $clean);
312:
313: $kleenex = sprintf(
314: '/(%s%s%s%s|%s%s%s%s)/',
315: preg_quote($options['before'], '/'),
316: $clean['word'],
317: preg_quote($options['after'], '/'),
318: $clean['gap'],
319: $clean['gap'],
320: preg_quote($options['before'], '/'),
321: $clean['word'],
322: preg_quote($options['after'], '/')
323: );
324: $str = preg_replace($kleenex, $clean['replacement'], $str);
325: break;
326: }
327: return $str;
328: }
329: }
330: