]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_string_replacer.php
Imported Upstream version 0.5
[roundcube.git] / program / include / rcube_string_replacer.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_string_replacer.php                             |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2009, Roundcube Dev. - Switzerland                      |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Handle string replacements based on preg_replace_callback           |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: rcube_string_replacer.php 4402 2011-01-10 14:50:48Z thomasb $
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]+)\]##/';
31   public $mailto_pattern;
32   public $link_pattern;
33   private $values = array();
34
35
36   function __construct()
37   {
38     // Simplified domain expression for UTF8 characters handling
39     $utf_domain = '[^?&@"\'\\/()\s\r\t\n]+\\.[a-z]{2,5}';
40     $url1 = '.:;';
41     $url2 = 'a-z0-9%=#@+?&\\/_~\\[\\]-';
42
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...
48         .")/i";
49   }
50
51   /**
52    * Add a string to the internal list
53    *
54    * @param string String value 
55    * @return int Index of value for retrieval
56    */
57   public function add($str)
58   {
59     $i = count($this->values);
60     $this->values[$i] = $str;
61     return $i;
62   }
63
64   /**
65    * Build replacement string
66    */
67   public function get_replacement($i)
68   {
69     return '##str_replacement['.$i.']##';
70   }
71
72   /**
73    * Callback function used to build HTML links around URL strings
74    *
75    * @param array Matches result from preg_replace_callback
76    * @return int Index of saved string value
77    */
78   public function link_callback($matches)
79   {
80     $i = -1;
81     $scheme = strtolower($matches[1]);
82
83     if (preg_match('!^(http|ftp|file)s?://!', $scheme)) {
84       $url = $matches[1] . $matches[2];
85     }
86     else if (preg_match('/^(\W)www\.$/', $matches[1], $m)) {
87       $url        = 'www.' . $matches[2];
88       $url_prefix = 'http://';
89       $prefix     = $m[1];
90     }
91
92     if ($url) {
93       $suffix = $this->parse_url_brackets($url);
94       $i = $this->add($prefix . html::a(array(
95           'href' => $url_prefix . $url,
96           'target' => '_blank'
97         ), Q($url)) . $suffix);
98     }
99
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];
102   }
103
104   /**
105    * Callback function used to build mailto: links around e-mail strings
106    *
107    * @param array Matches result from preg_replace_callback
108    * @return int Index of saved string value
109    */
110   public function mailto_callback($matches)
111   {
112     $href   = $matches[1];
113     $suffix = $this->parse_url_brackets($href);
114
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);
119
120     return $i >= 0 ? $this->get_replacement($i) : '';
121   }
122
123   /**
124    * Look up the index from the preg_replace matches array
125    * and return the substitution value.
126    *
127    * @param array Matches result from preg_replace_callback
128    * @return string Value at index $matches[1]
129    */
130   public function replace_callback($matches)
131   {
132     return $this->values[$matches[1]];
133   }
134
135   /**
136    * Replace substituted strings with original values
137    */
138   public function resolve($str)
139   {
140     return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
141   }
142
143   /**
144    * Fixes bracket characters in URL handling
145    */
146   public static function parse_url_brackets(&$url)
147   {
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)) {
154       $in = false;
155       for ($i=0, $len=strlen($url); $i<$len; $i++) {
156         if ($url[$i] == '[') {
157           if ($in)
158             break;
159           $in = true;
160         }
161         else if ($url[$i] == ']') {
162           if (!$in)
163             break;
164           $in = false;
165         }
166       }
167
168       if ($i<$len) {
169         $suffix = substr($url, $i);
170         $url    = substr($url, 0, $i);
171       }
172     }
173
174     return $suffix;
175   }
176
177 }