This commit is contained in:
Xes
2025-08-14 22:37:50 +02:00
parent fb6d5d5926
commit 3641e93527
9156 changed files with 1813532 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
<?php
/**
* Base class file for all Text_CAPTCHA drivers.
*
* PHP version 5
*
* @category Text
* @package Text_CAPTCHA
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
/**
* Base class file for all Text_CAPTCHA drivers.
*
* @category Text
* @package Text_CAPTCHA
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
abstract class Text_CAPTCHA_Driver_Base implements Text_CAPTCHA_Driver
{
/**
* Captcha
*
* @var object|string
*/
private $_captcha;
/**
* Phrase
*
* @var string
*/
private $_phrase;
/**
* Sets secret CAPTCHA phrase.
* This method sets the CAPTCHA phrase (use null for a random phrase)
*
* @param string $phrase The (new) phrase
*
* @return void
*/
public final function setPhrase($phrase)
{
$this->_phrase = $phrase;
}
/**
* Return secret CAPTCHA phrase
* This method returns the CAPTCHA phrase
*
* @return string secret phrase
*/
public final function getPhrase()
{
return $this->_phrase;
}
/**
* Sets the generated captcha.
*
* @param object|string $captcha the generated captcha
*
* @return void
*/
protected final function setCaptcha($captcha)
{
$this->_captcha = $captcha;
}
/**
* Place holder for the real getCAPTCHA() method
* used by extended classes to return the generated CAPTCHA
* (as an image resource, as an ASCII text, ...)
*
* @return string|object
*/
public final function getCAPTCHA()
{
return $this->_captcha;
}
/**
* Reset the phrase and the CAPTCHA.
*
* @return void
*/
public function resetDriver()
{
$this->setPhrase(null);
$this->setCaptcha(null);
}
}

View File

