TextHelper.php 8.5 KB

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