TextHelper.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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-2011, 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-2011, 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. */
  61. public function __construct(View $View, $settings = array()) {
  62. $settings = Set::merge(array('engine' => 'String'), $settings);
  63. parent::__construct($View, $settings);
  64. list($plugin, $engineClass) = pluginSplit($settings['engine'], true);
  65. App::uses($engineClass, $plugin . 'Utility');
  66. if (class_exists($engineClass)) {
  67. $this->_engine = new $engineClass($settings);
  68. } else {
  69. throw new CakeException(__d('cake_dev', '%s could not be found', $engineClass));
  70. }
  71. }
  72. /**
  73. * Call methods from String utility class
  74. */
  75. public function __call($method, $params) {
  76. return call_user_func_array(array($this->_engine, $method), $params);
  77. }
  78. /**
  79. * Adds links (<a href=....) to a given text, by finding text that begins with
  80. * strings like http:// and ftp://.
  81. *
  82. * ### Options
  83. *
  84. * - `escape` Control HTML escaping of input. Defaults to true.
  85. *
  86. * @param string $text Text
  87. * @param array $options Array of HTML options, and options listed above.
  88. * @return string The text with links
  89. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls
  90. */
  91. public function autoLinkUrls($text, $options = array()) {
  92. $this->_placeholders = array();
  93. $options += array('escape' => true);
  94. $text = preg_replace_callback(
  95. '#(?<!href="|src="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i',
  96. array(&$this, '_insertPlaceHolder'),
  97. $text
  98. );
  99. $text = preg_replace_callback(
  100. '#(?<!href="|">)(?<!\b[[:punct:]])(?<!http://|https://|ftp://|nntp://)www.[^\n\%\ <]+[^<\n\%\,\.\ <](?<!\))#i',
  101. array(&$this, '_insertPlaceHolder'),
  102. $text
  103. );
  104. if ($options['escape']) {
  105. $text = h($text);
  106. }
  107. return $this->_linkUrls($text, $options);
  108. }
  109. /**
  110. * Saves the placeholder for a string, for later use. This gets around double
  111. * escaping content in URL's.
  112. *
  113. * @param array $matches An array of regexp matches.
  114. * @return string Replaced values.
  115. */
  116. protected function _insertPlaceHolder($matches) {
  117. $key = md5($matches[0]);
  118. $this->_placeholders[$key] = $matches[0];
  119. return $key;
  120. }
  121. /**
  122. * Replace placeholders with links.
  123. *
  124. * @param string $text The text to operate on.
  125. * @param array $htmlOptions The options for the generated links.
  126. * @return string The text with links inserted.
  127. */
  128. protected function _linkUrls($text, $htmlOptions) {
  129. $replace = array();
  130. foreach ($this->_placeholders as $md5 => $url) {
  131. $link = $url;
  132. if (!preg_match('#^[a-z]+\://#', $url)) {
  133. $url = 'http://' . $url;
  134. }
  135. $replace[$md5] = $this->Html->link($link, $url, $htmlOptions);
  136. }
  137. return strtr($text, $replace);
  138. }
  139. /**
  140. * Links email addresses
  141. *
  142. * @param string $text The text to operate on
  143. * @param array $options An array of options to use for the HTML.
  144. * @return string
  145. * @see TextHelper::autoLinkEmails()
  146. */
  147. protected function _linkEmails($text, $options) {
  148. $replace = array();
  149. foreach ($this->_placeholders as $md5 => $url) {
  150. $replace[$md5] = $this->Html->link($url, 'mailto:' . $url, $options);
  151. }
  152. return strtr($text, $replace);
  153. }
  154. /**
  155. * Adds email links (<a href="mailto:....) to a given text.
  156. *
  157. * ### Options
  158. *
  159. * - `escape` Control HTML escaping of input. Defaults to true.
  160. *
  161. * @param string $text Text
  162. * @param array $options Array of HTML options, and options listed above.
  163. * @return string The text with links
  164. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails
  165. */
  166. public function autoLinkEmails($text, $options = array()) {
  167. $options += array('escape' => true);
  168. $this->_placeholders = array();
  169. $atom = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]';
  170. $text = preg_replace_callback(
  171. '/(' . $atom . '+(?:\.' . $atom . '+)*@[a-z0-9-]+(?:\.[a-z0-9-]+)+)/i',
  172. array(&$this, '_insertPlaceholder'),
  173. $text
  174. );
  175. if ($options['escape']) {
  176. $text = h($text);
  177. }
  178. return $this->_linkEmails($text, $options);
  179. }
  180. /**
  181. * Convert all links and email addresses to HTML links.
  182. *
  183. * ### Options
  184. *
  185. * - `escape` Control HTML escaping of input. Defaults to true.
  186. *
  187. * @param string $text Text
  188. * @param array $options Array of HTML options, and options listed above.
  189. * @return string The text with links
  190. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink
  191. */
  192. public function autoLink($text, $options = array()) {
  193. $text = $this->autoLinkUrls($text, $options);
  194. return $this->autoLinkEmails($text, array_merge($options, array('escape' => false)));
  195. }
  196. /**
  197. * @see String::highlight()
  198. *
  199. * @param string $text Text to search the phrase in
  200. * @param string $phrase The phrase that will be searched
  201. * @param array $options An array of html attributes and options.
  202. * @return string The highlighted text
  203. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::highlight
  204. */
  205. public function highlight($text, $phrase, $options = array()) {
  206. return $this->_engine->highlight($text, $phrase, $options);
  207. }
  208. /**
  209. * @see String::stripLinks()
  210. *
  211. * @param string $text Text
  212. * @return string The text without links
  213. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
  214. */
  215. public function stripLinks($text) {
  216. return $this->_engine->stripLinks($text);
  217. }
  218. /**
  219. * @see String::truncate()
  220. *
  221. * @param string $text String to truncate.
  222. * @param integer $length Length of returned string, including ellipsis.
  223. * @param array $options An array of html attributes and options.
  224. * @return string Trimmed string.
  225. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::truncate
  226. */
  227. public function truncate($text, $length = 100, $options = array()) {
  228. return $this->_engine->truncate($text, $length, $options);
  229. }
  230. /**
  231. * @see String::excerpt()
  232. *
  233. * @param string $text String to search the phrase in
  234. * @param string $phrase Phrase that will be searched for
  235. * @param integer $radius The amount of characters that will be returned on each side of the founded phrase
  236. * @param string $ending Ending that will be appended
  237. * @return string Modified string
  238. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::excerpt
  239. */
  240. public function excerpt($text, $phrase, $radius = 100, $ending = '...') {
  241. return $this->_engine->excerpt($text, $phrase, $radius, $ending);
  242. }
  243. /**
  244. * @see String::toList()
  245. *
  246. * @param array $list The list to be joined
  247. * @param string $and The word used to join the last and second last items together with. Defaults to 'and'
  248. * @param string $separator The separator used to join all the other items together. Defaults to ', '
  249. * @return string The glued together string.
  250. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::toList
  251. */
  252. public function toList($list, $and = 'and', $separator = ', ') {
  253. return $this->_engine->toList($list, $and, $separator);
  254. }
  255. }