user gets notified interatively or by email in case of a CIS error
[living-lab-site.git] / system / helpers / captcha_helper.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * CodeIgniter CAPTCHA Helper
20  *
21  * @package             CodeIgniter
22  * @subpackage  Helpers
23  * @category    Helpers
24  * @author              ExpressionEngine Dev Team
25  * @link                http://codeigniter.com/user_guide/helpers/xml_helper.html
26  */
27
28 // ------------------------------------------------------------------------
29
30 /**
31  * Create CAPTCHA
32  *
33  * @access      public
34  * @param       array   array of data for the CAPTCHA
35  * @param       string  path to create the image in
36  * @param       string  URL to the CAPTCHA image folder
37  * @param       string  server path to font
38  * @return      string
39  */
40 if ( ! function_exists('create_captcha'))
41 {
42         function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
43         {
44                 $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
45
46                 foreach ($defaults as $key => $val)
47                 {
48                         if ( ! is_array($data))
49                         {
50                                 if ( ! isset($$key) OR $$key == '')
51                                 {
52                                         $$key = $val;
53                                 }
54                         }
55                         else
56                         {
57                                 $$key = ( ! isset($data[$key])) ? $val : $data[$key];
58                         }
59                 }
60
61                 if ($img_path == '' OR $img_url == '')
62                 {
63                         return FALSE;
64                 }
65
66                 if ( ! @is_dir($img_path))
67                 {
68                         return FALSE;
69                 }
70
71                 if ( ! is_writable($img_path))
72                 {
73                         return FALSE;
74                 }
75
76                 if ( ! extension_loaded('gd'))
77                 {
78                         return FALSE;
79                 }
80                 
81                 // -----------------------------------
82                 // Remove old images
83                 // -----------------------------------
84
85                 list($usec, $sec) = explode(" ", microtime());
86                 $now = ((float)$usec + (float)$sec);
87
88                 $current_dir = @opendir($img_path);
89
90                 while ($filename = @readdir($current_dir))
91                 {
92                         if ($filename != "." and $filename != ".." and $filename != "index.html")
93                         {
94                                 $name = str_replace(".jpg", "", $filename);
95
96                                 if (($name + $expiration) < $now)
97                                 {
98                                         @unlink($img_path.$filename);
99                                 }
100                         }
101                 }
102
103                 @closedir($current_dir);
104
105                 // -----------------------------------
106                 // Do we have a "word" yet?
107                 // -----------------------------------
108
109            if ($word == '')
110            {
111                         $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
112
113                         $str = '';
114                         for ($i = 0; $i < 8; $i++)
115                         {
116                                 $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
117                         }
118
119                         $word = $str;
120            }
121
122                 // -----------------------------------
123                 // Determine angle and position
124                 // -----------------------------------
125
126                 $length = strlen($word);
127                 $angle  = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
128                 $x_axis = rand(6, (360/$length)-16);
129                 $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
130
131                 // -----------------------------------
132                 // Create image
133                 // -----------------------------------
134
135                 // PHP.net recommends imagecreatetruecolor(), but it isn't always available
136                 if (function_exists('imagecreatetruecolor'))
137                 {
138                         $im = imagecreatetruecolor($img_width, $img_height);
139                 }
140                 else
141                 {
142                         $im = imagecreate($img_width, $img_height);
143                 }
144
145                 // -----------------------------------
146                 //  Assign colors
147                 // -----------------------------------
148
149                 $bg_color               = imagecolorallocate ($im, 255, 255, 255);
150                 $border_color   = imagecolorallocate ($im, 153, 102, 102);
151                 $text_color             = imagecolorallocate ($im, 204, 153, 153);
152                 $grid_color             = imagecolorallocate($im, 255, 182, 182);
153                 $shadow_color   = imagecolorallocate($im, 255, 240, 240);
154
155                 // -----------------------------------
156                 //  Create the rectangle
157                 // -----------------------------------
158
159                 ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
160
161                 // -----------------------------------
162                 //  Create the spiral pattern
163                 // -----------------------------------
164
165                 $theta          = 1;
166                 $thetac         = 7;
167                 $radius         = 16;
168                 $circles        = 20;
169                 $points         = 32;
170
171                 for ($i = 0; $i < ($circles * $points) - 1; $i++)
172                 {
173                         $theta = $theta + $thetac;
174                         $rad = $radius * ($i / $points );
175                         $x = ($rad * cos($theta)) + $x_axis;
176                         $y = ($rad * sin($theta)) + $y_axis;
177                         $theta = $theta + $thetac;
178                         $rad1 = $radius * (($i + 1) / $points);
179                         $x1 = ($rad1 * cos($theta)) + $x_axis;
180                         $y1 = ($rad1 * sin($theta )) + $y_axis;
181                         imageline($im, $x, $y, $x1, $y1, $grid_color);
182                         $theta = $theta - $thetac;
183                 }
184
185                 // -----------------------------------
186                 //  Write the text
187                 // -----------------------------------
188
189                 $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
190
191                 if ($use_font == FALSE)
192                 {
193                         $font_size = 5;
194                         $x = rand(0, $img_width/($length/3));
195                         $y = 0;
196                 }
197                 else
198                 {
199                         $font_size      = 16;
200                         $x = rand(0, $img_width/($length/1.5));
201                         $y = $font_size+2;
202                 }
203
204                 for ($i = 0; $i < strlen($word); $i++)
205                 {
206                         if ($use_font == FALSE)
207                         {
208                                 $y = rand(0 , $img_height/2);
209                                 imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
210                                 $x += ($font_size*2);
211                         }
212                         else
213                         {
214                                 $y = rand($img_height/2, $img_height-3);
215                                 imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
216                                 $x += $font_size;
217                         }
218                 }
219
220
221                 // -----------------------------------
222                 //  Create the border
223                 // -----------------------------------
224
225                 imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
226
227                 // -----------------------------------
228                 //  Generate the image
229                 // -----------------------------------
230
231                 $img_name = $now.'.jpg';
232
233                 ImageJPEG($im, $img_path.$img_name);
234
235                 $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
236
237                 ImageDestroy($im);
238                 
239                 return array('word' => $word, 'time' => $now, 'image' => $img);
240         }
241 }
242
243 // ------------------------------------------------------------------------
244
245 /* End of file captcha_helper.php */
246 /* Location: ./system/heleprs/captcha_helper.php */