TextHelper.php 8.5 KB

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