Modified:
trunk/Tipos/Captcha/Image.php
Log:
Move image generation logic to its own method, for clarity.
Modified: trunk/Tipos/Captcha/Image.php
==============================================================================
--- trunk/Tipos/Captcha/Image.php (original)
+++ trunk/Tipos/Captcha/Image.php Wed Oct 8 03:09:14 2008
@@ -2,8 +2,9 @@
/**
*
* Class to generate CAPTCHA's (Completely Automated Public Turing Test to
Tell
- * Computers and Humans Apart). Based on KCAPTCHA class by Kruglov Sergei.
+ * Computers and Humans Apart).
*
+ * Image creation logic extracted from KCAPTCHA class by Kruglov Sergei:
* http://www.captcha.ru/en/kcaptcha/
*
* @category Tipos
@@ -76,15 +77,85 @@
}
/**
- *
+ *
* Creates a capcha image, adds the hashed solution to the session and
* returns the file name.
- *
+ *
* @return string Captcha file name.
- *
+ *
*/
public function getCaptcha()
{
+ // generate a random file name
+ $file = hash($this->_config['hash_algo'], uniqid(rand(), true));
+
+ // set image save path, including extension
+ $path = $this->_config['save_path'] . $file . '.jpg';
+
+ // generate a solution
+ $solution = $this->_getSolution();
+
+ // create the image using the solution
+ $this->_createImage($path, $solution);
+
+ // set a session flash with the hashed solution and file name
+ $hash = hash($this->_config['hash_algo'], $this->_config['salt']
+ . $solution);
+ $this->_session->setFlash('key', "$hash:$file");
+
+ // return the file name
+ return $file . '.jpg';
+ }
+
+ /**
+ *
+ * Checks if a CAPTCHA solution is valid.
+ *
+ * @return bool True if the solution is valid, false otherwise.
+ *
+ */
+ public function isValid($value)
+ {
+ if ($key = $this->_session->getFlash('key')) {
+ // parse session
+ settype($key, 'string');
+ list($solution, $file) = explode(':', $key . ':');
+
+ // hash the attempted value
+ $hash = hash($this->_config['hash_algo'],
+ $this->_config['salt'] . $value);
+
+ // valid solution?
+ if ($solution == $hash) {
+ // valid: sanitize file name and delete file
+ $file = preg_replace('/[^a-z0-9]/i', '', $file);
+ $path = $this->_config['save_path'] . $file . '.jpg';
+ if (file_exists($path)) {
+ unlink($path);
+ }
+
+ // done
+ return true;
+ }
+ }
+
+ // key not found or invalid solution
+ return false;
+ }
+
+ /**
+ *
+ * Creates and saves a capcha image using a given solution
+ *
+ * @param string $path Full path to save the image.
+ *
+ * @param string $solution Captcha solution.
+ *
+ * @return void.
+ *
+ */
+ protected function _createImage($path, $solution)
+ {
$length = $this->_config['length'];
$alphabet = $this->_config['alphabet'];
$font_path = $this->_config['font_path'];
@@ -99,8 +170,6 @@
$foreground_color = array(mt_rand(0,100), mt_rand(0,100),
mt_rand(0,100));
$background_color = array(mt_rand(200,255), mt_rand(200,255),
mt_rand(200,255));
- $solution = $this->_getSolution();
-
while (true) {
$font_file = $font_path . $fonts[mt_rand(0, count($fonts) -
1)];
$font = imagecreatefrompng($font_file);
@@ -256,10 +325,6 @@
}
}
- $algo = $this->_config['hash_algo'];
- $file = hash($algo, uniqid(rand(), true));
- $path = $this->_config['save_path'] . $file . '.jpg';
-
if (function_exists('imagejpeg')) {
imagejpeg($img2, $path, $jpeg_quality);
} elseif (function_exists('imagegif')) {
@@ -267,48 +332,6 @@
} elseif (function_exists('imagepng')) {
imagepng($img2, $path);
}
-
- // set a session flash with the hashed solution
- $hash = hash($algo, $this->_config['salt'] . $solution);
- $this->_session->setFlash('key', "$hash:$file");
-
- return $file . '.jpg';
- }
-
- /**
- *
- * Checks if a CAPTCHA solution is valid.
- *
- * @return bool True if the solution is valid, false otherwise.
- *
- */
- public function isValid($value)
- {
- if ($key = $this->_session->getFlash('key')) {
- // parse session
- settype($key, 'string');
- list($solution, $file) = explode(':', $key . ':');
-
- // hash the attempted value
- $hash = hash($this->_config['hash_algo'],
- $this->_config['salt'] . $value);
-
- // valid solution?
- if ($solution == $hash) {
- // valid: sanitize file name and delete file
- $file = preg_replace('/[^a-z0-9]/i', '', $file);
- $path = $this->_config['save_path'] . $file . '.jpg';
- if (file_exists($path)) {
- unlink($path);
- }
-
- // done
- return true;
- }
- }
-
- // invalid solution
- return false;
}
/**