Text_CAPTCHA
[ class tree: Text_CAPTCHA ] [ index: Text_CAPTCHA ] [ all elements ]

Source for file Equation.php

Documentation is available at Equation.php

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

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