| 68 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP
|
|
|
6 |
*
|
|
|
7 |
* This content is released under the MIT License (MIT)
|
|
|
8 |
*
|
| 2414 |
lars |
9 |
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
|
| 68 |
lars |
10 |
*
|
|
|
11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
12 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
13 |
* in the Software without restriction, including without limitation the rights
|
|
|
14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
15 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
16 |
* furnished to do so, subject to the following conditions:
|
|
|
17 |
*
|
|
|
18 |
* The above copyright notice and this permission notice shall be included in
|
|
|
19 |
* all copies or substantial portions of the Software.
|
|
|
20 |
*
|
|
|
21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
27 |
* THE SOFTWARE.
|
|
|
28 |
*
|
|
|
29 |
* @package CodeIgniter
|
|
|
30 |
* @author EllisLab Dev Team
|
|
|
31 |
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
|
| 2414 |
lars |
32 |
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
|
|
|
33 |
* @license https://opensource.org/licenses/MIT MIT License
|
| 68 |
lars |
34 |
* @link https://codeigniter.com
|
|
|
35 |
* @since Version 1.0.0
|
|
|
36 |
* @filesource
|
|
|
37 |
*/
|
|
|
38 |
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* CodeIgniter CAPTCHA Helper
|
|
|
42 |
*
|
|
|
43 |
* @package CodeIgniter
|
|
|
44 |
* @subpackage Helpers
|
|
|
45 |
* @category Helpers
|
|
|
46 |
* @author EllisLab Dev Team
|
|
|
47 |
* @link https://codeigniter.com/user_guide/helpers/captcha_helper.html
|
|
|
48 |
*/
|
|
|
49 |
|
|
|
50 |
// ------------------------------------------------------------------------
|
|
|
51 |
|
|
|
52 |
if ( ! function_exists('create_captcha'))
|
|
|
53 |
{
|
|
|
54 |
/**
|
|
|
55 |
* Create CAPTCHA
|
|
|
56 |
*
|
| 2257 |
lars |
57 |
* @param array $data Data for the CAPTCHA
|
|
|
58 |
* @param string $img_path Path to create the image in (deprecated)
|
|
|
59 |
* @param string $img_url URL to the CAPTCHA image folder (deprecated)
|
|
|
60 |
* @param string $font_path Server path to font (deprecated)
|
| 68 |
lars |
61 |
* @return string
|
|
|
62 |
*/
|
|
|
63 |
function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
|
|
|
64 |
{
|
|
|
65 |
$defaults = array(
|
|
|
66 |
'word' => '',
|
|
|
67 |
'img_path' => '',
|
|
|
68 |
'img_url' => '',
|
|
|
69 |
'img_width' => '150',
|
|
|
70 |
'img_height' => '30',
|
|
|
71 |
'font_path' => '',
|
|
|
72 |
'expiration' => 7200,
|
|
|
73 |
'word_length' => 8,
|
|
|
74 |
'font_size' => 16,
|
|
|
75 |
'img_id' => '',
|
|
|
76 |
'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
|
77 |
'colors' => array(
|
|
|
78 |
'background' => array(255,255,255),
|
|
|
79 |
'border' => array(153,102,102),
|
|
|
80 |
'text' => array(204,153,153),
|
|
|
81 |
'grid' => array(255,182,182)
|
|
|
82 |
)
|
|
|
83 |
);
|
|
|
84 |
|
|
|
85 |
foreach ($defaults as $key => $val)
|
|
|
86 |
{
|
|
|
87 |
if ( ! is_array($data) && empty($$key))
|
|
|
88 |
{
|
|
|
89 |
$$key = $val;
|
|
|
90 |
}
|
|
|
91 |
else
|
|
|
92 |
{
|
|
|
93 |
$$key = isset($data[$key]) ? $data[$key] : $val;
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if ($img_path === '' OR $img_url === ''
|
|
|
98 |
OR ! is_dir($img_path) OR ! is_really_writable($img_path)
|
|
|
99 |
OR ! extension_loaded('gd'))
|
|
|
100 |
{
|
|
|
101 |
return FALSE;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// -----------------------------------
|
|
|
105 |
// Remove old images
|
|
|
106 |
// -----------------------------------
|
|
|
107 |
|
|
|
108 |
$now = microtime(TRUE);
|
|
|
109 |
|
|
|
110 |
$current_dir = @opendir($img_path);
|
|
|
111 |
while ($filename = @readdir($current_dir))
|
|
|
112 |
{
|
| 1257 |
lars |
113 |
if (in_array(substr($filename, -4), array('.jpg', '.png'))
|
|
|
114 |
&& (str_replace(array('.jpg', '.png'), '', $filename) + $expiration) < $now)
|
| 68 |
lars |
115 |
{
|
|
|
116 |
@unlink($img_path.$filename);
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
@closedir($current_dir);
|
|
|
121 |
|
|
|
122 |
// -----------------------------------
|
|
|
123 |
// Do we have a "word" yet?
|
|
|
124 |
// -----------------------------------
|
|
|
125 |
|
|
|
126 |
if (empty($word))
|
|
|
127 |
{
|
|
|
128 |
$word = '';
|
|
|
129 |
$pool_length = strlen($pool);
|
|
|
130 |
$rand_max = $pool_length - 1;
|
|
|
131 |
|
|
|
132 |
// PHP7 or a suitable polyfill
|
|
|
133 |
if (function_exists('random_int'))
|
|
|
134 |
{
|
|
|
135 |
try
|
|
|
136 |
{
|
|
|
137 |
for ($i = 0; $i < $word_length; $i++)
|
|
|
138 |
{
|
|
|
139 |
$word .= $pool[random_int(0, $rand_max)];
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
catch (Exception $e)
|
|
|
143 |
{
|
|
|
144 |
// This means fallback to the next possible
|
|
|
145 |
// alternative to random_int()
|
|
|
146 |
$word = '';
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
if (empty($word))
|
|
|
152 |
{
|
|
|
153 |
// Nobody will have a larger character pool than
|
|
|
154 |
// 256 characters, but let's handle it just in case ...
|
|
|
155 |
//
|
|
|
156 |
// No, I do not care that the fallback to mt_rand() can
|
|
|
157 |
// handle it; if you trigger this, you're very obviously
|
|
|
158 |
// trying to break it. -- Narf
|
|
|
159 |
if ($pool_length > 256)
|
|
|
160 |
{
|
|
|
161 |
return FALSE;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
// We'll try using the operating system's PRNG first,
|
|
|
165 |
// which we can access through CI_Security::get_random_bytes()
|
|
|
166 |
$security = get_instance()->security;
|
|
|
167 |
|
|
|
168 |
// To avoid numerous get_random_bytes() calls, we'll
|
|
|
169 |
// just try fetching as much bytes as we need at once.
|
|
|
170 |
if (($bytes = $security->get_random_bytes($pool_length)) !== FALSE)
|
|
|
171 |
{
|
|
|
172 |
$byte_index = $word_index = 0;
|
|
|
173 |
while ($word_index < $word_length)
|
|
|
174 |
{
|
|
|
175 |
// Do we have more random data to use?
|
|
|
176 |
// It could be exhausted by previous iterations
|
|
|
177 |
// ignoring bytes higher than $rand_max.
|
|
|
178 |
if ($byte_index === $pool_length)
|
|
|
179 |
{
|
|
|
180 |
// No failures should be possible if the
|
|
|
181 |
// first get_random_bytes() call didn't
|
|
|
182 |
// return FALSE, but still ...
|
|
|
183 |
for ($i = 0; $i < 5; $i++)
|
|
|
184 |
{
|
|
|
185 |
if (($bytes = $security->get_random_bytes($pool_length)) === FALSE)
|
|
|
186 |
{
|
|
|
187 |
continue;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
$byte_index = 0;
|
|
|
191 |
break;
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
if ($bytes === FALSE)
|
|
|
195 |
{
|
|
|
196 |
// Sadly, this means fallback to mt_rand()
|
|
|
197 |
$word = '';
|
|
|
198 |
break;
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
list(, $rand_index) = unpack('C', $bytes[$byte_index++]);
|
|
|
203 |
if ($rand_index > $rand_max)
|
|
|
204 |
{
|
|
|
205 |
continue;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
$word .= $pool[$rand_index];
|
|
|
209 |
$word_index++;
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
if (empty($word))
|
|
|
215 |
{
|
|
|
216 |
for ($i = 0; $i < $word_length; $i++)
|
|
|
217 |
{
|
|
|
218 |
$word .= $pool[mt_rand(0, $rand_max)];
|
|
|
219 |
}
|
|
|
220 |
}
|
|
|
221 |
elseif ( ! is_string($word))
|
|
|
222 |
{
|
|
|
223 |
$word = (string) $word;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
// -----------------------------------
|
|
|
227 |
// Determine angle and position
|
|
|
228 |
// -----------------------------------
|
|
|
229 |
$length = strlen($word);
|
|
|
230 |
$angle = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
|
|
|
231 |
$x_axis = mt_rand(6, (360/$length)-16);
|
|
|
232 |
$y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
|
|
|
233 |
|
|
|
234 |
// Create image
|
|
|
235 |
// PHP.net recommends imagecreatetruecolor(), but it isn't always available
|
|
|
236 |
$im = function_exists('imagecreatetruecolor')
|
|
|
237 |
? imagecreatetruecolor($img_width, $img_height)
|
|
|
238 |
: imagecreate($img_width, $img_height);
|
|
|
239 |
|
|
|
240 |
// -----------------------------------
|
|
|
241 |
// Assign colors
|
|
|
242 |
// ----------------------------------
|
|
|
243 |
|
|
|
244 |
is_array($colors) OR $colors = $defaults['colors'];
|
|
|
245 |
|
|
|
246 |
foreach (array_keys($defaults['colors']) as $key)
|
|
|
247 |
{
|
|
|
248 |
// Check for a possible missing value
|
|
|
249 |
is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key];
|
|
|
250 |
$colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
// Create the rectangle
|
|
|
254 |
ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);
|
|
|
255 |
|
|
|
256 |
// -----------------------------------
|
|
|
257 |
// Create the spiral pattern
|
|
|
258 |
// -----------------------------------
|
|
|
259 |
$theta = 1;
|
|
|
260 |
$thetac = 7;
|
|
|
261 |
$radius = 16;
|
|
|
262 |
$circles = 20;
|
|
|
263 |
$points = 32;
|
|
|
264 |
|
|
|
265 |
for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
|
|
|
266 |
{
|
|
|
267 |
$theta += $thetac;
|
|
|
268 |
$rad = $radius * ($i / $points);
|
|
|
269 |
$x = ($rad * cos($theta)) + $x_axis;
|
|
|
270 |
$y = ($rad * sin($theta)) + $y_axis;
|
|
|
271 |
$theta += $thetac;
|
|
|
272 |
$rad1 = $radius * (($i + 1) / $points);
|
|
|
273 |
$x1 = ($rad1 * cos($theta)) + $x_axis;
|
|
|
274 |
$y1 = ($rad1 * sin($theta)) + $y_axis;
|
|
|
275 |
imageline($im, $x, $y, $x1, $y1, $colors['grid']);
|
|
|
276 |
$theta -= $thetac;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
// -----------------------------------
|
|
|
280 |
// Write the text
|
|
|
281 |
// -----------------------------------
|
|
|
282 |
|
|
|
283 |
$use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext'));
|
|
|
284 |
if ($use_font === FALSE)
|
|
|
285 |
{
|
|
|
286 |
($font_size > 5) && $font_size = 5;
|
|
|
287 |
$x = mt_rand(0, $img_width / ($length / 3));
|
|
|
288 |
$y = 0;
|
|
|
289 |
}
|
|
|
290 |
else
|
|
|
291 |
{
|
|
|
292 |
($font_size > 30) && $font_size = 30;
|
|
|
293 |
$x = mt_rand(0, $img_width / ($length / 1.5));
|
|
|
294 |
$y = $font_size + 2;
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
for ($i = 0; $i < $length; $i++)
|
|
|
298 |
{
|
|
|
299 |
if ($use_font === FALSE)
|
|
|
300 |
{
|
|
|
301 |
$y = mt_rand(0 , $img_height / 2);
|
|
|
302 |
imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']);
|
|
|
303 |
$x += ($font_size * 2);
|
|
|
304 |
}
|
|
|
305 |
else
|
|
|
306 |
{
|
|
|
307 |
$y = mt_rand($img_height / 2, $img_height - 3);
|
|
|
308 |
imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
|
|
|
309 |
$x += $font_size;
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
// Create the border
|
|
|
314 |
imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);
|
|
|
315 |
|
|
|
316 |
// -----------------------------------
|
|
|
317 |
// Generate the image
|
|
|
318 |
// -----------------------------------
|
|
|
319 |
$img_url = rtrim($img_url, '/').'/';
|
|
|
320 |
|
|
|
321 |
if (function_exists('imagejpeg'))
|
|
|
322 |
{
|
|
|
323 |
$img_filename = $now.'.jpg';
|
|
|
324 |
imagejpeg($im, $img_path.$img_filename);
|
|
|
325 |
}
|
|
|
326 |
elseif (function_exists('imagepng'))
|
|
|
327 |
{
|
|
|
328 |
$img_filename = $now.'.png';
|
|
|
329 |
imagepng($im, $img_path.$img_filename);
|
|
|
330 |
}
|
|
|
331 |
else
|
|
|
332 |
{
|
|
|
333 |
return FALSE;
|
|
|
334 |
}
|
|
|
335 |
|
| 2414 |
lars |
336 |
$img = '<img '.($img_id === '' ? '' : 'id="'.$img_id.'"').' src="'.$img_url.$img_filename.'" style="width: '.$img_width.'px; height: '.$img_height .'px; border: 0;" alt=" " />';
|
| 68 |
lars |
337 |
ImageDestroy($im);
|
|
|
338 |
|
|
|
339 |
return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);
|
|
|
340 |
}
|
|
|
341 |
}
|