Source for file Crypt.php
Documentation is available at Crypt.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Michael Dransfield <mike@blueroot.net> |
// +----------------------------------------------------------------------+
// $Id: Crypt.php,v 1.6 2006/11/26 21:48:42 cweiske Exp $
* HTML_Crypt provides methods to encrypt text, which
* can be later be decrypted using JavaScript on the client side
* This is very useful to prevent spam robots collecting email
* addresses from your site, included is a method to add mailto
* links to the text being generated.
* The "encryption" function is basically works like ROT13,
* with the difference that the $offset setting replaces the 13.
* It is also limited to ASCII characters between 32 and 127 (included).
* Other characters will not be encrypted.
* a basic example to encrypt an email address
* $c = new HTML_Crypt('yourname@emailaddress.com', 8);
* @author Michael Dransfield <mike@blueroot.net>
* @version $Revision: 1.6 $
* The full javascript to be sent to the browser
* The text encrypted - without any js
* The number to offset the text by
* Whether or not to use JS for encryption or simple html
* a preg expression for an <a href=mailto: ... tag
* a preg expression for an email
* @param string $text The text to encrypt
* @param int $offset The offset used to encrypt/decrypt
* @param boolean $JS If javascript shall be used on the client side
function HTML_Crypt($text = '', $offset = 3 , $JS = true )
$this->emailpreg = '[-_a-z0-9]+(\.[-_a-z0-9]+)*@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}';
$this->apreg = '\<[aA]\shref=[\"\']mailto:.*\<\/[aA]\>';
* Set name of the current realm
* @param string $text The text to be encrypted
* Turns the text into a mailto link (make sure
* the text only contains an email)
$this->text = '<a href="mailto:'. $email. '">'. $email. '</a>';
* @param string $text Text to encrypt
* @param int $offset Offset to use for encryption
* @return string Encrypted text
function cryptText ($text, $offset)
for ($i=0; $i < $length; $i++ ) {
$num = ord($current_chr);
$inter = $num + $this->offset;
$inter = ($inter - 32 ) % 95 + 32;
$enc_string .= ($enc_char == '\\' ? '\\\\' : $enc_char);
$enc_string .= $current_chr;
* Returns the script html source including the function
* @return string $script The javascript generated
// get a random string to use as a function name
$letters = array ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
// the actual js (in one line to confuse)
$script = '<script language="JavaScript" type="text/javascript">/*<![CDATA[*/'
. 'function ' . $rnd . '(s){'
. 'for(i=0;i<s.length;i++){'
. 'n=n-' . $this->offset . ';'
. 'r+=String.fromCharCode(n);'
. 'document.write(' . $rnd . '(a));'
* Outputs the full JS to the browser
* Returns the encrypted text.
* @return string Encrypted text
return str_replace(array ('@', '.'), array (' ^at^ ', '-dot-'), $this->text);
$c = new HTML_Crypt($text[0]);
Documentation generated on Mon, 11 Mar 2019 14:48:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|