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 / Equation.php next >
Encoding:
PHP Script  |  2008-07-02  |  5.6 KB  |  224 lines

  1. <?php
  2. /**
  3.  *  Equation driver for Text_CAPTCHA.
  4.  *  Returns simple equations as string, e.g. "9 - 2"
  5.  *
  6.  *  @author Christian Weiske <cweiske@php.net>
  7.  *  @author Christian Wenz <wenz@php.net>
  8.  */
  9. require_once 'Text/CAPTCHA.php';
  10.  
  11. class Text_CAPTCHA_Driver_Equation extends Text_CAPTCHA
  12. {
  13.     /**
  14.      * Operators that may be used in the equation.
  15.      * Two numbers have to be filled in, and
  16.      *  %s is needed since number2text conversion
  17.      *  may be applied and strings filled in.
  18.      *
  19.      * @access protected
  20.      * @var array
  21.      */
  22.     var $_operators = array(
  23.         '%s * %s',
  24.         '%s + %s',
  25.         '%s - %s',
  26.         'min(%s, %s)',
  27.         'max(%s, %s)'
  28.     );
  29.  
  30.     /**
  31.      * The equation to solve.
  32.      *
  33.      * @access protected
  34.      * @var string
  35.      */
  36.     var $_equation = null;
  37.  
  38.     /**
  39.      * Minimal number to use in an equation.
  40.      *
  41.      * @access protected
  42.      * @var int
  43.      */
  44.     var $_min = 1;
  45.  
  46.     /**
  47.      * Maximum number to use in an equation.
  48.      *
  49.      * @access protected
  50.      * @var int
  51.      */
  52.     var $_max = 10;
  53.  
  54.     /**
  55.      * Whether numbers shall be converted to text
  56.      *
  57.      * @access protected
  58.      * @var bool
  59.      */
  60.     var $_numbersToText = false;
  61.  
  62.     /**
  63.      * Complexity of the generated equations.
  64.      * 1 - simple ones such as "1 + 10"
  65.      * 2 - harder ones such as "(3-2)*(min(5,6))"
  66.      *
  67.      * @access protected
  68.      * @var int
  69.      */
  70.     var $_severity = 1;
  71.  
  72.     /**
  73.      * Last error
  74.      *
  75.      * @access protected
  76.      * @var PEAR_Error
  77.      */
  78.     var $_error = null;
  79.  
  80.  
  81.     /**
  82.      * Initialize the driver.
  83.      *
  84.      * @access public
  85.      * @return true on success, PEAR_Error on error.
  86.      */
  87.     function init($options = array()) {
  88.         if (isset($options['min'])) {
  89.             $this->_min = (int)$options['min'];
  90.         } else {
  91.             $this->_min = 1;
  92.         }
  93.         if (isset($options['max'])) {
  94.             $this->_max = (int)$options['max'];
  95.         } else {
  96.             $this->_max = 10;
  97.         }
  98.         if (isset($options['numbersToText'])) {
  99.             $this->_numbersToText = (bool)$options['numbersToText'];
  100.         } else {
  101.             $this->_numbersToText = false;
  102.         }
  103.         if (isset($options['severity'])) {
  104.             $this->_severity = (int)$options['severity'];
  105.         } else {
  106.             $this->_severity = 1;
  107.         }
  108.  
  109.         if ($this->_numbersToText) {
  110.             include_once 'Numbers/Words.php';
  111.             if (!class_exists('Numbers_Words')) {
  112.                 $this->_error = PEAR::raiseError('Number_Words package required', true);
  113.                 return $this->_error;
  114.             }
  115.         }
  116.  
  117.         return $this->_createPhrase();
  118.     }
  119.  
  120.     /**
  121.      * Create random CAPTCHA equation.
  122.      *
  123.      * This method creates a random equation. The equation is
  124.      * stored in $this->_equation, the solution in $this->_phrase.
  125.      *
  126.      * @access protected
  127.      * @return mixed    true on success, PEAR_Error on error
  128.      */
  129.     function _createPhrase()
  130.     {
  131.         switch ($this->_severity) {
  132.             case 1:
  133.                 list($this->_equation, $this->_phrase) = $this->_createSimpleEquation();
  134.                 break;
  135.  
  136.             case 2:
  137.                 list($eq1, $sol1) = $this->_createSimpleEquation();
  138.                 list($eq2, $sol2) = $this->_createSimpleEquation();
  139.                 $op3 = $this->_operators[rand(0, count($this->_operators) - 1)];
  140.                 list($eq3, $this->_phrase) = $this->_solveSimpleEquation($sol1, $sol2, $op3);
  141.                 $this->_equation = sprintf($op3, '(' . $eq1 . ')', '(' . $eq2 . ')');
  142.                 break;
  143.  
  144.             default:
  145.                 $this->_error = PEAR::raiseError('Equation complexity of ' . $this->_severity . ' not supported', true);
  146.                 return $this->_error;
  147.         }
  148.         return true;
  149.     }
  150.  
  151.     /**
  152.      * Creates a simple equation of type (number operator number)
  153.      *
  154.      * @access protected
  155.      * @return array    Array with equation and solution
  156.      */
  157.     function _createSimpleEquation()
  158.     {
  159.         $one = rand($this->_min, $this->_max);
  160.         $two = rand($this->_min, $this->_max);
  161.         $operator = $this->_operators[rand(0, count($this->_operators) - 1)];
  162.  
  163.         return $this->_solveSimpleEquation($one, $two, $operator);
  164.     }
  165.  
  166.     /**
  167.      * Solves a simple equation with two given numbers
  168.      * and one operator as defined in $this->_operators.
  169.      *
  170.      * Also converts the numbers to words if required.
  171.      *
  172.      * @access protected
  173.      * @return array    Array with equation and solution
  174.      */
  175.     function _solveSimpleEquation($one, $two, $operator)
  176.     {
  177.         $equation = sprintf($operator, $one, $two);
  178.         $code = '$solution=' . $equation . ';';
  179.         eval($code);
  180.  
  181.         if ($this->_numbersToText) {
  182.             $equation = sprintf($operator, Numbers_Words::toWords($one), Numbers_Words::toWords($two));
  183.         }
  184.  
  185.         return array($equation, $solution);
  186.     }
  187.  
  188.     /**
  189.      * Return the solution to the equation.
  190.      *
  191.      * This method returns the CAPTCHA phrase, which is
  192.      *  the solution to the equation.
  193.      *
  194.      * @access  public
  195.      * @return  string   secret phrase
  196.      */
  197.     function getPhrase()
  198.     {
  199.         return $this->_phrase;
  200.     }
  201.  
  202.     /**
  203.      * Creates the captcha. This method is a placeholder,
  204.      *  since the equation is created in _createPhrase()
  205.      *
  206.      * @access protected
  207.      * @return PEAR_Error
  208.      */
  209.     function _createCAPTCHA() {
  210.         //is already done in _createPhrase();
  211.     }
  212.  
  213.     /**
  214.      * Returns the CAPTCHA (as a string)
  215.      *
  216.      * @access public
  217.      * @return string
  218.      */
  219.     function getCAPTCHA() {
  220.         return $this->_equation;
  221.     }
  222.  
  223. }//class Text_CAPTCHA_Driver_TextEquation extends Text_CAPTCHA
  224. ?>