HTML_Crypt
[ class tree: HTML_Crypt ] [ index: HTML_Crypt ] [ all elements ]

Source for file Crypt.php

Documentation is available at Crypt.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2002 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.02 of the PHP license,      |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Michael Dransfield <mike@blueroot.net>                      |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Crypt.php,v 1.6 2006/11/26 21:48:42 cweiske Exp $
  19.  
  20.  
  21. /**
  22.  * HTML_Crypt
  23.  *
  24.  * HTML_Crypt provides methods to encrypt text, which
  25.  * can be later be decrypted using JavaScript on the client side
  26.  *
  27.  * This is very useful to prevent spam robots collecting email
  28.  * addresses from your site, included is a method to add mailto
  29.  * links to the text being generated.
  30.  *
  31.  * The "encryption" function is basically works like ROT13,
  32.  * with the difference that the $offset setting replaces the 13.
  33.  * It is also limited to ASCII characters between 32 and 127 (included).
  34.  * Other characters will not be encrypted.
  35.  *
  36.  * a basic example to encrypt an email address
  37.  * $c = new HTML_Crypt('yourname@emailaddress.com', 8);
  38.  * $c->addMailTo();
  39.  * $c->output();
  40.  *
  41.  * @author  Michael Dransfield <mike@blueroot.net>
  42.  * @package HTML_Crypt
  43.  * @version $Revision: 1.6 $
  44.  */
  45. class HTML_Crypt
  46. {
  47.     // {{{ properties
  48.  
  49.     /**
  50.      * The unencrypted text
  51.      *
  52.      * @access public
  53.      * @var    string 
  54.      * @see    setText()
  55.      */
  56.     var $text = '';
  57.  
  58.     /**
  59.      * The full javascript to be sent to the browser
  60.      *
  61.      * @access public
  62.      * @var    string 
  63.      * @see    getScript()
  64.      */
  65.     var $script = '';
  66.  
  67.  
  68.     /**
  69.      * The text encrypted - without any js
  70.      *
  71.      * @access public
  72.      * @var    string 
  73.      * @see    cyrptText
  74.      */
  75.     var $cryptString = '';
  76.  
  77.  
  78.     /**
  79.      * The number to offset the text by
  80.      *
  81.      * @access public
  82.      * @var    int 
  83.      */
  84.     var $offset;
  85.  
  86.     /**
  87.      * Whether or not to use JS for encryption or simple html
  88.      *
  89.      * @access public
  90.      * @var    int 
  91.      */
  92.     var $useJS;
  93.  
  94.     /**
  95.      * a preg expression for an <a href=mailto: ... tag
  96.      *
  97.      * @access public
  98.      * @var    string 
  99.      */
  100.      var $apreg;
  101.  
  102.     /**
  103.      * a preg expression for an email
  104.      *
  105.      * @access public
  106.      * @var    string 
  107.      */
  108.     var $emailpreg;
  109.  
  110.     // }}}
  111.     // {{{ HTML_Crypt()
  112.  
  113.     /**
  114.      * Constructor
  115.      *
  116.      * @access public
  117.      * @param string    $text       The text to encrypt
  118.      * @param int       $offset     The offset used to encrypt/decrypt
  119.      * @param boolean   $JS         If javascript shall be used on the client side
  120.      */
  121.     function HTML_Crypt($text ''$offset = 3$JS = true)
  122.     {
  123.         $this->offset = $offset % 95;
  124.         $this->text = $text;
  125.         $this->script = '';
  126.         $this->useJS = $JS;
  127.         $this->emailpreg = '[-_a-z0-9]+(\.[-_a-z0-9]+)*@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}';
  128.         $this->apreg = '\<[aA]\shref=[\"\']mailto:.*\<\/[aA]\>';
  129.     }
  130.  
  131.     // }}}
  132.     // {{{ setText()
  133.  
  134.     /**
  135.      * Set name of the current realm
  136.      *
  137.      * @access public
  138.      * @param  string $text  The text to be encrypted
  139.      */
  140.     function setText($text)
  141.     {
  142.         $this->text = $text;
  143.     }
  144.  
  145.     // }}}
  146.     // {{{ addMailTo()
  147.  
  148.     /**
  149.      * Turns the text into a mailto link (make sure
  150.      * the text only contains an email)
  151.      *
  152.      * @access public
  153.      */
  154.     function addMailTo()
  155.     {
  156.         $email $this->text;
  157.         $this->text = '<a href="mailto:'.$email.'">'.$email.'</a>';
  158.     }
  159.  
  160.     // }}}
  161.     // {{{ cryptText()
  162.  
  163.     /**
  164.      * Encrypts the text
  165.      *
  166.      * @param string    $text       Text to encrypt
  167.      * @param int       $offset     Offset to use for encryption
  168.      * @return string   Encrypted text
  169.      * @access private
  170.      */
  171.     function cryptText($text$offset)
  172.     {
  173.         $enc_string '';
  174.         $length strlen($this->text);
  175.  
  176.         for ($i=0; $i $length$i++{
  177.             $current_chr substr($this->text$i1);
  178.             $num ord($current_chr);
  179.             if ($num < 128{
  180.                 $inter $num $this->offset;
  181.                 if ($inter > 127{
  182.                     $inter ($inter - 32% 95 + 32;
  183.                 }
  184.                 $enc_char =  chr($inter);
  185.                 $enc_string .= ($enc_char == '\\' '\\\\' $enc_char);
  186.             else {
  187.                 $enc_string .= $current_chr;
  188.             }
  189.         }
  190.         return $enc_string;
  191.     }
  192.  
  193.     // }}}
  194.     // {{{ getScript()
  195.  
  196.     /**
  197.      * Returns the script html source including the function
  198.      * to decrypt it.
  199.      *
  200.      * @access public
  201.      * @return string $script The javascript generated
  202.      */
  203.     function getScript()
  204.     {
  205.         if ($this->cryptString == '' && $this->text != ''{
  206.             $this->cryptString = $this->cryptText($this->text$this->offset);
  207.         }
  208.         // get a random string to use as a function name
  209.         srand((float) microtime(* 10000000);
  210.         $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');
  211.         $rnd $letters[array_rand($letters)md5(time());
  212.         // the actual js (in one line to confuse)
  213.         $script '<script language="JavaScript" type="text/javascript">/*<![CDATA[*/'
  214.             . 'var a,s,n;'
  215.             . 'function ' $rnd '(s){'
  216.                 . 'r="";'
  217.                 . 'for(i=0;i<s.length;i++){'
  218.                     . 'n=s.charCodeAt(i);'
  219.                     . 'if(n<128){'
  220.                         . 'n=n-' $this->offset . ';'
  221.                         . 'if(n<32){'
  222.                             . 'n=127+(n-32);'
  223.                         . '}'
  224.                     . '}'
  225.                     . 'r+=String.fromCharCode(n);'
  226.                 . '}'
  227.                 . 'return r;'
  228.             . '}'
  229.             . 'a="' str_replace('"''\\"'$this->cryptString'";'
  230.             . 'document.write(' $rnd '(a));'
  231.             . '//]]></script>';
  232.         $this->script = $script;
  233.         return $script;
  234.     }
  235.  
  236.     // }}}
  237.     // {{{ output()
  238.  
  239.     /**
  240.      * Outputs the full JS to the browser
  241.      *
  242.      * @access public
  243.      */
  244.     function output()
  245.     {
  246.         echo $this->getOutput();
  247.     }
  248.  
  249.     // }}}
  250.     // {{{ getOutput()
  251.  
  252.     /**
  253.     *   Returns the encrypted text.
  254.     *
  255.     *   @return string  Encrypted text
  256.     */
  257.     function getOutput()
  258.     {
  259.         if ($this->useJS{
  260.             if ($this->script == ''{
  261.                 $this->getScript();
  262.             }
  263.             return $this->script;
  264.         else {
  265.             return str_replace(array('@''.')array(' ^at^ ''-dot-')$this->text);
  266.         }
  267.     }
  268.  
  269.     // }}}
  270.  
  271.     function obStart()
  272.     {
  273.         ob_start();
  274.     }
  275.  
  276.     function obEnd()
  277.     {
  278.         $text ob_get_contents();
  279.         $text preg_replace_callback("/{$this->apreg}/", array($this, '_fly'), $text);
  280.         ob_end_clean();
  281.         echo $text;
  282.     }
  283.  
  284.     function _fly($text)
  285.     {
  286.         $c = new HTML_Crypt($text[0]);
  287.         $c->setText($text[0]);
  288.         return $c->getScript();
  289.     }
  290. }

Documentation generated on Mon, 11 Mar 2019 14:48:56 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.