alecpl
2010-09-25 e019f2d0f2dc2fbfa345ab5d7ae85e67bfdd76b8
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                     |
A 8  | Copyright (C) 2009, Roundcube Dev. - Switzerland                      |
aa055c 9  | Licensed under the GNU GPL                                            |
T 10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Handle string replacements based on preg_replace_callback           |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
1d786c 18  $Id$
aa055c 19
T 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   {
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";
b6244d 42     $this->mailto_pattern = "/("
A 43         ."[-\w!\#\$%&\'*+~\/^`|{}=]+(?:\.[-\w!\#\$%&\'*+~\/^`|{}=]+)*"  // local-part
44         ."@([a-z0-9]([-a-z0-9]*[a-z0-9])?\\.)+[a-z]{2,5}"               // domain-part
45         .")/i";
88ed23 46   }
aa055c 47
T 48   /**
49    * Add a string to the internal list
50    *
51    * @param string String value 
52    * @return int Index of value for retrieval
53    */
54   public function add($str)
55   {
56     $i = count($this->values);
57     $this->values[$i] = $str;
58     return $i;
59   }
60
61   /**
62    * Build replacement string
63    */
64   public function get_replacement($i)
65   {
66     return '##str_replacement['.$i.']##';
67   }
68
69   /**
70    * Callback function used to build HTML links around URL strings
71    *
72    * @param array Matches result from preg_replace_callback
73    * @return int Index of saved string value
74    */
75   public function link_callback($matches)
76   {
77     $i = -1;
78     $scheme = strtolower($matches[1]);
79
a17696 80     if (preg_match('!^(http|ftp|file)s?://!', $scheme)) {
88ed23 81       $url = $matches[1] . $matches[2];
A 82       $i = $this->add(html::a(array('href' => $url, 'target' => '_blank'), Q($url)));
aa055c 83     }
88ed23 84     else if (preg_match('/^(\W)www\.$/', $matches[1], $m)) {
A 85       $url = 'www.' . $matches[2];
86       $i = $this->add($m[1] . html::a(array('href' => 'http://' . $url, 'target' => '_blank'), Q($url)));
aa055c 87     }
T 88
3d4ba6 89     // Return valid link for recognized schemes, otherwise, return the unmodified string for unrecognized schemes.
T 90     return $i >= 0 ? $this->get_replacement($i) : $matches[0];
aa055c 91   }
T 92
93   /**
94    * Callback function used to build mailto: links around e-mail strings
95    *
96    * @param array Matches result from preg_replace_callback
97    * @return int Index of saved string value
98    */
99   public function mailto_callback($matches)
100   {
101     $i = $this->add(html::a(array(
102         'href' => 'mailto:' . $matches[1],
103         'onclick' => "return ".JS_OBJECT_NAME.".command('compose','".JQ($matches[1])."',this)",
104       ),
105       Q($matches[1])));
106
107     return $i >= 0 ? $this->get_replacement($i) : '';
108   }
109
110   /**
111    * Look up the index from the preg_replace matches array
112    * and return the substitution value.
113    *
114    * @param array Matches result from preg_replace_callback
115    * @return string Value at index $matches[1]
116    */
117   public function replace_callback($matches)
118   {
119     return $this->values[$matches[1]];
120   }
121
122   /**
123    * Replace substituted strings with original values
124    */
125   public function resolve($str)
126   {
127     return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
128   }
129
130 }