email-encoder-bundle
Last commit date
js
15 years ago
lang
15 years ago
methods
15 years ago
GPL-license.txt
15 years ago
Lim_Email_Encoder.php
15 years ago
MIT-license.txt
15 years ago
email-encoder-bundle.php
15 years ago
readme.txt
15 years ago
screenshot-1.jpg
15 years ago
Lim_Email_Encoder.php
192 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Lim_Email_Encoder Class |
| 4 | * |
| 5 | * Protecting email-spamming by replacing them with one of the registered encoding- or javascript-methods |
| 6 | * |
| 7 | * @package Lim_Email_Encoder |
| 8 | * @author Victor Villaverde Laan |
| 9 | * @version 0.22 |
| 10 | * @link http://www.freelancephp.net/email-encoder-php-class/ |
| 11 | * @license MIT license |
| 12 | */ |
| 13 | class Lim_Email_Encoder { |
| 14 | |
| 15 | /** |
| 16 | * @var array |
| 17 | */ |
| 18 | var $methods = array(); |
| 19 | |
| 20 | /** |
| 21 | * @var string |
| 22 | */ |
| 23 | var $method = NULL; |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * PHP4 constructor |
| 28 | */ |
| 29 | function Lim_Email_Encoder() { |
| 30 | $this->__construct(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * PHP5 constructor |
| 35 | */ |
| 36 | function __construct( $method = NULL ) { |
| 37 | // include all available method files |
| 38 | $this->_load_methods(); |
| 39 | |
| 40 | // set method |
| 41 | $this->set_method( $method ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set the encode method to use |
| 46 | * @param string $method can be the name of the method or 'random' |
| 47 | * @return $this |
| 48 | */ |
| 49 | function set_method( $method ) { |
| 50 | if ( 'random' == $method ) { |
| 51 | // set a random method |
| 52 | $this->method = array_rand( $this->methods ); |
| 53 | } elseif ( ! key_exists( $method, $this->methods ) ) { |
| 54 | // set default method |
| 55 | $this->method = 'lim_email_html_encode'; |
| 56 | } else { |
| 57 | // add 'lim_email_' prefix if not already set |
| 58 | $this->method = ( strpos( $method, 'lim_email_' ) !== FALSE ) ? $method : 'lim_email_' . $method; |
| 59 | } |
| 60 | |
| 61 | return $this; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Encode the given email into an encoded HTML link |
| 66 | * @param string $email |
| 67 | * @param string $display Optional, if not set display will be the email |
| 68 | * @return string |
| 69 | */ |
| 70 | function encode( $email, $display = NULL ) { |
| 71 | // decode entities |
| 72 | if ( function_exists( 'wp_kses_decode_entities' ) ) |
| 73 | $email = wp_kses_decode_entities( $email ); |
| 74 | |
| 75 | // set email as display |
| 76 | if ( $display === NULL ) |
| 77 | $display = $email; |
| 78 | |
| 79 | // get encoded email code |
| 80 | return call_user_func( $this->method, $email, $display ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Encode all emails of the given content |
| 85 | * @param string $content |
| 86 | * @param boolean $enc_tags Optional, default TRUE |
| 87 | * @param boolean $enc_plain_emails Optional, default TRUE |
| 88 | * @param boolean $enc_mailtos Optional, default TRUE |
| 89 | * @return string |
| 90 | */ |
| 91 | function filter( $content, $enc_tags = TRUE, $enc_plain_emails = TRUE, $enc_mailtos = TRUE ) { |
| 92 | // encode mailto links |
| 93 | if ( $enc_mailtos ) { |
| 94 | $mailto_pattern = '/<a.*?href=["\']mailto:(.*?)["\'].*?>(.*?)<\/a>/i'; |
| 95 | $content = preg_replace_callback( $mailto_pattern, array( $this, '_callback' ), $content ); |
| 96 | } |
| 97 | |
| 98 | // replace content tags [encode_email email="?" display="?"] to mailto links |
| 99 | // this code is partly taken from the plugin "Fay Emails Encoder" |
| 100 | // Credits goes to Faycal Tirich (http://faycaltirich.blogspot.com) |
| 101 | if ( $enc_tags ) { |
| 102 | $tag_pattern = '/\[encode_email\s+email=["\'](.*?)["\']\s+display=["\'](.*?)["\']]/i'; |
| 103 | $content = preg_replace_callback( $tag_pattern, array( $this, '_callback' ), $content ); |
| 104 | } |
| 105 | |
| 106 | // replace plain emails |
| 107 | if ( $enc_plain_emails ) { |
| 108 | $email_pattern = '/([A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6})/i'; |
| 109 | $content = preg_replace_callback( $email_pattern, array( $this, '_callback' ), $content ); |
| 110 | } |
| 111 | |
| 112 | return $content; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Convert randomly chars to htmlentities |
| 117 | * This method is partly taken from WordPress |
| 118 | * @link http://codex.wordpress.org/Function_Reference/antispambot |
| 119 | * @static |
| 120 | * @param string $value |
| 121 | * @return string |
| 122 | */ |
| 123 | function get_htmlent( $value ) { |
| 124 | // check if antispambot WordPress function exists |
| 125 | if ( ! function_exists( 'antispambot' ) ) |
| 126 | return antispambot( $value ); |
| 127 | |
| 128 | $enc_value = ''; |
| 129 | srand( (float) microtime() * 1000000 ); |
| 130 | |
| 131 | for ( $i = 0; $i < strlen( $value ); $i = $i + 1 ) { |
| 132 | $j = floor( rand( 0, 1 ) ); |
| 133 | |
| 134 | if ( $j == 0 ) { |
| 135 | $enc_value .= '&#' . ord( substr( $value, $i, 1 ) ).';'; |
| 136 | } elseif ( $j == 1 ) { |
| 137 | $enc_value .= substr( $value, $i, 1 ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $enc_value = str_replace( '@', '@', $enc_value ); |
| 142 | |
| 143 | return $enc_value; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Callback for encoding email |
| 148 | * @param array $match |
| 149 | * @return string |
| 150 | */ |
| 151 | function _callback( $match ) { |
| 152 | if ( count( $match ) == 2 ) |
| 153 | return $this->encode( $match[1] ); |
| 154 | |
| 155 | return $this->encode( $match[1], $match[2] ); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Load available methods |
| 160 | * @return void |
| 161 | */ |
| 162 | function _load_methods() { |
| 163 | $method_dir = dirname(__FILE__) . '/methods'; |
| 164 | $handle = opendir( $method_dir ); |
| 165 | |
| 166 | // dir not found |
| 167 | if ( ! $handle ) |
| 168 | return; |
| 169 | |
| 170 | // include all methods inside the method folder |
| 171 | while ( false !== ($file = readdir($handle)) ) { |
| 172 | if ( '.php' == substr( $file, -4 ) ) { |
| 173 | require_once $method_dir . '/' . $file; |
| 174 | |
| 175 | $name = substr( $file, 0, -4 ); |
| 176 | $fn = 'lim_email_' . $name; |
| 177 | |
| 178 | if ( function_exists( $fn ) ) { |
| 179 | // set method with info |
| 180 | $this->methods[$fn] = ( isset( ${ $fn } ) ) |
| 181 | ? ${ $fn } |
| 182 | : array( 'name' => $name, 'description' => $name ); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | closedir( $handle ); |
| 188 | } |
| 189 | |
| 190 | } // end class Lim_Email_Encoder |
| 191 | |
| 192 | /*?> // ommit closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */ |