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

Source for file Numeral.php

Documentation is available at Numeral.php

  1. <?php
  2. // {{{ Class Text_CAPTCHA_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. /**
  43.  * Class used for numeral captchas
  44.  * 
  45.  * This class is intended to be used to generate
  46.  * numeral captchas as such as:
  47.  * Example:
  48.  *  Give me the answer to "54 + 2" to prove that you are human.
  49.  *
  50.  * @author   David Coallier <davidc@agoraproduction.com>
  51.  * @package  Text_CAPTCHA_Numeral
  52.  * @category CAPTCHA
  53.  */
  54. {
  55.     // {{{ Variables
  56.     /**
  57.      * Minimum range value
  58.      * 
  59.      * This variable holds the minimum range value
  60.      * default set to "1"
  61.      * 
  62.      * @access private
  63.      * @var    integer $minValue The minimum range value
  64.      */
  65.     private $minValue '1';
  66.     
  67.     /**
  68.      * Maximum range value
  69.      * 
  70.      * This variable holds the maximum range value
  71.      * default set to "50"
  72.      * 
  73.      * @access private
  74.      * @var    integer $maxValue The maximum value of the number range
  75.      */
  76.     private $maxValue '50';
  77.     
  78.     /**
  79.      * Operators
  80.      * 
  81.      * The valid operators to use
  82.      * in the numeral captcha. We could
  83.      * use / and * but not yet.
  84.      * 
  85.      * @access private
  86.      * @var    array $operators The operations for the captcha
  87.      */
  88.     private $operators = array('-''+');
  89.     
  90.     /**
  91.      * Operator to use
  92.      * 
  93.      * This variable is basically the operation
  94.      * that we're going to be using in the
  95.      * numeral captcha we are about to generate.
  96.      *
  97.      * @access private
  98.      * @var    string $operator The operation's operator
  99.      */
  100.     private  $operator '';
  101.     
  102.     /**
  103.      * Mathematical Operation
  104.      * 
  105.      * This is the mathematical operation
  106.      * that we are displaying to the user.
  107.      *
  108.      * @access private
  109.      * @var    string $operation The math operation
  110.      */
  111.     private $operation '';
  112.     
  113.     /**
  114.      * First number of the operation
  115.      *
  116.      * This variable holds the first number
  117.      * of the numeral operation we are about
  118.      * to generate.
  119.      * 
  120.      * @access private
  121.      * @var    integer $firstNumber The first number of the operation
  122.      */
  123.     private $firstNumber '';
  124.     
  125.     /**
  126.      * Second Number of the operation
  127.      * 
  128.      * This variable holds the value of the
  129.      * second variable of the operation we are
  130.      * about to generate for the captcha.
  131.      * 
  132.      * @access private
  133.      * @var    integer $secondNumber The second number of the operation
  134.      */ 
  135.     private $secondNumber '';
  136.     
  137.     /**
  138.      * The operation answer
  139.      * 
  140.      * The answer to the numeral operation
  141.      * we are about to do.
  142.      *
  143.      * @access private
  144.      * @var    integer $answer The mathematical operation answer value.
  145.      */
  146.     private $answer;
  147.     // }}}
  148.     // {{{ Constructor
  149.     public function __construct()
  150.     {
  151.         $this->generateFirstNumber();
  152.         $this->generateSecondNumber();
  153.         $this->generateOperator();
  154.         $this->generateOperation();
  155.     }
  156.     // }}}
  157.     // {{{ private function setRangeMinimum
  158.     /**
  159.      * Set Range Minimum value
  160.      * 
  161.      * This function give the developer the ability
  162.      * to set the range minimum value so the operations
  163.      * can be bigger, smaller, etc.
  164.      *
  165.      * @access private
  166.      * @param  integer $minValue The minimum value
  167.      */
  168.     private function setRangeMinimum($minValue '1'
  169.     {
  170.         $this->minValue = (int)$minValue;
  171.     }
  172.     // }}}
  173.     // {{{ private function generateFirstNumber
  174.     /**
  175.      * Sets the first number
  176.      * 
  177.      * This function sets the first number
  178.      * of the operation by calling the generateNumber
  179.      * function that generates a random number.
  180.      * 
  181.      * @access private
  182.      * @see    $this->firstNumber, $this->generateNumber
  183.      */
  184.     private function generateFirstNumber()
  185.     {
  186.         $this->setFirstNumber($this->generateNumber());
  187.     }
  188.     // }}}
  189.     // {{{ private function generateSecondNumber
  190.     /**
  191.      * Sets second number
  192.      * 
  193.      * This function sets the second number of the
  194.      * operation by calling generateNumber()
  195.      * 
  196.      * @access private
  197.      * @see    $this->secondNumber, $this->generateNumber()
  198.      */
  199.     private function generateSecondNumber()
  200.     {
  201.         $this->setSecondNumber($this->generateNumber());
  202.     }
  203.     // }}}
  204.     // {{{ private function generateOperator
  205.     /**
  206.      * Sets the operation operator
  207.      * 
  208.      * This function sets the operation operator by
  209.      * getting the array value of an array_rand() of
  210.      * the $this->operators() array.
  211.      *
  212.      * @access private
  213.      * @see    $this->operators, $this->operator
  214.      */
  215.     private function generateOperator()
  216.     {
  217.         $this->operator $this->operators[array_rand($this->operators)];
  218.     }
  219.     // }}}
  220.     // {{{ private function setAnswer
  221.     /**
  222.      * Sets the answer value
  223.      * 
  224.      * This function will accept the parameters which is
  225.      * basically the result of the function we have done
  226.      * and it will set $this->answer with it.
  227.      * 
  228.      * @access private
  229.      * @param  integer $answerValue The answer value
  230.      * @see    $this->answer
  231.      */
  232.     private function setAnswer($answerValue)
  233.     {   
  234.         $this->answer $answerValue;
  235.     }
  236.     // }}}
  237.     // {{{ private function setFirstNumber
  238.     /**
  239.      * Set First number
  240.      *
  241.      * This function sets the first number
  242.      * to the value passed to the function
  243.      *
  244.      * @access private
  245.      * @param  integer $value The first number value.
  246.      */
  247.     private function setFirstNumber($value
  248.     {
  249.         $this->firstNumber = (int)$value;
  250.     }
  251.     // }}}
  252.     // {{{ private function setSecondNumber
  253.     /**
  254.      * Sets the second number
  255.      * 
  256.      * This function sets the second number
  257.      * with the value passed to it.
  258.      *
  259.      * @access private
  260.      * @param  integer $value The second number new value.
  261.      */
  262.     private function setSecondNumber($value)
  263.     {
  264.         $this->secondNumber = (int)$value;
  265.     }
  266.     // }}}
  267.     // {{{ private function setOperation
  268.     /**
  269.      * Set operation
  270.      * 
  271.      * This variable sets the operation variable
  272.      * by taking the firstNumber, secondNumber and operator
  273.      *
  274.      * @access private
  275.      * @see    $this->operation
  276.      */
  277.     private function setOperation()
  278.     {
  279.         $this->operation $this->getFirstNumber(' ' .
  280.                            $this->operator ' ' .
  281.                            $this->getSecondNumber();
  282.     }
  283.     // }}}
  284.     // {{{ private function generateNumber
  285.     /**
  286.      * Generate a number
  287.      * 
  288.      * This function takes the parameters that are in
  289.      * the $this->maxValue and $this->minValue and get
  290.      * the random number from them using mt_rand()
  291.      *
  292.      * @access private
  293.      * @return integer Random value between minValue and maxValue
  294.      */
  295.     private function generateNumber()
  296.     {
  297.         return mt_rand($this->minValue$this->maxValue);
  298.     }
  299.     // }}}
  300.     // {{{ private function doAdd
  301.     /**
  302.      * Adds values
  303.      * 
  304.      * This function will add the firstNumber and the
  305.      * secondNumber value and then call setAnswer to
  306.      * set the answer value.
  307.      * 
  308.      * @access private
  309.      * @see    $this->firstNumber, $this->secondNumber, $this->setAnswer()
  310.      */
  311.     private function doAdd()
  312.     {
  313.         $answer $this->getFirstNumber($this->getSecondNumber();
  314.         $this->setAnswer($answer);
  315.     }
  316.     // }}}
  317.     // {{{ private function doSubstract
  318.     /**
  319.      * Does a substract on the values
  320.      * 
  321.      * This function executes a substraction on the firstNumber
  322.      * and the secondNumber to then call $this->setAnswer to set
  323.      * the answer value.
  324.      * 
  325.      * If the firstnumber value is smaller than the secondnumber value
  326.      * then we regenerate the first number and regenerate the operation.
  327.      * 
  328.      * @access private
  329.      * @see    $this->firstNumber, $this->secondNumber, $this->setAnswer()
  330.      */
  331.     private function doSubstract()
  332.     {
  333.         $first  $this->getFirstNumber();
  334.         $second $this->getSecondNumber();
  335.         
  336.         /**
  337.          * Check if firstNumber is smaller than secondNumber
  338.          */
  339.         if ($first $second{
  340.             $this->setFirstNumber($second);
  341.             $this->setSecondNumber($first);
  342.             $this->setOperation();
  343.         }
  344.  
  345.         $answer $this->getFirstNumber($this->getSecondNumber();
  346.         $this->setAnswer($answer);
  347.     }
  348.     // }}}
  349.     // {{{ private function generateOperation
  350.     /**
  351.      * Generate the operation
  352.      * 
  353.      * This function will call the setOperation() function
  354.      * to set the operation string that will be called
  355.      * to display the operation, and call the function necessary
  356.      * depending on which operation is set by this->operator.
  357.      * 
  358.      * @access private
  359.      * @see    $this->setOperation(), $this->operator
  360.      */
  361.     private function generateOperation()
  362.     {
  363.         $this->setOperation();
  364.                            
  365.         switch ($this->operator{
  366.         case '+':
  367.             $this->doAdd();
  368.             break;
  369.         case '-':
  370.             $this->doSubstract();
  371.             break;
  372.         default:
  373.             $this->doAdd();
  374.             break;
  375.         }
  376.     }
  377.     // }}}
  378.     // {{{ public function getOperation
  379.     /**
  380.      * Get operation
  381.      * 
  382.      * This function will get the operation
  383.      * string from $this->operation
  384.      *
  385.      * @access public
  386.      * @return string The operation String
  387.      */
  388.     public function getOperation()
  389.     {
  390.         return $this->operation;
  391.     }
  392.     // }}} 
  393.     // {{{ public function getAnswer
  394.     /**
  395.      * Get the answer value
  396.      * 
  397.      * This function will retrieve the answer
  398.      * value from this->answer and return it so
  399.      * we can then display it to the user.
  400.      *
  401.      * @access public
  402.      * @return string The operation answer value.
  403.      */
  404.     public function getAnswer()
  405.     {
  406.         return $this->answer;
  407.     }
  408.     // }}}
  409.     // {{{ public function getFirstNumber
  410.     /**
  411.      * Get the first number
  412.      * 
  413.      * This function will get the first number
  414.      * value from $this->firstNumber
  415.      * 
  416.      * @access public
  417.      * @return integer $this->firstNumber The firstNumber
  418.      */
  419.     public function getFirstNumber()
  420.     {
  421.         return $this->firstNumber;
  422.     }
  423.     // }}}
  424.     // {{{ public function getSecondNumber
  425.     /**
  426.      * Get the second number value
  427.      * 
  428.      * This function will return the second number value
  429.      * 
  430.      * @access public
  431.      * @return integer $this->secondNumber The second number
  432.      */
  433.     public function getSecondNumber()
  434.     {
  435.         return $this->secondNumber;
  436.     }
  437.     // }}}
  438. }
  439. // }}}
  440. ?>

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