/ Published in: PHP
URL: http://www.freelancephp.net/email-encoder-php-class-wp-plugin/
PHP Class for encoding email adresses to protect them from spambots and being used for spamming. * Encodes email adresses (plain text and mailto links) * Choose the preferred method (or on every request randomly pick one of the methods) * Easy to use. * Add your own encoding methods
Expand |
Embed | Plain Text
<?php /** * Class Lim_Email_Encoder * Protecting email-spamming by replacing them with one of the registered encoding- or javascript-methods * @author Victor Villaverde Laan * @package Lim_Email_Encoder * @version 0.1 * @link http://www.freelancephp.net/email-encoder * @license Dual licensed under the MIT and GPL licenses */ class Lim_Email_Encoder { /** * @var array */ /** * @var array */ 'method' => 'default_encode', 'encode_display' => TRUE, // encode display with the default encoder 'encode_mailto' => TRUE, 'replace_emails' => TRUE, ); /** * PHP4 constructor */ function Lim_Email_Encoder() { $this->__construct(); } /** * PHP5 constructor */ function __construct() { // include all available method files $this->_include_method_files(); } /** * Set the encode method to use * @param string $key can be a method key or 'random' */ function set_method( $key ) { if ( 'random' == $key ) { // set a random method // set default method $this->options['method'] = 'default_encode'; } else { $this->options['method'] = $key; } } /** * Encode the given email into an encoded link * @param string $email * @param string $display * @return string */ function encode_email( $email, $display = NULL ) { if ( $display === NULL ) $display = $email; // get the encode method to use $encode_method = $this->methods[ $this->options['method'] ]; // get encoded email code } /** * Filter for encoding emails in the given content * @param string $content * @return string */ function encode_filter( $content ) { // replace plain emails to a content tag if ( $this->options['replace_emails'] ) { $email_pattern = '/([ ])([A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6})/i'; $replacement = '${1}[encode_email email="${2}" display="${2}"]'; } // encode mailto links if ( $this->options['encode_mailto'] ) { $mailto_pattern = '/<a.*?href=["\']mailto:(.*?)["\'].*?>(.*?)<\/a>/i'; } // replace content tags [encode_email email="?" display="?"] to mailto links // this code is partly taken from the plugin "Fay Emails Encoder" // Credits goes to Faycal Tirich (http://faycaltirich.blogspot.com) $tag_pattern = '/\[encode_email\s+email=["\'](.*?)["\']\s+display=["\'](.*?)["\']]/i'; return $content; } /** * Convert randomly chars to htmlentities * This method is partly taken from WordPress * @link http://codex.wordpress.org/Function_Reference/antispambot * @static * @param string $value * @return string */ function get_htmlent( $value ) { $enc_value = ''; if ( $j == 0 ) { } elseif ( $j == 1 ) { } } return $enc_value; } /** * Callback for encoding email * @param array $match * @return string */ function _callback( $match ) { return $this->encode_email( $match[1], $match[2] ); } /** * Including all method files * @return void */ function _include_method_files() { // dir not found if ( ! $handle ) return; // include all methods inside the method folder require_once $method_dir . '/' . $file; $this->methods[$fn] = $fn; } } } } // end class Lim_Email_Encoder ?>
You need to login to post a comment.
