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
148 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 | $email = html_entity_decode( $email ); |
| 73 | |
| 74 | // set email as display |
| 75 | if ( $display === NULL ) |
| 76 | $display = $email; |
| 77 | |
| 78 | // get encoded email code |
| 79 | return call_user_func( $this->method, $email, $display ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Convert randomly chars to htmlentities |
| 84 | * This method is partly taken from WordPress |
| 85 | * @link http://codex.wordpress.org/Function_Reference/antispambot |
| 86 | * @static |
| 87 | * @param string $value |
| 88 | * @return string |
| 89 | */ |
| 90 | function get_htmlent( $value ) { |
| 91 | // check if antispambot WordPress function exists |
| 92 | if ( function_exists( 'antispambot' ) ) { |
| 93 | $enc_value = antispambot( $value ); |
| 94 | } else { |
| 95 | $enc_value = ''; |
| 96 | srand( (float) microtime() * 1000000 ); |
| 97 | |
| 98 | for ( $i = 0; $i < strlen( $value ); $i = $i + 1 ) { |
| 99 | $j = floor( rand( 0, 1 ) ); |
| 100 | |
| 101 | if ( $j == 0 ) { |
| 102 | $enc_value .= '&#' . ord( substr( $value, $i, 1 ) ).';'; |
| 103 | } elseif ( $j == 1 ) { |
| 104 | $enc_value .= substr( $value, $i, 1 ); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | $enc_value = str_replace( '@', '@', $enc_value ); |
| 110 | |
| 111 | return $enc_value; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Load available methods |
| 116 | * @return void |
| 117 | */ |
| 118 | function _load_methods() { |
| 119 | $method_dir = dirname(__FILE__) . '/methods'; |
| 120 | $handle = opendir( $method_dir ); |
| 121 | |
| 122 | // dir not found |
| 123 | if ( ! $handle ) |
| 124 | return; |
| 125 | |
| 126 | // include all methods inside the method folder |
| 127 | while ( false !== ($file = readdir($handle)) ) { |
| 128 | if ( '.php' == substr( $file, -4 ) ) { |
| 129 | require_once $method_dir . '/' . $file; |
| 130 | |
| 131 | $name = substr( $file, 0, -4 ); |
| 132 | $fn = 'lim_email_' . $name; |
| 133 | |
| 134 | if ( function_exists( $fn ) ) { |
| 135 | // set method with info |
| 136 | $this->methods[$fn] = ( isset( ${ $fn } ) ) |
| 137 | ? ${ $fn } |
| 138 | : array( 'name' => $name, 'description' => $name ); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | closedir( $handle ); |
| 144 | } |
| 145 | |
| 146 | } // end class Lim_Email_Encoder |
| 147 | |
| 148 | /*?> // ommit closing tag, to prevent unwanted whitespace at the end of the parts generated by the included files */ |