summaryrefslogtreecommitdiff
path: root/kohana/libraries/drivers/Captcha/Basic.php
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-15 08:56:18 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-15 08:56:18 +0000
commit3b35e8b91ce94c292b46a296d034542ac5f0f6da (patch)
tree7a1444b5e16475b66300be105b8fc39e5117c881 /kohana/libraries/drivers/Captcha/Basic.php
parent628058b4ed5aefb543ceb6ca9d3b87828c66bef1 (diff)
Refresh kohana from upstream svn trunk r3771.
During this process, remove a considerable number of files from kohana that we will not be needing in Gallery3, including the following files and directories: kohana/application kohana/example.htaccess kohana/index.php kohana/install.php kohana/kohana.png kohana/modules/archive kohana/modules/auth kohana/modules/flot kohana/modules/gmaps kohana/modules/kodoc kohana/modules/payment kohana/modules/smarty kohana/modules/unit_test/i18n kohana/modules/unit_test/tests/Example_Test.php kohana/modules/unit_test/tests/Valid_Test.php kohana/system/config/captcha.php kohana/system/controllers/captcha.php kohana/system/fonts kohana/system/i18n kohana/system/libraries/Calendar.php kohana/system/libraries/Calendar_Event.php kohana/system/libraries/Captcha.php kohana/system/libraries/Tagcloud.php kohana/system/vendor kohana/system/views/pagination kohana/system/views/kohana_calendar.php
Diffstat (limited to 'kohana/libraries/drivers/Captcha/Basic.php')
-rw-r--r--kohana/libraries/drivers/Captcha/Basic.php81
1 files changed, 81 insertions, 0 deletions
diff --git a/kohana/libraries/drivers/Captcha/Basic.php b/kohana/libraries/drivers/Captcha/Basic.php
new file mode 100644
index 00000000..01e8fc9a
--- /dev/null
+++ b/kohana/libraries/drivers/Captcha/Basic.php
@@ -0,0 +1,81 @@
+<?php defined('SYSPATH') OR die('No direct access allowed.');
+/**
+ * Captcha driver for "basic" style.
+ *
+ * $Id$
+ *
+ * @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