@@ -0,0 +1,215 @@
<?php
/**
* Equation driver for Text_CAPTCHA.
* Returns simple equations as string, e.g. "9 - 2"
*
* PHP version 5
*
* @category Text
* @package Text_CAPTCHA
* @author Christian Weiske <cweiske@php.net>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
/**
* Equation driver for Text_CAPTCHA.
* Returns simple equations as string, e.g. "9 - 2"
*
* @category Text
* @package Text_CAPTCHA
* @author Christian Weiske <cweiske@php.net>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
class Text_CAPTCHA_Driver_Equation extends Text_CAPTCHA_Driver_Base
{
/**
* 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.
*
* @var array
*/
private $_operators = array(
'%s * %s',
'%s + %s',
'%s - %s',
'min(%s, %s)',
'max(%s, %s)'
);
/**
* Minimal number to use in an equation.
*
* @var int
*/
private $_min = 1;
/**
* Maximum number to use in an equation.
*
* @var int
*/
private $_max = 10;
/**
* Whether numbers shall be converted to text.
*
* @var bool
*/
private $_numbersToText = false;
/**
* This variable holds the locale for Numbers_Words.
*
* @var string
*/
private $_locale = '';
/**
* Complexity of the generated equations.<br>
* 1 - simple ones such as "1 + 10"<br>
* 2 - harder ones such as "(3-2)*(min(5,6))"
*
* @var int
*/
private $_severity = 1;
/**
* Initialize the driver.
*
* @param array $options CAPTCHA options with these keys:<br>
* min minimum numeric value
* max maximum numeric value
* numbersToText boolean for number to text conversion
* locale locale for number to text conversion
* severity number for complexity
*
* @return void
* @throws Text_CAPTCHA_Exception when numbersToText is true, but Number_Words
* package is not available
*/
public function initDriver($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['locale'])) {
$this->_locale = (string)$options['locale'];
} else {
$this->_locale = '';
}
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')) {
throw new Text_CAPTCHA_Exception('Number_Words package required');
}
}
}
/**
* Create random CAPTCHA equation.
* This method creates a random equation.
*
* @return void
* @throws Text_CAPTCHA_Exception when invalid severity is specified
*/
public function createCAPTCHA()
{
switch ($this->_severity) {
case 1:
list($equation, $phrase) = $this->_createSimpleEquation();
break;
case 2:
list($eq1, $sol1) = $this->_createSimpleEquation();
list($eq2, $sol2) = $this->_createSimpleEquation();
$op3 = $this->_operators[mt_rand(0, count($this->_operators) - 1)];
list(, $phrase) = $this->_solveSimpleEquation($sol1, $sol2, $op3);
$equation = sprintf($op3, '(' . $eq1 . ')', '(' . $eq2 . ')');
break;
default:
throw new Text_CAPTCHA_Exception(
'Equation complexity of ' . $this->_severity . ' not supported'
);
}
$this->setCaptcha($equation);
$this->setPhrase($phrase);
}
/**
* Creates a simple equation of type (number operator number).
*
* @return array Array with equation and solution
*/
private function _createSimpleEquation()
{
$one = mt_rand($this->_min, $this->_max);
$two = mt_rand($this->_min, $this->_max);
$operator = $this->_operators[mt_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 used with those two numbers
*
* @return array Array with equation and solution
*/
private function _solveSimpleEquation($one, $two, $operator)
{
$equation = sprintf($operator, $one, $two);
$function = create_function('', 'return ' . $equation . ';');
if ($this->_numbersToText) {
$numberWords = new Numbers_Words();
$equation = sprintf(
$operator,
$numberWords->toWords($one, $this->_locale),
$numberWords->toWords($two, $this->_locale)
);
}
return array($equation, $function());
}
/**
* Creates the captcha. This method is a placeholder, since the equation is
* created in createCAPTCHA()
*
* @return void
* @see createCAPTCHA()
*/
public function createPhrase()
{
$this->setPhrase(null);
}
}

View File

@@ -0,0 +1,231 @@
<?php
/**
* Require Figlet class for rendering the text.
*
* PHP version 5
*
* @category Text
* @package Text_CAPTCHA
* @author Aaron Wormus <wormus@php.net>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
/**
* Text_CAPTCHA_Driver_Figlet - Text_CAPTCHA driver Figlet based CAPTCHAs
*
* @category Text
* @package Text_CAPTCHA
* @author Aaron Wormus <wormus@php.net>
* @author Christian Wenz <wenz@php.net>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
* @todo define an obfuscation algorithm
*/
class Text_CAPTCHA_Driver_Figlet extends Text_CAPTCHA_Driver_Base
{
/**
* Text_Password options.
*
* @var array
*/
private $_textPasswordOptions;
/**
* Width of CAPTCHA
*
* @var int
*/
private $_width;
/**
* Length of CAPTCHA
*
* @var int
*/
private $_length;
/**
* Figlet font
*
* @var string
*/
private $_font;
/**
* Figlet font
*
* @var array
*/
private $_style = array();
/**
* Output Format
*
* @var string
*/
private $_output;
/**
* init function
*
* Initializes the new Text_CAPTCHA_Driver_Figlet object and creates a GD image
*
* @param array $options CAPTCHA options
*
* @return void
* @throws Text_CAPTCHA_Exception when no options are given
*/
public function initDriver($options = array())
{
if (!empty($options['output'])) {
$this->_output = (string)$options['output'];
} else {
$this->_output = 'html';
}
if (isset($options['width']) && $options['width']) {
$this->_width = (int)$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->_textPasswordOptions = $phraseOptions;
} else {
$this->setPhrase($options['phrase']);
}
if (!empty($options['style'])
&& is_array($options['style'])
) {
$this->_style = $options['style'];
}
if (empty($this->_style['padding'])) {
$this->_style['padding'] = '5px';
}
if (!empty($options['font_file'])) {
if (is_array($options['font_file'])) {
$arr = $options['font_file'];
$this->_font = $arr[array_rand($arr)];
} else {
$this->_font = $options['font_file'];
}
}
}
/**
* Create the passphrase.
*
* @return string
*/
public function createPhrase()
{
$options = $this->_textPasswordOptions;
$textPassword = new Text_Password();
if (!is_array($options) || count($options) === 0) {
$this->setPhrase($textPassword->create($this->_length));
} else {
if (count($options) === 1) {
$this->setPhrase($textPassword->create($this->_length, $options[0]));
} else {
$this->setPhrase(
$textPassword->create($this->_length, $options[0], $options[1])
);
}
}
}
/**
* Create CAPTCHA image.
*
* This method creates a CAPTCHA image.
*
* @return void on error
* @throws Text_CAPTCHA_Exception when loading font fails
*/
public function createCAPTCHA()
{
$pear = new PEAR();
$figlet = new Text_Figlet();
if ($pear->isError($figlet->loadFont($this->_font))) {
throw new Text_CAPTCHA_Exception('Error loading Text_Figlet font');
}
$outputString = $figlet->lineEcho($this->getPhrase());
switch ($this->_output) {
case 'text':
$this->setCaptcha($outputString);
break;
case 'html':
$this->setCaptcha($this->_getCAPTCHAAsHTML($outputString));
break;
case 'javascript':
$this->setCaptcha($this->_getCAPTCHAAsJavascript($outputString));
break;
default:
throw new Text_CAPTCHA_Exception('Invalid output option given');
}
}
/**
* Return CAPTCHA as HTML.
*
* This method returns the CAPTCHA as HTML.
*
* @param string $figletOutput output string from Figlet.
*
* @return string HTML Figlet image or PEAR error
*/
private function _getCAPTCHAAsHTML($figletOutput)
{
$charWidth = strpos($figletOutput, "\n");
$data = str_replace("\n", '<br />', $figletOutput);
$textSize = ($this->_width / $charWidth) * 1.4;
$cssOutput = "";
foreach ($this->_style as $key => $value) {
$cssOutput .= "$key: $value;";
}
$htmlOutput = '<div style="font-family: courier;
font-size: ' . $textSize . 'px;
width:' . $this->_width . 'px;
text-align:center;">';
$htmlOutput .= '<div style="' . $cssOutput . 'margin:0px;">
<pre style="padding: 0px; margin: 0px;">' . $data . '</pre></div></div>';
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.
*
* @param string $figletOutput output string from Figlet.
*
* @return string javascript string or PEAR error
*/
private function _getCAPTCHAAsJavascript($figletOutput)
{
$obfusData = rawurlencode($figletOutput);
$javascript = "<script language=\"javascript\">
document.write(unescape(\"$obfusData\" ) );
</script>";
return $javascript;
}
}

View File

@@ -0,0 +1,289 @@
<?php
/**
* Require Image_Text class for generating the text.
*
* PHP version 5
*
* @category Text
* @package Text_CAPTCHA
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
/**
* Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
*
* Class to create a graphical Turing test
*
* @category Text
* @package Text_CAPTCHA
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
* @todo refine the obfuscation algorithm :-)
* @todo consider removing Image_Text dependency
*/
class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA_Driver_Base
{
/**
* Text_Password options.
*
* @var array
*/
private $_textPasswordOptions;
/**
* Width of CAPTCHA
*
* @var int
*/
private $_width;
/**
* Height of CAPTCHA
*
* @var int
*/
private $_height;
/**
* CAPTCHA output format
*
* @var string
*/
private $_output;
/**
* Further options (here: for Image_Text)
*
* @var array
*/
private $_imageOptions = array(
'font_size' => 24,
'font_path' => './',
'font_file' => 'COUR.TTF',
'text_color' => '#000000',
'lines_color' => '#CACACA',
'background_color' => '#555555',
'antialias' => false
);
/**
* Init function
*
* Initializes the new Text_CAPTCHA_Driver_Image object and creates a GD image
*
* @param array $options CAPTCHA options
*
* @return void
*/
public function initDriver($options = array())
{
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->_textPasswordOptions = $phraseOptions;
} else {
$this->setPhrase($options['phrase']);
}
if (!isset($options['output']) || empty($options['output'])) {
$this->_output = 'jpeg';
} 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']
);
}
}
}
/**
* Create CAPTCHA image.
*
* This method creates a CAPTCHA image.
*
* @return void
* @throws Text_CAPTCHA_Exception when image generation with Image_Text produces
* an error
*/
public function createCAPTCHA()
{
$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 {
$imageText = new Image_Text($this->getPhrase(), $options);
$imageText->init();
$result = $imageText->measurize();
} while ($result === false && --$options['font_size'] > 0);
if ($result === false) {
throw new Text_CAPTCHA_Exception(
'The text provided does not fit in the image dimensions'
);
}
$imageText->render();
$image = $imageText->getImg();
if ($this->_imageOptions['antialias'] && function_exists('imageantialias')) {
imageantialias($image, true);
}
$colors = Image_Text::convertString2RGB(
$this->_imageOptions['lines_color']
);
$linesColor = imagecolorallocate(
$image, $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($image, $x1, $y1, $x2, $y2, $linesColor);
$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($image, $x1, $y1, $x2, $y2, $linesColor);
$cx = rand(0, $this->_width - 50) + 25;
$cy = rand(0, $this->_height - 50) + 25;
$w = rand(1, 24);
imagearc($image, $cx, $cy, $w, $w, 0, 360, $linesColor);
}
// @todo remove hardcoded value
$this->_output = 'jpg';
if ($this->_output == 'gif' && imagetypes() & IMG_GIF) {
$this->setCaptcha($this->_getCAPTCHAAsGIF($image));
} else if (($this->_output == 'jpg' && imagetypes() & IMG_JPG)
|| ($this->_output == 'jpeg' && imagetypes() & IMG_JPEG)
) {
$this->setCaptcha($this->_getCAPTCHAAsJPEG($image));
} else if ($this->_output == 'png' && imagetypes() & IMG_PNG) {
$this->setCaptcha($this->_getCAPTCHAAsPNG($image));
} else if ($this->_output == 'resource') {
$this->setCaptcha($image);
} else {
throw new Text_CAPTCHA_Exception(
"Unknown or unsupported output type specified"
);
}
imagedestroy($image);
}
/**
* Return CAPTCHA as PNG.
*
* This method returns the CAPTCHA as PNG.
*
* @param resource $image generated image
*
* @return string image contents
*/
private function _getCAPTCHAAsPNG($image)
{
ob_start();
imagepng($image);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
/**
* Return CAPTCHA as JPEG.
*
* This method returns the CAPTCHA as JPEG.
*
* @param resource $image generated image
*
* @return string image contents
*/
public function _getCAPTCHAAsJPEG($image)
{
ob_start();
imagejpeg($image);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
/**
* Return CAPTCHA as GIF.
*
* This method returns the CAPTCHA as GIF.
*
* @param resource $image generated image
*
* @return string image contents
*/
private function _getCAPTCHAAsGIF($image)
{
ob_start();
imagegif($image);
$data = ob_get_contents();
ob_end_clean();
return $data;
}
/**
* Create random CAPTCHA phrase, Image edition (with size check).
*
* This method creates a random phrase, maximum 8 characters or width / 25,
* whatever is smaller.
*
* @return void
*/
public function createPhrase()
{
$len = intval(min(8, $this->_width / 25));
$options = $this->_textPasswordOptions;
$textPassword = new Text_Password();
if (!is_array($options) || count($options) === 0) {
$this->setPhrase($textPassword->create($len));
} else {
if (count($options) === 1) {
$this->setPhrase($textPassword->create($len, $options[0]));
} else {
$this->setPhrase(
$textPassword->create($len, $options[0], $options[1])
);
}
}
}
}

View File

@@ -0,0 +1,268 @@
<?php
/**
* Class used for numeral captchas
*
* PHP version 5
*
* @category Text
* @package Text_CAPTCHA
* @author David Coallier <davidc@agoraproduction.com>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
/**
* 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 <davidc@agoraproduction.com>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
class Text_CAPTCHA_Driver_Numeral extends Text_CAPTCHA_Driver_Base
{
/**
* This variable holds the minimum range value default set to "1".
*
* @var integer $_minValue The minimum value of the number range.
*/
private $_minValue = 1;
/**
* This variable holds the maximum range value default set to "50".
*
* @var integer $_maxValue The maximum value of the number range.
*/
private $_maxValue = 50;
/**
* The valid operators to use in the numeral captcha. We could use / and * but
* not yet.
*
* @var array $_operators The operations for the captcha.
*/
private $_operators = array('-', '+');
/**
* This variable is basically the operation that we're going to be using in the
* numeral captcha we are about to generate.
*
* @var string $_operator The operation's operator to use.
*/
private $_operator = '';
/**
* This variable holds the first number of the numeral operation we are about to
* generate.
*
* @var integer $_firstNumber The first number of the operation.
*/
private $_firstNumber = 0;
/**
* This variable holds the value of the second variable of the operation we are
* about to generate for the captcha.
*
* @var integer $_secondNumber The second number of the operation.
*/
private $_secondNumber = 0;
/**
* Initialize numeric CAPTCHA.
*
* @param array $options CAPTCHA options with these keys:<br>
* minValue minimum value<br>
* maxValue maximum value
*
* @return void
*/
public function initDriver($options = array())
{
if (isset($options['minValue'])) {
$this->_minValue = (int)$options['minValue'];
} else {
$this->_minValue = 1;
}
if (isset($options['maxValue'])) {
$this->_maxValue = (int)$options['maxValue'];
} else {
$this->_maxValue = 50;
}
if (isset($options['operator'])) {
$this->_operator = $options['operator'];
} else {
$this->_operator = '';
}
if (isset($options['firstValue'])) {
$this->_firstNumber = (int)$options['firstValue'];
} else {
$this->_firstNumber = 0;
}
if (isset($options['secondValue'])) {
$this->_secondNumber = (int)$options['secondValue'];
} else {
$this->_secondNumber = 0;
}
}
/**
* Create the CAPTCHA (the numeral expression).
*
* This function determines a random numeral expression and set the associated
* class properties.
*
* @return void
* @see _generateFirstNumber()
* @see _generateSecondNumber()
* @see _generateOperator()
* @see _generateOperation()
*/
public function createCAPTCHA()
{
if ($this->_firstNumber == 0) {
$this->_firstNumber = $this->_generateNumber();
}
if ($this->_secondNumber == 0) {
$this->_secondNumber = $this->_generateNumber();
}
if (empty($this->_operator)) {
$this->_operator = $this->_operators[array_rand($this->_operators)];
}
$this->_generateOperation();
}
/**
* Set operation.
*
* This variable sets the operation variable by taking the firstNumber,
* secondNumber and operator.
*
* @return void
* @see _operation
* @see _firstNumber
* @see _operator
* @see _secondNumber
*/
private function _setOperation()
{
$this->setCaptcha(
$this->_firstNumber . ' ' . $this->_operator . ' ' . $this->_secondNumber
);
}
/**
* 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().
*
* @return integer Random value between _minValue and _maxValue
* @see _minValue
* @see _maxValue
*/
private function _generateNumber()
{
return mt_rand($this->_minValue, $this->_maxValue);
}
/**
* Adds values.
*
* This function will add the firstNumber and the secondNumber value and then
* call setAnswer to set the answer value.
*
* @return void
* @see _firstNumber
* @see _secondNumber
* @see _setAnswer()
*/
private function _doAdd()
{
$phrase = $this->_firstNumber + $this->_secondNumber;
$this->setPhrase($phrase);
}
/**
* Does a subtract on the values.
*
* This function executes a subtraction 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.
*
* @return void
* @see _firstNumber
* @see _secondNumber
* @see _setOperation()
* @see Text_CAPTCHA::setPhrase()
*/
private function _doSubtract()
{
$first = $this->_firstNumber;
$second = $this->_secondNumber;
/**
* Check if firstNumber is smaller than secondNumber
*/
if ($first < $second) {
$this->_firstNumber = $second;
$this->_secondNumber = $first;
$this->_setOperation();
}
$phrase = $this->_firstNumber - $this->_secondNumber;
$this->setPhrase($phrase);
}
/**
* 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.
*
* @return void
* @see _setOperation()
* @see _operator
* @see _doAdd()
* @see _doSubtract()
*/
private function _generateOperation()
{
$this->_setOperation();
switch ($this->_operator) {
case '+':
$this->_doAdd();
break;
case '-':
$this->_doSubtract();
break;
default:
$this->_operator = "+";
$this->_setOperation();
$this->_doAdd();
break;
}
}
/**
* Create random CAPTCHA phrase. This method is a placeholder, since the equation
* is created in createCAPTCHA()
*
* @return string
*/
public function createPhrase()
{
$this->setCaptcha(null);
}
}

View File

@@ -0,0 +1,126 @@
<?php
/**
* Text_CAPTCHA_Driver_Word - Text_CAPTCHA driver word CAPTCHAs
* Class to create a textual Turing test
*
* PHP version 5
*
* @category Text
* @package Text_CAPTCHA
* @author Tobias Schlitt <schlitt@php.net>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
/**
* Require Numbers_Words class for generating the text.
*
* @category Text
* @package Text_CAPTCHA
* @author Tobias Schlitt <schlitt@php.net>
* @author Christian Wenz <wenz@php.net>
* @author Michael Cramer <michael@bigmichi1.de>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Text_CAPTCHA
*/
class Text_CAPTCHA_Driver_Word extends Text_CAPTCHA_Driver_Base
{
/**
* Phrase length.
* This variable holds the length of the Word.
*
* @var integer
*/
private $_length;
/**
* Numbers_Words mode.
* This variable holds the mode for Numbers_Words.
*
* @var String
*/
private $_mode;
/**
* Locale
* This variable holds the locale for Numbers_Words
*
* @var string
*/
private $_locale;
/**
* Initializes the new Text_CAPTCHA_Driver_Word object.
*
* @param array $options CAPTCHA options with these keys:<br>
* phrase The "secret word" of the CAPTCHA<br>
* length The number of characters in the phrase<br>
* locale The locale for Numbers_Words<br>
* mode The mode for Numbers_Words
*
* @return void
*/
public function initDriver($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->setPhrase((string)$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
*
* @return void
*/
public function createPhrase()
{
$phrase = new Text_Password();
$this->setPhrase(
$phrase->create(
$this->_length, 'unpronounceable', 'numeric'
)
);
}
/**
* Place holder for the real _createCAPTCHA() method
* used by extended classes to generate CAPTCHA from phrase
*
* @return void
*/
public function createCAPTCHA()
{
$res = '';
$numberWords = new Numbers_Words();
$phrase = $this->getPhrase();
if ($this->_mode == 'single') {
$phraseArr = str_split($phrase);
for ($i = 0; $i < strlen($phrase); $i++) {
$res .= ' ' . $numberWords->toWords($phraseArr[$i], $this->_locale);
}
} else {
$res = $numberWords->toWords($phrase, $this->_locale);
}
$this->setCaptcha($res);
}
}