Project

General

Profile

1
<?php
2

    
3
// $Id: filter-routines.php 1208 2009-12-03 18:06:39Z Luisehahne $
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
 * @category   frontend
24
 * @package    outputfilter
25
 * @author(s)  Dietmar W?llbrink <Luisehahne>, Dietrich Roland Pehlke <Aldus>
26
 * @platform   WB 2.8.0
27
 * @require    PHP 5.2.x
28
 * @license    http://www.gnu.org/licenses/gpl.html
29
 * @link       http://project.websitebaker2.org/browser/branches/2.8.x/wb/modules/output_filter/filter-routines.php
30
 * @changeset   2009/12/03 change searchstring mdcr.js, workout crypt emails
31

    
32
*/
33

    
34
// prevent this file from being accessed directly
35
if(!defined('WB_PATH')) die(header('Location: ../index.php'));
36

    
37
// function to read the current filter settings
38
if (!function_exists('get_output_filter_settings')) {
39
	function get_output_filter_settings() {
40
		global $database, $admin;
41
		// connect to database and read out filter settings
42
		$result = $database->query("SELECT * FROM " .TABLE_PREFIX ."mod_output_filter");
43
		if($result && $result->numRows() > 0) {
44
			// get all data
45
			$data = $result->fetchRow();
46
			$filter_settings['email_filter'] = $admin->strip_slashes($data['email_filter']);
47
			$filter_settings['mailto_filter'] = $admin->strip_slashes($data['mailto_filter']);
48
			$filter_settings['at_replacement'] = $admin->strip_slashes($data['at_replacement']);
49
			$filter_settings['dot_replacement'] = $admin->strip_slashes($data['dot_replacement']);
50
		} else {
51
			// something went wrong, use default values
52
			$filter_settings['email_filter'] = '0';
53
			$filter_settings['mailto_filter'] = '0';
54
			$filter_settings['at_replacement'] = '(at)';
55
			$filter_settings['dot_replacement'] = '(dot)';
56
		}
57
		
58
		// return array with filter settings
59
		return $filter_settings;
60
	}
61
}
62

    
63
// function to filter the output before displaying it on the frontend
64
if (!function_exists('filter_frontend_output')) {
65
	function filter_frontend_output($content) {
66
		// get output filter settings from database
67
		$filter_settings = get_output_filter_settings();
68
		
69
		// work out the defined output filter mode: possible output filter modes: [0], 1, 2, 3, 6, 7
70
		// 2^0 * (0.. disable, 1.. enable) filtering of mail addresses in text
71
		// 2^1 * (0.. disable, 1.. enable) filtering of mail addresses in mailto links
72
		// 2^2 * (0.. disable, 1.. enable) Javascript mailto encryption (only if mailto filtering enabled)
73

    
74
		// only filter output if we are supposed to
75
		if($filter_settings['email_filter'] != '1' && $filter_settings['mailto_filter'] != '1'){
76
			// nothing to do ...
77
			return $content;
78
		}
79

    
80
		// check if non mailto mail addresses needs to be filtered
81
		$output_filter_mode = ($filter_settings['email_filter'] == '1') ? 1 : 0;		// 0|1
82

    
83
		// check if mailto mail addresses needs to be filtered
84
		if($filter_settings['mailto_filter'] == '1') {
85
			$output_filter_mode = $output_filter_mode + 2;								// 0|2
86
						
87
			// check if Javascript mailto encryption is enabled (call register_frontend_functions in the template)
88
			$search = '<script src="' .WB_URL .'/modules/output_filter/js/mdcr.js" type="text/javascript"></script>';
89
			$search_droplet = '<script src="' .WB_URL .'/modules/droplets/js/mdcr.js" type="text/javascript"></script>';
90
			if(strpos($content, $search) !== false || strpos($content, $search_droplet) !== false) { 
91
				$output_filter_mode = $output_filter_mode + 4;							// 0|4
92
			}
93
		}
94
		
95
		// define some constants so we do not call the database in the callback function again
96
		define('OUTPUT_FILTER_MODE', (int) $output_filter_mode);
97
		define('OUTPUT_FILTER_AT_REPLACEMENT', $filter_settings['at_replacement']);
98
		define('OUTPUT_FILTER_DOT_REPLACEMENT', $filter_settings['dot_replacement']);
99
		
100
		// first search part to find all mailto email addresses
101
		$pattern = '#(<a[^<]*href\s*?=\s*?"\s*?mailto\s*?:\s*?)([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})([^"]*?)"([^>]*>)(.*?)</a>';
102
		// second part to find all non mailto email addresses
103
		$pattern .= '|(value\s*=\s*"|\')??\b([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})\b#i';
104
		/*
105
		Sub 1:\b(<a.[^<]*href\s*?=\s*?"\s*?mailto\s*?:\s*?)			-->	"<a id="yyy" class="xxx" href = " mailto :" ignoring white spaces
106
		Sub 2:([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4})			-->	the email address in the mailto: part of the mail link
107
		Sub 3:([^"]*?)"												--> possible ?Subject&cc... stuff attached to the mail address
108
		Sub 4:([^>]*>)												--> all class or id statements after the mailto but before closing ..>
109
		Sub 5:(.*?)</a>\b											--> the mailto text; all characters between >xxxxx</a>
110
		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)
111
		*/
112
			
113
		// find all email addresses embedded in the content and filter them using a callback function
114
		$content = preg_replace_callback($pattern, 'filter_mail_addresses', $content);
115
		return $content;
116
	}
117
}		
118

    
119

    
120
// function to filter mail addresses embedded in text or mailto links before outputing them on the frontend
121
if (!function_exists('filter_mail_addresses')) {
122
	function filter_mail_addresses($match) { 
123
		
124
		// check if required output filter mode is defined
125
		if(!(defined('OUTPUT_FILTER_MODE') && defined('OUTPUT_FILTER_MODE') && defined('OUTPUT_FILTER_MODE'))) {
126
			return $match[0];
127
		}
128
		
129
		$search = array('@', '.');
130
		$replace = array(OUTPUT_FILTER_AT_REPLACEMENT ,OUTPUT_FILTER_DOT_REPLACEMENT);
131
		
132
		// check if the match contains the expected number of subpatterns (6|8)
133
		if(count($match) == 8) {
134
			/**
135
				OUTPUT FILTER FOR EMAIL ADDRESSES EMBEDDED IN TEXT
136
			**/
137
			
138
			// 1.. text mails only, 3.. text mails + mailto (no JS), 7 text mails + mailto (JS)
139
			if(!in_array(OUTPUT_FILTER_MODE, array(1,3,7))) return $match[0];
140

    
141
			// do not filter mail addresses included in input tags (<input ... value = "test@mail)
142
			if (strpos($match[6], 'value') !== false) return $match[0];
143
			
144
			// filtering of non mailto email addresses enabled
145
			return str_replace($search, $replace, $match[0]);
146
				
147
		} elseif(count($match) == 6) {
148
			/**
149
				OUTPUT FILTER FOR EMAIL ADDRESSES EMBEDDED IN MAILTO LINKS
150
			**/
151

    
152
			// 2.. mailto only (no JS), 3.. text mails + mailto (no JS), 6.. mailto only (JS), 7.. all filters active
153
			if(!in_array(OUTPUT_FILTER_MODE, array(2,3,6,7))) return $match[0];
154
			
155
			// check if last part of the a href link: >xxxx</a> contains a email address we need to filter
156
			$pattern = '#[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}#i';
157
			if(preg_match_all($pattern, $match[5], $matches)) {
158
				foreach($matches as $submatch) {
159
					foreach($submatch as $value) {
160
						// replace all . and all @ in email address parts by (dot) and (at) strings
161
						$match[5] = str_replace($value, str_replace($search, $replace, $value), $match[5]);
162
					}
163
				}
164
			}
165

    
166
			// check if Javascript encryption routine is enabled
167
			if(in_array(OUTPUT_FILTER_MODE, array(6,7))) {
168
				/** USE JAVASCRIPT ENCRYPTION FOR MAILTO LINKS **/
169
				
170
				// extract possible class and id attribute from ahref link
171
				preg_match('/class\s*?=\s*?("|\')(.*?)\1/ix', $match[0], $class_attr);
172
				$class_attr = empty($class_attr) ? '' : 'class="' . $class_attr[2] . '" ';
173
				preg_match('/id\s*?=\s*?("|\')(.*?)\1/ix', $match[0], $id_attr);
174
				$id_attr = empty($id_attr) ? '' : 'id="' . $id_attr[2] . '" ';
175
				
176
				// preprocess mailto link parts for further usage
177
				$search = array('@', '.', '_', '-'); $replace = array('F', 'Z', 'X', 'K');
178
				$email_address = str_replace($search, $replace, strtolower($match[2]));
179
				$email_subject = rawurlencode(html_entity_decode($match[3]));
180
				
181
				// create a random encryption key for the Caesar cipher
182
				mt_srand((double)microtime()*1000000);	// (PHP < 4.2.0)
183
				$shift = mt_rand(1, 25);
184
				
185
				// encrypt the email using an adapted Caesar cipher
186
		  		$encrypted_email = "";
187
				for($i = strlen($email_address) -1; $i > -1; $i--) {
188
					if(preg_match('#[FZXK0-9]#', $email_address[$i], $characters)) {
189
						$encrypted_email .= $email_address[$i];
190
					} else {	
191
						$encrypted_email .= chr((ord($email_address[$i]) -97 + $shift) % 26 + 97);
192
					}
193
				}
194
				$encrypted_email .= chr($shift + 97);
195

    
196
				// build the encrypted Javascript mailto link
197
				$mailto_link  = "<a {$class_attr}{$id_attr}href=\"javascript:mdcr('$encrypted_email','$email_subject')\">" .$match[5] ."</a>";
198
				
199
				return $mailto_link;	
200

    
201
			} else {
202
				/** DO NOT USE JAVASCRIPT ENCRYPTION FOR MAILTO LINKS **/
203

    
204
				// as minimum protection, replace replace @ in the mailto part by (at)
205
				// dots are not transformed as this would transform my.name@domain.com into: my(dot)name(at)domain(dot)com
206
				
207
				// rebuild the mailto link from the subpatterns (at the missing characters " and </a>")
208
				return $match[1] .str_replace('@', OUTPUT_FILTER_AT_REPLACEMENT, $match[2]) .$match[3] .'"' .$match[4] .$match[5] .'</a>';
209
				// if you want to protect both, @ and dots, comment out the line above and remove the comment from the line below
210
				// return $match[1] .str_replace($search, $replace, $match[2]) .$match[3] .'"' .$match[4] .$match[5] .'</a>';
211
			}
212
		
213
		}
214
		
215
		// number of subpatterns do not match the requirements ... do nothing
216
		return $match[0];
217
	}		
218
}
219

    
220
?>
(1-1/6)