home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Crypt.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  8.0 KB  |  297 lines

  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.      * This method has an optional parameter that allows you
  153.      * to customize the email tag and tag text. To get the email
  154.      * address included, use "%email%' as template variable.
  155.      *
  156.      * @param string $template  Mailto template string
  157.      * @access public
  158.      */
  159.     function addMailTo($template = '<a href="mailto:%email%">%email%</a>')
  160.     {
  161.         $email = $this->text;
  162.         $this->text = str_replace('%email%', $email, $template);
  163.     }
  164.  
  165.     // }}}
  166.     // {{{ cryptText()
  167.  
  168.     /**
  169.      * Encrypts the text
  170.      *
  171.      * @param string    $text       Text to encrypt
  172.      * @param int       $offset     Offset to use for encryption
  173.      * @return string   Encrypted text
  174.      * @access private
  175.      */
  176.     function cryptText($text, $offset)
  177.     {
  178.         $enc_string = '';
  179.         $length = strlen($this->text);
  180.  
  181.         for ($i=0; $i < $length; $i++) {
  182.             $current_chr = substr($this->text, $i, 1);
  183.             $num = ord($current_chr);
  184.             if ($num < 128) {
  185.                 $inter = $num + $this->offset;
  186.                 if ($inter > 127) {
  187.                     $inter = ($inter - 32) % 95 + 32;
  188.                 }
  189.                 $enc_char =  chr($inter);
  190.                 $enc_string .= ($enc_char == '\\' ? '\\\\' : $enc_char);
  191.             } else {
  192.                 $enc_string .= $current_chr;
  193.             }
  194.         }
  195.         return $enc_string;
  196.     }
  197.  
  198.     // }}}
  199.     // {{{ getScript()
  200.  
  201.     /**
  202.      * Returns the script html source including the function
  203.      * to decrypt it.
  204.      *
  205.      * @access public
  206.      * @return string $script The javascript generated
  207.      */
  208.     function getScript()
  209.     {
  210.         if ($this->cryptString == '' && $this->text != '') {
  211.             $this->cryptString = $this->cryptText($this->text, $this->offset);
  212.         }
  213.         // get a random string to use as a function name
  214.         srand((float) microtime() * 10000000);
  215.         $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');
  216.         $rnd = $letters[array_rand($letters)] . md5(time());
  217.         // the actual js (in one line to confuse)
  218.         $script = '<script language="JavaScript" type="text/javascript">/*<![CDATA[*/'
  219.             . 'var a,s,n;'
  220.             . 'function ' . $rnd . '(s){'
  221.                 . 'r="";'
  222.                 . 'for(i=0;i<s.length;i++){'
  223.                     . 'n=s.charCodeAt(i);'
  224.                     . 'if(n<128){'
  225.                         . 'n=n-' . $this->offset . ';'
  226.                         . 'if(n<32){'
  227.                             . 'n=127+(n-32);'
  228.                         . '}'
  229.                     . '}'
  230.                     . 'r+=String.fromCharCode(n);'
  231.                 . '}'
  232.                 . 'return r;'
  233.             . '}'
  234.             . 'a="' . str_replace('"', '\\"', $this->cryptString) . '";'
  235.             . 'document.write(' . $rnd . '(a));'
  236.             . '//]]></script>';
  237.         $this->script = $script;
  238.         return $script;
  239.     }
  240.  
  241.     // }}}
  242.     // {{{ output()
  243.  
  244.     /**
  245.      * Outputs the full JS to the browser
  246.      *
  247.      * @access public
  248.      */
  249.     function output()
  250.     {
  251.         echo $this->getOutput();
  252.     }
  253.  
  254.     // }}}
  255.     // {{{ getOutput()
  256.  
  257.     /**
  258.     *   Returns the encrypted text.
  259.     *
  260.     *   @return string  Encrypted text
  261.     */
  262.     function getOutput()
  263.     {
  264.         if ($this->useJS) {
  265.             if ($this->script == '') {
  266.                 $this->getScript();
  267.             }
  268.             return $this->script;
  269.         } else {
  270.             return str_replace(array('@', '.'), array(' ^at^ ', '-dot-'), $this->text);
  271.         }
  272.     }
  273.  
  274.     // }}}
  275.  
  276.     function obStart()
  277.     {
  278.         ob_start();
  279.     }
  280.  
  281.     function obEnd()
  282.     {
  283.         $text = ob_get_contents();
  284.         $text = preg_replace_callback("/{$this->apreg}/", array($this, '_fly'), $text);
  285.         ob_end_clean();
  286.         echo $text;
  287.     }
  288.  
  289.     function _fly($text)
  290.     {
  291.         $c = new HTML_Crypt($text[0]);
  292.         $c->setText($text[0]);
  293.         return $c->getScript();
  294.     }
  295. }
  296. ?>
  297.