wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
outputfilter.obfuscate_email.php
Go to the documentation of this file.
1 <?php
2 //
3 // code from: http://www.phpinsider.com/smarty-forum/viewtopic.php?t=2166
4 //
5 function smarty_outputfilter_obfuscate_email( $tpl_source, &$smarty ) {
6  global $obfuscated_email_count;
7  $obfuscated_email_count = 0;
8  $tpl_source = preg_replace_callback(
9  '!<a\s([^>]*)href=["\']mailto:([^"\']+)["\']([^>]*)>(.*?)</a[^>]*>!is',
10  'do_it',
11  $tpl_source);
12  return $tpl_source;
13 }
14 
15 function do_it($matches) {
16  global $obfuscated_email_count;
17 
18  // $matches[0] contains full matched string: <a href="...">...</a>
19  // $matches[1] contains additional parameters
20  // $matches[2] contains the email address which was specified as href
21  // $matches[3] contains additional parameters
22  // $matches[4] contains the text between the opening and closing <a> tag
23 
24  $address = $matches[2];
25  $obfuscated_address = str_replace(array(".","@"), array(" dot ", " at "), $address);
26  $extra = trim($matches[1]." ".$matches[3]);
27  $text = $matches[4];
28  $obfuscated_text = str_replace(array(".","@"), array(" dot ", " at "), $text);
29 
30  $string = "var e; if (e = document.getElementById('obfuscated_email_".$obfuscated_email_count."')) e.style.display = 'none';\n";
31  $string .= "document.write('<a href=\"mailto:".$address."\" ".$extra.">".$text."</a>');";
32  $js_encode = '';
33  for ($x=0; $x < strlen($string); $x++) {
34  $js_encode .= '%' . bin2hex($string[$x]);
35  }
36  $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>';
37 
38  ++$obfuscated_email_count;
39 
40  return $replace;
41 }
42 ?>
do_it($matches)
smarty_outputfilter_obfuscate_email($tpl_source, &$smarty)