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 BSD License
  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.                 $phraseoptions (isset($options['phraseOptions']&& is_array($options['phraseOptions'])) $options['phraseOptions': array();
  117.                 $this->_createPhrase($options);
  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($options)
  151.     {
  152.         if (!is_array($options|| count($options=== 0{
  153.             $this->_phrase = Text_Password::create($this->_length);
  154.         else {
  155.             if (count($options=== 1{
  156.                 $this->_phrase = Text_Password::create($this->_length$options[0]);
  157.             else {
  158.                 $this->_phrase = Text_Password::create($this->_length$options[0]$options[1]);
  159.             }
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * Create CAPTCHA image
  165.      *
  166.      * This method creates a CAPTCHA image
  167.      *
  168.      * @access  private
  169.      * @return  void   PEAR_Error on error
  170.      */
  171.     function _createCAPTCHA()
  172.  
  173.     {
  174.         $this->_fig = new Text_Figlet();
  175.         
  176.         if (PEAR::isError($this->_fig->LoadFont($this->_font))){
  177.             $this->_error = PEAR::raiseError('Error loading Text_Figlet font');
  178.             return $this->_error;
  179.         }
  180.  
  181.           $this->_output_string $this->_fig->LineEcho($this->_phrase);        
  182.     }
  183.  
  184.     /**
  185.      * Return CAPTCHA in the specified format
  186.      *
  187.      * This method returns the CAPTCHA depending on the output format
  188.      *
  189.      * @access  public
  190.      * @return  mixed        Formatted captcha or PEAR error
  191.      */
  192.     function getCAPTCHA()
  193.     {
  194.         $retval $this->_createCAPTCHA();
  195.         if (PEAR::isError($retval)) {
  196.             return PEAR::raiseError($retval->getMessage());
  197.         }
  198.  
  199.         switch ($this->_output{
  200.             case 'text':
  201.                 return $this->_output_string;
  202.                 break;
  203.             case 'html':
  204.                 return $this->getCAPTCHAAsHTML();
  205.                 break; 
  206.             case 'javascript':
  207.                 return $this->getCAPTCHAAsJavascript();
  208.                 break;
  209.         }
  210.     }
  211.  
  212.     /**
  213.      * Return CAPTCHA as HTML
  214.      *
  215.      * This method returns the CAPTCHA as HTML
  216.      *
  217.      * @access  public
  218.      * @return  mixed        HTML Figlet image or PEAR error
  219.      */
  220.     function getCAPTCHAAsHTML()
  221.     {
  222.         $retval $this->_createCAPTCHA();
  223.         if (PEAR::isError($retval)) {
  224.             return PEAR::raiseError($retval->getMessage());
  225.         }
  226.         
  227.         $charwidth strpos($this->_output_string"\n");
  228.         $data str_replace("\n"'<br />'$this->_output_string);
  229.  
  230.         $textsize ($this->_width $charwidth* 1.4;
  231.         
  232.         $css_output "";
  233.         foreach ($this->_style as $key => $value){
  234.             $css_output .= "$key$value;"; 
  235.         }
  236.         
  237.         $htmloutput '<div style="font-family: courier; 
  238.           font-size: '.$textsize.'px; 
  239.           width:'.$this->_width.'px; 
  240.           text-align:center;">';
  241.         $htmloutput .= '<div style="'.$css_output.'margin:0px;">
  242.           <pre style="padding: 0px; margin: 0px;">'$data'</pre></div></div>';
  243.  
  244.         return $htmloutput
  245.     }
  246.  
  247.     /**
  248.      * Return CAPTCHA as Javascript version of HTML
  249.      *
  250.      * This method returns the CAPTCHA as a Javascript string
  251.      * I'm not exactly sure what the point of doing this would be.
  252.      *
  253.      * @access  public
  254.      * @return  mixed        javascript string or PEAR error
  255.      */
  256.     function getCAPTCHAAsJavascript()
  257.     {
  258.         $data $this->getCAPTCHAAsHTML();
  259.         if (PEAR::isError($data)) {
  260.             return PEAR::raiseError($data->getMessage());
  261.         }
  262.         
  263.         $obfus_data rawurlencode($data);
  264.         
  265.         $javascript = "<script language=\"javascript\">
  266.           document.write(unescape(\"$obfus_data.\" ) );
  267.           </script>";
  268.         
  269.         return $javascript;
  270.     }
  271. }

Documentation generated on Mon, 11 Mar 2019 15:32:35 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.