TextHelper.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Text Helper
  4. *
  5. * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package Cake.View.Helper
  18. * @since CakePHP(tm) v 0.10.0.1076
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('AppHelper', 'View/Helper');
  22. /**
  23. * Text helper library.
  24. *
  25. * Text manipulations: Highlight, excerpt, truncate, strip of links, convert email addresses to mailto: links...
  26. *
  27. * @package Cake.View.Helper
  28. * @property HtmlHelper $Html
  29. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html
  30. * @see String
  31. */
  32. class TextHelper extends AppHelper {
  33. /**
  34. * helpers
  35. *
  36. * @var array
  37. */
  38. public $helpers = array('Html');
  39. /**
  40. * An array of md5sums and their contents.
  41. * Used when inserting links into text.
  42. *
  43. * @var array
  44. */
  45. protected $_placeholders = array();
  46. /**
  47. * String utility instance
  48. */
  49. protected $_engine;
  50. /**
  51. * Constructor
  52. *
  53. * ### Settings:
  54. *
  55. * - `engine` Class name to use to replace String functionality.
  56. * The class needs to be placed in the `Utility` directory.
  57. *
  58. * @param View $View the view object the helper is attached to.
  59. * @param array $settings Settings array Settings array
  60. * @throws CakeException when the engine class could not be found.
  61. */
  62. public function __construct(View $View, $settings = array()) {
  63. $settings = Hash::merge(array('engine' => 'String'), $settings);
  64. parent::__construct($View, $settings);
  65. list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
  66. App::uses($engineClass, $plugin . 'Utility');
  67. if (class_exists($engineClass)) {
  68. $this->_engine = new $engineClass($settings);
  69. } else {
  70. throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
  71. }
  72. }
  73. /**
  74. * Call methods from String utility class
  75. */
  76. public function __call($method, $params) {
  77. return call_user_func_array(array($this->_engine, $method), $params);
  78. }
  79. /**
  80. * Adds links (<a href=....) to a given text, by finding text that begins with
  81. * strings like http:// and ftp://.
  82. *
  83. * ### Options
  84. *
  85. * - `escape` Control HTML escaping of input. Defaults to true.
  86. *
  87. * @param string $text Text
  88. * @param array $options Array of HTML options, and options listed above.
  89. * @return string The text with links
  90. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls
  91. */
  92. public function autoLinkUrls($text, $options = array()) {
  93. $this->_placeholders = array();
  94. $options += array('escape' => true);
  95. $pattern = '#(?<!href="|src="|">)((?:https?|ftp|nntp)://[^\s<>()]+\.[a-z]+(?:\/[^\s]+)?)#i';
  96. $text = preg_replace_callback(
  97. $pattern,
  98. array(&$this, '_insertPlaceHolder'),
  99. $text
  100. );
  101. $text = preg_replace_callback(
  102. '#(?<!href="|">)(?<!\b[[:punct:]])(?<!http://|https://|ftp://|nntp://)www.[^\n\%\ <]+[^<\n\%\,\.\ <](?<!\))#i',
  103. array(&$this, '_insertPlaceHolder'),
  104. $text
  105. );
  106. if ($options['escape']) {
  107. $text = h($text);
  108. }
  109. return $this->_linkUrls($text, $options);
  110. }
  111. /**
  112. * Saves the placeholder for a string, for later use. This gets around double
  113. * escaping content in URL's.
  114. *
  115. * @param array $matches An array of regexp matches.
  116. * @return string Replaced values.
  117. */
  118. protected function _insertPlaceHolder($matches) {
  119. $key = md5($matches[0]);
  120. $this->_placeholders[$key] = $matches[0];
  121. return $key;
  122. }
  123. /**
  124. * Replace placeholders with links.
  125. *
  126. * @param string $text The text to operate on.
  127. * @param array $htmlOptions The options for the generated links.
  128. * @return string The text with links inserted.
  129. */
  130. protected function _linkUrls($text, $htmlOptions) {
  131. $replace = array();
  132. foreach ($this->_placeholders as $hash => $url) {
  133. $link = $url;
  134. if (!preg_match('#^[a-z]+\://#', $url)) {
  135. $url = 'http://' . $url;
  136. }
  137. $replace[$hash] = $this->Html->link($link, $url, $htmlOptions);
  138. }
  139. return strtr($text, $replace);
  140. }
  141. /**
  142. * Links email addresses
  143. *
  144. * @param string $text The text to operate on
  145. * @param array $options An array of options to use for the HTML.
  146. * @return string
  147. * @see TextHelper::autoLinkEmails()
  148. */
  149. protected function _linkEmails($text, $options) {
  150. $replace = array();
  151. foreach ($this->_placeholders as $hash => $url) {
  152. $replace[$hash] = $this->Html->link($url, 'mailto:' . $url, $options);
  153. }
  154. return strtr($text, $replace);
  155. }
  156. /**
  157. * Adds email links (<a href="mailto:....) to a given text.
  158. *
  159. * ### Options
  160. *
  161. * - `escape` Control HTML escaping of input. Defaults to true.
  162. *
  163. * @param string $text Text
  164. * @param array $options Array of HTML options, and options listed above.
  165. * @return string The text with links
  166. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails
  167. */
  168. public function autoLinkEmails($text, $options = array()) {
  169. $options += array('escape' => true);
  170. $this->_placeholders = array();
  171. $atom = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]';
  172. $text = preg_replace_callback(
  173. '/(' . $atom . '+(?:\.' . $atom . '+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)+)/i',
  174. array(&$this, '_insertPlaceholder'),
  175. $text
  176. );
  177. if ($options['escape']) {
  178. $text = h($text);
  179. }
  180. return $this->_linkEmails($text, $options);
  181. }
  182. /**
  183. * Convert all links and email addresses to HTML links.
  184. *
  185. * ### Options
  186. *
  187. * - `escape` Control HTML escaping of input. Defaults to true.
  188. *
  189. * @param string $text Text
  190. * @param array $options Array of HTML options, and options listed above.
  191. * @return string The text with links
  192. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink
  193. */
  194. public function autoLink($text, $options = array()) {
  195. $text = $this->autoLinkUrls($text, $options);
  196. return $this->autoLinkEmails($text, array_merge($options, array('escape' => false)));
  197. }
  198. /**
  199. * @see String::highlight()
  200. *
  201. * @param string $text Text to search the phrase in
  202. * @param string $phrase The phrase that will be searched
  203. * @param array $options An array of html attributes and options.
  204. * @return string The highlighted text
  205. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
  206. */
  207. public function highlight($text, $phrase, $options = array()) {
  208. return $this->_engine->highlight($text, $phrase, $options);
  209. }
  210. /**
  211. * @see String::stripLinks()
  212. *
  213. * @param string $text Text
  214. * @return string The text without links
  215. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
  216. */
  217. public function stripLinks($text) {
  218. return $this->_engine->stripLinks($text);
  219. }
  220. /**
  221. * @see String::truncate()
  222. *
  223. * @param string $text String to truncate.
  224. * @param integer $length Length of returned string, including ellipsis.
  225. * @param array $options An array of html attributes and options.
  226. * @return string Trimmed string.
  227. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
  228. */
  229. public function truncate($text, $length = 100, $options = array()) {
  230. return $this->_engine->truncate($text, $length, $options);
  231. }
  232. /**
  233. * @see String::excerpt()
  234. *
  235. * @param string $text String to search the phrase in
  236. * @param string $phrase Phrase that will be searched for
  237. * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
  238. * @param string $ending Ending that will be appended
  239. * @return string Modified string
  240. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
  241. */
  242. public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
  243. return $this->_engine->excerpt($text, $phrase, $radius, $ending);
  244. }
  245. /**
  246. * @see String::toList()
  247. *
  248. * @param array $list The list to be joined
  249. * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
  250. * @param string $separator The separator used to join all the other items together. Defaults to ', '
  251. * @return string The glued together string.
  252. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
  253. */
  254. public function toList($list, $and = 'and', $separator = ', ') {
  255. return $this->_engine->toList($list, $and, $separator);
  256. }
  257. }