TextHelper.php 8.5 KB

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