4 +-----------------------------------------------------------------------+
5 | program/include/rcube_string_replacer.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2009, Roundcube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Handle string replacements based on preg_replace_callback |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_string_replacer.php 4729 2011-05-04 18:53:11Z alec $
24 * Helper class for string replacements based on preg_replace_callback
28 class rcube_string_replacer
30 public static $pattern = '/##str_replacement\[([0-9]+)\]##/';
31 public $mailto_pattern;
33 private $values = array();
36 function __construct()
38 // Simplified domain expression for UTF8 characters handling
39 // Support unicode/punycode in top-level domain part
40 $utf_domain = '[^?&@"\'\\/()\s\r\t\n]+\\.([^\\x00-\\x40\\x5b-\\x60\\x7b-\\x7f]{2,}|xn--[a-z0-9]{2,})';
42 $url2 = 'a-z0-9%=#@+?&\\/_~\\[\\]-';
44 $this->link_pattern = "/([\w]+:\/\/|\Wwww\.)($utf_domain([$url1]?[$url2]+)*)/i";
45 $this->mailto_pattern = "/("
46 ."[-\w!\#\$%&\'*+~\/^`|{}=]+(?:\.[-\w!\#\$%&\'*+~\/^`|{}=]+)*" // local-part
47 ."@$utf_domain" // domain-part
48 ."(\?[$url1$url2]+)?" // e.g. ?subject=test...
53 * Add a string to the internal list
55 * @param string String value
56 * @return int Index of value for retrieval
58 public function add($str)
60 $i = count($this->values);
61 $this->values[$i] = $str;
66 * Build replacement string
68 public function get_replacement($i)
70 return '##str_replacement['.$i.']##';
74 * Callback function used to build HTML links around URL strings
76 * @param array Matches result from preg_replace_callback
77 * @return int Index of saved string value
79 public function link_callback($matches)
82 $scheme = strtolower($matches[1]);
84 if (preg_match('!^(http|ftp|file)s?://!', $scheme)) {
85 $url = $matches[1] . $matches[2];
87 else if (preg_match('/^(\W)www\.$/', $matches[1], $m)) {
88 $url = 'www.' . $matches[2];
89 $url_prefix = 'http://';
94 $suffix = $this->parse_url_brackets($url);
95 $i = $this->add($prefix . html::a(array(
96 'href' => $url_prefix . $url,
98 ), Q($url)) . $suffix);
101 // Return valid link for recognized schemes, otherwise, return the unmodified string for unrecognized schemes.
102 return $i >= 0 ? $this->get_replacement($i) : $matches[0];
106 * Callback function used to build mailto: links around e-mail strings
108 * @param array Matches result from preg_replace_callback
109 * @return int Index of saved string value
111 public function mailto_callback($matches)
114 $suffix = $this->parse_url_brackets($href);
116 $i = $this->add(html::a(array(
117 'href' => 'mailto:' . $href,
118 'onclick' => "return ".JS_OBJECT_NAME.".command('compose','".JQ($href)."',this)",
119 ), Q($href)) . $suffix);
121 return $i >= 0 ? $this->get_replacement($i) : '';
125 * Look up the index from the preg_replace matches array
126 * and return the substitution value.
128 * @param array Matches result from preg_replace_callback
129 * @return string Value at index $matches[1]
131 public function replace_callback($matches)
133 return $this->values[$matches[1]];
137 * Replace substituted strings with original values
139 public function resolve($str)
141 return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
145 * Fixes bracket characters in URL handling
147 public static function parse_url_brackets(&$url)
149 // #1487672: special handling of square brackets,
150 // URL regexp allows [] characters in URL, for example:
151 // "http://example.com/?a[b]=c". However we need to handle
152 // properly situation when a bracket is placed at the end
153 // of the link e.g. "[http://example.com]"
154 if (preg_match('/(\\[|\\])/', $url)) {
156 for ($i=0, $len=strlen($url); $i<$len; $i++) {
157 if ($url[$i] == '[') {
162 else if ($url[$i] == ']') {
170 $suffix = substr($url, $i);
171 $url = substr($url, 0, $i);