You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_string_replacer.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2009-2012, The Roundcube Dev Team |
  6. | |
  7. | Licensed under the GNU General Public License version 3 or |
  8. | any later version with exceptions for skins & plugins. |
  9. | See the README file for a full license statement. |
  10. | |
  11. | PURPOSE: |
  12. | Handle string replacements based on preg_replace_callback |
  13. +-----------------------------------------------------------------------+
  14. | Author: Thomas Bruederli <roundcube@gmail.com> |
  15. +-----------------------------------------------------------------------+
  16. */
  17. /**
  18. * Helper class for string replacements based on preg_replace_callback
  19. *
  20. * @package Framework
  21. * @subpackage Utils
  22. */
  23. class rcube_string_replacer
  24. {
  25. public static $pattern = '/##str_replacement_(\d+)##/';
  26. public $mailto_pattern;
  27. public $link_pattern;
  28. public $linkref_index;
  29. public $linkref_pattern;
  30. protected $values = array();
  31. protected $options = array();
  32. protected $linkrefs = array();
  33. protected $urls = array();
  34. protected $noword = '[^\w@.#-]';
  35. function __construct($options = array())
  36. {
  37. // Simplified domain expression for UTF8 characters handling
  38. // Support unicode/punycode in top-level domain part
  39. $utf_domain = '[^?&@"\'\\/()<>\s\r\t\n]+\\.?([^\\x00-\\x2f\\x3b-\\x40\\x5b-\\x60\\x7b-\\x7f]{2,}|xn--[a-zA-Z0-9]{2,})';
  40. $url1 = '.:;,';
  41. $url2 = 'a-zA-Z0-9%=#$@+?|!&\\/_~\\[\\]\\(\\){}\*\x80-\xFE-';
  42. // Supported link prefixes
  43. $link_prefix = "([\w]+:\/\/|{$this->noword}[Ww][Ww][Ww]\.|^[Ww][Ww][Ww]\.)";
  44. $this->options = $options;
  45. $this->linkref_index = '/\[([^\]#]+)\](:?\s*##str_replacement_(\d+)##)/';
  46. $this->linkref_pattern = '/\[([^\]#]+)\]/';
  47. $this->link_pattern = "/$link_prefix($utf_domain([$url1]*[$url2]+)*)/";
  48. $this->mailto_pattern = "/("
  49. ."[-\w!\#\$%&\'*+~\/^`|{}=]+(?:\.[-\w!\#\$%&\'*+~\/^`|{}=]+)*" // local-part
  50. ."@$utf_domain" // domain-part
  51. ."(\?[$url1$url2]+)?" // e.g. ?subject=test...
  52. .")/";
  53. }
  54. /**
  55. * Add a string to the internal list
  56. *
  57. * @param string String value
  58. *
  59. * @return int Index of value for retrieval
  60. */
  61. public function add($str)
  62. {
  63. $i = count($this->values);
  64. $this->values[$i] = $str;
  65. return $i;
  66. }
  67. /**
  68. * Build replacement string
  69. */
  70. public function get_replacement($i)
  71. {
  72. return '##str_replacement_' . $i . '##';
  73. }
  74. /**
  75. * Callback function used to build HTML links around URL strings
  76. *
  77. * @param array Matches result from preg_replace_callback
  78. * @return int Index of saved string value
  79. */
  80. public function link_callback($matches)
  81. {
  82. $i = -1;
  83. $scheme = strtolower($matches[1]);
  84. if (preg_match('!^(http|ftp|file)s?://!i', $scheme)) {
  85. $url = $matches[1] . $matches[2];
  86. }
  87. else if (preg_match("/^({$this->noword}*)(www\.)$/i", $matches[1], $m)) {
  88. $url = $m[2] . $matches[2];
  89. $url_prefix = 'http://';
  90. $prefix = $m[1];
  91. }
  92. if ($url) {
  93. $suffix = $this->parse_url_brackets($url);
  94. $attrib = (array)$this->options['link_attribs'];
  95. $attrib['href'] = $url_prefix . $url;
  96. $i = $this->add(html::a($attrib, rcube::Q($url)) . $suffix);
  97. $this->urls[$i] = $attrib['href'];
  98. }
  99. // Return valid link for recognized schemes, otherwise
  100. // return the unmodified string for unrecognized schemes.
  101. return $i >= 0 ? $prefix . $this->get_replacement($i) : $matches[0];
  102. }
  103. /**
  104. * Callback to add an entry to the link index
  105. */
  106. public function linkref_addindex($matches)
  107. {
  108. $key = $matches[1];
  109. $this->linkrefs[$key] = $this->urls[$matches[3]];
  110. return $this->get_replacement($this->add('['.$key.']')) . $matches[2];
  111. }
  112. /**
  113. * Callback to replace link references with real links
  114. */
  115. public function linkref_callback($matches)
  116. {
  117. $i = 0;
  118. if ($url = $this->linkrefs[$matches[1]]) {
  119. $attrib = (array)$this->options['link_attribs'];
  120. $attrib['href'] = $url;
  121. $i = $this->add(html::a($attrib, rcube::Q($matches[1])));
  122. }
  123. return $i > 0 ? '['.$this->get_replacement($i).']' : $matches[0];
  124. }
  125. /**
  126. * Callback function used to build mailto: links around e-mail strings
  127. *
  128. * @param array Matches result from preg_replace_callback
  129. *
  130. * @return int Index of saved string value
  131. */
  132. public function mailto_callback($matches)
  133. {
  134. $href = $matches[1];
  135. $suffix = $this->parse_url_brackets($href);
  136. $i = $this->add(html::a('mailto:' . $href, rcube::Q($href)) . $suffix);
  137. return $i >= 0 ? $this->get_replacement($i) : '';
  138. }
  139. /**
  140. * Look up the index from the preg_replace matches array
  141. * and return the substitution value.
  142. *
  143. * @param array Matches result from preg_replace_callback
  144. * @return string Value at index $matches[1]
  145. */
  146. public function replace_callback($matches)
  147. {
  148. return $this->values[$matches[1]];
  149. }
  150. /**
  151. * Replace all defined (link|mailto) patterns with replacement string
  152. *
  153. * @param string $str Text
  154. *
  155. * @return string Text
  156. */
  157. public function replace($str)
  158. {
  159. // search for patterns like links and e-mail addresses
  160. $str = preg_replace_callback($this->link_pattern, array($this, 'link_callback'), $str);
  161. $str = preg_replace_callback($this->mailto_pattern, array($this, 'mailto_callback'), $str);
  162. // resolve link references
  163. $str = preg_replace_callback($this->linkref_index, array($this, 'linkref_addindex'), $str);
  164. $str = preg_replace_callback($this->linkref_pattern, array($this, 'linkref_callback'), $str);
  165. return $str;
  166. }
  167. /**
  168. * Replace substituted strings with original values
  169. */
  170. public function resolve($str)
  171. {
  172. return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
  173. }
  174. /**
  175. * Fixes bracket characters in URL handling
  176. */
  177. public static function parse_url_brackets(&$url)
  178. {
  179. // #1487672: special handling of square brackets,
  180. // URL regexp allows [] characters in URL, for example:
  181. // "http://example.com/?a[b]=c". However we need to handle
  182. // properly situation when a bracket is placed at the end
  183. // of the link e.g. "[http://example.com]"
  184. // Yes, this is not perfect handles correctly only paired characters
  185. // but it should work for common cases
  186. if (preg_match('/(\\[|\\])/', $url)) {
  187. $in = false;
  188. for ($i=0, $len=strlen($url); $i<$len; $i++) {
  189. if ($url[$i] == '[') {
  190. if ($in)
  191. break;
  192. $in = true;
  193. }
  194. else if ($url[$i] == ']') {
  195. if (!$in)
  196. break;
  197. $in = false;
  198. }
  199. }
  200. if ($i < $len) {
  201. $suffix = substr($url, $i);
  202. $url = substr($url, 0, $i);
  203. }
  204. }
  205. // Do the same for parentheses
  206. if (preg_match('/(\\(|\\))/', $url)) {
  207. $in = false;
  208. for ($i=0, $len=strlen($url); $i<$len; $i++) {
  209. if ($url[$i] == '(') {
  210. if ($in)
  211. break;
  212. $in = true;
  213. }
  214. else if ($url[$i] == ')') {
  215. if (!$in)
  216. break;
  217. $in = false;
  218. }
  219. }
  220. if ($i < $len) {
  221. $suffix = substr($url, $i);
  222. $url = substr($url, 0, $i);
  223. }
  224. }
  225. return $suffix;
  226. }
  227. }