email-encoder-bundle
Last commit date
images
15 years ago
js
15 years ago
lang
15 years ago
methods
15 years ago
Lim_Email_Encoder.php
15 years ago
email-encoder-bundle.php
15 years ago
readme.txt
15 years ago
screenshot-1.png
15 years ago
screenshot-2.png
15 years ago
Lim_Email_Encoder.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Lim_Email_Encoder Class |
| 4 | * |
| 5 | * Protecting email-spamming by replacing them with one of the registered encoding-methods |
| 6 | * |
| 7 | * @package Lim_Email_Encoder |
| 8 | * @author Victor Villaverde Laan |
| 9 | * @version 0.32 |
| 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 | $this->method = $this->_get_method( $method ); |
| 51 | |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Encode the given email into an encoded HTML link |
| 57 | * @param string $email |
| 58 | * @param string $display Optional, if not set display will be the email |
| 59 | * @param string $method Optional, else the default setted method will; be used |
| 60 | * @return string |
| 61 | */ |
| 62 | function encode( $email, $display = NULL, $method = NULL ) { |
| 63 | // decode entities |
| 64 | $email = html_entity_decode( $email ); |
| 65 | |
| 66 | // set email as display |
| 67 | if ( $display === NULL ) |
| 68 | $display = $email; |
| 69 | |
| 70 | // set encode method |
| 71 | if ( $method === NULL ) { |
| 72 | $method = $this->method; |
| 73 | } else { |
| 74 | $method = $this->_get_method( $method ); |
| 75 | } |
| 76 | |
| 77 | // get encoded email code |
| 78 | return call_user_func( $method, $email, $display ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Convert randomly chars to htmlentities |
| 83 | * This method is partly taken from WordPress |
| 84 | * @link http://codex.wordpress.org/Function_Reference/antispambot |
| 85 | * @static |
| 86 | * @param string $value |
| 87 | * @return string |
| 88 | */ |
| 89 | function get_htmlent( $value ) { |
| 90 | // check if antispambot WordPress function exists |
| 91 | if ( function_exists( 'antispambot' ) ) { |
| 92 | $enc_value = antispambot( $value ); |
| 93 | } else { |
| 94 | $enc_value = ''; |
| 95 | srand( (float) microtime() * 1000000 ); |
| 96 | |
| 97 | for ( $i = 0; $i < strlen( $value ); $i = $i + 1 ) { |
| 98 | $j = floor( rand( 0, 1 ) ); |
| 99 | |
| 100 | if ( $j == 0 ) { |
| 101 | $enc_value .= '&#' . ord( substr( $value, $i, 1 ) ).';'; |
| 102 | } elseif ( $j == 1 ) { |
| 103 | $enc_value .= substr( $value, $i, 1 ); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | $enc_value = str_replace( '@', '@', $enc_value ); |
| 109 | |
| 110 | return $enc_value; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Load available methods |
| 115 | * @return void |
| 116 | */ |
| 117 | function _load_methods() { |
| 118 | $method_dir = dirname(__FILE__) . '/methods'; |
| 119 | $handle = opendir( $method_dir ); |
| 120 | |
| 121 | // dir not found |
| 122 | if ( ! $handle ) |
| 123 | return; |
| 124 | |
| 125 | // include all methods inside the method folder |
| 126 | while ( false !== ($file = readdir($handle)) ) { |
| 127 | if ( '.php' == substr( $file, -4 ) ) { |
| 128 | require_once $method_dir . '/' . $file; |
| 129 | |
| 130 | $name = substr( $file, 0, -4 ); |
| 131 | $fn = 'lim_email_' . $name; |
| 132 | |
| 133 | if ( function_exists( $fn ) ) { |
| 134 | // set method with info |
| 135 | $this->methods[$fn] = ( isset( ${ $fn } ) ) |
| 136 | ? ${ $fn } |
| 137 | : array( 'name' => $name, 'description' => $name ); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | closedir( $handle ); |
| 143 | } |
| 144 | |
| 145 | function _get_method( $method ) { |
| 146 | $method = strtolower( $method ); |
| 147 | |
| 148 | if ( 'random' == $method ) { |
| 149 | // set a random method |
| 150 | $method = array_rand( $this->methods ); |
| 151 | } else { |
| 152 | // add 'lim_email_' prefix if not already set |
| 153 | $method = ( strpos( $method, 'lim_email_' ) !== FALSE ) ? $method : 'lim_email_' . $method; |
| 154 | |
| 155 | if ( ! key_exists( $method, $this->methods ) ) |
| 156 | $method = 'lim_email_html_encode'; // set default method |
| 157 | } |
| 158 | |
| 159 | return $method; |
| 160 | } |
| 161 | |
| 162 | } // end class Lim_Email_Encoder |
| 163 | |
| 164 | /*?> // ommit closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */ |