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.php next >
Encoding:
PHP Script  |  2008-07-02  |  6.0 KB  |  254 lines

  1. <?php
  2. /**
  3.  * Text_CAPTCHA - creates a CAPTCHA for Turing tests
  4.  *
  5.  * Class to create a Turing test for websites by
  6.  * creating an image, ASCII art or something else 
  7.  * with some (obfuscated) characters 
  8.  *
  9.  * 
  10.  * @package Text_CAPTCHA
  11.  * @license PHP License, version 3.0
  12.  * @author Christian Wenz <wenz@php.net>
  13.  * @category Text
  14.  */
  15.  
  16.  
  17. /**
  18.  *
  19.  * Require PEAR class for error handling.
  20.  *
  21.  */
  22. require_once 'PEAR.php';
  23.  
  24. /**
  25.  *
  26.  * Require Text_Password class for generating the phrase.
  27.  *
  28.  */
  29. require_once 'Text/Password.php';
  30.  
  31. /**
  32.  * Text_CAPTCHA - creates a CAPTCHA for Turing tests
  33.  *
  34.  * Class to create a Turing test for websites by
  35.  * creating an image, ASCII art or something else 
  36.  * with some (obfuscated) characters 
  37.  *
  38.  * @package Text_CAPTCHA
  39.  */
  40.  
  41. /*  
  42.     // This is a simple example script
  43.  
  44.     <?php
  45.     if (!function_exists('file_put_contents')) {
  46.         function file_put_contents($filename, $content) {
  47.             if (!($file = fopen($filename, 'w'))) {
  48.                 return false;
  49.             }
  50.             $n = fwrite($file, $content);
  51.             fclose($file);
  52.             return $n ? $n : false;
  53.         }
  54.     }
  55.  
  56.     // Start PHP session support
  57.     session_start();
  58.  
  59.     $ok = false;
  60.  
  61.     $msg = 'Please enter the text in the image in the field below!';
  62.  
  63.     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  64.  
  65.         if (isset($_POST['phrase']) && is_string($_SESSION['phrase']) && isset($_SESSION['phrase']) &&
  66.             strlen($_POST['phrase']) > 0 && strlen($_SESSION['phrase']) > 0 &&
  67.             $_POST['phrase'] == $_SESSION['phrase']) {
  68.             $msg = 'OK!';
  69.             $ok = true;
  70.             unset($_SESSION['phrase']);
  71.         } else {
  72.             $msg = 'Please try again!';
  73.         }
  74.  
  75.         unlink(md5(session_id()) . '.png');   
  76.  
  77.     }
  78.  
  79.     print "<p>$msg</p>";
  80.  
  81.     if (!$ok) {
  82.     
  83.         require_once 'Text/CAPTCHA.php';
  84.                    
  85.         // Set CAPTCHA image options (font must exist!)
  86.         $imageOptions = array(
  87.             'font_size'        => 24,
  88.             'font_path'        => './',
  89.             'font_file'        => 'COUR.TTF',
  90.             'text_color'       => '#DDFF99',
  91.             'lines_color'      => '#CCEEDD',
  92.             'background_color' => '#555555'
  93.         );
  94.  
  95.         // Set CAPTCHA options
  96.         $options = array(
  97.             'width' => 200,
  98.             'height' => 80,
  99.             'output' => 'png',
  100.             'imageOptions' => $imageOptions
  101.         );
  102.  
  103.         // Generate a new Text_CAPTCHA object, Image driver
  104.         $c = Text_CAPTCHA::factory('Image');
  105.         $retval = $c->init($options);
  106.         if (PEAR::isError($retval)) {
  107.             printf('Error initializing CAPTCHA: %s!',
  108.                 $retval->getMessage());
  109.             exit;
  110.         }
  111.     
  112.         // Get CAPTCHA secret passphrase
  113.         $_SESSION['phrase'] = $c->getPhrase();
  114.     
  115.         // Get CAPTCHA image (as PNG)
  116.         $png = $c->getCAPTCHA();
  117.         if (PEAR::isError($png)) {
  118.             printf('Error generating CAPTCHA: %s!',
  119.                 $png->getMessage());
  120.             exit;
  121.         }
  122.         file_put_contents(md5(session_id()) . '.png', $png);
  123.     
  124.         echo '<form method="post">' . 
  125.              '<img src="' . md5(session_id()) . '.png?' . time() . '" />' . 
  126.              '<input type="text" name="phrase" />' .
  127.              '<input type="submit" /></form>';
  128.     }
  129.     ?>
  130. */
  131.  
  132. class Text_CAPTCHA {
  133.  
  134.     /**
  135.      * Version number
  136.      *
  137.      * @access private
  138.      * @var string
  139.      */
  140.     var $_version = '0.3.1';
  141.  
  142.     /**
  143.      * Phrase
  144.      *
  145.      * @access private
  146.      * @var string
  147.      */
  148.     var $_phrase;
  149.  
  150.     /**
  151.      * Create a new Text_CAPTCHA object
  152.      *
  153.      * @param string $driver name of driver class to initialize
  154.      *
  155.      * @return mixed a newly created Text_CAPTCHA object, or a PEAR
  156.      * error object on error
  157.      *
  158.      * @see PEAR::isError()
  159.      */
  160.     function &factory($driver)
  161.     {
  162.         if ($driver == '') {
  163.             return PEAR::raiseError('No CAPTCHA type specified ... aborting. You must call ::factory() with one parameter, the CAPTCHA type.', true);
  164.         }
  165.         $driver = basename($driver);
  166.         include_once "Text/CAPTCHA/Driver/$driver.php";
  167.  
  168.         $classname = "Text_CAPTCHA_Driver_$driver";
  169.         $obj =& new $classname;
  170.         return $obj;
  171.     }
  172.  
  173.     /**
  174.      * Create random CAPTCHA phrase
  175.      *
  176.      * This method creates a random phrase, 8 characters long
  177.      *
  178.      * @access  private
  179.      */
  180.     function _createPhrase()
  181.     {
  182.         $len = 8;
  183.         $this->_phrase = Text_Password::create($len);
  184.     }
  185.  
  186.     /**
  187.      * Return secret CAPTCHA phrase
  188.      *
  189.      * This method returns the CAPTCHA phrase
  190.      *
  191.      * @access  public
  192.      * @return  phrase   secret phrase
  193.      */
  194.     function getPhrase()
  195.     {
  196.         return $this->_phrase;
  197.     }
  198.  
  199.     /**
  200.      * Sets secret CAPTCHA phrase
  201.      *
  202.      * This method sets the CAPTCHA phrase 
  203.      * (use null for a random phrase)
  204.      *
  205.      * @access  public
  206.      * @param   string   $phrase    the (new) phrase
  207.      * @void 
  208.      */
  209.     function setPhrase($phrase = null)
  210.     {
  211.         if (!empty($phrase)) {
  212.             $this->_phrase = $phrase;
  213.         } else {
  214.             $this->_createPhrase();
  215.         }
  216.     }
  217.  
  218.     /**
  219.      * Place holder for the real init() method
  220.      * used by extended classes to initialize CAPTCHA 
  221.      *
  222.      * @access private
  223.      * @return PEAR_Error
  224.      */
  225.     function init() {
  226.         return PEAR::raiseError('CAPTCHA type not selected', true);
  227.     }
  228.  
  229.     /**
  230.      * Place holder for the real _createCAPTCHA() method
  231.      * used by extended classes to generate CAPTCHA from phrase
  232.      *
  233.      * @access private
  234.      * @return PEAR_Error
  235.      */
  236.     function _createCAPTCHA() {
  237.         return PEAR::raiseError('CAPTCHA type not selected', true);
  238.     }
  239.  
  240.     /**
  241.      * Place holder for the real getCAPTCHA() method
  242.      * used by extended classes to return the generated CAPTCHA 
  243.      * (as an image resource, as an ASCII text, ...)
  244.      *
  245.      * @access private
  246.      * @return PEAR_Error
  247.      */
  248.     function getCAPTCHA() {
  249.         return PEAR::raiseError('CAPTCHA type not selected', true);
  250.     }
  251.  
  252. }
  253. ?>
  254.