Initial Kohana install
[speedfreak] / Server / system / libraries / drivers / Captcha / Basic.php
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3  * Captcha driver for "basic" style.
4  *
5  * $Id: Basic.php 3769 2008-12-15 00:48:56Z zombor $
6  *
7  * @package    Captcha
8  * @author     Kohana Team
9  * @copyright  (c) 2007-2008 Kohana Team
10  * @license    http://kohanaphp.com/license.html
11  */
12 class Captcha_Basic_Driver extends Captcha_Driver {
13
14         /**
15          * Generates a new Captcha challenge.
16          *
17          * @return  string  the challenge answer
18          */
19         public function generate_challenge()
20         {
21                 // Complexity setting is used as character count
22                 return text::random('distinct', max(1, Captcha::$config['complexity']));
23         }
24
25         /**
26          * Outputs the Captcha image.
27          *
28          * @param   boolean  html output
29          * @return  mixed
30          */
31         public function render($html)
32         {
33                 // Creates $this->image
34                 $this->image_create(Captcha::$config['background']);
35
36                 // Add a random gradient
37                 if (empty(Captcha::$config['background']))
38                 {
39                         $color1 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
40                         $color2 = imagecolorallocate($this->image, mt_rand(200, 255), mt_rand(200, 255), mt_rand(150, 255));
41                         $this->image_gradient($color1, $color2);
42                 }
43
44                 // Add a few random lines
45                 for ($i = 0, $count = mt_rand(5, Captcha::$config['complexity'] * 4); $i < $count; $i++)
46                 {
47                         $color = imagecolorallocatealpha($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(100, 255), mt_rand(50, 120));
48                         imageline($this->image, mt_rand(0, Captcha::$config['width']), 0, mt_rand(0, Captcha::$config['width']), Captcha::$config['height'], $color);
49                 }
50
51                 // Calculate character font-size and spacing
52                 $default_size = min(Captcha::$config['width'], Captcha::$config['height'] * 2) / (strlen($this->response) + 1);
53                 $spacing = (int) (Captcha::$config['width'] * 0.9 / strlen($this->response));
54
55                 // Draw each Captcha character with varying attributes
56                 for ($i = 0, $strlen = strlen($this->response); $i < $strlen; $i++)
57                 {
58                         // Use different fonts if available
59                         $font = Captcha::$config['fontpath'].Captcha::$config['fonts'][array_rand(Captcha::$config['fonts'])];
60
61                         // Allocate random color, size and rotation attributes to text
62                         $color = imagecolorallocate($this->image, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
63                         $angle = mt_rand(-40, 20);
64
65                         // Scale the character size on image height
66                         $size = $default_size / 10 * mt_rand(8, 12);
67                         $box = imageftbbox($size, $angle, $font, $this->response[$i]);
68
69                         // Calculate character starting coordinates
70                         $x = $spacing / 4 + $i * $spacing;
71                         $y = Captcha::$config['height'] / 2 + ($box[2] - $box[5]) / 4;
72
73                         // Write text character to image
74                         imagefttext($this->image, $size, $angle, $x, $y, $color, $font, $this->response[$i]);
75                 }
76
77                 // Output
78                 return $this->image_render($html);
79         }
80
81 } // End Captcha Basic Driver Class