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