1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
<?php
/*
+-----------------------------------------------------------------------+
| program/include/rcube_string_replacer.php |
| |
| This file is part of the RoundCube Webmail client |
| Copyright (C) 2009, RoundCube Dev. - Switzerland |
| Licensed under the GNU GPL |
| |
| PURPOSE: |
| Handle string replacements based on preg_replace_callback |
| |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
$Id: $
*/
/**
* Helper class for string replacements based on preg_replace_callback
*
* @package Core
*/
class rcube_string_replacer
{
public static $pattern = '/##str_replacement\[([0-9]+)\]##/';
private $values = array();
/**
* Add a string to the internal list
*
* @param string String value
* @return int Index of value for retrieval
*/
public function add($str)
{
$i = count($this->values);
$this->values[$i] = $str;
return $i;
}
/**
* Build replacement string
*/
public function get_replacement($i)
{
return '##str_replacement['.$i.']##';
}
/**
* Callback function used to build HTML links around URL strings
*
* @param array Matches result from preg_replace_callback
* @return int Index of saved string value
*/
public function link_callback($matches)
{
$i = -1;
$scheme = strtolower($matches[1]);
if ($scheme == 'http' || $scheme == 'https' || $scheme == 'ftp') {
$url = $matches[1] . '://' . $matches[2];
$i = $this->add(html::a(array('href' => $url, 'target' => "_blank"), Q($url)));
}
else if ($matches[2] == 'www.') {
$url = $matches[2] . $matches[3];
$i = $this->add($matches[1] . html::a(array('href' => 'http://' . $url, 'target' => "_blank"), Q($url)));
}
return $i >= 0 ? $this->get_replacement($i) : '';
}
/**
* Callback function used to build mailto: links around e-mail strings
*
* @param array Matches result from preg_replace_callback
* @return int Index of saved string value
*/
public function mailto_callback($matches)
{
$i = $this->add(html::a(array(
'href' => 'mailto:' . $matches[1],
'onclick' => "return ".JS_OBJECT_NAME.".command('compose','".JQ($matches[1])."',this)",
),
Q($matches[1])));
return $i >= 0 ? $this->get_replacement($i) : '';
}
/**
* Look up the index from the preg_replace matches array
* and return the substitution value.
*
* @param array Matches result from preg_replace_callback
* @return string Value at index $matches[1]
*/
public function replace_callback($matches)
{
return $this->values[$matches[1]];
}
/**
* Replace substituted strings with original values
*/
public function resolve($str)
{
return preg_replace_callback(self::$pattern, array($this, 'replace_callback'), $str);
}
}
|