Thomas Bruederli
2012-08-15 13969cf5406c14ba5dd5f830d7a8e2e2134e244b
commit | author | age
aa055c 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_string_replacer.php                             |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2009, The Roundcube Dev Team                            |
7fe381 9  |                                                                       |
T 10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
aa055c 13  |                                                                       |
T 14  | PURPOSE:                                                              |
15  |   Handle string replacements based on preg_replace_callback           |
16  |                                                                       |
17  +-----------------------------------------------------------------------+
18  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19  +-----------------------------------------------------------------------+
20 */
21
22
23 /**
24  * Helper class for string replacements based on preg_replace_callback
25  *
26  * @package Core
27  */
28 class rcube_string_replacer
29 {
30   public static $pattern = '/##str_replacement\[([0-9]+)\]##/';
88ed23 31   public $mailto_pattern;
A 32   public $link_pattern;
aa055c 33   private $values = array();
T 34
88ed23 35
A 36   function __construct()
37   {
9cc93a 38     // Simplified domain expression for UTF8 characters handling
5c1dfb 39     // Support unicode/punycode in top-level domain part
303ade 40     $utf_domain = '[^?&@"\'\\/()\s\r\t\n]+\\.([^\\x00-\\x2f\\x3b-\\x40\\x5b-\\x60\\x7b-\\x7f]{2,}|xn--[a-z0-9]{2,})';
96e24f 41     $url1 = '.:;,';
ae7ac9 42     $url2 = 'a-z0-9%=#@+?!&\\/_~\\[\\]{}-';
88ed23 43
a1013c 44     $this->link_pattern = "/([\w]+:\/\/|\Wwww\.)($utf_domain([$url1]?[$url2]+)*)/i";
b6244d 45     $this->mailto_pattern = "/("
A 46         ."[-\w!\#\$%&\'*+~\/^`|{}=]+(?:\.[-\w!\#\$%&\'*+~\/^`|{}=]+)*"  // local-part
9cc93a 47         ."@$utf_domain"                                                 // domain-part
a1013c 48         ."(\?[$url1$url2]+)?"                                           // e.g. ?subject=test...
b6244d 49         .")/i";
88ed23 50   }
aa055c 51
T 52   /**
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;
63   }
64
65   /**
66    * Build replacement string
67    */
68   public function get_replacement($i)
69   {
70     return '##str_replacement['.$i.']##';
71   }
72
73   /**
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]);
83
a17696 84     if (preg_match('!^(http|ftp|file)s?://!', $scheme)) {
88ed23 85       $url = $matches[1] . $matches[2];
aa055c 86     }
88ed23 87     else if (preg_match('/^(\W)www\.$/', $matches[1], $m)) {
0ff554 88       $url        = 'www.' . $matches[2];
A 89       $url_prefix = 'http://';
90       $prefix     = $m[1];
aa055c 91     }
0ff554 92
a1013c 93     if ($url) {
T 94       $suffix = $this->parse_url_brackets($url);
95       $i = $this->add($prefix . html::a(array(
96           'href' => $url_prefix . $url,
97           'target' => '_blank'
1aceb9 98         ), rcmail::Q($url)) . $suffix);
a1013c 99     }
aa055c 100
3d4ba6 101     // Return valid link for recognized schemes, otherwise, return the unmodified string for unrecognized schemes.
T 102     return $i >= 0 ? $this->get_replacement($i) : $matches[0];
aa055c 103   }
T 104
105   /**
106    * Callback function used to build mailto: links around e-mail strings
107    *
108    * @param array Matches result from preg_replace_callback
109    * @return int Index of saved string value
110    */
111   public function mailto_callback($matches)
112   {
0ff554 113     $href   = $matches[1];
A 114     $suffix = $this->parse_url_brackets($href);
115
aa055c 116     $i = $this->add(html::a(array(
0ff554 117         'href' => 'mailto:' . $href,
1aceb9 118         'onclick' => "return ".rcmail::JS_OBJECT_NAME.".command('compose','".rcmail::JQ($href)."',this)",
A 119       ), rcmail::Q($href)) . $suffix);
aa055c 120
T 121     return $i >= 0 ? $this->get_replacement($i) : '';
122   }
123
124   /**
125    * Look up the index from the preg_replace matches array
126    * and return the substitution value.
127    *
128    * @param array Matches result from preg_replace_callback
129    * @return string Value at index $matches[1]
130    */
131   public function replace_callback($matches)
132   {
133     return $this->values[$matches[1]];
134   }
135
136   /**
137    * Replace substituted strings with original values
138    */
139   public function resolve($str)
140   {
141     return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
142   }
143
0ff554 144   /**
A 145    * Fixes bracket characters in URL handling
146    */
147   public static function parse_url_brackets(&$url)
148   {
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)) {
155       $in = false;
156       for ($i=0, $len=strlen($url); $i<$len; $i++) {
157         if ($url[$i] == '[') {
158           if ($in)
159             break;
160           $in = true;
161         }
162         else if ($url[$i] == ']') {
163           if (!$in)
164             break;
165           $in = false;
166         }
167       }
168
169       if ($i<$len) {
170         $suffix = substr($url, $i);
171         $url    = substr($url, 0, $i);
172       }
173     }
174
175     return $suffix;
176   }
177
178 }