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