Skip to content

Commit

Permalink
2016-07-11 AC: Standardizes the module's code (PSR2-based standard). C…
Browse files Browse the repository at this point in the history
…loses #4 - Make the module more compliant to PSR-2 standards.
  • Loading branch information
andrewscaya committed Jul 12, 2016
1 parent dbd52ae commit 056c158
Show file tree
Hide file tree
Showing 10 changed files with 2,870 additions and 2,701 deletions.
54 changes: 28 additions & 26 deletions CAPTCHA.php
Expand Up @@ -4,10 +4,10 @@
* 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
* creating an image, ASCII art or something else
* with some (obfuscated) characters
*
*
*
* @package Text_CAPTCHA
* @license BSD License
* @author Christian Wenz <wenz@php.net>
Expand All @@ -33,13 +33,13 @@
* 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
* creating an image, ASCII art or something else
* with some (obfuscated) characters
*
* @package Text_CAPTCHA
*/
/*

/*
// This is a simple example script
<?php
Expand Down Expand Up @@ -73,16 +73,16 @@ public function file_put_contents($filename, $content) {
$msg = 'Please try again!';
}
unlink(sha1(session_id()) . '.png');
unlink(sha1(session_id()) . '.png');
}
print "<p>$msg</p>";
if (!$ok) {
require_once 'Text/CAPTCHA.php';
// Set CAPTCHA image options (font must exist!)
$imageOptions = array(
'font_size' => 24,
Expand All @@ -109,10 +109,10 @@ public function file_put_contents($filename, $content) {
$retval->getMessage());
exit;
}
// Get CAPTCHA secret passphrase
$_SESSION['phrase'] = $c->getPhrase();
// Get CAPTCHA image (as PNG)
$png = $c->getCAPTCHA();
if (PEAR::isError($png)) {
Expand All @@ -121,16 +121,17 @@ public function file_put_contents($filename, $content) {
exit;
}
file_put_contents(sha1(session_id()) . '.png', $png);
echo '<form method="post">' .
'<img src="' . sha1(session_id()) . '.png?' . time() . '" />' .
echo '<form method="post">' .
'<img src="' . sha1(session_id()) . '.png?' . time() . '" />' .
'<input type="text" name="phrase" />' .
'<input type="submit" /></form>';
}
?>
*/

