home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / CAPTCHA / Driver / Numeral.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  13.1 KB  |  437 lines

  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. class Text_CAPTCHA_Driver_Numeral extends Text_CAPTCHA
  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.     var $_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.     var $_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.     var $_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.     var $_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.     var $_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.     var $_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.     var $_secondNumber = '';
  139.     // }}}
  140.     // {{{ Constructor
  141.     function init($options = array())
  142.     {
  143.         if (isset($options['minValue'])) {
  144.             $this->_minValue = (int)$options['minValue'];
  145.         }
  146.         if (isset($options['maxValue'])) {
  147.             $this->_maxValue = (int)$options['maxValue'];
  148.         }
  149.         
  150.         $this->_createCAPTCHA();
  151.     }
  152.     // }}}
  153.     // {{{ private function _createCAPTCHA
  154.     /**
  155.      * Create the CAPTCHA (the numeral expressio)
  156.      * 
  157.      * This function determines a random numeral expression
  158.      * and set the associated class properties
  159.      *
  160.      * @access private
  161.      */
  162.     function _createCAPTCHA()
  163.     { 
  164.         $this->_generateFirstNumber();
  165.         $this->_generateSecondNumber();
  166.         $this->_generateOperator();
  167.         $this->_generateOperation();
  168.     }
  169.     // }}}
  170.     // {{{ private function _setRangeMinimum
  171.     /**
  172.      * Set Range Minimum value
  173.      * 
  174.      * This function give the developer the ability
  175.      * to set the range minimum value so the operations
  176.      * can be bigger, smaller, etc.
  177.      *
  178.      * @access private
  179.      * @param  integer $minValue The minimum value
  180.      */
  181.     function _setRangeMinimum($minValue = 1) 
  182.     {
  183.         $this->minValue = (int)$minValue;
  184.     }
  185.     // }}}
  186.     // {{{ private function _generateFirstNumber
  187.     /**
  188.      * Sets the first number
  189.      * 
  190.      * This function sets the first number 
  191.      * of the operation by calling the _generateNumber
  192.      * function that generates a random number.
  193.      * 
  194.      * @access private
  195.      * @see    $this->_firstNumber, $this->_generateNumber
  196.      */
  197.     function _generateFirstNumber()
  198.     {
  199.         $this->_setFirstNumber($this->_generateNumber());
  200.     }
  201.     // }}}
  202.     // {{{ private function generateSecondNumber
  203.     /**
  204.      * Sets second number
  205.      * 
  206.      * This function sets the second number of the
  207.      * operation by calling _generateNumber()
  208.      * 
  209.      * @access private
  210.      * @see    $this->_secondNumber, $this->_generateNumber()
  211.      */
  212.     function _generateSecondNumber()
  213.     {
  214.         $this->_setSecondNumber($this->_generateNumber());
  215.     }
  216.     // }}}
  217.     // {{{ private function generateOperator
  218.     /**
  219.      * Sets the operation operator
  220.      * 
  221.      * This function sets the operation operator by
  222.      * getting the array value of an array_rand() of
  223.      * the $this->_operators() array.
  224.      *
  225.      * @access private
  226.      * @see    $this->_operators, $this->_operator
  227.      */
  228.     function _generateOperator()
  229.     {
  230.         $this->_operator = $this->_operators[array_rand($this->_operators)];
  231.     }
  232.     // }}}
  233.     // {{{ private function setAnswer
  234.     /**
  235.      * Sets the answer value
  236.      * 
  237.      * This function will accept the parameters which is
  238.      * basically the result of the function we have done 
  239.      * and it will set $this->answer with it.
  240.      * 
  241.      * @access private
  242.      * @param  integer $phraseValue The answer value
  243.      * @see    $this->_phrase
  244.      */
  245.     function _setPhrase($phraseValue)
  246.     {   
  247.         $this->_phrase = $phraseValue;
  248.     }
  249.     // }}}
  250.     // {{{ private function setFirstNumber
  251.     /**
  252.      * Set First number
  253.      *
  254.      * This function sets the first number
  255.      * to the value passed to the function
  256.      *
  257.      * @access private
  258.      * @param  integer $value The first number value.
  259.      */
  260.     function _setFirstNumber($value) 
  261.     {
  262.         $this->_firstNumber = (int)$value;
  263.     }
  264.     // }}}
  265.     // {{{ private function setSecondNumber
  266.     /**
  267.      * Sets the second number
  268.      * 
  269.      * This function sets the second number
  270.      * with the value passed to it.
  271.      *
  272.      * @access private
  273.      * @param  integer $value The second number new value.
  274.      */
  275.     function _setSecondNumber($value)
  276.     {
  277.         $this->_secondNumber = (int)$value;
  278.     }
  279.     // }}}
  280.     // {{{ private function setOperation
  281.     /**
  282.      * Set operation
  283.      * 
  284.      * This variable sets the operation variable
  285.      * by taking the firstNumber, secondNumber and operator
  286.      *
  287.      * @access private
  288.      * @see    $this->_operation
  289.      */
  290.     function _setOperation()
  291.     {
  292.         $this->_operation = $this->_getFirstNumber() . ' ' .
  293.                             $this->_operator . ' ' .
  294.                             $this->_getSecondNumber();
  295.     }
  296.     // }}}
  297.     // {{{ private function _generateNumber
  298.     /**
  299.      * Generate a number
  300.      * 
  301.      * This function takes the parameters that are in 
  302.      * the $this->_maxValue and $this->_minValue and get
  303.      * the random number from them using mt_rand()
  304.      *
  305.      * @access private
  306.      * @return integer Random value between _minValue and _maxValue
  307.      */
  308.     function _generateNumber()
  309.     {
  310.         return mt_rand($this->_minValue, $this->_maxValue);
  311.     }
  312.     // }}}
  313.     // {{{ private function _doAdd
  314.     /**
  315.      * Adds values
  316.      * 
  317.      * This function will add the firstNumber and the
  318.      * secondNumber value and then call setAnswer to
  319.      * set the answer value.
  320.      * 
  321.      * @access private
  322.      * @see    $this->_firstNumber, $this->_secondNumber, $this->_setAnswer()
  323.      */
  324.     function _doAdd()
  325.     {
  326.         $phrase = $this->_getFirstNumber() + $this->_getSecondNumber();
  327.         $this->_setPhrase($phrase);
  328.     }
  329.     // }}}
  330.     // {{{ private function _doSubstract
  331.     /**
  332.      * Does a substract on the values
  333.      * 
  334.      * This function executes a substraction on the firstNumber
  335.      * and the secondNumber to then call $this->setAnswer to set
  336.      * the answer value. 
  337.      * 
  338.      * If the firstnumber value is smaller than the secondnumber value
  339.      * then we regenerate the first number and regenerate the operation.
  340.      * 
  341.      * @access private
  342.      * @see    $this->_firstNumber, $this->_secondNumber, $this->_setAnswer()
  343.      */
  344.     function _doSubstract()
  345.     {
  346.         $first  = $this->_getFirstNumber();
  347.         $second = $this->_getSecondNumber();
  348.         
  349.         /**
  350.          * Check if firstNumber is smaller than secondNumber
  351.          */
  352.     if ($first < $second) {
  353.         $this->_setFirstNumber($second);
  354.         $this->_setSecondNumber($first);
  355.         $this->_setOperation();
  356.     }
  357.  
  358.         $phrase = $this->_getFirstNumber() - $this->_getSecondNumber();
  359.         $this->_setPhrase($phrase);
  360.     }
  361.     // }}}
  362.     // {{{ private function _generateOperation
  363.     /**
  364.      * Generate the operation
  365.      * 
  366.      * This function will call the _setOperation() function
  367.      * to set the operation string that will be called
  368.      * to display the operation, and call the function necessary
  369.      * depending on which operation is set by this->operator.
  370.      * 
  371.      * @access private
  372.      * @see    $this->_setOperation(), $this->_operator
  373.      */
  374.     function _generateOperation()
  375.     {
  376.         $this->_setOperation();
  377.                            
  378.         switch ($this->_operator) {
  379.         case '+':
  380.             $this->_doAdd();
  381.             break;
  382.         case '-':
  383.             $this->_doSubstract();
  384.             break;
  385.         default:
  386.             $this->_doAdd();
  387.             break;
  388.         }
  389.     }
  390.     // }}}
  391.     // {{{ public function _getFirstNumber
  392.     /**
  393.      * Get the first number
  394.      * 
  395.      * This function will get the first number
  396.      * value from $this->_firstNumber
  397.      * 
  398.      * @access public
  399.      * @return integer $this->_firstNumber The firstNumber
  400.      */
  401.     function _getFirstNumber()
  402.     {
  403.         return $this->_firstNumber;
  404.     }
  405.     // }}}
  406.     // {{{ public function _getSecondNumber
  407.     /**
  408.      * Get the second number value
  409.      * 
  410.      * This function will return the second number value
  411.      * 
  412.      * @access public
  413.      * @return integer $this->_secondNumber The second number
  414.      */
  415.     function _getSecondNumber()
  416.     {
  417.         return $this->_secondNumber;
  418.     }
  419.     // }}}
  420.     // {{{ public function getCAPTCHA
  421.     /**
  422.      * Get operation
  423.      * 
  424.      * This function will get the operation
  425.      * string from $this->_operation
  426.      *
  427.      * @access public
  428.      * @return string The operation String
  429.      */
  430.     function getCAPTCHA()
  431.     {
  432.         return $this->_operation;
  433.     }
  434. }
  435. // }}}
  436. ?>
  437.