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 / Image.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  10.1 KB  |  362 lines

  1. <?php
  2. /**
  3.  *
  4.  * Require Image_Text class for generating the text.
  5.  *
  6.  */
  7. require_once 'Text/CAPTCHA.php';
  8. require_once 'Image/Text.php';
  9.  
  10. /**
  11.  * Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
  12.  *
  13.  * Class to create a graphical Turing test 
  14.  *
  15.  * 
  16.  * @license PHP License, version 3.0
  17.  * @author Christian Wenz <wenz@php.net>
  18.  * @todo refine the obfuscation algorithm :-)
  19.  * @todo learn how to use Image_Text better (or remove dependency)
  20.  */
  21.  
  22. class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
  23. {
  24.  
  25.     /**
  26.      * Image object
  27.      *
  28.      * @access private
  29.      * @var resource
  30.      */
  31.     var $_im;
  32.  
  33.     /**
  34.      * Image_Text object
  35.      *
  36.      * @access private
  37.      * @var resource
  38.      */
  39.     var $_imt;
  40.  
  41.     /**
  42.      * Width of CAPTCHA
  43.      *
  44.      * @access private
  45.      * @var int
  46.      */
  47.     var $_width;
  48.  
  49.     /**
  50.      * Height of CAPTCHA
  51.      *
  52.      * @access private
  53.      * @var int
  54.      */
  55.     var $_height;
  56.  
  57.     /**
  58.      * CAPTCHA output format
  59.      *
  60.      * @access private
  61.      * @var string
  62.      */
  63.     var $_output;
  64.  
  65.     /**
  66.      * Further options (here: for Image_Text)
  67.      *
  68.      * @access private
  69.      * @var array
  70.      */
  71.     var $_imageOptions = array(
  72.         'font_size'        => 24,
  73.         'font_path'        => './',
  74.         'font_file'        => 'COUR.TTF',
  75.         'text_color'       => '#000000',
  76.         'lines_color'      => '#CACACA',
  77.         'background_color' => '#555555');
  78.         
  79.     /**
  80.      * Whether the immage resource has been created
  81.      *
  82.      * @access private
  83.      * @var boolean
  84.      */
  85.     var $_created = false;
  86.  
  87.     /**
  88.      * Last error
  89.      *
  90.      * @access protected
  91.      * @var PEAR_Error
  92.      */
  93.     var $_error = null;
  94.  
  95.     /**
  96.      * init function
  97.      *
  98.      * Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
  99.      *
  100.      * @param   array   $options    CAPTCHA options
  101.      * @access public
  102.      * @return  mixed   true upon success, PEAR error otherwise
  103.      */
  104.     function init($options = array())
  105.     {
  106.         if (!is_array($options)) {
  107.             // Compatibility mode ... in future versions, these two
  108.             // lines of code will be used: 
  109.             // $this->_error = PEAR::raiseError('You need to provide a set of CAPTCHA options!');
  110.             // return $this->_error;                  
  111.             $o = array();
  112.             $args = func_get_args();
  113.             if (isset($args[0])) {
  114.                 $o['width'] = $args[0];
  115.             }    
  116.             if (isset($args[1])) {
  117.                 $o['height'] = $args[1];
  118.             }    
  119.             if (isset($args[2]) && $args[2] != null) {
  120.                 $o['phrase'] = $args[2];
  121.             }
  122.             if (isset($args[3]) && is_array($args[3])) {
  123.                 $o['imageOptions'] = $args[3];
  124.             }
  125.             $options = $o;
  126.         }
  127.         if (is_array($options)) { 
  128.             if (isset($options['width']) && is_int($options['width'])) {
  129.               $this->_width = $options['width'];
  130.             } else {
  131.               $this->_width = 200; 
  132.             }
  133.             if (isset($options['height']) && is_int($options['height'])) {
  134.               $this->_height = $options['height'];
  135.             } else {
  136.               $this->_height = 80; 
  137.             }
  138.             if (!isset($options['phrase']) || empty($options['phrase'])) {
  139.                 $this->_createPhrase();
  140.             } else {
  141.                 $this->_phrase = $options['phrase'];
  142.             }
  143.             if (!isset($options['output']) || empty($options['output'])) {
  144.                 $this->_output = 'resource';
  145.             } else {
  146.                 $this->_output = $options['output'];
  147.             } 
  148.             if (isset($options['imageOptions']) && is_array($options['imageOptions']) && count($options['imageOptions']) > 0) {
  149.                 $this->_imageOptions = array_merge($this->_imageOptions, $options['imageOptions']); 
  150.             }
  151.             return true;
  152.         }
  153.     }
  154.  
  155.     /**
  156.      * Create random CAPTCHA phrase, Image edition (with size check)
  157.      *
  158.      * This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller
  159.      *
  160.      * @access  private
  161.      */
  162.     function _createPhrase()
  163.     {
  164.         $len = intval(min(8, $this->_width / 25));
  165.         $this->_phrase = Text_Password::create($len);
  166.         $this->_created = false;
  167.     }
  168.  
  169.     /**
  170.      * Create CAPTCHA image
  171.      *
  172.      * This method creates a CAPTCHA image
  173.      *
  174.      * @access  private
  175.      * @return  void   PEAR_Error on error
  176.      */
  177.     function _createCAPTCHA()
  178.     {
  179.         if ($this->_error) {
  180.             return $this->_error;
  181.         }
  182.         if ($this->_created) {
  183.             return;
  184.         }
  185.         $options['canvas'] = array(
  186.             'width' => $this->_width,
  187.             'height' => $this->_height
  188.         ); 
  189.         $options['width'] = $this->_width - 20;
  190.         $options['height'] = $this->_height - 20; 
  191.         $options['cx'] = ceil(($this->_width) / 2 + 10);
  192.         $options['cy'] = ceil(($this->_height) / 2 + 10); 
  193.         $options['angle'] = rand(0, 30) - 15;
  194.         $options['font_size'] = $this->_imageOptions['font_size'];
  195.         $options['font_path'] = $this->_imageOptions['font_path'];
  196.         $options['font_file'] = $this->_imageOptions['font_file'];
  197.         $options['color'] = array($this->_imageOptions['text_color']);
  198.         $options['background_color'] = $this->_imageOptions['background_color'];
  199.         $options['max_lines'] = 1;
  200.         $options['mode'] = 'auto';
  201.         $this->_imt = new Image_Text( 
  202.             $this->_phrase,
  203.             $options
  204.         );
  205.         if (PEAR::isError($e = $this->_imt->init())) {
  206.             $this->_error = PEAR::raiseError(
  207.                 sprintf('Error initializing Image_Text (%s)',
  208.                 $e->getMessage()));
  209.             return $this->_error;
  210.         } else {
  211.             $this->_created = true; 
  212.         }
  213.         $this->_imt->measurize();
  214.         $this->_imt->render(); 
  215.         $this->_im =& $this->_imt->getImg(); 
  216.         $colors = $this->_imt->_convertString2RGB($this->_imageOptions['lines_color']);
  217.         $lines_color = imagecolorallocate($this->_im, $colors['r'], $colors['g'], $colors['b']);
  218.         //some obfuscation
  219.         for ($i = 0; $i < 3; $i++) {
  220.             $x1 = rand(0, $this->_width - 1);
  221.             $y1 = rand(0, round($this->_height / 10, 0));
  222.             $x2 = rand(0, round($this->_width / 10, 0));
  223.             $y2 = rand(0, $this->_height - 1);
  224.             imageline($this->_im, $x1, $y1, $x2, $y2, $lines_color);
  225.             $x1 = rand(0, $this->_width - 1);
  226.             $y1 = $this->_height - rand(1, round($this->_height / 10, 0));
  227.             $x2 = $this->_width - rand(1, round($this->_width / 10, 0));
  228.             $y2 = rand(0, $this->_height - 1);
  229.             imageline($this->_im, $x1, $y1, $x2, $y2, $lines_color);
  230.             $cx = rand(0, $this->_width - 50) + 25;
  231.             $cy = rand(0, $this->_height - 50) + 25;
  232.             $w = rand(1, 24);
  233.             imagearc($this->_im, $cx, $cy, $w, $w, 0, 360, $lines_color);
  234.         }
  235.     }
  236.  
  237.     /**
  238.      * Return CAPTCHA as image resource
  239.      *
  240.      * This method returns the CAPTCHA depending on the output format
  241.      *
  242.      * @access  public
  243.      * @return  mixed        image resource or PEAR error
  244.      */
  245.     function getCAPTCHA()
  246.     {
  247.         $retval = $this->_createCAPTCHA();
  248.         if (PEAR::isError($retval)) {
  249.             return PEAR::raiseError($retval->getMessage());
  250.         }
  251.         
  252.         if ($this->_output == 'gif' && !function_exists('imagegif')) {
  253.             $this->_output = 'png';
  254.         }
  255.  
  256.         switch ($this->_output) {
  257.             case 'png':
  258.                 return $this->getCAPTCHAAsPNG();
  259.                 break;
  260.             case 'jpg': 
  261.             case 'jpeg':
  262.                 return $this->getCAPTCHAAsJPEG();
  263.                 break;
  264.             case 'gif':
  265.                 return $this->getCAPTCHAAsGIF();
  266.                 break;
  267.             case 'resource':
  268.             default:
  269.                 return $this->_im;
  270.         }
  271.     }
  272.  
  273.     /**
  274.      * Return CAPTCHA as PNG
  275.      *
  276.      * This method returns the CAPTCHA as PNG
  277.      *
  278.      * @access  public
  279.      * @return  mixed        image contents or PEAR error
  280.      */
  281.     function getCAPTCHAAsPNG()
  282.     {
  283.         $retval = $this->_createCAPTCHA();
  284.         if (PEAR::isError($retval)) {
  285.             return PEAR::raiseError($retval->getMessage());
  286.         }
  287.  
  288.         if (is_resource($this->_im)) {
  289.             ob_start();
  290.             imagepng($this->_im);
  291.             $data = ob_get_contents();
  292.             ob_end_clean();
  293.             return $data;
  294.         } else {
  295.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  296.             return $this->_error;
  297.         }
  298.     }
  299.  
  300.     /**
  301.      * Return CAPTCHA as JPEG
  302.      *
  303.      * This method returns the CAPTCHA as JPEG
  304.      *
  305.      * @access  public
  306.      * @return  mixed        image contents or PEAR error
  307.      */
  308.     function getCAPTCHAAsJPEG()
  309.     {
  310.         $retval = $this->_createCAPTCHA();
  311.         if (PEAR::isError($retval)) {
  312.             return PEAR::raiseError($retval->getMessage());
  313.         }
  314.  
  315.         if (is_resource($this->_im)) {
  316.             ob_start();
  317.             imagejpeg($this->_im);
  318.             $data = ob_get_contents();
  319.             ob_end_clean();
  320.             return $data;
  321.         } else {
  322.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  323.             return $this->_error;
  324.         }
  325.     }
  326.  
  327.     /**
  328.      * Return CAPTCHA as GIF
  329.      *
  330.      * This method returns the CAPTCHA as GIF
  331.      *
  332.      * @access  public
  333.      * @return  mixed        image contents or PEAR error
  334.      */
  335.     function getCAPTCHAAsGIF()
  336.     {
  337.         $retval = $this->_createCAPTCHA();
  338.         if (PEAR::isError($retval)) {
  339.             return PEAR::raiseError($retval->getMessage());
  340.         }
  341.  
  342.         if (is_resource($this->_im)) {
  343.             ob_start();
  344.             imagegif($this->_im);
  345.             $data = ob_get_contents();
  346.             ob_end_clean();
  347.             return $data;
  348.         } else {
  349.             $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)');
  350.             return $this->_error;
  351.         }
  352.     }
  353.  
  354.     /**
  355.      * __wakeup method (PHP 5 only)
  356.      */
  357.     function __wakeup()
  358.     {
  359.         $this->_created = false;
  360.     } 
  361. }
  362.