1
|
<?php
|
2
|
|
3
|
// $Id: filter-routines.php 1017 2009-06-27 21:51:30Z ruud $
|
4
|
|
5
|
/*
|
6
|
|
7
|
Website Baker Project <http://www.websitebaker.org/>
|
8
|
Copyright (C) 2004-2009, Ryan Djurovich
|
9
|
|
10
|
Website Baker is free software; you can redistribute it and/or modify
|
11
|
it under the terms of the GNU General Public License as published by
|
12
|
the Free Software Foundation; either version 2 of the License, or
|
13
|
(at your option) any later version.
|
14
|
|
15
|
Website Baker is distributed in the hope that it will be useful,
|
16
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
GNU General Public License for more details.
|
19
|
|
20
|
You should have received a copy of the GNU General Public License
|
21
|
along with Website Baker; if not, write to the Free Software
|
22
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
|
24
|
*/
|
25
|
|
26
|
// prevent this file from being accessed directly
|
27
|
if(!defined('WB_PATH')) die(header('Location: ../index.php'));
|
28
|
|
29
|
// function to read the current filter settings
|
30
|
if (!function_exists('get_output_filter_settings')) {
|
31
|
function get_output_filter_settings() {
|
32
|
global $database, $admin;
|
33
|
// connect to database and read out filter settings
|
34
|
$result = $database->query("SELECT * FROM " .TABLE_PREFIX ."mod_output_filter");
|
35
|
if($result && $result->numRows() > 0) {
|
36
|
// get all data
|
37
|
$data = $result->fetchRow();
|
38
|
$filter_settings['email_filter'] = $admin->strip_slashes($data['email_filter']);
|
39
|
$filter_settings['mailto_filter'] = $admin->strip_slashes($data['mailto_filter']);
|
40
|
$filter_settings['at_replacement'] = $admin->strip_slashes($data['at_replacement']);
|
41
|
$filter_settings['dot_replacement'] = $admin->strip_slashes($data['dot_replacement']);
|
42
|
} else {
|
43
|
// something went wrong, use default values
|
44
|
$filter_settings['email_filter'] = '0';
|
45
|
$filter_settings['mailto_filter'] = '0';
|
46
|
$filter_settings['at_replacement'] = '(at)';
|
47
|
$filter_settings['dot_replacement'] = '(dot)';
|
48
|
}
|
49
|
|
50
|
// return array with filter settings
|
51
|
return $filter_settings;
|
52
|
}
|
53
|
}
|
54
|
|
55
|
// function to filter the output before displaying it on the frontend
|
56
|
if (!function_exists('filter_frontend_output')) {
|
57
|
function filter_frontend_output($content) {
|
58
|
// get output filter settings from database
|
59
|
$filter_settings = get_output_filter_settings();
|
60
|
|
61
|
// work out the defined output filter mode: possible output filter modes: [0], 1, 2, 3, 6, 7
|
62
|
// 2^0 * (0.. disable, 1.. enable) filtering of mail addresses in text
|
63
|
// 2^1 * (0.. disable, 1.. enable) filtering of mail addresses in mailto links
|
64
|
// 2^2 * (0.. disable, 1.. enable) Javascript mailto encryption (only if mailto filtering enabled)
|
65
|
|
66
|
// only filter output if we are supposed to
|
67
|
if($filter_settings['email_filter'] != '1' && $filter_settings['mailto_filter'] != '1'){
|
68
|
// nothing to do ...
|
69
|
return $content;
|
70
|
}
|
71
|
|
72
|
// check if non mailto mail addresses needs to be filtered
|
73
|
$output_filter_mode = ($filter_settings['email_filter'] == '1') ? 1 : 0; // 0|1
|
74
|
|
75
|
// check if mailto mail addresses needs to be filtered
|
76
|
if($filter_settings['mailto_filter'] == '1') {
|
77
|
$output_filter_mode = $output_filter_mode + 2; // 0|2
|
78
|
|
79
|
// check if Javascript mailto encryption is enabled (call register_frontend_functions in the template)
|
80
|
$search = '<script type="text/javascript" src="' .WB_URL .'/modules/output_filter/js/mdcr.js"></script>';
|
81
|
$search_droplet = '<script type="text/javascript" src="' .WB_URL .'/modules/droplets/js/mdcr.js"></script>';
|
82
|
if(strpos($content, $search) !== false || strpos($content, $search_droplet) !== false) {
|
83
|
$output_filter_mode = $output_filter_mode + 4; // 0|4
|
84
|
}
|
85
|
}
|
86
|
|
87
|
// define some constants so we do not call the database in the callback function again
|
88
|
define('OUTPUT_FILTER_MODE', (int) $output_filter_mode);
|
89
|
define('OUTPUT_FILTER_AT_REPLACEMENT', $filter_settings['at_replacement']);
|
90
|
define('OUTPUT_FILTER_DOT_REPLACEMENT', $filter_settings['dot_replacement']);
|
91
|
|
92
|
// first search part to find all mailto email addresses
|
93
|
$pattern = '#(<a[^<]*href\s*?=\s*?"\s*?mailto\s*?:\s*?)([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})([^"]*?)"([^>]*>)(.*?)</a>';
|
94
|
// second part to find all non mailto email addresses
|
95
|
$pattern .= '|(value\s*=\s*"|\')??\b([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})\b#i';
|
96
|
/*
|
97
|
Sub 1:\b(<a.[^<]*href\s*?=\s*?"\s*?mailto\s*?:\s*?) --> "<a id="yyy" class="xxx" href = " mailto :" ignoring white spaces
|
98
|
Sub 2:([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}) --> the email address in the mailto: part of the mail link
|
99
|
Sub 3:([^"]*?)" --> possible ?Subject&cc... stuff attached to the mail address
|
100
|
Sub 4:([^>]*>) --> all class or id statements after the mailto but before closing ..>
|
101
|
Sub 5:(.*?)</a>\b --> the mailto text; all characters between >xxxxx</a>
|
102
|
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)
|
103
|
*/
|
104
|
|
105
|
// find all email addresses embedded in the content and filter them using a callback function
|
106
|
$content = preg_replace_callback($pattern, 'filter_mail_addresses', $content);
|
107
|
return $content;
|
108
|
}
|
109
|
}
|
110
|
|
111
|
|
112
|
// function to filter mail addresses embedded in text or mailto links before outputing them on the frontend
|
113
|
if (!function_exists('filter_mail_addresses')) {
|
114
|
function filter_mail_addresses($match) {
|
115
|
|
116
|
// check if required output filter mode is defined
|
117
|
if(!(defined('OUTPUT_FILTER_MODE') && defined('OUTPUT_FILTER_MODE') && defined('OUTPUT_FILTER_MODE'))) {
|
118
|
return $match[0];
|
119
|
}
|
120
|
|
121
|
$search = array('@', '.');
|
122
|
$replace = array(OUTPUT_FILTER_AT_REPLACEMENT ,OUTPUT_FILTER_DOT_REPLACEMENT);
|
123
|
|
124
|
// check if the match contains the expected number of subpatterns (6|8)
|
125
|
if(count($match) == 8) {
|
126
|
/**
|
127
|
OUTPUT FILTER FOR EMAIL ADDRESSES EMBEDDED IN TEXT
|
128
|
**/
|
129
|
|
130
|
// 1.. text mails only, 3.. text mails + mailto (no JS), 7 text mails + mailto (JS)
|
131
|
if(!in_array(OUTPUT_FILTER_MODE, array(1,3,7))) return $match[0];
|
132
|
|
133
|
// do not filter mail addresses included in input tags (<input ... value = "test@mail)
|
134
|
if (strpos($match[6], 'value') !== false) return $match[0];
|
135
|
|
136
|
// filtering of non mailto email addresses enabled
|
137
|
return str_replace($search, $replace, $match[0]);
|
138
|
|
139
|
} elseif(count($match) == 6) {
|
140
|
/**
|
141
|
OUTPUT FILTER FOR EMAIL ADDRESSES EMBEDDED IN MAILTO LINKS
|
142
|
**/
|
143
|
|
144
|
// 2.. mailto only (no JS), 3.. text mails + mailto (no JS), 6.. mailto only (JS), 7.. all filters active
|
145
|
if(!in_array(OUTPUT_FILTER_MODE, array(2,3,6,7))) return $match[0];
|
146
|
|
147
|
// check if last part of the a href link: >xxxx</a> contains a email address we need to filter
|
148
|
$pattern = '#[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}#i';
|
149
|
if(preg_match_all($pattern, $match[5], $matches)) {
|
150
|
foreach($matches as $submatch) {
|
151
|
foreach($submatch as $value) {
|
152
|
// replace all . and all @ in email address parts by (dot) and (at) strings
|
153
|
$match[5] = str_replace($value, str_replace($search, $replace, $value), $match[5]);
|
154
|
}
|
155
|
}
|
156
|
}
|
157
|
|
158
|
// check if Javascript encryption routine is enabled
|
159
|
if(in_array(OUTPUT_FILTER_MODE, array(6,7))) {
|
160
|
/** USE JAVASCRIPT ENCRYPTION FOR MAILTO LINKS **/
|
161
|
|
162
|
// extract possible class and id attribute from ahref link
|
163
|
preg_match('/class\s*?=\s*?("|\')(.*?)\1/ix', $match[0], $class_attr);
|
164
|
$class_attr = empty($class_attr) ? '' : 'class="' . $class_attr[2] . '" ';
|
165
|
preg_match('/id\s*?=\s*?("|\')(.*?)\1/ix', $match[0], $id_attr);
|
166
|
$id_attr = empty($id_attr) ? '' : 'id="' . $id_attr[2] . '" ';
|
167
|
|
168
|
// preprocess mailto link parts for further usage
|
169
|
$search = array('@', '.', '_', '-'); $replace = array('F', 'Z', 'X', 'K');
|
170
|
$email_address = str_replace($search, $replace, strtolower($match[2]));
|
171
|
$email_subject = rawurlencode(html_entity_decode($match[3]));
|
172
|
|
173
|
// create a random encryption key for the Caesar cipher
|
174
|
mt_srand((double)microtime()*1000000); // (PHP < 4.2.0)
|
175
|
$shift = mt_rand(1, 25);
|
176
|
|
177
|
// encrypt the email using an adapted Caesar cipher
|
178
|
$encrypted_email = "";
|
179
|
for($i = strlen($email_address) -1; $i > -1; $i--) {
|
180
|
if(preg_match('#[FZXK0-9]#', $email_address[$i], $characters)) {
|
181
|
$encrypted_email .= $email_address[$i];
|
182
|
} else {
|
183
|
$encrypted_email .= chr((ord($email_address[$i]) -97 + $shift) % 26 + 97);
|
184
|
}
|
185
|
}
|
186
|
$encrypted_email .= chr($shift + 97);
|
187
|
|
188
|
// build the encrypted Javascript mailto link
|
189
|
$mailto_link = "<a {$class_attr}{$id_attr}href=\"javascript:mdcr('$encrypted_email','$email_subject')\">" .$match[5] ."</a>";
|
190
|
|
191
|
return $mailto_link;
|
192
|
|
193
|
} else {
|
194
|
/** DO NOT USE JAVASCRIPT ENCRYPTION FOR MAILTO LINKS **/
|
195
|
|
196
|
// as minimum protection, replace replace @ in the mailto part by (at)
|
197
|
// dots are not transformed as this would transform my.name@domain.com into: my(dot)name(at)domain(dot)com
|
198
|
|
199
|
// rebuild the mailto link from the subpatterns (at the missing characters " and </a>")
|
200
|
return $match[1] .str_replace('@', OUTPUT_FILTER_AT_REPLACEMENT, $match[2]) .$match[3] .'"' .$match[4] .$match[5] .'</a>';
|
201
|
// if you want to protect both, @ and dots, comment out the line above and remove the comment from the line below
|
202
|
// return $match[1] .str_replace($search, $replace, $match[2]) .$match[3] .'"' .$match[4] .$match[5] .'</a>';
|
203
|
}
|
204
|
|
205
|
}
|
206
|
|
207
|
// number of subpatterns do not match the requirements ... do nothing
|
208
|
return $match[0];
|
209
|
}
|
210
|
}
|
211
|
|
212
|
?>
|