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 4402 2011-01-10 14:50:48Z thomasb $
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 $utf_domain = '[^?&@"\'\\/()\s\r\t\n]+\\.[a-z]{2,5}';
41 $url2 = 'a-z0-9%=#@+?&\\/_~\\[\\]-';
43 $this->link_pattern = "/([\w]+:\/\/|\Wwww\.)($utf_domain([$url1]?[$url2]+)*)/i";
44 $this->mailto_pattern = "/("
45 ."[-\w!\#\$%&\'*+~\/^`|{}=]+(?:\.[-\w!\#\$%&\'*+~\/^`|{}=]+)*" // local-part
46 ."@$utf_domain" // domain-part
47 ."(\?[$url1$url2]+)?" // e.g. ?subject=test...
52 * Add a string to the internal list
54 * @param string String value
55 * @return int Index of value for retrieval
57 public function add($str)
59 $i = count($this->values);
60 $this->values[$i] = $str;
65 * Build replacement string
67 public function get_replacement($i)
69 return '##str_replacement['.$i.']##';
73 * Callback function used to build HTML links around URL strings
75 * @param array Matches result from preg_replace_callback
76 * @return int Index of saved string value
78 public function link_callback($matches)
81 $scheme = strtolower($matches[1]);
83 if (preg_match('!^(http|ftp|file)s?://!', $scheme)) {
84 $url = $matches[1] . $matches[2];
86 else if (preg_match('/^(\W)www\.$/', $matches[1], $m)) {
87 $url = 'www.' . $matches[2];
88 $url_prefix = 'http://';
93 $suffix = $this->parse_url_brackets($url);
94 $i = $this->add($prefix . html::a(array(
95 'href' => $url_prefix . $url,
97 ), Q($url)) . $suffix);
100 // Return valid link for recognized schemes, otherwise, return the unmodified string for unrecognized schemes.
101 return $i >= 0 ? $this->get_replacement($i) : $matches[0];
105 * Callback function used to build mailto: links around e-mail strings
107 * @param array Matches result from preg_replace_callback
108 * @return int Index of saved string value
110 public function mailto_callback($matches)
113 $suffix = $this->parse_url_brackets($href);
115 $i = $this->add(html::a(array(
116 'href' => 'mailto:' . $href,
117 'onclick' => "return ".JS_OBJECT_NAME.".command('compose','".JQ($href)."',this)",
118 ), Q($href)) . $suffix);
120 return $i >= 0 ? $this->get_replacement($i) : '';
124 * Look up the index from the preg_replace matches array
125 * and return the substitution value.
127 * @param array Matches result from preg_replace_callback
128 * @return string Value at index $matches[1]
130 public function replace_callback($matches)
132 return $this->values[$matches[1]];
136 * Replace substituted strings with original values
138 public function resolve($str)
140 return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
144 * Fixes bracket characters in URL handling
146 public static function parse_url_brackets(&$url)
148 // #1487672: special handling of square brackets,
149 // URL regexp allows [] characters in URL, for example:
150 // "http://example.com/?a[b]=c". However we need to handle
151 // properly situation when a bracket is placed at the end
152 // of the link e.g. "[http://example.com]"
153 if (preg_match('/(\\[|\\])/', $url)) {
155 for ($i=0, $len=strlen($url); $i<$len; $i++) {
156 if ($url[$i] == '[') {
161 else if ($url[$i] == ']') {
169 $suffix = substr($url, $i);
170 $url = substr($url, 0, $i);