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