PHP Classes

File: Mailto.php

Recommend this page to a friend!
  Packages of Debugged Interactive Designs   Mailto   Mailto.php   Download  
File: Mailto.php
Role: Class source
Content type: text/plain
Description: The class source
Class: Mailto
Obfuscate e-mail links using ROT13
Author: By
Last change: Added a second way to encode email links using an 8-bit, random key, ASCII Caesar Cipher
Date: 17 years ago
Size: 5,169 bytes
 

Contents

Class file image Download
<?php

/**
 * Project:     Mailto: A PHP class for obfuscating e-mail addresses from spam harvesters<br />
 * File:        Mailto.php<br />
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.<br /><br />
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.<br /><br />
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA<br /><br />
 *
 * Any modifications to the library should be indicated clearly in the source code
 * to inform users that the changes are not a part of the original software.<br /><br />
 *
 * @link http://www.debuggeddesigns.com/open-source-projects/mailto Mailto Link Obfuscator
 * @link http://www.debuggeddesigns.com/open-source-projects/mailto/code Download Latest Version
 * @link http://www.debuggeddesigns.com/open-source-projects/docs Online Documentation
 * @copyright 2008 Debugged Interactive Designs
 * @author debuggeddesigns <info@debuggeddesigns.com>
 * @version 0.0.2 (January 26, 2009)
 *
 */
 
 
/**
 * Mailto Link Obfuscator Class.
 *
 */
class Mailto {
    
    
    /**
      * The encoding type used for encryption
      *
      * @access public
      * @var string
      */
    var $encoding_type;
    
    
    /**
      * Class constructor.<br />
      * Accepts a parameter 
      *
      * <code>
      *   <?php
      *   include 'Mailto.php';
      *   $mailto = new Mailto();
      *   //$mailto = new Mailto('cc8b');
      *   ?>
      * </code>
      *
      * @param string $encoding_type The encoding type used for encryption
      * 
      */
      
    function Mailto($encoding_type = 'rot13') {
        $this->encoding_type = $encoding_type;
    }
  
  
    /**
      * Create an encrypted mailto link using the e-mail address and content passed as parameters.
      *
      * <code>
      *   <?php
      *   include 'Mailto.php';
      *   $mailto = new Mailto();
      *   //$mailto = new Mailto('cc8b'); 
      *   $mailto->createLink('info@debuggeddesigns.com','Debugged Interactive Designs');
      *   ?>
      * </code>
      *
      * @param string $addr  The e-mail address you want to hide from spam harvesters
      * @param string $content  The term that will be clickable
      * @return string  An encrypted anchor tag 
      */
   
    function createLink($addr, $content) {
        
        //build this for people with js turned off
        $noscript_link = '<span style="unicode-bidi:bidi-override;direction:rtl;">'.strrev($content.' > '.$addr.' <').'</span>';
        
        //if encoding type is rot13
        if($this->encoding_type=='rot13'){
            //build the mailto link
            $unencrypted_link = '<a href="mailto:'.$addr.'">'.$content.'</a>';
            //put them together and encrypt
            $encrypted_link = '<script type="text/javascript">Rot13.write(\''.str_rot13($unencrypted_link).'\');</script><noscript>'.$noscript_link . '</noscript>';
        }
        
        //if encoding type is cc8b
        else if($this->encoding_type=='cc8b'){
            //get a random number from 10 to 254 
            $key = rand(10, 254);
            //encrypt the email address
            $encrypted_email = $this->cc8b_encrypt($key,$addr);
            //encrypt the link text
            $encrypted_text = $this->cc8b_encrypt($key,$content);
            //put together the 
            $encrypted_link = "<script type=\"text/javascript\">\n" . "Cc8b.write(\"" . $key . "." . $encrypted_email . "." . $encrypted_text . "\");\n" . "</script>" . "<noscript>" . $noscript_link . "</noscript>";
        }
        
        //if encoding type is not recognized
        else
        {
            $encrypted_link = $noscript_link;
        }
        return $encrypted_link;
    }
    
    /**
      * 
      *
      * @access private
      * @param string $key The random key used to encrypt
      * @param string $plain_text The text to encrypt
      * @return string  Encrypted ascii code from the original plain text
      */
    private function cc8b_encrypt($key,$plain_text)
    {
        $cipher_text = '';
        //turn each character in the string into its hexidecimal ascii value
        $ascii_plain_text = (string) bin2hex($plain_text);
        //get each acii octet 
        for ($i = 0; $i <= strlen($ascii_plain_text) - 2; $i += 2) {
            //get next octet
            $temp = substr($ascii_plain_text, $i, 2);
            //turn it to decimal
            $temp = hexdec($temp);

            //add key value to temp
            $temp += $key;
            //mod temp by 255 so its value is at most FF
            $temp = $temp % 255;
            
            //concatinate the hex value to ecrypted email
            //check to make sure we have 2 digits
            //if its 1 digit place a leading 0 infront of it (0 -> F)
            if (strlen((string) dechex($temp)) < 2) {
                $cipher_text .= '0' . (string) dechex($temp);
            }
            //else just concate the 2 digit string
            else {
                $cipher_text .= (string) dechex($temp);
            }
        }
        
        return $cipher_text;        
    }
}
?>