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

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