package.xml100666 0 0 24360 11531456744 6266 Text_CAPTCHA pear.php.net Generation of CAPTCHAs Implementation of CAPTCHAs (completely automated public Turing test to tell computers and humans apart) Christian Wenz wenz wenz@php.net yes 2011-02-24 0.4.3 0.4.0 alpha alpha BSD License + fixed bug #18300/18198: Figlet driver options not correctly passed to password generation (patch by green) + fixed JavaScript figlet generation (Christian) 4.0.0 1.4.0b1 Text_Password pear.php.net Numbers_Words pear.php.net Text_Figlet pear.php.net Image_Text pear.php.net 0.6.0beta gd 0.4.3 0.4.0 alpha alpha 2011-02-24 PHP License + fixed bug #18300/18198: Figlet driver options not correctly passed to password generation (patch by green) + fixed JavaScript figlet generation (Christian) 0.4.2 0.4.0 alpha alpha 2011-01-16 PHP License + implemented feature request #17601: add antialias option for lines (patch by troyanderson) + implemented feature request #17424: Compatible _createPhrase() declarations (patch by cweiske, neufeind) + fixed bug #18151: Deprecation warnings because of "return of new by reference" (Christian) + many CS fixes (neufeind) 0.4.1 0.4.0 alpha alpha 2010-10-25 PHP License + QA release (no new features) 0.4.0 0.4.0 alpha alpha 2009-07-27 PHP License + ** license change from PHP license to BSD license ** + implemented feature request #16433 (fixing behavior from bug #13478): If image is too small, no text is created (patch by tacker) + implemented feature request #11653: new 'phraseOptions' setting to provide options to Text_Password::create() call + updated and new examples + various cosmetic changes 0.3.1 0.3.0 alpha alpha 2007-09-02 PHP License + implemented feature request #11957: Providing better Image driver error messages in various places + fixed example/documentation bug #11960 + various cosmetic changes 0.3.0 0.3.0 alpha alpha 2007-08-01 PHP License + new feature: now supports setting background and line color for image CAPTCHAs (see CAPTCHA_test.php example file). Requires Image_Text >= 0.6.0beta to work. Many thanks to isnull! + bugfix: CAPTCHA drivers now also load Text/CAPTCHA.php via require_once (suggested by Philippe Jausions) 0.2.1 0.2.1 alpha alpha 2007-02-18 PHP License + bugfix: image height could not be set (reported by Hendrik Vorwerk) + cosmetic changes 0.2.0 0.2.0 alpha alpha 2006-12-24 PHP License *********************************** ********* MERRY CHRISTMAS ********* *********************************** *********************************** *** Upcoming BC BREAKING CHANGES ** *********************************** + CAPTCHA options are now provided as one array (wormus' suggestion) + image CAPTCHA is now only created upon request, making it serializable (jausions' suggestion) + New drivers: Figlet (wormus), Word (toby), Numeral (davidc), Equation (cweiske) + some other stuff ... 0.1.6 0.1.6 alpha alpha 2005-10-27 PHP License + fixed a bug in the sample (noticed by Nima Sadjadi) 0.1.5 0.1.5 alpha alpha 2005-09-26 PHP License + cosmetic changes (CS) + small changes sample script (suggested by Lukas Smith, thanks!) 0.1.4 0.1.4 alpha alpha 2005-08-11 PHP License + cosmetic changes (whitespace, line endings) 0.1.3 0.1.3 alpha alpha 2005-06-19 PHP License + better check for errors thrown from Image_Text + updated examples + cosmetic changes 0.1.2 0.1.2 alpha alpha 2005-01-26 PHP License Bugfix release + fixed bug #3271 (thanks to Justin) + some cosmetic changes 0.1.1 0.1.1 alpha alpha 2004-11-08 PHP License Bugfix release (Int'l PHP Conference Edition) + fixed bug #2584 (thanks to wormus) + various small fixes 0.1.0 0.1.0 alpha alpha 2004-10-21 PHP License Initial release (PHP World, Munich) Text_CAPTCHA-0.4.3/CAPTCHA/Driver/Equation.php100666 0 0 13641 11531456744 13454 * @author Christian Wenz * @license BSD License */ require_once 'Text/CAPTCHA.php'; class Text_CAPTCHA_Driver_Equation extends Text_CAPTCHA { /** * Operators that may be used in the equation. * Two numbers have to be filled in, and * %s is needed since number2text conversion * may be applied and strings filled in. * * @access protected * @var array */ var $_operators = array( '%s * %s', '%s + %s', '%s - %s', 'min(%s, %s)', 'max(%s, %s)' ); /** * The equation to solve. * * @access protected * @var string */ var $_equation = null; /** * Minimal number to use in an equation. * * @access protected * @var int */ var $_min = 1; /** * Maximum number to use in an equation. * * @access protected * @var int */ var $_max = 10; /** * Whether numbers shall be converted to text * * @access protected * @var bool */ var $_numbersToText = false; /** * Complexity of the generated equations. * 1 - simple ones such as "1 + 10" * 2 - harder ones such as "(3-2)*(min(5,6))" * * @access protected * @var int */ var $_severity = 1; /** * Last error * * @access protected * @var PEAR_Error */ var $_error = null; /** * Initialize the driver. * * @param array $options Optionally supply options for the initialization phase * * @access public * @return true on success, PEAR_Error on error. */ function init($options = array()) { if (isset($options['min'])) { $this->_min = (int)$options['min']; } else { $this->_min = 1; } if (isset($options['max'])) { $this->_max = (int)$options['max']; } else { $this->_max = 10; } if (isset($options['numbersToText'])) { $this->_numbersToText = (bool)$options['numbersToText']; } else { $this->_numbersToText = false; } if (isset($options['severity'])) { $this->_severity = (int)$options['severity']; } else { $this->_severity = 1; } if ($this->_numbersToText) { include_once 'Numbers/Words.php'; if (!class_exists('Numbers_Words')) { $this->_error = PEAR::raiseError('Number_Words package required', true); return $this->_error; } } return $this->_createPhrase(); } /** * Create random CAPTCHA equation. * * This method creates a random equation. The equation is * stored in $this->_equation, the solution in $this->_phrase. * * @access protected * @return mixed true on success, PEAR_Error on error */ function _createPhrase() { switch ($this->_severity) { case 1: list($this->_equation, $this->_phrase) = $this->_createSimpleEquation(); break; case 2: list($eq1, $sol1) = $this->_createSimpleEquation(); list($eq2, $sol2) = $this->_createSimpleEquation(); $op3 = $this->_operators[rand(0, count($this->_operators) - 1)]; list($eq3, $this->_phrase) = $this->_solveSimpleEquation($sol1, $sol2, $op3); $this->_equation = sprintf($op3, '(' . $eq1 . ')', '(' . $eq2 . ')'); break; default: $this->_error = PEAR::raiseError('Equation complexity of ' . $this->_severity . ' not supported', true); return $this->_error; } return true; } /** * Creates a simple equation of type (number operator number) * * @access protected * @return array Array with equation and solution */ function _createSimpleEquation() { $one = rand($this->_min, $this->_max); $two = rand($this->_min, $this->_max); $operator = $this->_operators[rand(0, count($this->_operators) - 1)]; return $this->_solveSimpleEquation($one, $two, $operator); } /** * Solves a simple equation with two given numbers * and one operator as defined in $this->_operators. * * Also converts the numbers to words if required. * * @param int $one First number * @param int $two Second number * @param string $operator Operator * * @access protected * @return array Array with equation and solution */ function _solveSimpleEquation($one, $two, $operator) { $equation = sprintf($operator, $one, $two); $code = '$solution=' . $equation . ';'; eval($code); if ($this->_numbersToText) { $equation = sprintf($operator, Numbers_Words::toWords($one), Numbers_Words::toWords($two)); } return array($equation, $solution); } /** * Return the solution to the equation. * * This method returns the CAPTCHA phrase, which is * the solution to the equation. * * @access public * @return string secret phrase */ function getPhrase() { return $this->_phrase; } /** * Creates the captcha. This method is a placeholder, * since the equation is created in _createPhrase() * * @access protected * @return PEAR_Error */ function _createCAPTCHA() { //is already done in _createPhrase(); } /** * Returns the CAPTCHA (as a string) * * @access public * @return string */ function getCAPTCHA() { return $this->_equation; } }//class Text_CAPTCHA_Driver_TextEquation extends Text_CAPTCHA Text_CAPTCHA-0.4.3/CAPTCHA/Driver/Figlet.php100666 0 0 16362 11531456744 13104 * @author Christian Wenz * @license BSD License * @todo define an obfuscation algorithm */ class Text_CAPTCHA_Driver_Figlet extends Text_CAPTCHA { /** * Text_Figlet object * * @access private * @var resource */ var $_fig; /** * Width of CAPTCHA * * @access private * @var int */ var $_width; /** * Figlet output string * * @access private * @var string */ var $_output_string; /** * Figlet font options * * @access private * @var array */ var $_fonts = array(); /** * Figlet font * * @access private * @var string */ var $_font; /** * Figlet font * * @access private * @var array */ var $_style = array(); /** * Output Format * * @access private * @var string */ var $_output; /** * Last error * * @access protected * @var PEAR_Error */ var $_error = null; /** * init function * * Initializes the new Text_CAPTCHA_Driver_Figlet object and creates a GD image * * @param array $options CAPTCHA options * @access public * @return mixed true upon success, PEAR error otherwise */ function init($options = array()) { if (is_array($options)) { if (!empty($options['output'])) { $this->_output = $options['output']; } else { $this->_output = 'html'; } if (isset($options['width']) && is_int($options['width'])) { $this->_width = $options['width']; } else { $this->_width = 200; } if (!empty($options['length'])) { $this->_length = $options['length']; } else { $this->_length = 6; } if (!isset($options['phrase']) || empty($options['phrase'])) { $phraseoptions = (isset($options['phraseOptions']) && is_array($options['phraseOptions'])) ? $options['phraseOptions'] : array(); $this->_createPhrase($phraseoptions); } else { $this->_phrase = $options['phrase']; } } if (!isset($options['options']) || empty($options['options']) || !is_array($options['options'])) { die; } else { if (isset($options['options']['style']) && !empty($options['options']['style']) && is_array($options['options']['style'])) { $this->_style = $options['options']['style']; } if (!isset($this->_style['padding']) || empty($this->_style['padding'])) { $this->_style['padding'] = '5px'; } if (isset($options['options']['font_file']) && !empty($options['options']['font_file'])) { if (is_array($options['options']['font_file'])) { $this->_font = $options['options']['font_file'][array_rand($options['options']['font_file'])]; } else { $this->_font = $options['options']['font_file']; } } } } /** * Create random CAPTCHA phrase * This method creates a random phrase * * @param array $options Optionally supply advanced options for the phase creation; * used for the initialization of Text_Password * * @access private * @return void */ function _createPhrase($options = array()) { if (!is_array($options) || count($options) === 0) { $this->_phrase = Text_Password::create($this->_length); } else { if (count($options) === 1) { $this->_phrase = Text_Password::create($this->_length, $options[0]); } else { $this->_phrase = Text_Password::create($this->_length, $options[0], $options[1]); } } } /** * Create CAPTCHA image * * This method creates a CAPTCHA image * * @access private * @return void PEAR_Error on error */ function _createCAPTCHA() { $this->_fig = new Text_Figlet(); if (PEAR::isError($this->_fig->LoadFont($this->_font))) { $this->_error = PEAR::raiseError('Error loading Text_Figlet font'); return $this->_error; } $this->_output_string = $this->_fig->LineEcho($this->_phrase); } /** * Return CAPTCHA in the specified format * * This method returns the CAPTCHA depending on the output format * * @access public * @return mixed Formatted captcha or PEAR error */ function getCAPTCHA() { $retval = $this->_createCAPTCHA(); if (PEAR::isError($retval)) { return PEAR::raiseError($retval->getMessage()); } switch ($this->_output) { case 'text': return $this->_output_string; break; case 'html': return $this->getCAPTCHAAsHTML(); break; case 'javascript': return $this->getCAPTCHAAsJavascript(); break; } } /** * Return CAPTCHA as HTML * * This method returns the CAPTCHA as HTML * * @access public * @return mixed HTML Figlet image or PEAR error */ function getCAPTCHAAsHTML() { $retval = $this->_createCAPTCHA(); if (PEAR::isError($retval)) { return PEAR::raiseError($retval->getMessage()); } $charwidth = strpos($this->_output_string, "\n"); $data = str_replace("\n", '
', $this->_output_string); $textsize = ($this->_width / $charwidth) * 1.4; $css_output = ""; foreach ($this->_style as $key => $value) { $css_output .= "$key: $value;"; } $htmloutput = '
'; $htmloutput .= '
'. $data. '
'; return $htmloutput; } /** * Return CAPTCHA as JavaScript version of HTML * * This method returns the CAPTCHA as a JavaScript string * I'm not exactly sure what the point of doing this would be. * * @access public * @return mixed JavaScript string or PEAR error */ function getCAPTCHAAsJavascript() { $data = $this->getCAPTCHAAsHTML(); if (PEAR::isError($data)) { return PEAR::raiseError($data->getMessage()); } $obfus_data = rawurlencode($data); $javascript = ""; return $javascript; } } Text_CAPTCHA-0.4.3/CAPTCHA/Driver/Image.php100666 0 0 26416 11531456744 12715 * @license BSD License * @todo refine the obfuscation algorithm :-) * @todo consider removing Image_Text dependency */ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA { /** * Image object * * @access private * @var resource */ var $_im; /** * Image_Text object * * @access private * @var resource */ var $_imt; /** * Width of CAPTCHA * * @access private * @var int */ var $_width; /** * Height of CAPTCHA * * @access private * @var int */ var $_height; /** * CAPTCHA output format * * @access private * @var string */ var $_output; /** * Further options (here: for Image_Text) * * @access private * @var array */ var $_imageOptions = array( 'font_size' => 24, 'font_path' => './', 'font_file' => 'COUR.TTF', 'text_color' => '#000000', 'lines_color' => '#CACACA', 'background_color' => '#555555', 'antialias' => false); /** * Whether the immage resource has been created * * @access private * @var boolean */ var $_created = false; /** * Last error * * @access protected * @var PEAR_Error */ var $_error = null; /** * init function * * Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image * * @param array $options CAPTCHA options * * @access public * @return mixed true upon success, PEAR error otherwise */ function init($options = array()) { if (!is_array($options)) { // Compatibility mode ... in future versions, these two // lines of code will be used: // $this->_error = PEAR::raiseError('You need to provide a set of CAPTCHA options!'); // return $this->_error; $o = array(); $args = func_get_args(); if (isset($args[0])) { $o['width'] = $args[0]; } if (isset($args[1])) { $o['height'] = $args[1]; } if (isset($args[2]) && $args[2] != null) { $o['phrase'] = $args[2]; } if (isset($args[3]) && is_array($args[3])) { $o['imageOptions'] = $args[3]; } $options = $o; } if (is_array($options)) { if (isset($options['width']) && is_int($options['width'])) { $this->_width = $options['width']; } else { $this->_width = 200; } if (isset($options['height']) && is_int($options['height'])) { $this->_height = $options['height']; } else { $this->_height = 80; } if (!isset($options['phrase']) || empty($options['phrase'])) { $phraseoptions = (isset($options['phraseOptions']) && is_array($options['phraseOptions'])) ? $options['phraseOptions'] : array(); $this->_createPhrase($phraseoptions); } else { $this->_phrase = $options['phrase']; } if (!isset($options['output']) || empty($options['output'])) { $this->_output = 'resource'; } else { $this->_output = $options['output']; } if (isset($options['imageOptions']) && is_array($options['imageOptions']) && count($options['imageOptions']) > 0) { $this->_imageOptions = array_merge($this->_imageOptions, $options['imageOptions']); } return true; } } /** * Create random CAPTCHA phrase, Image edition (with size check) * * This method creates a random phrase, maximum 8 characters or width / 25, whatever is smaller * * @param array $options Optionally supply advanced options for the phrase creation * * @access private * @return void */ function _createPhrase($options = array()) { $len = intval(min(8, $this->_width / 25)); if (!is_array($options) || count($options) === 0) { $this->_phrase = Text_Password::create($len); } else { if (count($options) === 1) { $this->_phrase = Text_Password::create($len, $options[0]); } else { $this->_phrase = Text_Password::create($len, $options[0], $options[1]); } } $this->_created = false; } /** * Create CAPTCHA image * * This method creates a CAPTCHA image * * @access private * @return void PEAR_Error on error */ function _createCAPTCHA() { if ($this->_error) { return $this->_error; } if ($this->_created) { return; } $options['canvas'] = array( 'width' => $this->_width, 'height' => $this->_height ); $options['width'] = $this->_width - 20; $options['height'] = $this->_height - 20; $options['cx'] = ceil(($this->_width) / 2 + 10); $options['cy'] = ceil(($this->_height) / 2 + 10); $options['angle'] = rand(0, 30) - 15; $options['font_size'] = $this->_imageOptions['font_size']; $options['font_path'] = $this->_imageOptions['font_path']; $options['font_file'] = $this->_imageOptions['font_file']; $options['color'] = array($this->_imageOptions['text_color']); $options['background_color'] = $this->_imageOptions['background_color']; $options['max_lines'] = 1; $options['mode'] = 'auto'; do { $this->_imt = new Image_Text( $this->_phrase, $options ); if (PEAR::isError($e = $this->_imt->init())) { $this->_error = PEAR::raiseError( sprintf('Error initializing Image_Text (%s)', $e->getMessage())); return $this->_error; } else { $this->_created = true; } $result = $this->_imt->measurize(); } while ($result === false && --$options['font_size'] > 0); if ($result === false) { $this->_error = PEAR::raiseError('The text provided does not fit in the image dimensions'); return $this->_error; } $this->_imt->render(); $this->_im =& $this->_imt->getImg(); if (isset($this->_imageOptions['antialias']) && $this->_imageOptions['antialias'] === true && function_exists('imageantialias')) { imageantialias($this->_im, true); } $colors = $this->_imt->_convertString2RGB($this->_imageOptions['lines_color']); $lines_color = imagecolorallocate($this->_im, $colors['r'], $colors['g'], $colors['b']); //some obfuscation for ($i = 0; $i < 3; $i++) { $x1 = rand(0, $this->_width - 1); $y1 = rand(0, round($this->_height / 10, 0)); $x2 = rand(0, round($this->_width / 10, 0)); $y2 = rand(0, $this->_height - 1); imageline($this->_im, $x1, $y1, $x2, $y2, $lines_color); $x1 = rand(0, $this->_width - 1); $y1 = $this->_height - rand(1, round($this->_height / 10, 0)); $x2 = $this->_width - rand(1, round($this->_width / 10, 0)); $y2 = rand(0, $this->_height - 1); imageline($this->_im, $x1, $y1, $x2, $y2, $lines_color); $cx = rand(0, $this->_width - 50) + 25; $cy = rand(0, $this->_height - 50) + 25; $w = rand(1, 24); imagearc($this->_im, $cx, $cy, $w, $w, 0, 360, $lines_color); } } /** * Return CAPTCHA as image resource * * This method returns the CAPTCHA depending on the output format * * @access public * @return mixed image resource or PEAR error */ function getCAPTCHA() { $retval = $this->_createCAPTCHA(); if (PEAR::isError($retval)) { return PEAR::raiseError($retval->getMessage()); } if ($this->_output == 'gif' && !function_exists('imagegif')) { $this->_output = 'png'; } switch ($this->_output) { case 'png': return $this->getCAPTCHAAsPNG(); break; case 'jpg': case 'jpeg': return $this->getCAPTCHAAsJPEG(); break; case 'gif': return $this->getCAPTCHAAsGIF(); break; case 'resource': default: return $this->_im; } } /** * Return CAPTCHA as PNG * * This method returns the CAPTCHA as PNG * * @access public * @return mixed image contents or PEAR error */ function getCAPTCHAAsPNG() { $retval = $this->_createCAPTCHA(); if (PEAR::isError($retval)) { return PEAR::raiseError($retval->getMessage()); } if (is_resource($this->_im)) { ob_start(); imagepng($this->_im); $data = ob_get_contents(); ob_end_clean(); return $data; } else { $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)'); return $this->_error; } } /** * Return CAPTCHA as JPEG * * This method returns the CAPTCHA as JPEG * * @access public * @return mixed image contents or PEAR error */ function getCAPTCHAAsJPEG() { $retval = $this->_createCAPTCHA(); if (PEAR::isError($retval)) { return PEAR::raiseError($retval->getMessage()); } if (is_resource($this->_im)) { ob_start(); imagejpeg($this->_im); $data = ob_get_contents(); ob_end_clean(); return $data; } else { $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)'); return $this->_error; } } /** * Return CAPTCHA as GIF * * This method returns the CAPTCHA as GIF * * @access public * @return mixed image contents or PEAR error */ function getCAPTCHAAsGIF() { $retval = $this->_createCAPTCHA(); if (PEAR::isError($retval)) { return PEAR::raiseError($retval->getMessage()); } if (is_resource($this->_im)) { ob_start(); imagegif($this->_im); $data = ob_get_contents(); ob_end_clean(); return $data; } else { $this->_error = PEAR::raiseError('Error creating CAPTCHA image (font missing?!)'); return $this->_error; } } /** * __wakeup method (PHP 5 only) * * @return void */ function __wakeup() { $this->_created = false; } } Text_CAPTCHA-0.4.3/CAPTCHA/Driver/Numeral.php100666 0 0 32620 11531456744 13270 | // +----------------------------------------------------------------------+ // require_once 'Text/CAPTCHA.php'; /** * Class used for numeral captchas * * This class is intended to be used to generate * numeral captchas as such as: * Example: * Give me the answer to "54 + 2" to prove that you are human. * * @category Text * @package Text_CAPTCHA * @author David Coallier * @author Christian Wenz */ class Text_CAPTCHA_Driver_Numeral extends Text_CAPTCHA { // {{{ Variables /** * Minimum range value * * This variable holds the minimum range value * default set to "1" * * @access private * @var integer $_minValue The minimum range value */ var $_minValue = 1; /** * Maximum range value * * This variable holds the maximum range value * default set to "50" * * @access private * @var integer $_maxValue The maximum value of the number range */ var $_maxValue = 50; /** * Operators * * The valid operators to use * in the numeral captcha. We could * use / and * but not yet. * * @access private * @var array $_operators The operations for the captcha */ var $_operators = array('-', '+'); /** * Operator to use * * This variable is basically the operation * that we're going to be using in the * numeral captcha we are about to generate. * * @access private * @var string $_operator The operation's operator */ var $_operator = ''; /** * Mathematical Operation * * This is the mathematical operation * that we are displaying to the user. * * @access private * @var string $_operation The math operation */ var $_operation = ''; /** * First number of the operation * * This variable holds the first number * of the numeral operation we are about * to generate. * * @access private * @var integer $_firstNumber The first number of the operation */ var $_firstNumber = ''; /** * Second Number of the operation * * This variable holds the value of the * second variable of the operation we are * about to generate for the captcha. * * @access private * @var integer $_secondNumber The second number of the operation */ var $_secondNumber = ''; // }}} // {{{ Constructor function init($options = array()) { if (isset($options['minValue'])) { $this->_minValue = (int)$options['minValue']; } if (isset($options['maxValue'])) { $this->_maxValue = (int)$options['maxValue']; } $this->_createCAPTCHA(); } // }}} // {{{ private function _createCAPTCHA /** * Create the CAPTCHA (the numeral expression) * * This function determines a random numeral expression * and set the associated class properties * * @access private * @return void */ function _createCAPTCHA() { $this->_generateFirstNumber(); $this->_generateSecondNumber(); $this->_generateOperator(); $this->_generateOperation(); } // }}} // {{{ private function _setRangeMinimum /** * Set Range Minimum value * * This function give the developer the ability * to set the range minimum value so the operations * can be bigger, smaller, etc. * * @param integer $minValue The minimum value * * @access private * @return void */ function _setRangeMinimum($minValue = 1) { $this->minValue = (int)$minValue; } // }}} // {{{ private function _generateFirstNumber /** * Sets the first number * * This function sets the first number * of the operation by calling the _generateNumber * function that generates a random number. * * @access private * @return void * @see $this->_firstNumber, $this->_generateNumber */ function _generateFirstNumber() { $this->_setFirstNumber($this->_generateNumber()); } // }}} // {{{ private function generateSecondNumber /** * Sets second number * * This function sets the second number of the * operation by calling _generateNumber() * * @access private * @return void * @see $this->_secondNumber, $this->_generateNumber() */ function _generateSecondNumber() { $this->_setSecondNumber($this->_generateNumber()); } // }}} // {{{ private function generateOperator /** * Sets the operation operator * * This function sets the operation operator by * getting the array value of an array_rand() of * the $this->_operators() array. * * @access private * @return void * @see $this->_operators, $this->_operator */ function _generateOperator() { $this->_operator = $this->_operators[array_rand($this->_operators)]; } // }}} // {{{ private function setAnswer /** * Sets the answer value * * This function will accept the parameters which is * basically the result of the function we have done * and it will set $this->answer with it. * * @param integer $phraseValue The answer value * * @access private * @return void * @see $this->_phrase */ function _setPhrase($phraseValue) { $this->_phrase = $phraseValue; } // }}} // {{{ private function setFirstNumber /** * Set First number * * This function sets the first number * to the value passed to the function * * @param integer $value The first number value. * * @access private * @return void */ function _setFirstNumber($value) { $this->_firstNumber = (int)$value; } // }}} // {{{ private function setSecondNumber /** * Sets the second number * * This function sets the second number * with the value passed to it. * * @param integer $value The second number new value. * * @access private * @return void */ function _setSecondNumber($value) { $this->_secondNumber = (int)$value; } // }}} // {{{ private function setOperation /** * Set operation * * This variable sets the operation variable * by taking the firstNumber, secondNumber and operator * * @access private * @return void * @see $this->_operation */ function _setOperation() { $this->_operation = $this->_getFirstNumber() . ' ' . $this->_operator . ' ' . $this->_getSecondNumber(); } // }}} // {{{ private function _generateNumber /** * Generate a number * * This function takes the parameters that are in * the $this->_maxValue and $this->_minValue and get * the random number from them using mt_rand() * * @access private * @return integer Random value between _minValue and _maxValue */ function _generateNumber() { return mt_rand($this->_minValue, $this->_maxValue); } // }}} // {{{ private function _doAdd /** * Adds values * * This function will add the firstNumber and the * secondNumber value and then call setAnswer to * set the answer value. * * @access private * @return void * @see $this->_firstNumber, $this->_secondNumber, $this->_setAnswer() */ function _doAdd() { $phrase = $this->_getFirstNumber() + $this->_getSecondNumber(); $this->_setPhrase($phrase); } // }}} // {{{ private function _doSubstract /** * Does a substract on the values * * This function executes a substraction on the firstNumber * and the secondNumber to then call $this->setAnswer to set * the answer value. * * If the firstnumber value is smaller than the secondnumber value * then we regenerate the first number and regenerate the operation. * * @access private * @return void * @see $this->_firstNumber, $this->_secondNumber, $this->_setAnswer() */ function _doSubstract() { $first = $this->_getFirstNumber(); $second = $this->_getSecondNumber(); /** * Check if firstNumber is smaller than secondNumber */ if ($first < $second) { $this->_setFirstNumber($second); $this->_setSecondNumber($first); $this->_setOperation(); } $phrase = $this->_getFirstNumber() - $this->_getSecondNumber(); $this->_setPhrase($phrase); } // }}} // {{{ private function _generateOperation /** * Generate the operation * * This function will call the _setOperation() function * to set the operation string that will be called * to display the operation, and call the function necessary * depending on which operation is set by this->operator. * * @access private * @return void * @see $this->_setOperation(), $this->_operator */ function _generateOperation() { $this->_setOperation(); switch ($this->_operator) { case '+': $this->_doAdd(); break; case '-': $this->_doSubstract(); break; default: $this->_doAdd(); break; } } // }}} // {{{ public function _getFirstNumber /** * Get the first number * * This function will get the first number * value from $this->_firstNumber * * @access public * @return integer $this->_firstNumber The firstNumber */ function _getFirstNumber() { return $this->_firstNumber; } // }}} // {{{ public function _getSecondNumber /** * Get the second number value * * This function will return the second number value * * @access public * @return integer $this->_secondNumber The second number */ function _getSecondNumber() { return $this->_secondNumber; } // }}} // {{{ public function getCAPTCHA /** * Get operation * * This function will get the operation * string from $this->_operation * * @access public * @return string The operation String */ function getCAPTCHA() { return $this->_operation; } } // }}} Text_CAPTCHA-0.4.3/CAPTCHA/Driver/Word.php100666 0 0 5635 11531456744 12566 * @author Christian Wenz * @license BSD License */ /** * * Require Numbers_Words class for generating the text. * */ require_once 'Text/CAPTCHA.php'; require_once 'Numbers/Words.php'; class Text_CAPTCHA_Driver_Word extends Text_CAPTCHA { /** * Phrase length * * This variable holds the length of the Word * * @access private */ var $_length; /** * Numbers_Words mode * * This variable holds the mode for Numbers_Words * * @access private */ var $_mode; /** * Locale * * This variable holds the locale for Numbers_Words * * @access private */ var $_locale; /** * init function * * Initializes the new Text_CAPTCHA_Driver_Word object * * @param array $options CAPTCHA options with these keys: * phrase The "secret word" of the CAPTCHA * length The number of characters in the phrase * locale The locale for Numbers_Words * mode The mode for Numbers_Words * * @access public */ function init($options = array()) { if (isset($options['length']) && is_int($options['length'])) { $this->_length = $options['length']; } else { $this->_length = 4; } if (isset($options['phrase']) && !empty($options['phrase'])) { $this->_phrase = (string)(int)$options['phrase']; } else { $this->_createPhrase(); } if (isset($options['mode']) && !empty($options['mode'])) { $this->_mode = $options['mode']; } else { $this->_mode = 'single'; } if (isset($options['locale']) && !empty($options['locale'])) { $this->_locale = $options['locale']; } else { $this->_locale = 'en_US'; } } /** * Create random CAPTCHA phrase, "Word edition" (numbers only) * * This method creates a random phrase * * @access private */ function _createPhrase() { $this->_phrase = (string)Text_Password::create($this->_length, 'unpronounceable', 'numeric'); } /** * Return CAPTCHA as a string * * This method returns the CAPTCHA as string * * @access public * @return text string */ function getCAPTCHA() { $res = ''; if ($this->_mode == 'single') { for ($i = 0; $i < strlen($this->_phrase); $i++) { $res .= ' '.Numbers_Words::toWords($this->_phrase{$i}, $this->_locale); } } else { $res = Numbers_Words::toWords($this->_phrase, $this->_locale); } return $res; } } Text_CAPTCHA-0.4.3/examples/CAPTCHA_Equation_test.php100666 0 0 1070 11531456744 15127 init(); if (PEAR::isError($ret)) { echo $ret->getMessage(); exit(1); } echo 'Equation: ' . $c->getCAPTCHA() . "
"; echo 'Solution: ' . $c->getPhrase() . "
"; $options = array( 'min' => 1, 'max' => 4, 'numbersToText' => true, 'severity' => 2 ); $ret = $c->init($options); if (PEAR::isError($ret)) { echo $ret->getMessage(); exit(1); } echo 'Equation: ' . $c->getCAPTCHA() . "
"; echo 'Solution: ' . $c->getPhrase() . "
"; ?>Text_CAPTCHA-0.4.3/examples/CAPTCHA_Figlet_test.php100666 0 0 2237 11531456744 14562 glob('*.flf'), 'style' => array('border' => '1px dashed red', 'color' => 'yellow', 'background' => 'black'), ); // Set CAPTCHA options // There is no way to set the 'height' property // output options are 'javascript', 'html' or 'text' default is html // default length is 6 $options = array( 'width' => 200, 'output' => 'javascript', 'length' => 8, 'options' => $textOptions ); // Generate a new Text_CAPTCHA object, Image driver $c = Text_CAPTCHA::factory('Figlet'); $retval = $c->init($options); if (PEAR::isError($retval)) { echo 'Error initializing CAPTCHA!'; exit; } // Get CAPTCHA secret passphrase $phrase = strtoupper($c->getPhrase()); $text = $c->getCAPTCHA(); if (PEAR::isError($text)) { echo $text->getMessage(); echo 'Error generating CAPTCHA!'; exit; } echo "
$text

"; echo "Solution: $phrase"; ?> Text_CAPTCHA-0.4.3/examples/CAPTCHA_Numeral_test.php100666 0 0 265 11531456744 14732 init(); print 'Operation: ' . $c->getCAPTCHA(); print '
Solution: ' . $c->getPhrase(); ?>Text_CAPTCHA-0.4.3/examples/CAPTCHA_test.php100666 0 0 5035 11531456744 13267 0 && strlen($_SESSION['phrase']) > 0 && $_POST['phrase'] == $_SESSION['phrase']) { $msg = 'OK!'; $ok = true; unset($_SESSION['phrase']); } else { $msg = 'Please try again!'; } unlink(sha1(session_id()) . '.png'); } print "

$msg

"; if (!$ok) { require_once 'Text/CAPTCHA.php'; // Set CAPTCHA image options (font must exist!) $imageOptions = array( 'font_size' => 24, 'font_path' => './', 'font_file' => 'COUR.TTF', 'text_color' => '#DDFF99', 'lines_color' => '#CCEEDD', 'background_color' => '#555555', 'antialias' => true ); // Set CAPTCHA options $options = array( 'width' => 200, 'height' => 80, 'output' => 'png', 'imageOptions' => $imageOptions ); // Generate a new Text_CAPTCHA object, Image driver $c = Text_CAPTCHA::factory('Image'); $retval = $c->init($options); if (PEAR::isError($retval)) { printf('Error initializing CAPTCHA: %s!', $retval->getMessage()); exit; } // Get CAPTCHA secret passphrase $_SESSION['phrase'] = $c->getPhrase(); // Get CAPTCHA image (as PNG) $png = $c->getCAPTCHA(); if (PEAR::isError($png)) { printf('Error generating CAPTCHA: %s!', $png->getMessage()); exit; } file_put_contents(sha1(session_id()) . '.png', $png); echo '
' . '' . '' . '
'; } ?>Text_CAPTCHA-0.4.3/examples/CAPTCHA_Word_test.php100666 0 0 263 11531456744 14240 init(array('length' => 4, 'locale' => 'de')); echo $c->getCAPTCHA(); ?> Text_CAPTCHA-0.4.3/examples/CAPTCHA_test_measure.php100666 0 0 5052 11531456744 15007 0 && strlen($_SESSION['phrase']) > 0 && $_POST['phrase'] == $_SESSION['phrase']) { $msg = 'OK!'; $ok = true; unset($_SESSION['phrase']); } else { $msg = 'Please try again!'; } unlink(sha1(session_id()) . '.png'); } print "

$msg

"; if (!$ok) { require_once 'Text/CAPTCHA.php'; // Set CAPTCHA image options (font must exist!) $imageOptions = array( 'font_size' => 24, 'font_path' => './', 'font_file' => 'COUR.TTF', 'text_color' => '#DDFF99', 'lines_color' => '#CCEEDD', 'background_color' => '#555555' ); // Set CAPTCHA options $options = array( 'width' => 200, 'height' => 80, 'output' => 'png', 'imageOptions' => $imageOptions, 'phrase' => 'tooLongForDefaultFontSize' ); // Generate a new Text_CAPTCHA object, Image driver $c = Text_CAPTCHA::factory('Image'); $retval = $c->init($options); if (PEAR::isError($retval)) { printf('Error initializing CAPTCHA: %s!', $retval->getMessage()); exit; } // Get CAPTCHA secret passphrase $_SESSION['phrase'] = $c->getPhrase(); // Get CAPTCHA image (as PNG) $png = $c->getCAPTCHA(); if (PEAR::isError($png)) { printf('Error generating CAPTCHA: %s!', $png->getMessage()); exit; } file_put_contents(sha1(session_id()) . '.png', $png); echo '
' . '' . '' . '
'; } ?>Text_CAPTCHA-0.4.3/examples/CAPTCHA_test_phraseoptions.php100666 0 0 5076 11531456744 16252 0 && strlen($_SESSION['phrase']) > 0 && $_POST['phrase'] == $_SESSION['phrase']) { $msg = 'OK!'; $ok = true; unset($_SESSION['phrase']); } else { $msg = 'Please try again!'; } unlink(sha1(session_id()) . '.png'); } print "

$msg

"; if (!$ok) { require_once 'Text/CAPTCHA.php'; // Set CAPTCHA image options (font must exist!) $imageOptions = array( 'font_size' => 24, 'font_path' => './', 'font_file' => 'COUR.TTF', 'text_color' => '#DDFF99', 'lines_color' => '#CCEEDD', 'background_color' => '#555555' ); // Set CAPTCHA options $options = array( 'width' => 200, 'height' => 80, 'output' => 'png', 'imageOptions' => $imageOptions, 'phraseOptions' => array('unpronounceable', 'alphanumeric') ); // Generate a new Text_CAPTCHA object, Image driver $c = Text_CAPTCHA::factory('Image'); $retval = $c->init($options); if (PEAR::isError($retval)) { printf('Error initializing CAPTCHA: %s!', $retval->getMessage()); exit; } // Get CAPTCHA secret passphrase $_SESSION['phrase'] = $c->getPhrase(); // Get CAPTCHA image (as PNG) $png = $c->getCAPTCHA(); if (PEAR::isError($png)) { printf('Error generating CAPTCHA: %s!', $png->getMessage()); exit; } file_put_contents(sha1(session_id()) . '.png', $png); echo '
' . '' . '' . '
'; } ?>Text_CAPTCHA-0.4.3/CAPTCHA.php100666 0 0 15007 11531456744 10432 * @license BSD License */ /** * * Require PEAR class for error handling. * */ require_once 'PEAR.php'; /** * * Require Text_Password class for generating the phrase. * */ require_once 'Text/Password.php'; /** * Text_CAPTCHA - creates a CAPTCHA for Turing tests * * Class to create a Turing test for websites by * creating an image, ASCII art or something else * with some (obfuscated) characters * * @package Text_CAPTCHA */ /* // This is a simple example script 0 && strlen($_SESSION['phrase']) > 0 && $_POST['phrase'] == $_SESSION['phrase']) { $msg = 'OK!'; $ok = true; unset($_SESSION['phrase']); } else { $msg = 'Please try again!'; } unlink(sha1(session_id()) . '.png'); } print "

$msg

"; if (!$ok) { require_once 'Text/CAPTCHA.php'; // Set CAPTCHA image options (font must exist!) $imageOptions = array( 'font_size' => 24, 'font_path' => './', 'font_file' => 'COUR.TTF', 'text_color' => '#DDFF99', 'lines_color' => '#CCEEDD', 'background_color' => '#555555', 'antialias' => true ); // Set CAPTCHA options $options = array( 'width' => 200, 'height' => 80, 'output' => 'png', 'imageOptions' => $imageOptions ); // Generate a new Text_CAPTCHA object, Image driver $c = Text_CAPTCHA::factory('Image'); $retval = $c->init($options); if (PEAR::isError($retval)) { printf('Error initializing CAPTCHA: %s!', $retval->getMessage()); exit; } // Get CAPTCHA secret passphrase $_SESSION['phrase'] = $c->getPhrase(); // Get CAPTCHA image (as PNG) $png = $c->getCAPTCHA(); if (PEAR::isError($png)) { printf('Error generating CAPTCHA: %s!', $png->getMessage()); exit; } file_put_contents(sha1(session_id()) . '.png', $png); echo '
' . '' . '' . '
'; } ?> */ class Text_CAPTCHA { /** * Version number * * @access private * @var string */ var $_version = '0.4.2'; /** * Phrase * * @access private * @var string */ var $_phrase; /** * Create a new Text_CAPTCHA object * * @param string $driver name of driver class to initialize * * @return mixed a newly created Text_CAPTCHA object, or a PEAR * error object on error * * @see PEAR::isError() */ function &factory($driver) { if ($driver == '') { return PEAR::raiseError('No CAPTCHA type specified ... aborting. You must call ::factory() with one parameter, the CAPTCHA type.', true); } $driver = basename($driver); include_once "Text/CAPTCHA/Driver/$driver.php"; $classname = "Text_CAPTCHA_Driver_$driver"; $obj = new $classname; return $obj; } /** * Create random CAPTCHA phrase * * This method creates a random phrase, 8 characters long * * @param array $options optionally supply advanced options for the phrase creation * * @access private * @return void */ function _createPhrase($options = array()) { $len = 8; if (!is_array($options) || count($options) === 0) { $this->_phrase = Text_Password::create($len); } else { if (count($options) === 1) { $this->_phrase = Text_Password::create($len, $options[0]); } else { $this->_phrase = Text_Password::create($len, $options[0], $options[1]); } } } /** * Return secret CAPTCHA phrase * * This method returns the CAPTCHA phrase * * @access public * @return phrase secret phrase */ function getPhrase() { return $this->_phrase; } /** * Sets secret CAPTCHA phrase * * This method sets the CAPTCHA phrase * (use null for a random phrase) * * @param string $phrase the (new) phrase * * @access public * @return void */ function setPhrase($phrase = null) { if (!empty($phrase)) { $this->_phrase = $phrase; } else { $this->_createPhrase(); } } /** * Place holder for the real init() method * used by extended classes to initialize CAPTCHA * * @access private * @return PEAR_Error */ function init() { return PEAR::raiseError('CAPTCHA type not selected', true); } /** * Place holder for the real _createCAPTCHA() method * used by extended classes to generate CAPTCHA from phrase * * @access private * @return PEAR_Error */ function _createCAPTCHA() { return PEAR::raiseError('CAPTCHA type not selected', true); } /** * Place holder for the real getCAPTCHA() method * used by extended classes to return the generated CAPTCHA * (as an image resource, as an ASCII text, ...) * * @access private * @return PEAR_Error */ function getCAPTCHA() { return PEAR::raiseError('CAPTCHA type not selected', true); } }