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

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