outputfilter.obfuscate_email.php
1 <?php
2  /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 
12 /*
13 * Smarty plugin
14 * -------------------------------------------------------------
15 * File: smarty_outputfilter_obfuscate_email
16 * code from: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=2166
17 * -------------------------------------------------------------
18 */
19 function smarty_outputfilter_obfuscate_email($output, \Smarty_Internal_Template $template) {
20  global $obfuscated_email_count;
21  $obfuscated_email_count = 0;
22  $output = preg_replace_callback(
23  '!<a\s([^>]*)href=["\']mailto:([^"\']+)["\']([^>]*)>(.*?)</a[^>]*>!is',
24  'do_it',
25  $output);
26  return $output;
27 }
28 
29 function do_it($matches) {
30  global $obfuscated_email_count;
31 
32  // $matches[0] contains full matched string: <a href="...">...</a>
33  // $matches[1] contains additional parameters
34  // $matches[2] contains the email address which was specified as href
35  // $matches[3] contains additional parameters
36  // $matches[4] contains the text between the opening and closing <a> tag
37 
38  $address = $matches[2];
39  $obfuscated_address = str_replace(array(".","@"), array(" dot ", " at "), $address);
40  $extra = trim($matches[1]." ".$matches[3]);
41  $text = $matches[4];
42  $obfuscated_text = str_replace(array(".","@"), array(" dot ", " at "), $text);
43 
44  $string = "var e; if (e = document.getElementById('obfuscated_email_".$obfuscated_email_count."')) e.style.display = 'none';\n";
45  $string .= "document.write('<a href=\"mailto:".$address."\" ".$extra.">".$text."</a>');";
46  $js_encode = '';
47  for ($x=0; $x < strlen($string); $x++) {
48  $js_encode .= '%' . bin2hex($string[$x]);
49  }
50  $replace = '<a id="obfuscated_email_'.$obfuscated_email_count.'" href="mailto:'.$obfuscated_address.'">'.$obfuscated_text.'</a><script type="text/javascript" language="javascript">eval(unescape(\''.$js_encode.'\'))</script>';
51 
52  ++$obfuscated_email_count;
53 
54  return $replace;
55 }
56 ?>