]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_string_replacer.php
Imported Upstream version 0.2.1
[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
32   private $values = array();
33
34
35   /**
36    * Add a string to the internal list
37    *
38    * @param string String value 
39    * @return int Index of value for retrieval
40    */
41   public function add($str)
42   {
43     $i = count($this->values);
44     $this->values[$i] = $str;
45     return $i;
46   }
47
48   /**
49    * Build replacement string
50    */
51   public function get_replacement($i)
52   {
53     return '##str_replacement['.$i.']##';
54   }
55
56   /**
57    * Callback function used to build HTML links around URL strings
58    *
59    * @param array Matches result from preg_replace_callback
60    * @return int Index of saved string value
61    */
62   public function link_callback($matches)
63   {
64     $i = -1;
65     $scheme = strtolower($matches[1]);
66
67     if ($scheme == 'http' || $scheme == 'https' || $scheme == 'ftp') {
68       $url = $matches[1] . '://' . $matches[2];
69       $i = $this->add(html::a(array('href' => $url, 'target' => "_blank"), Q($url)));
70     }
71     else if ($matches[2] == 'www.') {
72       $url = $matches[2] . $matches[3];
73       $i = $this->add($matches[1] . html::a(array('href' => 'http://' . $url, 'target' => "_blank"), Q($url)));
74     }
75
76     return $i >= 0 ? $this->get_replacement($i) : '';
77   }
78
79   /**
80    * Callback function used to build mailto: links around e-mail strings
81    *
82    * @param array Matches result from preg_replace_callback
83    * @return int Index of saved string value
84    */
85   public function mailto_callback($matches)
86   {
87     $i = $this->add(html::a(array(
88         'href' => 'mailto:' . $matches[1],
89         'onclick' => "return ".JS_OBJECT_NAME.".command('compose','".JQ($matches[1])."',this)",
90       ),
91       Q($matches[1])));
92
93     return $i >= 0 ? $this->get_replacement($i) : '';
94   }
95
96   /**
97    * Look up the index from the preg_replace matches array
98    * and return the substitution value.
99    *
100    * @param array Matches result from preg_replace_callback
101    * @return string Value at index $matches[1]
102    */
103   public function replace_callback($matches)
104   {
105     return $this->values[$matches[1]];
106   }
107
108   /**
109    * Replace substituted strings with original values
110    */
111   public function resolve($str)
112   {
113     return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
114   }
115
116 }