Project

General

Profile

1
<?php
2

    
3
// $Id: filter-routines.php 586 2008-01-22 12:44:14Z Ruebenwurzel $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, 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
// direct access prevention
27
defined('WB_PATH') OR die(header('Location: ../index.php'));
28

    
29

    
30
// function to read the current filter settings
31
if (!function_exists('get_mail_filter_settings')) {
32
	function get_mail_filter_settings() {
33
		global $database, $admin;
34
		// connect to database and read out filter settings
35
		$result = $database->query("SELECT * FROM " .TABLE_PREFIX ."mod_mail_filter");
36
		if($result->numRows() > 0) {
37
			// get all data
38
			$data = $result->fetchRow();
39
			$filter_settings['email_filter'] = $admin->strip_slashes($data['email_filter']);
40
			$filter_settings['mailto_filter'] = $admin->strip_slashes($data['mailto_filter']);
41
			$filter_settings['at_replacement'] = $admin->strip_slashes($data['at_replacement']);
42
			$filter_settings['dot_replacement'] = $admin->strip_slashes($data['dot_replacement']);
43
		} else {
44
			// something went wrong, use dummy value
45
			$filter_settings['email_filter'] = '0';
46
			$filter_settings['mailto_filter'] = '0';
47
			$filter_settings['at_replacement'] = '(at)';
48
			$filter_settings['dot_replacement'] = '(dot)';
49
		}
50
		
51
		// return array with filter settings
52
		return $filter_settings;
53
	}
54
}
55

    
56

    
57
// function to rewrite email before beeing displayed the frontend
58
if (!function_exists('filter_email_links')) {
59
	function filter_email_links($content) {
60
		// get the mailer settings from the global variable defined in /wb/index.php
61
		global $mail_filter_settings;
62

    
63
		// check if there is anything to do
64
		global $mail_filter_settings;
65
		if(!isset($mail_filter_settings['email_filter']) || $mail_filter_settings['email_filter'] !=='1' ||
66
			!isset($mail_filter_settings['mailto_filter']) || !isset($mail_filter_settings['at_replacement']) || 
67
			!isset($mail_filter_settings['dot_replacement'])) {
68
			return $content;
69
		}
70

    
71
		// search pattern to find all mail addresses embedded in the frontend content (both text and mailto emails)
72
		$pattern = '#(<a href="mailto:)?(\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,})([^">]*">)?([^<]*</a>)?#i';
73
		/*
74
			exp 1:  (<a href=mailto:)?	-> Optional: search for <a href=mailto:
75
			exp 2a: (\w[\w|\.|\-]+@ 	 	-> 1st char word element, one ore more (+) word elements, or (|) . or (|) - followed by @
76
			exp 2b: \w[\w|\.|\-]+ 		-> see exp 2 for explanation
77
			exp 2c: \.[a-zA-Z]{2,})  	-> dot followed by at least 2 characters (TLD filter: .de, .com, .info) 
78
			exp 3: ([^">]*">)?  			-> Optional: all characters except > followed by ">; could contain ?subject=...&body=...
79
			exp 4: ([^<]*</a>)?  			-> Optional: all characters except <; email link message
80
			? 1 or 0 matches, # encapsulate regex, () subpattern as additional array element, i.. inherent (not case sensitive)
81
		*/
82

    
83
		// find all email addresses embedded in the content and encrypt them via callback function encrypt_emails
84
		$content = preg_replace_callback($pattern, 'encrypt_emails', $content);
85
		return $content;
86
	}
87
}
88

    
89

    
90
// function to encrypt mailto links before beeing displayed at the frontend
91
if (!function_exists('encrypt_emails')) {
92
	function encrypt_emails($match) { 
93
		// get the mailer settings from the global variable defined in /wb/index.php
94
		global $mail_filter_settings;
95

    
96
		// check if there is anything to do
97
		if(!isset($mail_filter_settings['email_filter']) || $mail_filter_settings['email_filter'] !=='1' ||
98
			!isset($mail_filter_settings['mailto_filter']) || !isset($mail_filter_settings['at_replacement']) || 
99
			!isset($mail_filter_settings['dot_replacement'])) {
100
			return $match[0];
101
		}
102
		
103
		// work out replacements
104
		$at_replace = strip_tags($mail_filter_settings['at_replacement']);
105
		$dot_replace = strip_tags($mail_filter_settings['dot_replacement']);
106

    
107
		// check if extracted email address is mailto or text ($match[0] contains entire regexp pattern)
108
		if (strpos($match[0], "mailto:") > 0) {
109
			// mailto email
110

    
111
			// do we need to consider mailto links
112
			if($mail_filter_settings['mailto_filter'] !='1') {
113
				// do not touch mailto links
114
				return $match[0];
115
			}
116
			
117
			// email sub parts: [<a href="mailto:] [name@domain.com] [?subject=blubb&Body=text">] [Mail description</a>]
118
			$email_href			= $match[1];	// a href part
119
			$email_address		= $match[2];	// email address
120
			$email_options 	= $match[3];	// optional: subject or body text for mail clients
121
			$email_link_text 	= $match[4];	// optional: mail description
122

    
123
			// do some cleaning
124
			$email_options 	= str_replace("\">", "", $email_options);					// strip off '">'
125
			$email_link_text 	= str_replace("</a>", "", trim($email_link_text));		// strip off closing </a>
126

    
127
			// make sure that all emails have a description
128
			if(strlen($email_link_text) == 0 ) {
129
				$email_link_text = $email_address;
130
			}
131

    
132
			// replace "@" and "." within top level domain (TLD) if link text is a valid email address
133
			if(preg_match('#\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,}#i', $email_link_text, $email)) {
134
				$email_link_text = str_replace("@", $at_replace, $email_address);		// replace @
135
				// remove "." from top level domain (TLD) part
136
				if (preg_match('#\.[a-zA-Z]{2,}$#im', $email_link_text, $tld) !==0) {
137
					$tld_replace =  str_replace(".", $dot_replace, $tld[0]);
138
					$email_link_text = str_replace($tld[0], $tld_replace, $email_link_text);				// replace . in email TLD part
139
				}
140
			}
141
		
142
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////
143
			// encrypt the email address
144
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////
145
			// create random encryption key
146
			mt_srand((double)microtime()*1000000);			// initialize the randomizer (PHP < 4.2.0)
147
			$char_shift = mt_rand(1, 5);						// shift:=1; a->b, shift:=5; a-->f
148
			$decryption_key = chr($char_shift+97);			// ASCII a:=97
149
		
150
			// prepare mailto string for encryption (mail protocol, decryption key, mail address)
151
			$email_address = "mailto:" .$decryption_key .$email_address;
152
		
153
			// encrypt email address by shifting characters
154
		  	$encrypted_email = "";
155
			for($i=0; $i<strlen($email_address); $i++) {
156
				$encrypted_email .= chr(ord($email_address[$i]) + $char_shift);
157
			}
158
			$encrypted_email[7] = $decryption_key;			// replace first character after mailto: with decryption key 
159
			$encrypted_email = rawurlencode($encrypted_email);
160

    
161
			// return encrypted javascript mailto link
162
			$mailto_link  = "<a href=\"javascript:mdcr('";		// a href part with javascript function to decrypt the email address
163
			$mailto_link .= "$encrypted_email')\">";				// add encrypted email address as paramter to JS function mdcr
164
			$mailto_link .= $email_link_text ."</a>";				// add email link text and closing </a> tag
165
			return $mailto_link;
166
		
167
		} else {
168
			// text email (e.g. name@domain.com)
169
			$match[0] = str_replace("@", $at_replace, $match[0]);		// replace @
170
			// remove "." from top level domain (TLD) part
171
			if (preg_match('#\.[a-zA-Z]{2,}$#im', $match[0], $tld) !==0) {
172
				$tld_replace =  str_replace(".", $dot_replace, $tld[0]);
173
				return str_replace($tld[0], $tld_replace, $match[0]);							// replace . in email TLD part
174
			}
175
		}
176
	}
177
}
178

    
179
?>
(1-1/6)