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.  *  @license BSD License
  9.  */
  10. require_once 'Text/CAPTCHA.php';
  11.  
  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.      * @param array $options Optionally supply options for the initialization phase
  85.      *
  86.      * @access public
  87.      * @return true on success, PEAR_Error on error.
  88.      */
  89.     function init($options = array()) 
  90.     {
  91.         if (isset($options['min'])) {
  92.             $this->_min = (int)$options['min'];
  93.         else {
  94.             $this->_min = 1;
  95.         }
  96.         if (isset($options['max'])) {
  97.             $this->_max = (int)$options['max'];
  98.         else {
  99.             $this->_max = 10;
  100.         }
  101.         if (isset($options['numbersToText'])) {
  102.             $this->_numbersToText = (bool)$options['numbersToText'];
  103.         else {
  104.             $this->_numbersToText = false;
  105.         }
  106.         if (isset($options['severity'])) {
  107.             $this->_severity = (int)$options['severity'];
  108.         else {
  109.             $this->_severity = 1;
  110.         }
  111.  
  112.         if ($this->_numbersToText{
  113.             include_once 'Numbers/Words.php';
  114.             if (!class_exists('Numbers_Words')) {
  115.                 $this->_error = PEAR::raiseError('Number_Words package required'true);
  116.                 return $this->_error;
  117.             }
  118.         }
  119.  
  120.         return $this->_createPhrase();
  121.     }
  122.  
  123.     /**
  124.      * Create random CAPTCHA equation.
  125.      *
  126.      * This method creates a random equation. The equation is
  127.      * stored in $this->_equation, the solution in $this->_phrase.
  128.      *
  129.      * @access protected
  130.      * @return mixed    true on success, PEAR_Error on error
  131.      */
  132.     function _createPhrase()
  133.     {
  134.         switch ($this->_severity{
  135.             case 1:
  136.                 list($this->_equation$this->_phrase$this->_createSimpleEquation();
  137.                 break;
  138.  
  139.             case 2:
  140.                 list($eq1$sol1$this->_createSimpleEquation();
  141.                 list($eq2$sol2$this->_createSimpleEquation();
  142.                 $op3 $this->_operators[rand(0count($this->_operators- 1)];
  143.                 list($eq3$this->_phrase$this->_solveSimpleEquation($sol1$sol2$op3);
  144.                 $this->_equation = sprintf($op3'(' $eq1 ')''(' $eq2 ')');
  145.                 break;
  146.  
  147.             default:
  148.                 $this->_error = PEAR::raiseError('Equation complexity of ' $this->_severity . ' not supported'true);
  149.                 return $this->_error;
  150.         }
  151.         return true;
  152.     }
  153.  
  154.     /**
  155.      * Creates a simple equation of type (number operator number)
  156.      *
  157.      * @access protected
  158.      * @return array    Array with equation and solution
  159.      */
  160.     function _createSimpleEquation()
  161.     {
  162.         $one rand($this->_min$this->_max);
  163.         $two rand($this->_min$this->_max);
  164.         $operator $this->_operators[rand(0count($this->_operators- 1)];
  165.  
  166.         return $this->_solveSimpleEquation($one$two$operator);
  167.     }
  168.  
  169.     /**
  170.      * Solves a simple equation with two given numbers
  171.      * and one operator as defined in $this->_operators.
  172.      *
  173.      * Also converts the numbers to words if required.
  174.      *
  175.      * @param int    $one      First number
  176.      * @param int    $two      Second number
  177.      * @param string $operator Operator
  178.      *
  179.      * @access protected
  180.      * @return array    Array with equation and solution
  181.      */
  182.     function _solveSimpleEquation($one$two$operator)
  183.     {
  184.         $equation sprintf($operator$one$two);
  185.         $code '$solution=' $equation ';';
  186.         eval($code);
  187.  
  188.         if ($this->_numbersToText{
  189.             $equation sprintf($operatorNumbers_Words::toWords($one)Numbers_Words::toWords($two));
  190.         }
  191.  
  192.         return array($equation$solution);
  193.     }
  194.  
  195.     /**
  196.      * Return the solution to the equation.
  197.      *
  198.      * This method returns the CAPTCHA phrase, which is
  199.      *  the solution to the equation.
  200.      *
  201.      * @access  public
  202.      * @return  string   secret phrase
  203.      */
  204.     function getPhrase()
  205.     {
  206.         return $this->_phrase;
  207.     }
  208.  
  209.     /**
  210.      * Creates the captcha. This method is a placeholder,
  211.      *  since the equation is created in _createPhrase()
  212.      *
  213.      * @access protected
  214.      * @return PEAR_Error 
  215.      */
  216.     function _createCAPTCHA(
  217.     {
  218.         //is already done in _createPhrase();
  219.     }
  220.  
  221.     /**
  222.      * Returns the CAPTCHA (as a string)
  223.      *
  224.      * @access public
  225.      * @return string 
  226.      */
  227.     function getCAPTCHA(
  228.     {
  229.         return $this->_equation;
  230.     }
  231.  
  232. }//class Text_CAPTCHA_Driver_TextEquation extends Text_CAPTCHA

Documentation generated on Sat, 16 Mar 2013 15:30:03 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.