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. // {{{ Class Text_CAPTCHA_Numeral
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 5                                                        |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 1998-2007 David Coallier                               |
  8. // | All rights reserved.                                                 |
  9. // +----------------------------------------------------------------------+
  10. // |                                                                      |
  11. // | Redistribution and use in source and binary forms, with or without   |
  12. // | modification, are permitted provided that the following conditions   |
  13. // | are met:                                                             |
  14. // |                                                                      |
  15. // | Redistributions of source code must retain the above copyright       |
  16. // | notice, this list of conditions and the following disclaimer.        |
  17. // |                                                                      |
  18. // | Redistributions in binary form must reproduce the above copyright    |
  19. // | notice, this list of conditions and the following disclaimer in the  |
  20. // | documentation and/or other materials provided with the distribution. |
  21. // |                                                                      |
  22. // | Neither the name of David Coallier nor the names of his contributors |
  23. // | may be used to endorse                                               |
  24. // | or promote products derived from this software without specific prior|
  25. // | written permission.                                                  |
  26. // |                                                                      |
  27. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  28. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  29. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  30. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  31. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  32. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  33. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  34. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  35. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  36. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  37. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  38. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  39. // +----------------------------------------------------------------------+
  40. // | Author: David Coallier <davidc@agoraproduction.com>                  |
  41. // +----------------------------------------------------------------------+
  42. //
  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.  * @package  Text_CAPTCHA_Numeral
  53.  * @category CAPTCHA
  54.  */
  55. class Text_CAPTCHA_Numeral implements Text_CAPTCHA_Numeral_Interface
  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.     private $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.     private $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.     private $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.     private  $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.     private $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.     private $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.     private $secondNumber '';
  138.    
  139.     /**
  140.      * The operation answer
  141.      *
  142.      * The answer to the numeral operation
  143.      * we are about to do.
  144.      *
  145.      * @access private
  146.      * @var    integer $answer The mathematical operation answer value.
  147.      */
  148.     private $answer;
  149.  
  150.     /**
  151.      * A constant that indicates the complexity of mathematical operations
  152.      *
  153.      * @access public
  154.      *
  155.      */
  156.     const TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY = 1;
  157.     
  158.      
  159.     /**
  160.      * A constant that indicates the complexity of mathematical operations
  161.      *
  162.      * @access public
  163.      *
  164.      */
  165.     const TEXT_CAPTCHA_NUMERAL_COMPLEXITY_HIGH_SCHOOL = 2;
  166.     
  167.      
  168.     /**
  169.      * A constant that indicates the complexity of mathematical operations
  170.      *
  171.      * @access public
  172.      *
  173.      */
  174.     const TEXT_CAPTCHA_NUMERAL_COMPLEXITY_UNIVERSITY = 4;
  175.  
  176.  
  177.  
  178.     // }}}
  179.     // {{{ Constructor
  180.     /**
  181.      * Constructor with different levels of mathematical operations sets
  182.      *
  183.      * @param constant $complexityType 
  184.      */
  185.     public function __construct($complexityType = self::TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY)
  186.     {
  187.         
  188.         switch ($complexityType{
  189.                 case 2:
  190.                      $this->operators = array('+''-''*');
  191.                      break;
  192.                 case 4:
  193.                      $this->operators = array('+''-''*''%''/');
  194.                      break;
  195.                 case 1:
  196.                 default:
  197.                      $this->operators = array('-''+');
  198.                      break;
  199.         }
  200.         
  201.         $this->generateFirstNumber();
  202.         $this->generateSecondNumber();
  203.         $this->generateOperator();
  204.         $this->generateOperation();
  205.     }
  206.     // }}}
  207.     // {{{ private function setRangeMinimum
  208.     /**
  209.      * Set Range Minimum value
  210.      *
  211.      * This function give the developer the ability
  212.      * to set the range minimum value so the operations
  213.      * can be bigger, smaller, etc.
  214.      *
  215.      * @access private
  216.      * @param  integer $minValue The minimum value
  217.      */
  218.     private function setRangeMinimum($minValue '1')
  219.     {
  220.         $this->minValue = (int)$minValue;
  221.     }
  222.     // }}}
  223.     // {{{ private function generateFirstNumber
  224.     /**
  225.      * Sets the first number
  226.      *
  227.      * This function sets the first number
  228.      * of the operation by calling the generateNumber
  229.      * function that generates a random number.
  230.      *
  231.      * @access private
  232.      * @see    $this->firstNumber, $this->generateNumber
  233.      */
  234.     private function generateFirstNumber()
  235.     {
  236.         $this->setFirstNumber($this->generateNumber());
  237.     }
  238.     // }}}
  239.     // {{{ private function generateSecondNumber
  240.     /**
  241.      * Sets second number
  242.      *
  243.      * This function sets the second number of the
  244.      * operation by calling generateNumber()
  245.      *
  246.      * @access private
  247.      * @see    $this->secondNumber, $this->generateNumber()
  248.      */
  249.     private function generateSecondNumber()
  250.     {
  251.         $this->setSecondNumber($this->generateNumber());
  252.     }
  253.     // }}}
  254.     // {{{ private function generateOperator
  255.     /**
  256.      * Sets the operation operator
  257.      *
  258.      * This function sets the operation operator by
  259.      * getting the array value of an array_rand() of
  260.      * the $this->operators() array.
  261.      *
  262.      * @access private
  263.      * @see    $this->operators, $this->operator
  264.      */
  265.     private function generateOperator()
  266.     {
  267.         $this->operator $this->operators[array_rand($this->operators)];
  268.     }
  269.     // }}}
  270.     // {{{ private function setAnswer
  271.     /**
  272.      * Sets the answer value
  273.      *
  274.      * This function will accept the parameters which is
  275.      * basically the result of the function we have done
  276.      * and it will set $this->answer with it.
  277.      *
  278.      * @access private
  279.      * @param  integer $answerValue The answer value
  280.      * @see    $this->answer
  281.      */
  282.     private function setAnswer($answerValue)
  283.     {  
  284.         $this->answer $answerValue;
  285.         return $this;
  286.     }
  287.     // }}}
  288.     // {{{ private function setFirstNumber
  289.     /**
  290.      * Set First number
  291.      *
  292.      * This function sets the first number
  293.      * to the value passed to the function
  294.      *
  295.      * @access private
  296.      * @param  integer $value The first number value.
  297.      * @return object $this  The self object
  298.      */
  299.     private function setFirstNumber($value)
  300.     {
  301.         $this->firstNumber = (int)$value;
  302.         return $this;
  303.     }
  304.     // }}}
  305.     // {{{ private function setSecondNumber
  306.     /**
  307.      * Sets the second number
  308.      *
  309.      * This function sets the second number
  310.      * with the value passed to it.
  311.      *
  312.      * @access private
  313.      * @param  integer $value The second number new value.
  314.      * @return object  $this  The self object
  315.      */
  316.     private function setSecondNumber($value)
  317.     {
  318.         $this->secondNumber = (int)$value;
  319.         return $this;
  320.     }
  321.     // }}}
  322.     // {{{ private function setOperation
  323.     /**
  324.      * Set operation
  325.      *
  326.      * This variable sets the operation variable
  327.      * by taking the firstNumber, secondNumber and operator
  328.      *
  329.      * @access private
  330.      * @see    $this->operation
  331.      */
  332.     private function setOperation()
  333.     {
  334.         $this->operation $this->getFirstNumber(' ' .
  335.                            $this->operator ' ' .
  336.                            $this->getSecondNumber();
  337.         return $this;
  338.     }
  339.     // }}}
  340.     // {{{ private function generateNumber
  341.     /**
  342.      * Generate a number
  343.      *
  344.      * This function takes the parameters that are in
  345.      * the $this->maxValue and $this->minValue and get
  346.      * the random number from them using mt_rand()
  347.      *
  348.      * @access private
  349.      * @return integer Random value between minValue and maxValue
  350.      */
  351.     private function generateNumber()
  352.     {
  353.         return mt_rand($this->minValue$this->maxValue);
  354.     }
  355.     // }}}
  356.     // {{{ private function doAdd
  357.     /**
  358.      * Adds values
  359.      *
  360.      * This function will add the firstNumber and the
  361.      * secondNumber value and then call setAnswer to
  362.      * set the answer value.
  363.      *
  364.      * @access private
  365.      * @see    $this->firstNumber, $this->secondNumber, $this->setAnswer()
  366.      */
  367.     private function doAdd()
  368.     {
  369.         $answer $this->getFirstNumber($this->getSecondNumber();
  370.         $this->setAnswer($answer);
  371.     }
  372.     // }}} 
  373.     // {{{ private function doMultiplication
  374.     /**
  375.      * Do Multiplication
  376.      * 
  377.      * This method will multiply two numbers
  378.      * 
  379.      * @access private
  380.      * @see $this->firstNumber, $this->secondNumber, $this->setAnswer
  381.      * 
  382.      */
  383.     private function doMultiplication()
  384.     {
  385.         $this->setAnswer($this->getFirstNumber($this->getSecondNumber());
  386.     }
  387.     // }}}
  388.     // {{{ private function doDivision
  389.     /**
  390.      * Do Division
  391.      *
  392.      * This function executes a division based on the two
  393.      * numbers.
  394.      *
  395.      * @param integer $firstNumber The first number of the operation.
  396.      *                              This is by default set to null.
  397.      *
  398.      * @param integer $secondNumber The second number of the operation
  399.      *                               This is by default set to null.
  400.      *
  401.      */
  402.     private function doDivision($firstNumber = null$secondNumber = null)
  403.     {
  404.         if (is_null($firstNumber)) {
  405.             $firstNumber $this->getFirstNumber();
  406.         }
  407.  
  408.         if (is_null($secondNumber)) {
  409.             $secondNumber $this->getSecondNumber();
  410.         }
  411.  
  412.         if ($secondNumber == 0{
  413.             ++$secondNumber;
  414.             $this->doDivision($firstNumber$secondNumber);
  415.             return;
  416.         }
  417.         
  418.         if ($firstNumber $secondNumber != 0{
  419.             --$firstNumber;
  420.             --$secondNumber;
  421.             
  422.             $this->doDivision($firstNumber$secondNumber);
  423.             return;
  424.         }
  425.  
  426.         $this->setFirstNumber($firstNumber)
  427.              ->setSecondNumber($secondNumber)
  428.              ->setOperation()
  429.              ->setAnswer($this->getFirstNumber($this->getSecondNumber());
  430.     }
  431.     // }}}
  432.     // {{{ private function doModulus
  433.     /**
  434.      * Do modulus
  435.      * 
  436.      * This method will do a modulus operation between two numbers
  437.      *
  438.      * 
  439.      * @access private
  440.      * @see $this->firstNumber, $this->secondNumber, $this->setAnswer()
  441.      * 
  442.      */
  443.     private function doModulus()
  444.     {
  445.        $this->setAnswer($this->getFirstNumber($this->getSecondNumber());
  446.     }
  447.     // }}}
  448.     // {{{ private function doSubstract
  449.     /**
  450.      * Does a substract on the values
  451.      *
  452.      * This function executes a substraction on the firstNumber
  453.      * and the secondNumber to then call $this->setAnswer to set
  454.      * the answer value.
  455.      *
  456.      * If the firstnumber value is smaller than the secondnumber value
  457.      * then we regenerate the first number and regenerate the operation.
  458.      *
  459.      * @access private
  460.      * @see    $this->firstNumber, $this->secondNumber, $this->setAnswer()
  461.      */
  462.     private function doSubstract()
  463.     {
  464.          $first  $this->getFirstNumber();
  465.          $second $this->getSecondNumber();
  466.  
  467.         /**
  468.          * Check if firstNumber is smaller than secondNumber
  469.          */
  470.         if ($first $second{
  471.             $this->setFirstNumber($second)
  472.                  ->setSecondNumber($first)
  473.                  ->setOperation();
  474.         }
  475.  
  476.         $answer $this->getFirstNumber($this->getSecondNumber();
  477.         $this->setAnswer($answer);
  478.     }
  479.     // }}}
  480.     // {{{ private function generateOperation
  481.     /**
  482.      * Generate the operation
  483.      *
  484.      * This function will call the setOperation() function
  485.      * to set the operation string that will be called
  486.      * to display the operation, and call the function necessary
  487.      * depending on which operation is set by this->operator.
  488.      *
  489.      * @access private
  490.      * @see    $this->setOperation(), $this->operator
  491.      */
  492.     private function generateOperation()
  493.     {
  494.         $this->setOperation();
  495.                           
  496.         switch ($this->operator{
  497.         case '+':
  498.             $this->doAdd();
  499.             break;
  500.         case '-':
  501.             $this->doSubstract();
  502.             break;
  503.         case '*':
  504.             $this->doMultiplication();
  505.             break;
  506.         case '%':
  507.             $this->doModulus();
  508.             break;
  509.         case '/':
  510.             $this->doDivision();
  511.             break;
  512.         default:
  513.             $this->doAdd();
  514.             break;
  515.         }
  516.     }
  517.     // }}}
  518.     // {{{ public function getOperation
  519.     /**
  520.      * Get operation
  521.      *
  522.      * This function will get the operation
  523.      * string from $this->operation
  524.      *
  525.      * @access public
  526.      * @return string The operation String
  527.      */
  528.     public function getOperation()
  529.     {
  530.         return $this->operation;
  531.     }
  532.     // }}}
  533.     // {{{ public function getAnswer
  534.     /**
  535.      * Get the answer value
  536.      *
  537.      * This function will retrieve the answer
  538.      * value from this->answer and return it so
  539.      * we can then display it to the user.
  540.      *
  541.      * @access public
  542.      * @return string The operation answer value.
  543.      */
  544.     public function getAnswer()
  545.     {
  546.         return $this->answer;
  547.     }
  548.     // }}}
  549.     // {{{ public function getFirstNumber
  550.     /**
  551.      * Get the first number
  552.      *
  553.      * This function will get the first number
  554.      * value from $this->firstNumber
  555.      *
  556.      * @access public
  557.      * @return integer $this->firstNumber The firstNumber
  558.      */
  559.     public function getFirstNumber()
  560.     {
  561.         return $this->firstNumber;
  562.     }
  563.     // }}}
  564.     // {{{ public function getSecondNumber
  565.     /**
  566.      * Get the second number value
  567.      *
  568.      * This function will return the second number value
  569.      *
  570.      * @access public
  571.      * @return integer $this->secondNumber The second number
  572.      */
  573.     public function getSecondNumber()
  574.     {
  575.         return $this->secondNumber;
  576.     }
  577.     // }}}
  578. }
  579. // }}}
  580. ?>

Documentation generated on Mon, 11 Mar 2019 15:00:31 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.