Project

General

Profile

1
<?php
2
  /**
3
  * QuickSkin Extension mailtoencode
4
  * Protects email address from being scanned by spam bots
5
  * and optionally builds full mailto: link
6
  *
7
  * Usage Example 1:
8
  * Content:  $template->assign('AUTHOR', 'yourname@yourdomain.com' );
9
  * Template: Author: {mailtoencode:AUTHOR}
10
  * Result:   Author: <a href="&#109&#97&#105&#108&#116&#111&#58&#121;&#111;&#117;&#114;&#110;&#97;&#109;&#101;&#64;&#121;&#111;&#117;&#114;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;">yourname&#64;yourdomain&#46;com</a>
11
  *
12
  * Usage Example 2 (Email address only):
13
  * Template: Author: {mailtoencode:"yourname@yourdomain.com"}
14
  * Result:   Author: <a href="&#109&#97&#105&#108&#116&#111&#58&#121;&#111;&#117;&#114;&#110;&#97;&#109;&#101;&#64;&#121;&#111;&#117;&#114;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;">yourname&#64;yourdomain&#46;com</a>
15
  *
16
  * Usage Example 3 (Email address and name):
17
  * Template: Author: {mailtoencode:"yourname@yourdomain.com","Your Name"}
18
  * Result:   Author: <a href="&#109&#97&#105&#108&#116&#111&#58&#121;&#111;&#117;&#114;&#110;&#97;&#109;&#101;&#64;&#121;&#111;&#117;&#114;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;">Your Name</a>
19
  *
20
  * Usage Example 4 (Email address and name, encode set to true, and class name):
21
  * Template: Author: {mailtoencode:"yourname@yourdomain.com","Your Name",true,'white'}
22
  * Result:   Author: <a href="&#109&#97&#105&#108&#116&#111&#58&#121;&#111;&#117;&#114;&#110;&#97;&#109;&#101;&#64;&#121;&#111;&#117;&#114;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;" class="white">Your Name</a>
23
  *
24
  * Usage Example 5 (Email address and name, encode set to true, class name and style defined):
25
  * Template: Author: {mailtoencode:"yourname@yourdomain.com","Your Name",true,'white','font-size:18px;'}
26
  * Result:   Author: <a href="&#109&#97&#105&#108&#116&#111&#58&#121;&#111;&#117;&#114;&#110;&#97;&#109;&#101;&#64;&#121;&#111;&#117;&#114;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;" class="white" style="font-size:18px;">Your Name</a>
27
  *
28
  * Usage Example 6 (Email address and name, encode set to true, no class name and style defined):
29
  * Template: Author: {mailtoencode:"yourname@yourdomain.com","Your Name",true,'','font-size:18px;'}
30
  * Result:   Author: <a href="&#109&#97&#105&#108&#116&#111&#58&#121;&#111;&#117;&#114;&#110;&#97;&#109;&#101;&#64;&#121;&#111;&#117;&#114;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;" style="font-size:18px;">Your Name</a>
31
  *
32
  * @author Andy Prevost andy@codeworxtech.com
33
  */
34

    
35
  if(!function_exists('str_split')){
36
    function str_split($text, $split = 1){
37
      $array = array();
38
      for($i=0; $i < strlen($text); $i++){
39
        $key = NULL;
40
        for ($j = 0; $j < $split; $j++){
41
          $key .= $text[$i];
42
        }
43
        array_push($array, $key);
44
      }
45
        return $array;
46
    }
47
  }
48

    
49
  function qx_mailtoencode ($emailAddy,$text='',$buildLink=true,$class='',$style='') {
50
    if ( $buildLink ) {
51
      // mailto: portion
52
      $obfuscatedMailTo = '';
53
      $mailto           = "mailto:";
54
      $length           = strlen($mailto);
55
      for ($i = 0; $i < $length; $i++) {
56
        $obfuscatedMailTo .= "&#" . ord($mailto[$i]);
57
      }
58
      // END - mailto: portion
59
      $emailLink  = '<a href="';
60
      $emailLink .= $obfuscatedMailTo;
61
    }
62
    $emailLink .= obfuscate($emailAddy);
63
    if ( $buildLink ) {
64
      $emailLink .= '"';
65
      if ( trim($class) != '' ) {
66
        $emailLink .= ' class="' . $class . '"';
67
      }
68
      if ( trim($style) != '' ) {
69
        $emailLink .= ' style="' . $style . '"';
70
      }
71
      $emailLink .= '>';
72
      if ( trim($text) != '' ) {
73
        $newText    = trim($text);
74
        $newText    = str_replace('@','&#64;',$newText);
75
        $newText    = str_replace('.','&#46;',$newText);
76
        $emailLink .= $newText;
77
      } else {
78
        $newText    = trim($emailAddy);
79
        $newText    = str_replace('@','&#64;',$newText);
80
        $newText    = str_replace('.','&#46;',$newText);
81
        $emailLink .= $newText;
82
      }
83
      $emailLink .= "</a>";
84
    }
85
    return $emailLink;
86
  }
87

    
88
  function obfuscate($text) { // NOTE: Uses function str_split
89
    preg_match_all("/[-a-z0-9\._]+@[-a-z0-9\._]+\.[a-z]{2,4}/", $text, $emails);
90
    $patterns = array();
91
    $replacements = array();
92
    foreach($emails[0] as $email) {
93
      if(!in_array("/$email/", $patterns)) {
94
        $obfuscated = '';
95
        foreach(str_split($email) as $char) {
96
          $obfuscated .= '&#'.ord($char).';';
97
        }
98
        $patterns[] = "/$email/";
99
        $replacements[] = $obfuscated;
100
      }
101
    }
102
    $text = preg_replace($patterns, $replacements, $text);
103
    return $text;
104
  }
105

    
106
?>
(21-21/34)