]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_string_replacer.php
Imported Upstream version 0.3
[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:  $
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     $url_chars = 'a-z0-9_\-\+\*\$\/&%=@#:;';
39     $url_chars_within = '\?\.~,!';
40
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";
43   }
44
45   /**
46    * Add a string to the internal list
47    *
48    * @param string String value 
49    * @return int Index of value for retrieval
50    */
51   public function add($str)
52   {
53     $i = count($this->values);
54     $this->values[$i] = $str;
55     return $i;
56   }
57
58   /**
59    * Build replacement string
60    */
61   public function get_replacement($i)
62   {
63     return '##str_replacement['.$i.']##';
64   }
65
66   /**
67    * Callback function used to build HTML links around URL strings
68    *
69    * @param array Matches result from preg_replace_callback
70    * @return int Index of saved string value
71    */
72   public function link_callback($matches)
73   {
74     $i = -1;
75     $scheme = strtolower($matches[1]);
76
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)));
80     }
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)));
84     }
85
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];
88   }
89
90   /**
91    * Callback function used to build mailto: links around e-mail strings
92    *
93    * @param array Matches result from preg_replace_callback
94    * @return int Index of saved string value
95    */
96   public function mailto_callback($matches)
97   {
98     $i = $this->add(html::a(array(
99         'href' => 'mailto:' . $matches[1],
100         'onclick' => "return ".JS_OBJECT_NAME.".command('compose','".JQ($matches[1])."',this)",
101       ),
102       Q($matches[1])));
103
104     return $i >= 0 ? $this->get_replacement($i) : '';
105   }
106
107   /**
108    * Look up the index from the preg_replace matches array
109    * and return the substitution value.
110    *
111    * @param array Matches result from preg_replace_callback
112    * @return string Value at index $matches[1]
113    */
114   public function replace_callback($matches)
115   {
116     return $this->values[$matches[1]];
117   }
118
119   /**
120    * Replace substituted strings with original values
121    */
122   public function resolve($str)
123   {
124     return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
125   }
126
127 }