class Text_CAPTCHA {

class Text_CAPTCHA
{

/**
* Version number
Expand Down Expand Up @@ -208,12 +209,12 @@ public function getPhrase()
/**
* Sets secret CAPTCHA phrase
*
* This method sets the CAPTCHA phrase
* This method sets the CAPTCHA phrase
* (use null for a random phrase)
*
* @access public
* @param string $phrase the (new) phrase
* @void
* @void
*/
public function setPhrase($phrase = null)
{
Expand All @@ -226,12 +227,13 @@ public function setPhrase($phrase = null)

/**
* Place holder for the real init() method
* used by extended classes to initialize CAPTCHA
* used by extended classes to initialize CAPTCHA
*
* @access private
* @return PEAR_Error
*/
public function init() {
public function init()
{
return PEAR::raiseError('CAPTCHA type not selected', true);
}

Expand All @@ -242,21 +244,21 @@ public function init() {
* @access private
* @return PEAR_Error
*/
public function _createCAPTCHA() {
public 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
* used by extended classes to return the generated CAPTCHA
* (as an image resource, as an ASCII text, ...)
*
* @access private
* @return PEAR_Error
*/
public function getCAPTCHA() {
public function getCAPTCHA()
{
return PEAR::raiseError('CAPTCHA type not selected', true);
}

}
?>
75 changes: 39 additions & 36 deletions Image.php
Expand Up @@ -3,7 +3,7 @@

/**
*
* Require Image_Text class for generating the text.
* Require ImageText class for generating the text.
*
*/
require_once 'CAPTCHA.php';
Expand All @@ -12,13 +12,13 @@
/**
* Text_CAPTCHA_Driver_Image - Text_CAPTCHA driver graphical CAPTCHAs
*
* Class to create a graphical Turing test
* Class to create a graphical Turing test
*
*
*
* @license BSD License
* @author Christian Wenz <wenz@php.net>
* @todo refine the obfuscation algorithm :-)
* @todo consider removing Image_Text dependency
* @todo consider removing ImageText dependency
*/

class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
Expand All @@ -33,7 +33,7 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
public $_im;

/**
* Image_Text object
* ImageText object
*
* @access private
* @public resource
Expand All @@ -59,7 +59,7 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
/**
* Phrase length of CAPTCHA
*
* @access public static
* @access public static
* @public int
*/
public static $_phraseLength;
Expand All @@ -73,7 +73,7 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
public $_output;

/**
* Further options (here: for Image_Text)
* Further options (here: for ImageText)
*
* @access private
* @public array
Expand All @@ -85,7 +85,7 @@ class Text_CAPTCHA_Driver_Image extends Text_CAPTCHA
'text_color' => '#000000',
'lines_color' => '#CACACA',
'background_color' => '#555555');

/**
* Whether the immage resource has been created
*
Expand Down Expand Up @@ -115,17 +115,17 @@ public function init($options = array())
{
if (!is_array($options)) {
// Compatibility mode ... in future versions, these two
// lines of code will be used:
// lines of code will be used:
// $this->_error = PEAR::raiseError('You need to provide a set of CAPTCHA options!');
// return $this->_error;
// 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];
}
Expand All @@ -134,16 +134,16 @@ public function init($options = array())
}
$options = $o;
}
if (is_array($options)) {
if (is_array($options)) {
if (isset($options['width']) && is_int($options['width'])) {
$this->_width = $options['width'];
$this->_width = $options['width'];
} else {
$this->_width = 200;
$this->_width = 200;
}
if (isset($options['height']) && is_int($options['height'])) {
$this->_height = $options['height'];
$this->_height = $options['height'];
} else {
$this->_height = 80;
$this->_height = 80;
}
if (!isset($options['phrase']) || empty($options['phrase'])) {
$phraseoptions = (isset($options['phraseOptions']) && is_array($options['phraseOptions'])) ? $options['phraseOptions'] : array();
Expand All @@ -155,9 +155,9 @@ public function init($options = array())
$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']);
$this->_imageOptions = array_merge($this->_imageOptions, $options['imageOptions']);
}
return true;
}
Expand All @@ -172,11 +172,11 @@ public function init($options = array())
*/
public function _createPhrase($options = array())
{
if (isset(self::$_phraseLength)) {
$len = intval(min(self::$_phraseLength, $this->_width / 25));
} else {
$len = intval(min(8, $this->_width / 25));
}
if (isset(self::$_phraseLength)) {
$len = intval(min(self::$_phraseLength, $this->_width / 25));
} else {
$len = intval(min(8, $this->_width / 25));
}
if (!is_array($options) || count($options) === 0) {
$this->_phrase = Text_Password::create($len);
} else {
Expand Down Expand Up @@ -208,11 +208,11 @@ public function _createCAPTCHA()
$options['canvas'] = array(
'width' => $this->_width,
'height' => $this->_height
);
);
$options['width'] = $this->_width - 20;
$options['height'] = $this->_height - 20;
$options['height'] = $this->_height - 20;
$options['cx'] = ceil(($this->_width) / 2 + 10);
$options['cy'] = ceil(($this->_height) / 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'];
Expand All @@ -222,26 +222,29 @@ public function _createCAPTCHA()
$options['max_lines'] = 1;
$options['mode'] = 'auto';
do {
$this->_imt = new Image_Text(
$this->_imt = new ImageText(
$this->_phrase,
$options
);
if (PEAR::isError($e = $this->_imt->init())) {
$this->_error = PEAR::raiseError(
sprintf('Error initializing Image_Text (%s)',
$e->getMessage()));
sprintf(
'Error initializing ImageText (%s)',
$e->getMessage()
)
);
return $this->_error;
} else {
$this->_created = true;
$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();
$this->_imt->render();
$this->_im =& $this->_imt->getImg();
$colors = $this->_imt->_convertString2RGB($this->_imageOptions['lines_color']);
$lines_color = imagecolorallocate($this->_im, $colors['r'], $colors['g'], $colors['b']);
//some obfuscation
Expand Down Expand Up @@ -277,7 +280,7 @@ public function getCAPTCHA()
if (PEAR::isError($retval)) {
return PEAR::raiseError($retval->getMessage());
}

if ($this->_output == 'gif' && !function_exists('imagegif')) {
$this->_output = 'png';
}
Expand All @@ -286,7 +289,7 @@ public function getCAPTCHA()
case 'png':
return $this->getCAPTCHAAsPNG();
break;
case 'jpg':
case 'jpg':
case 'jpeg':
return $this->getCAPTCHAAsJPEG();
break;
Expand Down Expand Up @@ -386,5 +389,5 @@ public function getCAPTCHAAsGIF()
public function __wakeup()
{
$this->_created = false;
}
}
}

0 comments on commit 056c158

Please sign in to comment.