summaryrefslogtreecommitdiff
path: root/system/libraries/drivers/Captcha
diff options
context:
space:
mode:
Diffstat (limited to 'system/libraries/drivers/Captcha')
-rw-r--r--system/libraries/drivers/Captcha/Alpha.php92
-rw-r--r--system/libraries/drivers/Captcha/Basic.php81
-rw-r--r--system/libraries/drivers/Captcha/Black.php72
-rw-r--r--system/libraries/drivers/Captcha/Math.php61
-rw-r--r--system/libraries/drivers/Captcha/Riddle.php47
-rw-r--r--system/libraries/drivers/Captcha/Word.php37
6 files changed, 390 insertions, 0 deletions
diff --git a/system/libraries/drivers/Captcha/Alpha.php b/system/libraries/drivers/Captcha/Alpha.php
new file mode 100644
index 00000000..b3a9c9d7
--- /dev/null
+++ b/system/libraries/drivers/Captcha/Alpha.php
@@ -0,0 +1,92 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Captcha driver for "alpha" style.
+ *
+ * $Id: Alpha.php 3769 2008-12-15 00:48:56Z zombor $
+ *
+ * @package Captcha
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Captcha_Alpha_Driver extends Captcha_Driver {
+
+ /**
+ * Generates a new Captcha challenge.
+ *
+ * @return string the challenge answer
+ */
+ public function generate_challenge()
+ {
+ // Complexity setting is used as character count
+ return text::random('distinct', max(1, Captcha::$config['complexity']));
+ }
+
+ /**
+ * Outputs the Captcha image.
+ *
+ * @param boolean html output
+ * @return mixed
+ */
+ public function render($html)
+ {
+ // Creates $this->image
+ $this->image_create(Captcha::$config['background']);
+
+ // Add a random gradient
+ if (empty(Captcha::$config['background']))
+ {
+ $color1 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
+ $color2 = imagecolorallocate($this->image, mt_rand(0, 100), mt_rand(0, 100), mt_rand(0, 100));
+ $this->image_gradient($color1, $color2);
+ }
+
+ // Add a few random circles
+ for ($i = 0, $count = mt_rand(10, Captcha::$config['complexity'] * 3); $i < $count; $i++)
+ {
+ $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255), mt_rand(80, 120));
+ $size = mt_rand(5, Captcha::$config['height'] / 3);
+ imagefilledellipse($this->image, mt_rand(0, Captcha::$config['width']), mt_rand(0, Captcha::$config['height']), $size, $size, $color);
+ }
+
+ // Calculate character font-size and spacing
+ $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / strlen($this->response);
+ $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
+
+ // Background alphabetic character attributes
+ $color_limit = mt_rand(96, 160);
+ $chars = 'ABEFGJKLPQRTVY';
+
+ // Draw each Captcha character with varying attributes
+ for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++)
+ {
+ // Use different fonts if available
+ $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
+
+ $angle = mt_rand(-40, 20);
+ // Scale the character size on image height
+ $size = $default_size / 10 * mt_rand(8, 12);
+ $box = imageftbbox($size, $angle, $font, $this->response[$i]);
+
+ // Calculate character starting coordinates
+ $x = $spacing / 4 + $i * $spacing;
+ $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
+
+ // Draw captcha text character
+ // Allocate random color, size and rotation attributes to text
+ $color = imagecolorallocate($this->image, mt_rand(150, 255), mt_rand(200, 255), mt_rand(0, 255));
+
+ // Write text character to image
+ imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
+
+ // Draw "ghost" alphabetic character
+ $text_color = imagecolorallocatealpha($this->image, mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand($color_limit + 8, 255), mt_rand(70, 120));
+ $char = substr($chars, mt_rand(0, 14), 1);
+ imagettftext($this->image, $size * 2, mt_rand(-45, 45), ($x - (mt_rand(5, 10))), ($y + (mt_rand(5, 10))), $text_color, $font, $char);
+ }
+
+ // Output
+ return $this->image_render($html);
+ }
+
+} // End Captcha Alpha Driver Class \ No newline at end of file
diff --git a/system/libraries/drivers/Captcha/Basic.php b/system/libraries/drivers/Captcha/Basic.php
new file mode 100644
index 00000000..d212e72c
--- /dev/null
+++ b/system/libraries/drivers/Captcha/Basic.php
@@ -0,0 +1,81 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Captcha driver for "basic" style.
+ *
+ * $Id: Basic.php 3769 2008-12-15 00:48:56Z zombor $
+ *
+ * @package Captcha
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Captcha_Basic_Driver extends Captcha_Driver {
+
+ /**
+ * Generates a new Captcha challenge.
+ *
+ * @return string the challenge answer
+ */
+ public function generate_challenge()
+ {
+ // Complexity setting is used as character count
+ return text::random('distinct', max(1, Captcha::$config['complexity']));
+ }
+
+ /**
+ * Outputs the Captcha image.
+ *
+ * @param boolean html output
+ * @return mixed
+ */
+ public function render($html)
+ {
+ // Creates $this->image
+ $this->image_create(Captcha::$config['background']);
+
+ // Add a random gradient
+ if (empty(Captcha::$config['background']))
+ {
+ $color1 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
+ $color2 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
+ $this->image_gradient($color1, $color2);
+ }
+
+ // Add a few random lines
+ for ($i = 0, $count = mt_rand(5, Captcha::$config['complexity'] * 4); $i < $count; $i++)
+ {
+ $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(100, 255), mt_rand(50, 120));
+ imageline($this->image, mt_rand(0, Captcha::$config['width']), 0, mt_rand(0, Captcha::$config['width']), Captcha::$config['height'], $color);
+ }
+
+ // Calculate character font-size and spacing
+ $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / (strlen($this->response) + 1);
+ $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
+
+ // Draw each Captcha character with varying attributes
+ for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++)
+ {
+ // Use different fonts if available
+ $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
+
+ // Allocate random color, size and rotation attributes to text
+ $color = imagecolorallocate($this->image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
+ $angle = mt_rand(-40, 20);
+
+ // Scale the character size on image height
+ $size = $default_size / 10 * mt_rand(8, 12);
+ $box = imageftbbox($size, $angle, $font, $this->response[$i]);
+
+ // Calculate character starting coordinates
+ $x = $spacing / 4 + $i * $spacing;
+ $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
+
+ // Write text character to image
+ imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
+ }
+
+ // Output
+ return $this->image_render($html);
+ }
+
+} // End Captcha Basic Driver Class \ No newline at end of file
diff --git a/system/libraries/drivers/Captcha/Black.php b/system/libraries/drivers/Captcha/Black.php
new file mode 100644
index 00000000..6a2e2226
--- /dev/null
+++ b/system/libraries/drivers/Captcha/Black.php
@@ -0,0 +1,72 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Captcha driver for "black" style.
+ *
+ * $Id: Black.php 3769 2008-12-15 00:48:56Z zombor $
+ *
+ * @package Captcha
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Captcha_Black_Driver extends Captcha_Driver {
+
+ /**
+ * Generates a new Captcha challenge.
+ *
+ * @return string the challenge answer
+ */
+ public function generate_challenge()
+ {
+ // Complexity setting is used as character count
+ return text::random('distinct', max(1, ceil(Captcha::$config['complexity'] / 1.5)));
+ }
+
+ /**
+ * Outputs the Captcha image.
+ *
+ * @param boolean html output
+ * @return mixed
+ */
+ public function render($html)
+ {
+ // Creates a black image to start from
+ $this->image_create(Captcha::$config['background']);
+
+ // Add random white/gray arcs, amount depends on complexity setting
+ $count = (Captcha::$config['width'] + Captcha::$config['height']) / 2;
+ $count = $count / 5 * min(10, Captcha::$config['complexity']);
+ for ($i = 0; $i < $count; $i++)
+ {
+ imagesetthickness($this->image, mt_rand(1, 2));
+ $color = imagecolorallocatealpha($this->image, 255, 255, 255, mt_rand(0, 120));
+ imagearc($this->image, mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(-Captcha::$config['width'], Captcha::$config['width']), mt_rand(-Captcha::$config['height'], Captcha::$config['height']), mt_rand(0, 360), mt_rand(0, 360), $color);
+ }
+
+ // Use different fonts if available
+ $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
+
+ // Draw the character's white shadows
+ $size = (int) min(Captcha::$config['height'] / 2, Captcha::$config['width'] * 0.8 / strlen($this->response));
+ $angle = mt_rand(-15 + strlen($this->response), 15 - strlen($this->response));
+ $x = mt_rand(1, Captcha::$config['width'] * 0.9 - $size * strlen($this->response));
+ $y = ((Captcha::$config['height'] - $size) / 2) + $size;
+ $color = imagecolorallocate($this->image, 255, 255, 255);
+ imagefttext($this->image, $size, $angle, $x + 1, $y + 1, $color, $font, $this->response);
+
+ // Add more shadows for lower complexities
+ (Captcha::$config['complexity'] < 10) and imagefttext($this->image, $size, $angle, $x - 1, $y - 1, $color, $font , $this->response);
+ (Captcha::$config['complexity'] < 8) and imagefttext($this->image, $size, $angle, $x - 2, $y + 2, $color, $font , $this->response);
+ (Captcha::$config['complexity'] < 6) and imagefttext($this->image, $size, $angle, $x + 2, $y - 2, $color, $font , $this->response);
+ (Captcha::$config['complexity'] < 4) and imagefttext($this->image, $size, $angle, $x + 3, $y + 3, $color, $font , $this->response);
+ (Captcha::$config['complexity'] < 2) and imagefttext($this->image, $size, $angle, $x - 3, $y - 3, $color, $font , $this->response);
+
+ // Finally draw the foreground characters
+ $color = imagecolorallocate($this->image, 0, 0, 0);
+ imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response);
+
+ // Output
+ return $this->image_render($html);
+ }
+
+} // End Captcha Black Driver Class \ No newline at end of file
diff --git a/system/libraries/drivers/Captcha/Math.php b/system/libraries/drivers/Captcha/Math.php
new file mode 100644
index 00000000..4ac20248
--- /dev/null
+++ b/system/libraries/drivers/Captcha/Math.php
@@ -0,0 +1,61 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Captcha driver for "math" style.
+ *
+ * $Id: Math.php 3769 2008-12-15 00:48:56Z zombor $
+ *
+ * @package Captcha
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Captcha_Math_Driver extends Captcha_Driver {
+
+ private $math_exercice;
+
+ /**
+ * Generates a new Captcha challenge.
+ *
+ * @return string the challenge answer
+ */
+ public function generate_challenge()
+ {
+ // Easy
+ if (Captcha::$config['complexity'] < 4)
+ {
+ $numbers[] = mt_rand(1, 5);
+ $numbers[] = mt_rand(1, 4);
+ }
+ // Normal
+ elseif (Captcha::$config['complexity'] < 7)
+ {
+ $numbers[] = mt_rand(10, 20);
+ $numbers[] = mt_rand(1, 10);
+ }
+ // Difficult, well, not really ;)
+ else
+ {
+ $numbers[] = mt_rand(100, 200);
+ $numbers[] = mt_rand(10, 20);
+ $numbers[] = mt_rand(1, 10);
+ }
+
+ // Store the question for output
+ $this->math_exercice = implode(' + ', $numbers).' = ';
+
+ // Return the answer
+ return array_sum($numbers);
+ }
+
+ /**
+ * Outputs the Captcha riddle.
+ *
+ * @param boolean html output
+ * @return mixed
+ */
+ public function render($html)
+ {
+ return $this->math_exercice;
+ }
+
+} // End Captcha Math Driver Class \ No newline at end of file
diff --git a/system/libraries/drivers/Captcha/Riddle.php b/system/libraries/drivers/Captcha/Riddle.php
new file mode 100644
index 00000000..765eeaad
--- /dev/null
+++ b/system/libraries/drivers/Captcha/Riddle.php
@@ -0,0 +1,47 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Captcha driver for "riddle" style.
+ *
+ * $Id: Riddle.php 3769 2008-12-15 00:48:56Z zombor $
+ *
+ * @package Captcha
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Captcha_Riddle_Driver extends Captcha_Driver {
+
+ private $riddle;
+
+ /**
+ * Generates a new Captcha challenge.
+ *
+ * @return string the challenge answer
+ */
+ public function generate_challenge()
+ {
+ // Load riddles from the current language
+ $riddles = Kohana::lang('captcha.riddles');
+
+ // Pick a random riddle
+ $riddle = $riddles[array_rand($riddles)];
+
+ // Store the question for output
+ $this->riddle = $riddle[0];
+
+ // Return the answer
+ return $riddle[1];
+ }
+
+ /**
+ * Outputs the Captcha riddle.
+ *
+ * @param boolean html output
+ * @return mixed
+ */
+ public function render($html)
+ {
+ return $this->riddle;
+ }
+
+} // End Captcha Riddle Driver Class \ No newline at end of file
diff --git a/system/libraries/drivers/Captcha/Word.php b/system/libraries/drivers/Captcha/Word.php
new file mode 100644
index 00000000..856bd9b4
--- /dev/null
+++ b/system/libraries/drivers/Captcha/Word.php
@@ -0,0 +1,37 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Captcha driver for "word" style.
+ *
+ * $Id: Word.php 3769 2008-12-15 00:48:56Z zombor $
+ *
+ * @package Captcha
+ * @author Kohana Team
+ * @copyright (c) 2007-2008 Kohana Team
+ * @license http://kohanaphp.com/license.html
+ */
+class Captcha_Word_Driver extends Captcha_Basic_Driver {
+
+ /**
+ * Generates a new Captcha challenge.
+ *
+ * @return string the challenge answer
+ */
+ public function generate_challenge()
+ {
+ // Load words from the current language and randomize them
+ $words = Kohana::lang('captcha.words');
+ shuffle($words);
+
+ // Loop over each word...
+ foreach ($words as $word)
+ {
+ // ...until we find one of the desired length
+ if (abs(Captcha::$config['complexity'] - strlen($word)) < 2)
+ return strtoupper($word);
+ }
+
+ // Return any random word as final fallback
+ return strtoupper($words[array_rand($words)]);
+ }
+
+} // End Captcha Word Driver Class \ No newline at end of file