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: class CakeText {
25:
26: 27: 28: 29: 30: 31:
32: public static function uuid() {
33: $node = env('SERVER_ADDR');
34:
35: if (strpos($node, ':') !== false) {
36: if (substr_count($node, '::')) {
37: $node = str_replace(
38: '::', str_repeat(':0000', 8 - substr_count($node, ':')) . ':', $node
39: );
40: }
41: $node = explode(':', $node);
42: $ipSix = '';
43:
44: foreach ($node as $id) {
45: $ipSix .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
46: }
47: $node = base_convert($ipSix, 2, 10);
48:
49: if (strlen($node) < 38) {
50: $node = null;
51: } else {
52: $node = crc32($node);
53: }
54: } elseif (empty($node)) {
55: $host = env('HOSTNAME');
56:
57: if (empty($host)) {
58: $host = env('HOST');
59: }
60:
61: if (!empty($host)) {
62: $ip = gethostbyname($host);
63:
64: if ($ip === $host) {
65: $node = crc32($host);
66: } else {
67: $node = ip2long($ip);
68: }
69: }
70: } elseif ($node !== '127.0.0.1') {
71: $node = ip2long($node);
72: } else {
73: $node = null;
74: }
75:
76: if (empty($node)) {
77: $node = crc32(Configure::read('Security.salt'));
78: }
79:
80: if (function_exists('hphp_get_thread_id')) {
81: $pid = hphp_get_thread_id();
82: } elseif (function_exists('zend_thread_id')) {
83: $pid = zend_thread_id();
84: } else {
85: $pid = getmypid();
86: }
87:
88: if (!$pid || $pid > 65535) {
89: $pid = mt_rand(0, 0xfff) | 0x4000;
90: }
91:
92: list($timeMid, $timeLow) = explode(' ', microtime());
93: return sprintf(
94: "%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
95: mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node
96: );
97: }
98:
99: 100: 101: 102: 103: 104: 105: 106: 107: 108:
109: public static function tokenize($data, $separator = ',', $leftBound = '(', $rightBound = ')') {
110: if (empty($data)) {
111: return array();
112: }
113:
114: $depth = 0;
115: $offset = 0;
116: $buffer = '';
117: $results = array();
118: $length = mb_strlen($data);
119: $open = false;
120:
121: while ($offset <= $length) {
122: $tmpOffset = -1;
123: $offsets = array(
124: mb_strpos($data, $separator, $offset),
125: mb_strpos($data, $leftBound, $offset),
126: mb_strpos($data, $rightBound, $offset)
127: );
128: for ($i = 0; $i < 3; $i++) {
129: if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
130: $tmpOffset = $offsets[$i];
131: }
132: }
133: if ($tmpOffset !== -1) {
134: $buffer .= mb_substr($data, $offset, ($tmpOffset - $offset));
135: $char = mb_substr($data, $tmpOffset, 1);
136: if (!$depth && $char === $separator) {
137: $results[] = $buffer;
138: $buffer = '';
139: } else {
140: $buffer .= $char;
141: }
142: if ($leftBound !== $rightBound) {
143: if ($char === $leftBound) {
144: $depth++;
145: }
146: if ($char === $rightBound) {
147: $depth--;
148: }
149: } else {
150: if ($char === $leftBound) {
151: if (!$open) {
152: $depth++;
153: $open = true;
154: } else {
155: $depth--;
156: }
157: }
158: }
159: $offset = ++$tmpOffset;
160: } else {
161: $results[] = $buffer . mb_substr($data, $offset);
162: $offset = $length + 1;
163: }
164: }
165: if (empty($results) && !empty($buffer)) {
166: $results[] = $buffer;
167: }
168:
169: if (!empty($results)) {
170: return array_map('trim', $results);
171: }
172:
173: return array();
174: }
175:
176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196:
197: public static function insert($str, $data, $options = array()) {
198: $defaults = array(
199: 'before' => ':', 'after' => null, 'escape' => '\\', 'format' => null, 'clean' => false
200: );
201: $options += $defaults;
202: $format = $options['format'];
203: $data = (array)$data;
204: if (empty($data)) {
205: return ($options['clean']) ? CakeText::cleanInsert($str, $options) : $str;
206: }
207:
208: if (!isset($format)) {
209: $format = sprintf(
210: '/(?<!%s)%s%%s%s/',
211: preg_quote($options['escape'], '/'),
212: str_replace('%', '%%', preg_quote($options['before'], '/')),
213: str_replace('%', '%%', preg_quote($options['after'], '/'))
214: );
215: }
216:
217: if (strpos($str, '?') !== false && is_numeric(key($data))) {
218: $offset = 0;
219: while (($pos = strpos($str, '?', $offset)) !== false) {
220: $val = array_shift($data);
221: $offset = $pos + strlen($val);
222: $str = substr_replace($str, $val, $pos, 1);
223: }
224: return ($options['clean']) ? CakeText::cleanInsert($str, $options) : $str;
225: }
226:
227: asort($data);
228:
229: $dataKeys = array_keys($data);
230: $hashKeys = array_map('crc32', $dataKeys);
231: $tempData = array_combine($dataKeys, $hashKeys);
232: krsort($tempData);
233:
234: foreach ($tempData as $key => $hashVal) {
235: $key = sprintf($format, preg_quote($key, '/'));
236: $str = preg_replace($key, $hashVal, $str);
237: }
238: $dataReplacements = array_combine($hashKeys, array_values($data));
239: foreach ($dataReplacements as $tmpHash => $tmpValue) {
240: $tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
241: $str = str_replace($tmpHash, $tmpValue, $str);
242: }
243:
244: if (!isset($options['format']) && isset($options['before'])) {
245: $str = str_replace($options['escape'] . $options['before'], $options['before'], $str);
246: }
247: return ($options['clean']) ? CakeText::cleanInsert($str, $options) : $str;
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260:
261: public static function cleanInsert($str, $options) {
262: $clean = $options['clean'];
263: if (!$clean) {
264: return $str;
265: }
266: if ($clean === true) {
267: $clean = array('method' => 'text');
268: }
269: if (!is_array($clean)) {
270: $clean = array('method' => $options['clean']);
271: }
272: switch ($clean['method']) {
273: case 'html':
274: $clean = array_merge(array(
275: 'word' => '[\w,.]+',
276: 'andText' => true,
277: 'replacement' => '',
278: ), $clean);
279: $kleenex = sprintf(
280: '/[\s]*[a-z]+=(")(%s%s%s[\s]*)+\\1/i',
281: preg_quote($options['before'], '/'),
282: $clean['word'],
283: preg_quote($options['after'], '/')
284: );
285: $str = preg_replace($kleenex, $clean['replacement'], $str);
286: if ($clean['andText']) {
287: $options['clean'] = array('method' => 'text');
288: $str = CakeText::cleanInsert($str, $options);
289: }
290: break;
291: case 'text':
292: $clean = array_merge(array(
293: 'word' => '[\w,.]+',
294: 'gap' => '[\s]*(?:(?:and|or)[\s]*)?',
295: 'replacement' => '',
296: ), $clean);
297:
298: $kleenex = sprintf(
299: '/(%s%s%s%s|%s%s%s%s)/',
300: preg_quote($options['before'], '/'),
301: $clean['word'],
302: preg_quote($options['after'], '/'),
303: $clean['gap'],
304: $clean['gap'],
305: preg_quote($options['before'], '/'),
306: $clean['word'],
307: preg_quote($options['after'], '/')
308: );
309: $str = preg_replace($kleenex, $clean['replacement'], $str);
310: break;
311: }
312: return $str;
313: }
314:
315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328:
329: public static function wrap($text, $options = array()) {
330: if (is_numeric($options)) {
331: $options = array('width' => $options);
332: }
333: $options += array('width' => 72, 'wordWrap' => true, 'indent' => null, 'indentAt' => 0);
334: if ($options['wordWrap']) {
335: $wrapped = static::wordWrap($text, $options['width'], "\n");
336: } else {
337: $wrapped = trim(chunk_split($text, $options['width'] - 1, "\n"));
338: }
339: if (!empty($options['indent'])) {
340: $chunks = explode("\n", $wrapped);
341: for ($i = $options['indentAt'], $len = count($chunks); $i < $len; $i++) {
342: $chunks[$i] = $options['indent'] . $chunks[$i];
343: }
344: $wrapped = implode("\n", $chunks);
345: }
346: return $wrapped;
347: }
348:
349: 350: 351: 352: 353: 354: 355: 356: 357:
358: public static function wordWrap($text, $width = 72, $break = "\n", $cut = false) {
359: $paragraphs = explode($break, $text);
360: foreach ($paragraphs as &$paragraph) {
361: $paragraph = static::_wordWrap($paragraph, $width, $break, $cut);
362: }
363: return implode($break, $paragraphs);
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373: 374:
375: protected static function _wordWrap($text, $width = 72, $break = "\n", $cut = false) {
376: if ($cut) {
377: $parts = array();
378: while (mb_strlen($text) > 0) {
379: $part = mb_substr($text, 0, $width);
380: $parts[] = trim($part);
381: $text = trim(mb_substr($text, mb_strlen($part)));
382: }
383: return implode($break, $parts);
384: }
385:
386: $parts = array();
387: while (mb_strlen($text) > 0) {
388: if ($width >= mb_strlen($text)) {
389: $parts[] = trim($text);
390: break;
391: }
392:
393: $part = mb_substr($text, 0, $width);
394: $nextChar = mb_substr($text, $width, 1);
395: if ($nextChar !== ' ') {
396: $breakAt = mb_strrpos($part, ' ');
397: if ($breakAt === false) {
398: $breakAt = mb_strpos($text, ' ', $width);
399: }
400: if ($breakAt === false) {
401: $parts[] = trim($text);
402: break;
403: }
404: $part = mb_substr($text, 0, $breakAt);
405: }
406:
407: $part = trim($part);
408: $parts[] = $part;
409: $text = trim(mb_substr($text, mb_strlen($part)));
410: }
411:
412: return implode($break, $parts);
413: }
414:
415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430:
431: public static function highlight($text, $phrase, $options = array()) {
432: if (empty($phrase)) {
433: return $text;
434: }
435:
436: $defaults = array(
437: 'format' => '<span class="highlight">\1</span>',
438: 'html' => false,
439: 'regex' => "|%s|iu"
440: );
441: $options += $defaults;
442: extract($options);
443:
444: if (is_array($phrase)) {
445: $replace = array();
446: $with = array();
447:
448: foreach ($phrase as $key => $segment) {
449: $segment = '(' . preg_quote($segment, '|') . ')';
450: if ($html) {
451: $segment = "(?![^<]+>)$segment(?![^<]+>)";
452: }
453:
454: $with[] = (is_array($format)) ? $format[$key] : $format;
455: $replace[] = sprintf($options['regex'], $segment);
456: }
457:
458: return preg_replace($replace, $with, $text);
459: }
460:
461: $phrase = '(' . preg_quote($phrase, '|') . ')';
462: if ($html) {
463: $phrase = "(?![^<]+>)$phrase(?![^<]+>)";
464: }
465:
466: return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
467: }
468:
469: 470: 471: 472: 473: 474: 475:
476: public static function stripLinks($text) {
477: return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
478: }
479:
480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495:
496: public static function tail($text, $length = 100, $options = array()) {
497: $defaults = array(
498: 'ellipsis' => '...', 'exact' => true
499: );
500: $options += $defaults;
501: extract($options);
502:
503: if (!function_exists('mb_strlen')) {
504: class_exists('Multibyte');
505: }
506:
507: if (mb_strlen($text) <= $length) {
508: return $text;
509: }
510:
511: $truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
512: if (!$exact) {
513: $spacepos = mb_strpos($truncate, ' ');
514: $truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
515: }
516:
517: return $ellipsis . $truncate;
518: }
519:
520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537:
538: public static function truncate($text, $length = 100, $options = array()) {
539: $defaults = array(
540: 'ellipsis' => '...', 'exact' => true, 'html' => false
541: );
542: if (isset($options['ending'])) {
543: $defaults['ellipsis'] = $options['ending'];
544: } elseif (!empty($options['html']) && Configure::read('App.encoding') === 'UTF-8') {
545: $defaults['ellipsis'] = "\xe2\x80\xa6";
546: }
547: $options += $defaults;
548: extract($options);
549:
550: if (!function_exists('mb_strlen')) {
551: class_exists('Multibyte');
552: }
553:
554: if ($html) {
555: if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
556: return $text;
557: }
558: $totalLength = mb_strlen(strip_tags($ellipsis));
559: $openTags = array();
560: $truncate = '';
561:
562: preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
563: foreach ($tags as $tag) {
564: if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
565: if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
566: array_unshift($openTags, $tag[2]);
567: } elseif (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
568: $pos = array_search($closeTag[1], $openTags);
569: if ($pos !== false) {
570: array_splice($openTags, $pos, 1);
571: }
572: }
573: }
574: $truncate .= $tag[1];
575:
576: $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
577: if ($contentLength + $totalLength > $length) {
578: $left = $length - $totalLength;
579: $entitiesLength = 0;
580: if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
581: foreach ($entities[0] as $entity) {
582: if ($entity[1] + 1 - $entitiesLength <= $left) {
583: $left--;
584: $entitiesLength += mb_strlen($entity[0]);
585: } else {
586: break;
587: }
588: }
589: }
590:
591: $truncate .= mb_substr($tag[3], 0, $left + $entitiesLength);
592: break;
593: } else {
594: $truncate .= $tag[3];
595: $totalLength += $contentLength;
596: }
597: if ($totalLength >= $length) {
598: break;
599: }
600: }
601: } else {
602: if (mb_strlen($text) <= $length) {
603: return $text;
604: }
605: $truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
606: }
607: if (!$exact) {
608: $spacepos = mb_strrpos($truncate, ' ');
609: if ($html) {
610: $truncateCheck = mb_substr($truncate, 0, $spacepos);
611: $lastOpenTag = mb_strrpos($truncateCheck, '<');
612: $lastCloseTag = mb_strrpos($truncateCheck, '>');
613: if ($lastOpenTag > $lastCloseTag) {
614: preg_match_all('/<[\w]+[^>]*>/s', $truncate, $lastTagMatches);
615: $lastTag = array_pop($lastTagMatches[0]);
616: $spacepos = mb_strrpos($truncate, $lastTag) + mb_strlen($lastTag);
617: }
618: $bits = mb_substr($truncate, $spacepos);
619: preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
620: if (!empty($droppedTags)) {
621: if (!empty($openTags)) {
622: foreach ($droppedTags as $closingTag) {
623: if (!in_array($closingTag[1], $openTags)) {
624: array_unshift($openTags, $closingTag[1]);
625: }
626: }
627: } else {
628: foreach ($droppedTags as $closingTag) {
629: $openTags[] = $closingTag[1];
630: }
631: }
632: }
633: }
634: $truncate = mb_substr($truncate, 0, $spacepos);
635: }
636: $truncate .= $ellipsis;
637:
638: if ($html) {
639: foreach ($openTags as $tag) {
640: $truncate .= '</' . $tag . '>';
641: }
642: }
643:
644: return $truncate;
645: }
646:
647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657:
658: public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...') {
659: if (empty($text) || empty($phrase)) {
660: return static::truncate($text, $radius * 2, array('ellipsis' => $ellipsis));
661: }
662:
663: $append = $prepend = $ellipsis;
664:
665: $phraseLen = mb_strlen($phrase);
666: $textLen = mb_strlen($text);
667:
668: $pos = mb_strpos(mb_strtolower($text), mb_strtolower($phrase));
669: if ($pos === false) {
670: return mb_substr($text, 0, $radius) . $ellipsis;
671: }
672:
673: $startPos = $pos - $radius;
674: if ($startPos <= 0) {
675: $startPos = 0;
676: $prepend = '';
677: }
678:
679: $endPos = $pos + $phraseLen + $radius;
680: if ($endPos >= $textLen) {
681: $endPos = $textLen;
682: $append = '';
683: }
684:
685: $excerpt = mb_substr($text, $startPos, $endPos - $startPos);
686: $excerpt = $prepend . $excerpt . $append;
687:
688: return $excerpt;
689: }
690:
691: 692: 693: 694: 695: 696: 697: 698: 699:
700: public static function toList($list, $and = null, $separator = ', ') {
701: if ($and === null) {
702: $and = __d('cake', 'and');
703: }
704: if (count($list) > 1) {
705: return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
706: }
707:
708: return array_pop($list);
709: }
710: }
711: