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

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