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

Source for file Figlet.php

Documentation is available at Figlet.php

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

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