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

Source for file Numeral.php

Documentation is available at Numeral.php

  1. <?php
  2. // {{{ Class Text_CAPTCHA_Driver_Numeral
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 5                                                       |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2006 David Coallier                               | 
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // |                                                                      |
  10. // | Redistribution and use in source and binary forms, with or without   |
  11. // | modification, are permitted provided that the following conditions   |
  12. // | are met:                                                             |
  13. // |                                                                      |
  14. // | Redistributions of source code must retain the above copyright       |
  15. // | notice, this list of conditions and the following disclaimer.        |
  16. // |                                                                      |
  17. // | Redistributions in binary form must reproduce the above copyright    |
  18. // | notice, this list of conditions and the following disclaimer in the  |
  19. // | documentation and/or other materials provided with the distribution. |
  20. // |                                                                      |
  21. // | Neither the name of David Coallier nor the names of his contributors |
  22. // | may be used to endorse                                               |
  23. // | or promote products derived from this software without specific prior|
  24. // | written permission.                                                  |
  25. // |                                                                      |
  26. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  27. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  28. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  29. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  30. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  31. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  32. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  33. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  34. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  35. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  36. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  37. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  38. // +----------------------------------------------------------------------+
  39. // | Author: David Coallier <davidc@agoraproduction.com>                  |
  40. // +----------------------------------------------------------------------+
  41. //
  42. require_once 'Text/CAPTCHA.php';
  43. /**
  44.  * Class used for numeral captchas
  45.  * 
  46.  * This class is intended to be used to generate
  47.  * numeral captchas as such as:
  48.  * Example:
  49.  *  Give me the answer to "54 + 2" to prove that you are human.
  50.  *
  51.  * @author   David Coallier <davidc@agoraproduction.com>
  52.  * @author   Christian Wenz <wenz@php.net>
  53.  * @package  Text_CAPTCHA
  54.  * @category Text
  55.  */
  56. {
  57.     // {{{ Variables
  58.     /**
  59.      * Minimum range value
  60.      * 
  61.      * This variable holds the minimum range value
  62.      * default set to "1"
  63.      * 
  64.      * @access private
  65.      * @var    integer $_minValue The minimum range value
  66.      */
  67.     var $_minValue = 1;
  68.     
  69.     /**
  70.      * Maximum range value
  71.      * 
  72.      * This variable holds the maximum range value
  73.      * default set to "50"
  74.      * 
  75.      * @access private
  76.      * @var    integer $_maxValue The maximum value of the number range
  77.      */
  78.     var $_maxValue = 50;
  79.     
  80.     /**
  81.      * Operators
  82.      * 
  83.      * The valid operators to use
  84.      * in the numeral captcha. We could
  85.      * use / and * but not yet.
  86.      * 
  87.      * @access private
  88.      * @var    array $_operators The operations for the captcha
  89.      */
  90.     var $_operators = array('-''+');
  91.     
  92.     /**
  93.      * Operator to use
  94.      * 
  95.      * This variable is basically the operation
  96.      * that we're going to be using in the
  97.      * numeral captcha we are about to generate.
  98.      *
  99.      * @access private
  100.      * @var    string $_operator The operation's operator
  101.      */
  102.     var $_operator '';
  103.     
  104.     /**
  105.      * Mathematical Operation
  106.      * 
  107.      * This is the mathematical operation
  108.      * that we are displaying to the user.
  109.      *
  110.      * @access private
  111.      * @var    string $_operation The math operation
  112.      */
  113.     var $_operation '';
  114.     
  115.     /**
  116.      * First number of the operation
  117.      *
  118.      * This variable holds the first number
  119.      * of the numeral operation we are about
  120.      * to generate.
  121.      * 
  122.      * @access private
  123.      * @var    integer $_firstNumber The first number of the operation
  124.      */
  125.     var $_firstNumber '';
  126.     
  127.     /**
  128.      * Second Number of the operation
  129.      * 
  130.      * This variable holds the value of the
  131.      * second variable of the operation we are
  132.      * about to generate for the captcha.
  133.      * 
  134.      * @access private
  135.      * @var    integer $_secondNumber The second number of the operation
  136.      */ 
  137.     var $_secondNumber '';
  138.     // }}}
  139.     // {{{ Constructor
  140.     function init($options = array())
  141.     {
  142.         if (isset($options['minValue'])) {
  143.             $this->_minValue = (int)$options['minValue'];
  144.         }
  145.         if (isset($options['maxValue'])) {
  146.             $this->_maxValue = (int)$options['maxValue'];
  147.         }
  148.         
  149.         $this->_createCAPTCHA();
  150.     }
  151.     // }}}
  152.     // {{{ private function _createCAPTCHA
  153.     /**
  154.      * Create the CAPTCHA (the numeral expressio)
  155.      * 
  156.      * This function determines a random numeral expression
  157.      * and set the associated class properties
  158.      *
  159.      * @access private
  160.      */
  161.     function _createCAPTCHA()
  162.     
  163.         $this->_generateFirstNumber();
  164.         $this->_generateSecondNumber();
  165.         $this->_generateOperator();
  166.         $this->_generateOperation();
  167.     }
  168.     // }}}
  169.     // {{{ private function _setRangeMinimum
  170.     /**
  171.      * Set Range Minimum value
  172.      * 
  173.      * This function give the developer the ability
  174.      * to set the range minimum value so the operations
  175.      * can be bigger, smaller, etc.
  176.      *
  177.      * @access private
  178.      * @param  integer $minValue The minimum value
  179.      */
  180.     function _setRangeMinimum($minValue = 1
  181.     {
  182.         $this->minValue = (int)$minValue;
  183.     }
  184.     // }}}
  185.     // {{{ private function _generateFirstNumber
  186.     /**
  187.      * Sets the first number
  188.      * 
  189.      * This function sets the first number
  190.      * of the operation by calling the _generateNumber
  191.      * function that generates a random number.
  192.      * 
  193.      * @access private
  194.      * @see    $this->_firstNumber, $this->_generateNumber
  195.      */
  196.     function _generateFirstNumber()
  197.     {
  198.         $this->_setFirstNumber($this->_generateNumber());
  199.     }
  200.     // }}}
  201.     // {{{ private function generateSecondNumber
  202.     /**
  203.      * Sets second number
  204.      * 
  205.      * This function sets the second number of the
  206.      * operation by calling _generateNumber()
  207.      * 
  208.      * @access private
  209.      * @see    $this->_secondNumber, $this->_generateNumber()
  210.      */
  211.     function _generateSecondNumber()
  212.     {
  213.         $this->_setSecondNumber($this->_generateNumber());
  214.     }
  215.     // }}}
  216.     // {{{ private function generateOperator
  217.     /**
  218.      * Sets the operation operator
  219.      * 
  220.      * This function sets the operation operator by
  221.      * getting the array value of an array_rand() of
  222.      * the $this->_operators() array.
  223.      *
  224.      * @access private
  225.      * @see    $this->_operators, $this->_operator
  226.      */
  227.     function _generateOperator()
  228.     {
  229.         $this->_operator $this->_operators[array_rand($this->_operators)];
  230.     }
  231.     // }}}
  232.     // {{{ private function setAnswer
  233.     /**
  234.      * Sets the answer value
  235.      * 
  236.      * This function will accept the parameters which is
  237.      * basically the result of the function we have done
  238.      * and it will set $this->answer with it.
  239.      * 
  240.      * @access private
  241.      * @param  integer $phraseValue The answer value
  242.      * @see    $this->_phrase
  243.      */
  244.     function _setPhrase($phraseValue)
  245.     {   
  246.         $this->_phrase $phraseValue;
  247.     }
  248.     // }}}
  249.     // {{{ private function setFirstNumber
  250.     /**
  251.      * Set First number
  252.      *
  253.      * This function sets the first number
  254.      * to the value passed to the function
  255.      *
  256.      * @access private
  257.      * @param  integer $value The first number value.
  258.      */
  259.     function _setFirstNumber($value
  260.     {
  261.         $this->_firstNumber = (int)$value;
  262.     }
  263.     // }}}
  264.     // {{{ private function setSecondNumber
  265.     /**
  266.      * Sets the second number
  267.      * 
  268.      * This function sets the second number
  269.      * with the value passed to it.
  270.      *
  271.      * @access private
  272.      * @param  integer $value The second number new value.
  273.      */
  274.     function _setSecondNumber($value)
  275.     {
  276.         $this->_secondNumber = (int)$value;
  277.     }
  278.     // }}}
  279.     // {{{ private function setOperation
  280.     /**
  281.      * Set operation
  282.      * 
  283.      * This variable sets the operation variable
  284.      * by taking the firstNumber, secondNumber and operator
  285.      *
  286.      * @access private
  287.      * @see    $this->_operation
  288.      */
  289.     function _setOperation()
  290.     {
  291.         $this->_operation $this->_getFirstNumber(' ' .
  292.                             $this->_operator ' ' .
  293.                             $this->_getSecondNumber();
  294.     }
  295.     // }}}
  296.     // {{{ private function _generateNumber
  297.     /**
  298.      * Generate a number
  299.      * 
  300.      * This function takes the parameters that are in
  301.      * the $this->_maxValue and $this->_minValue and get
  302.      * the random number from them using mt_rand()
  303.      *
  304.      * @access private
  305.      * @return integer Random value between _minValue and _maxValue
  306.      */
  307.     function _generateNumber()
  308.     {
  309.         return mt_rand($this->_minValue$this->_maxValue);
  310.     }
  311.     // }}}
  312.     // {{{ private function _doAdd
  313.     /**
  314.      * Adds values
  315.      * 
  316.      * This function will add the firstNumber and the
  317.      * secondNumber value and then call setAnswer to
  318.      * set the answer value.
  319.      * 
  320.      * @access private
  321.      * @see    $this->_firstNumber, $this->_secondNumber, $this->_setAnswer()
  322.      */
  323.     function _doAdd()
  324.     {
  325.         $phrase $this->_getFirstNumber($this->_getSecondNumber();
  326.         $this->_setPhrase($phrase);
  327.     }
  328.     // }}}
  329.     // {{{ private function _doSubstract
  330.     /**
  331.      * Does a substract on the values
  332.      * 
  333.      * This function executes a substraction on the firstNumber
  334.      * and the secondNumber to then call $this->setAnswer to set
  335.      * the answer value.
  336.      * 
  337.      * If the firstnumber value is smaller than the secondnumber value
  338.      * then we regenerate the first number and regenerate the operation.
  339.      * 
  340.      * @access private
  341.      * @see    $this->_firstNumber, $this->_secondNumber, $this->_setAnswer()
  342.      */
  343.     function _doSubstract()
  344.     {
  345.         $first  $this->_getFirstNumber();
  346.         $second $this->_getSecondNumber();
  347.         
  348.         /**
  349.          * Check if firstNumber is smaller than secondNumber
  350.          */
  351.     if ($first $second{
  352.         $this->_setFirstNumber($second);
  353.         $this->_setSecondNumber($first);
  354.         $this->_setOperation();
  355.     }
  356.  
  357.         $phrase $this->_getFirstNumber($this->_getSecondNumber();
  358.         $this->_setPhrase($phrase);
  359.     }
  360.     // }}}
  361.     // {{{ private function _generateOperation
  362.     /**
  363.      * Generate the operation
  364.      * 
  365.      * This function will call the _setOperation() function
  366.      * to set the operation string that will be called
  367.      * to display the operation, and call the function necessary
  368.      * depending on which operation is set by this->operator.
  369.      * 
  370.      * @access private
  371.      * @see    $this->_setOperation(), $this->_operator
  372.      */
  373.     function _generateOperation()
  374.     {
  375.         $this->_setOperation();
  376.                            
  377.         switch ($this->_operator{
  378.         case '+':
  379.             $this->_doAdd();
  380.             break;
  381.         case '-':
  382.             $this->_doSubstract();
  383.             break;
  384.         default:
  385.             $this->_doAdd();
  386.             break;
  387.         }
  388.     }
  389.     // }}}
  390.     // {{{ public function _getFirstNumber
  391.     /**
  392.      * Get the first number
  393.      * 
  394.      * This function will get the first number
  395.      * value from $this->_firstNumber
  396.      * 
  397.      * @access public
  398.      * @return integer $this->_firstNumber The firstNumber
  399.      */
  400.     function _getFirstNumber()
  401.     {
  402.         return $this->_firstNumber;
  403.     }
  404.     // }}}
  405.     // {{{ public function _getSecondNumber
  406.     /**
  407.      * Get the second number value
  408.      * 
  409.      * This function will return the second number value
  410.      * 
  411.      * @access public
  412.      * @return integer $this->_secondNumber The second number
  413.      */
  414.     function _getSecondNumber()
  415.     {
  416.         return $this->_secondNumber;
  417.     }
  418.     // }}}
  419.     // {{{ public function getCAPTCHA
  420.     /**
  421.      * Get operation
  422.      * 
  423.      * This function will get the operation
  424.      * string from $this->_operation
  425.      *
  426.      * @access public
  427.      * @return string The operation String
  428.      */
  429.     function getCAPTCHA()
  430.     {
  431.         return $this->_operation;
  432.     }
  433. }
  434. // }}}
  435. ?>

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