1
|
//:Emailfiltering on your output - output filtering with the options below - Mailto links can be encrypted by a Javascript
|
2
|
//:usage: [[EmailFilter]]
|
3
|
|
4
|
// You can configure the output filtering with the options below.
|
5
|
// Tip: Mailto links can be encrypted by a Javascript function.
|
6
|
// To make use of this option, one needs to add the PHP code
|
7
|
// register_frontend_modfiles('js');
|
8
|
// into the <head> section of the index.php of your template.
|
9
|
// Without this modification, only the @ character in the mailto part will be replaced.
|
10
|
|
11
|
// Basic Email Configuration:
|
12
|
// Filter Email addresses in text 0 = no, 1 = yes - default 1
|
13
|
$filter_settings['email_filter'] = '1';
|
14
|
|
15
|
// Filter Email addresses in mailto links 0 = no, 1 = yes - default 1
|
16
|
$filter_settings['mailto_filter'] = '1';
|
17
|
|
18
|
// Email Replacements, replace the '@' and the '.' by default (at) and (dot)
|
19
|
$filter_settings['at_replacement'] = '(at)';
|
20
|
$filter_settings['dot_replacement'] = '(dot)';
|
21
|
|
22
|
// No need to change stuff underneatch unless you know what you are doing.
|
23
|
|
24
|
// work out the defined output filter mode: possible output filter modes: [0], 1, 2, 3, 6, 7
|
25
|
// 2^0 * (0.. disable, 1.. enable) filtering of mail addresses in text
|
26
|
// 2^1 * (0.. disable, 1.. enable) filtering of mail addresses in mailto links
|
27
|
// 2^2 * (0.. disable, 1.. enable) Javascript mailto encryption (only if mailto filtering enabled)
|
28
|
|
29
|
// only filter output if we are supposed to
|
30
|
if($filter_settings['email_filter'] != '1' && $filter_settings['mailto_filter'] != '1'){
|
31
|
// nothing to do ...
|
32
|
return true;
|
33
|
}
|
34
|
|
35
|
// check if non mailto mail addresses needs to be filtered
|
36
|
$output_filter_mode = ($filter_settings['email_filter'] == '1') ? 1 : 0; // 0|1
|
37
|
|
38
|
// check if mailto mail addresses needs to be filtered
|
39
|
if($filter_settings['mailto_filter'] == '1') {
|
40
|
$output_filter_mode = $output_filter_mode + 2; // 0|2
|
41
|
|
42
|
// check if Javascript mailto encryption is enabled (call register_frontend_functions in the template)
|
43
|
$search = '<script type="text/javascript" src="' .WB_URL .'/modules/droplets/js/mdcr.js"></script>';
|
44
|
if(strpos($wb_page_data, $search) !== false) {
|
45
|
$output_filter_mode = $output_filter_mode + 4; // 0|4
|
46
|
}
|
47
|
}
|
48
|
|
49
|
// define some constants so we do not call the database in the callback function again
|
50
|
define('OUTPUT_FILTER_MODE', (int) $output_filter_mode);
|
51
|
define('OUTPUT_FILTER_AT_REPLACEMENT', $filter_settings['at_replacement']);
|
52
|
define('OUTPUT_FILTER_DOT_REPLACEMENT', $filter_settings['dot_replacement']);
|
53
|
|
54
|
// function to filter mail addresses embedded in text or mailto links before outputing them on the frontend
|
55
|
if (!function_exists('filter_mail_addresses')) {
|
56
|
function filter_mail_addresses($match) {
|
57
|
|
58
|
// check if required output filter mode is defined
|
59
|
if(!(defined('OUTPUT_FILTER_MODE') && defined('OUTPUT_FILTER_MODE') && defined('OUTPUT_FILTER_MODE'))) {
|
60
|
return $match[0];
|
61
|
}
|
62
|
|
63
|
$search = array('@', '.');
|
64
|
$replace = array(OUTPUT_FILTER_AT_REPLACEMENT ,OUTPUT_FILTER_DOT_REPLACEMENT);
|
65
|
|
66
|
// check if the match contains the expected number of subpatterns (6|8)
|
67
|
if(count($match) == 8) {
|
68
|
/**
|
69
|
OUTPUT FILTER FOR EMAIL ADDRESSES EMBEDDED IN TEXT
|
70
|
**/
|
71
|
|
72
|
// 1.. text mails only, 3.. text mails + mailto (no JS), 7 text mails + mailto (JS)
|
73
|
if(!in_array(OUTPUT_FILTER_MODE, array(1,3,7))) return $match[0];
|
74
|
|
75
|
// do not filter mail addresses included in input tags (<input ... value = "test@mail)
|
76
|
if (strpos($match[6], 'value') !== false) return $match[0];
|
77
|
|
78
|
// filtering of non mailto email addresses enabled
|
79
|
return str_replace($search, $replace, $match[0]);
|
80
|
|
81
|
} elseif(count($match) == 6) {
|
82
|
/**
|
83
|
OUTPUT FILTER FOR EMAIL ADDRESSES EMBEDDED IN MAILTO LINKS
|
84
|
**/
|
85
|
|
86
|
// 2.. mailto only (no JS), 3.. text mails + mailto (no JS), 6.. mailto only (JS), 7.. all filters active
|
87
|
if(!in_array(OUTPUT_FILTER_MODE, array(2,3,6,7))) return $match[0];
|
88
|
|
89
|
// check if last part of the a href link: >xxxx</a> contains a email address we need to filter
|
90
|
$pattern = '#[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}#i';
|
91
|
if(preg_match_all($pattern, $match[5], $matches)) {
|
92
|
foreach($matches as $submatch) {
|
93
|
foreach($submatch as $value) {
|
94
|
// replace all . and all @ in email address parts by (dot) and (at) strings
|
95
|
$match[5] = str_replace($value, str_replace($search, $replace, $value), $match[5]);
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
|
100
|
// check if Javascript encryption routine is enabled
|
101
|
if(in_array(OUTPUT_FILTER_MODE, array(6,7))) {
|
102
|
/** USE JAVASCRIPT ENCRYPTION FOR MAILTO LINKS **/
|
103
|
|
104
|
// extract possible class and id attribute from ahref link
|
105
|
preg_match('/class\s*?=\s*?("|\')(.*?)\1/ix', $match[0], $class_attr);
|
106
|
$class_attr = empty($class_attr) ? '' : 'class="' . $class_attr[2] . '" ';
|
107
|
preg_match('/id\s*?=\s*?("|\')(.*?)\1/ix', $match[0], $id_attr);
|
108
|
$id_attr = empty($id_attr) ? '' : 'id="' . $id_attr[2] . '" ';
|
109
|
|
110
|
// preprocess mailto link parts for further usage
|
111
|
$search = array('@', '.', '_', '-'); $replace = array('F', 'Z', 'X', 'K');
|
112
|
$email_address = str_replace($search, $replace, strtolower($match[2]));
|
113
|
$email_subject = rawurlencode(html_entity_decode($match[3]));
|
114
|
|
115
|
// create a random encryption key for the Caesar cipher
|
116
|
mt_srand((double)microtime()*1000000); // (PHP < 4.2.0)
|
117
|
$shift = mt_rand(1, 25);
|
118
|
|
119
|
// encrypt the email using an adapted Caesar cipher
|
120
|
$encrypted_email = "";
|
121
|
for($i = strlen($email_address) -1; $i > -1; $i--) {
|
122
|
if(preg_match('#[FZXK0-9]#', $email_address[$i], $characters)) {
|
123
|
$encrypted_email .= $email_address[$i];
|
124
|
} else {
|
125
|
$encrypted_email .= chr((ord($email_address[$i]) -97 + $shift) % 26 + 97);
|
126
|
}
|
127
|
}
|
128
|
$encrypted_email .= chr($shift + 97);
|
129
|
|
130
|
// build the encrypted Javascript mailto link
|
131
|
$mailto_link = "<a {$class_attr}{$id_attr}href=\"javascript:mdcr('$encrypted_email','$email_subject')\">" .$match[5] ."</a>";
|
132
|
|
133
|
return $mailto_link;
|
134
|
|
135
|
} else {
|
136
|
/** DO NOT USE JAVASCRIPT ENCRYPTION FOR MAILTO LINKS **/
|
137
|
|
138
|
// as minimum protection, replace replace @ in the mailto part by (at)
|
139
|
// dots are not transformed as this would transform my.name@domain.com into: my(dot)name(at)domain(dot)com
|
140
|
|
141
|
// rebuild the mailto link from the subpatterns (at the missing characters " and </a>")
|
142
|
return $match[1] .str_replace('@', OUTPUT_FILTER_AT_REPLACEMENT, $match[2]) .$match[3] .'"' .$match[4] .$match[5] .'</a>';
|
143
|
// if you want to protect both, @ and dots, comment out the line above and remove the comment from the line below
|
144
|
// return $match[1] .str_replace($search, $replace, $match[2]) .$match[3] .'"' .$match[4] .$match[5] .'</a>';
|
145
|
}
|
146
|
|
147
|
}
|
148
|
|
149
|
// number of subpatterns do not match the requirements ... do nothing
|
150
|
return $match[0];
|
151
|
}
|
152
|
}
|
153
|
|
154
|
// first search part to find all mailto email addresses
|
155
|
$pattern = '#(<a[^<]*href\s*?=\s*?"\s*?mailto\s*?:\s*?)([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})([^"]*?)"([^>]*>)(.*?)</a>';
|
156
|
// second part to find all non mailto email addresses
|
157
|
$pattern .= '|(value\s*=\s*"|\')??\b([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})\b#i';
|
158
|
|
159
|
// Sub 1:\b(<a.[^<]*href\s*?=\s*?"\s*?mailto\s*?:\s*?) --> "<a id="yyy" class="xxx" href = " mailto :" ignoring white spaces
|
160
|
// Sub 2:([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}) --> the email address in the mailto: part of the mail link
|
161
|
// Sub 3:([^"]*?)" --> possible ?Subject&cc... stuff attached to the mail address
|
162
|
// Sub 4:([^>]*>) --> all class or id statements after the mailto but before closing ..>
|
163
|
// Sub 5:(.*?)</a>\b --> the mailto text; all characters between >xxxxx</a>
|
164
|
// Sub 6:|\b([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})\b --> email addresses which may appear in the text (require word boundaries)
|
165
|
$content = $wb_page_data;
|
166
|
// find all email addresses embedded in the content and filter them using a callback function
|
167
|
$content = preg_replace_callback($pattern, 'filter_mail_addresses', $content);
|
168
|
$wb_page_data = $content;
|
169
|
return true;
|
170
|
|