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 / Text / CAPTCHA / Driver / Figlet.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  6.5 KB  |  264 lines

  1. <?php
  2. /**
  3.  *
  4.  * Require Figlet class for rendering the text.
  5.  *
  6.  */
  7. require_once 'Text/CAPTCHA.php';
  8. require_once 'Text/Figlet.php';
  9.  
  10.  
  11. /**
  12.  * Text_CAPTCHA_Driver_Figlet - Text_CAPTCHA driver Figlet based CAPTCHAs
  13.  *
  14.  * @license PHP License, version 3.0
  15.  * @author Aaron Wormus <wormus@php.net>
  16.  * @author Christian Wenz <wenz@php.net>
  17.  * @todo define an obfuscation algorithm 
  18.  */
  19.  
  20. class Text_CAPTCHA_Driver_Figlet extends Text_CAPTCHA
  21. {
  22.     /**
  23.      * Text_Figlet object
  24.      *
  25.      * @access private
  26.      * @var resource
  27.      */
  28.     var $_fig;
  29.  
  30.     /**
  31.      * Width of CAPTCHA
  32.      *
  33.      * @access private
  34.      * @var int
  35.      */
  36.     var $_width;
  37.  
  38.     /**
  39.      * Figlet output string
  40.      *
  41.      * @access private
  42.      * @var string
  43.      */
  44.     var $_output_string;
  45.  
  46.      /**
  47.      * Figlet font options
  48.      *
  49.      * @access private
  50.      * @var array
  51.      */
  52.     var $_fonts = array();
  53.  
  54.     /**
  55.      * Figlet font
  56.      *
  57.      * @access private
  58.      * @var string
  59.      */
  60.     var $_font;
  61.    
  62.     /**
  63.      * Figlet font
  64.      *
  65.      * @access private
  66.      * @var array
  67.      */
  68.     var $_style = array();
  69.     
  70.     /**
  71.      * Output Format
  72.      *
  73.      * @access private
  74.      * @var string
  75.      */
  76.     var $_output;
  77.  
  78.     /**
  79.      * Last error
  80.      *
  81.      * @access protected
  82.      * @var PEAR_Error
  83.      */
  84.     var $_error = null;
  85.  
  86.     /**
  87.      * init function
  88.      *
  89.      * Initializes the new Text_CAPTCHA_Driver_Figlet object and creates a GD image
  90.      *
  91.      * @param   array   $options    CAPTCHA options
  92.      * @access public
  93.      * @return  mixed   true upon success, PEAR error otherwise
  94.      */
  95.     function init($options = array())
  96.     {
  97.         if (is_array($options)) {
  98.             if (!empty($options['output'])){
  99.               $this->_output = $options['output'];
  100.             } else {
  101.               $this->_output = 'html';
  102.             }
  103.          
  104.             if (isset($options['width']) && is_int($options['width'])) {
  105.               $this->_width = $options['width'];
  106.             } else {
  107.               $this->_width = 200; 
  108.             }
  109.  
  110.             if (!empty($options['length'])){
  111.                 $this->_length = $options['length'];
  112.             } else {
  113.                 $this->_length = 6;
  114.             }
  115.             
  116.             if (!isset($options['phrase']) || empty($options['phrase'])) {
  117.                 $this->_createPhrase($this->_length);
  118.             } else {
  119.                 $this->_phrase = $options['phrase'];
  120.             }
  121.         }
  122.         
  123.         if (empty($options['options']) || !is_array($options['options'])){
  124.             die;
  125.         } else {
  126.             if (!empty($options['options']['style']) && is_array($options['options']['style'])){
  127.                 $this->_style = $options['options']['style'];
  128.             }
  129.             
  130.             if (empty($this->style['padding'])){
  131.                 $this->_style['padding'] = '5px';    
  132.             }
  133.             
  134.             if (!empty($options['options']['font_file'])){
  135.                 if (is_array($options['options']['font_file'])){
  136.                     $this->_font = $options['options']['font_file'][array_rand($options['options']['font_file'])];
  137.                 } else {
  138.                     $this->_font = $options['options']['font_file'];
  139.                 }
  140.             }
  141.         }
  142.     }
  143.  
  144.     /**
  145.      * Create random CAPTCHA phrase
  146.      * This method creates a random phrase
  147.      *
  148.      * @access  private
  149.      */
  150.     function _createPhrase()
  151.     {
  152.         $this->_phrase = Text_Password::create($this->_length);
  153.     }
  154.  
  155.     /**
  156.      * Create CAPTCHA image
  157.      *
  158.      * This method creates a CAPTCHA image
  159.      *
  160.      * @access  private
  161.      * @return  void   PEAR_Error on error
  162.      */
  163.     function _createCAPTCHA()
  164.  
  165.     {
  166.         $this->_fig = new Text_Figlet();
  167.         
  168.         if (PEAR::isError($this->_fig->LoadFont($this->_font))){
  169.             $this->_error = PEAR::raiseError('Error loading Text_Figlet font');
  170.             return $this->_error;
  171.         }
  172.  
  173.           $this->_output_string = $this->_fig->LineEcho($this->_phrase);        
  174.     }
  175.  
  176.     /**
  177.      * Return CAPTCHA in the specified format
  178.      *
  179.      * This method returns the CAPTCHA depending on the output format
  180.      *
  181.      * @access  public
  182.      * @return  mixed        Formatted captcha or PEAR error
  183.      */
  184.     function getCAPTCHA()
  185.     {
  186.         $retval = $this->_createCAPTCHA();
  187.         if (PEAR::isError($retval)) {
  188.             return PEAR::raiseError($retval->getMessage());
  189.         }
  190.  
  191.         switch ($this->_output) {
  192.             case 'text':
  193.                 return $this->_output_string;
  194.                 break;
  195.             case 'html':
  196.                 return $this->getCAPTCHAAsHTML();
  197.                 break; 
  198.             case 'javascript':
  199.                 return $this->getCAPTCHAAsJavascript();
  200.                 break;
  201.         }
  202.     }
  203.  
  204.     /**
  205.      * Return CAPTCHA as HTML
  206.      *
  207.      * This method returns the CAPTCHA as HTML
  208.      *
  209.      * @access  public
  210.      * @return  mixed        HTML Figlet image or PEAR error
  211.      */
  212.     function getCAPTCHAAsHTML()
  213.     {
  214.         $retval = $this->_createCAPTCHA();
  215.         if (PEAR::isError($retval)) {
  216.             return PEAR::raiseError($retval->getMessage());
  217.         }
  218.         
  219.         $charwidth = strpos($this->_output_string, "\n");
  220.         $data = str_replace("\n", '<br />', $this->_output_string);
  221.  
  222.         $textsize = ($this->_width / $charwidth) * 1.4;
  223.         
  224.         $css_output = "";
  225.         foreach ($this->_style as $key => $value){
  226.             $css_output .= "$key: $value;"; 
  227.         }
  228.         
  229.         $htmloutput = '<div style="font-family: courier; 
  230.           font-size: '.$textsize.'px; 
  231.           width:'.$this->_width.'px; 
  232.           text-align:center;">';
  233.         $htmloutput .= '<div style="'.$css_output.'margin:0px;">
  234.           <pre style="padding: 0px; margin: 0px;">'. $data. '</pre></div></div>';
  235.  
  236.         return $htmloutput; 
  237.     }
  238.  
  239.     /**
  240.      * Return CAPTCHA as Javascript version of HTML
  241.      *
  242.      * This method returns the CAPTCHA as a Javascript string
  243.      * I'm not exactly sure what the point of doing this would be.
  244.      *
  245.      * @access  public
  246.      * @return  mixed        javascript string or PEAR error
  247.      */
  248.     function getCAPTCHAAsJavascript()
  249.     {
  250.         $data = $this->getCAPTCHAAsHTML();
  251.         if (PEAR::isError($data)) {
  252.             return PEAR::raiseError($data->getMessage());
  253.         }
  254.         
  255.         $obfus_data = rawurlencode($data);
  256.         
  257.         $javascript = "<script language=\"javascript\">
  258.           document.write(unescape(\"$obfus_data.\" ) );
  259.           </script>";
  260.         
  261.         return $javascript;
  262.     }
  263. }
  264.