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 +-----------------------------------------------------------------------+
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 $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;';
39 $url_chars_within = '\?\.~,!';
41 $this->link_pattern = "/([\w]+:\/\/|\Wwww\.)([a-z0-9\-\.]+[a-z]{2,4}([$url_chars$url_chars_within]*[$url_chars])?)/i";
42 $this->mailto_pattern = "/([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9]\\.[a-z]{2,5})/i";
46 * Add a string to the internal list
48 * @param string String value
49 * @return int Index of value for retrieval
51 public function add($str)
53 $i = count($this->values);
54 $this->values[$i] = $str;
59 * Build replacement string
61 public function get_replacement($i)
63 return '##str_replacement['.$i.']##';
67 * Callback function used to build HTML links around URL strings
69 * @param array Matches result from preg_replace_callback
70 * @return int Index of saved string value
72 public function link_callback($matches)
75 $scheme = strtolower($matches[1]);
77 if (preg_match('!^(http|ftp|file)s?://!', $scheme)) {
78 $url = $matches[1] . $matches[2];
79 $i = $this->add(html::a(array('href' => $url, 'target' => '_blank'), Q($url)));
81 else if (preg_match('/^(\W)www\.$/', $matches[1], $m)) {
82 $url = 'www.' . $matches[2];
83 $i = $this->add($m[1] . html::a(array('href' => 'http://' . $url, 'target' => '_blank'), Q($url)));
86 // Return valid link for recognized schemes, otherwise, return the unmodified string for unrecognized schemes.
87 return $i >= 0 ? $this->get_replacement($i) : $matches[0];
91 * Callback function used to build mailto: links around e-mail strings
93 * @param array Matches result from preg_replace_callback
94 * @return int Index of saved string value
96 public function mailto_callback($matches)
98 $i = $this->add(html::a(array(
99 'href' => 'mailto:' . $matches[1],
100 'onclick' => "return ".JS_OBJECT_NAME.".command('compose','".JQ($matches[1])."',this)",
104 return $i >= 0 ? $this->get_replacement($i) : '';
108 * Look up the index from the preg_replace matches array
109 * and return the substitution value.
111 * @param array Matches result from preg_replace_callback
112 * @return string Value at index $matches[1]
114 public function replace_callback($matches)
116 return $this->values[$matches[1]];
120 * Replace substituted strings with original values
122 public function resolve($str)
124 return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);