PageRenderTime 62ms CodeModel.GetById 42ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/htdocs/application/helpers/captcha_helper.php

http://github.com/claudehohl/Stikked
PHP | 412 lines | 223 code | 86 blank | 103 comment | 52 complexity | 239a325ec1a9f2111dd67bf5b12a62c7 MD5 | raw file
  1<?php if (!defined('BASEPATH')) {
  2    exit('No direct script access allowed');
  3}
  4
  5/**
  6 * CodeIgniter
  7 *
  8 * An open source application development framework for PHP 5.1.6 or newer
  9 *
 10 * @package        CodeIgniter
 11 * @author        ExpressionEngine Dev Team
 12 * @copyright    Copyright (c) 2008 - 2011, EllisLab, Inc.
 13 * @license        http://codeigniter.com/user_guide/license.html
 14 * @link        http://codeigniter.com
 15 * @since        Version 1.0
 16 * @filesource
 17 */
 18
 19// ------------------------------------------------------------------------
 20
 21/**
 22 * CodeIgniter CAPTCHA Helper
 23 *
 24 * @package        CodeIgniter
 25 * @subpackage    Helpers
 26 * @category    Helpers
 27 * @author        ExpressionEngine Dev Team
 28 * @link        http://codeigniter.com/user_guide/helpers/xml_helper.html
 29 */
 30
 31// ------------------------------------------------------------------------
 32
 33/**
 34 * Create CAPTCHA
 35 *
 36 * @access    public
 37 * @param    array    array of data for the CAPTCHA
 38 * @param    string    path to create the image in
 39 * @param    string    URL to the CAPTCHA image folder
 40 * @param    string    server path to font
 41 * @return    string
 42 */
 43
 44if (!function_exists('create_captcha')) {
 45
 46    function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
 47    {
 48        $defaults = array(
 49            'word' => '',
 50            'img_path' => '',
 51            'img_url' => '',
 52            'img_width' => '150',
 53            'img_height' => '30',
 54            'font_path' => '',
 55            'expiration' => 7200,
 56        );
 57        foreach ($defaults as $key => $val) {
 58
 59            if (!is_array($data)) {
 60
 61                if (!isset($$key) or $$key == '') {
 62                    $$key = $val;
 63                }
 64            } else {
 65                $$key = (!isset($data[$key])) ? $val : $data[$key];
 66            }
 67        }
 68
 69        if ($img_path == '' or $img_url == '') {
 70            return false;
 71        }
 72
 73        if (!@is_dir($img_path)) {
 74            return false;
 75        }
 76
 77        if (!is_writable($img_path)) {
 78            return false;
 79        }
 80
 81        if (!extension_loaded('gd')) {
 82            return false;
 83        }
 84
 85        // -----------------------------------
 86        // Remove old images
 87
 88        // -----------------------------------
 89
 90        list($usec, $sec) = explode(" ", microtime());
 91        $now = ((float) $usec + (float) $sec);
 92        $current_dir = @opendir($img_path);
 93        while ($filename = @readdir($current_dir)) {
 94
 95            if ($filename != "." and $filename != ".." and $filename != "index.html") {
 96                $name = str_replace(".jpg", "", $filename);
 97
 98                if (($name + $expiration) < $now) {
 99                    @unlink($img_path . $filename);
100                }
101            }
102        }
103        @closedir($current_dir);
104
105        // -----------------------------------
106        // Do we have a "word" yet?
107
108        // -----------------------------------
109
110        if ($word == '') {
111            $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
112            $str = '';
113            for ($i = 0; $i < 8; $i++) {
114                $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
115            }
116            $word = $str;
117        }
118
119        // -----------------------------------
120        // Determine angle and position
121
122        // -----------------------------------
123
124        $length = strlen($word);
125        $angle = ($length >= 6) ? rand(-($length - 6), ($length - 6)) : 0;
126        $x_axis = rand(6, (360 / $length) - 16);
127        $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height);
128
129        // -----------------------------------
130        // Create image
131
132        // -----------------------------------
133
134        // PHP.net recommends imagecreatetruecolor(), but it isn't always available
135
136        if (function_exists('imagecreatetruecolor')) {
137            $im = imagecreatetruecolor($img_width, $img_height);
138        } else {
139            $im = imagecreate($img_width, $img_height);
140        }
141
142        // -----------------------------------
143        //  Assign colors
144
145        // -----------------------------------
146
147        $bg_color = imagecolorallocate($im, 255, 255, 255);
148        $border_color = imagecolorallocate($im, 153, 102, 102);
149        $text_color = imagecolorallocate($im, 204, 153, 153);
150        $grid_color = imagecolorallocate($im, 255, 182, 182);
151        $shadow_color = imagecolorallocate($im, 255, 240, 240);
152
153        // -----------------------------------
154        //  Create the rectangle
155
156        // -----------------------------------
157
158        ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
159
160        // -----------------------------------
161        //  Create the spiral pattern
162
163        // -----------------------------------
164
165        $theta = 1;
166        $thetac = 7;
167        $radius = 16;
168        $circles = 20;
169        $points = 32;
170        for ($i = 0; $i < ($circles * $points) - 1; $i++) {
171            $theta = $theta + $thetac;
172            $rad = $radius * ($i / $points);
173            $x = ($rad * cos($theta)) + $x_axis;
174            $y = ($rad * sin($theta)) + $y_axis;
175            $theta = $theta + $thetac;
176            $rad1 = $radius * (($i + 1) / $points);
177            $x1 = ($rad1 * cos($theta)) + $x_axis;
178            $y1 = ($rad1 * sin($theta)) + $y_axis;
179            imageline($im, $x, $y, $x1, $y1, $grid_color);
180            $theta = $theta - $thetac;
181        }
182
183        // -----------------------------------
184        //  Write the text
185
186        // -----------------------------------
187
188        $use_font = ($font_path != '' and file_exists($font_path) and function_exists('imagettftext')) ? true : false;
189
190        if ($use_font == false) {
191            $font_size = 5;
192            $x = rand(0, $img_width / ($length / 3));
193            $y = 0;
194        } else {
195            $font_size = 16;
196            $x = rand(0, $img_width / ($length / 1.5));
197            $y = $font_size + 2;
198        }
199        for ($i = 0; $i < strlen($word); $i++) {
200
201            if ($use_font == false) {
202                $y = rand(0, $img_height / 2);
203                imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
204                $x += ($font_size * 2);
205            } else {
206                $y = rand($img_height / 2, $img_height - 3);
207                imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
208                $x += $font_size;
209            }
210        }
211
212        // -----------------------------------
213        //  Create the border
214
215        // -----------------------------------
216
217        imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color);
218
219        // -----------------------------------
220        //  Generate the image
221
222        // -----------------------------------
223
224        $img_name = $now . '.jpg';
225        ImageJPEG($im, $img_path . $img_name);
226        $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
227        ImageDestroy($im);
228        return array(
229            'word' => $word,
230            'time' => $now,
231            'image' => $img,
232        );
233    }
234}
235/**
236 * Display CAPTCHA
237 *
238 * @access    public
239 * @return    string
240 */
241
242if (!function_exists('display_captcha')) {
243
244    function display_captcha($word = '')
245    {
246        $data = '';
247        list($usec, $sec) = explode(" ", microtime());
248        $now = ((float) $usec + (float) $sec);
249        $defaults = array(
250            'word' => $word,
251            'img_path' => '',
252            'img_url' => '',
253            'img_width' => '180',
254            'img_height' => '40',
255            'font_path' => '',
256            'expiration' => 7200,
257        );
258        foreach ($defaults as $key => $val) {
259
260            if (!is_array($data)) {
261
262                if (!isset($$key) or $$key == '') {
263                    $$key = $val;
264                }
265            } else {
266                $$key = (!isset($data[$key])) ? $val : $data[$key];
267            }
268        }
269
270        if (!extension_loaded('gd')) {
271            return false;
272        }
273
274        // -----------------------------------
275        // Do we have a "word" yet?
276
277        // -----------------------------------
278
279        if ($word == '') {
280            $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
281            $str = '';
282            for ($i = 0; $i < 4; $i++) {
283                $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
284            }
285            $word = $str;
286        }
287
288        // -----------------------------------
289        // Determine angle and position
290
291        // -----------------------------------
292
293        $length = strlen($word);
294        $angle = ($length >= 6) ? mt_rand(-($length - 6), ($length - 6)) : 0;
295        $angle = 10;
296        $x_axis = mt_rand(6, (360 / $length) - 16);
297        $y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
298
299        // -----------------------------------
300        // Create image
301
302        // -----------------------------------
303
304        // PHP.net recommends imagecreatetruecolor(), but it isn't always available
305
306        if (function_exists('imagecreatetruecolor')) {
307            $im = imagecreatetruecolor($img_width, $img_height);
308        } else {
309            $im = imagecreate($img_width, $img_height);
310        }
311
312        // -----------------------------------
313        //  Assign colors
314
315        // -----------------------------------
316
317        function b()
318        {
319            return mt_rand(240, 255);
320        }
321        $bg_color = imagecolorallocate($im, b(), b(), b());
322        $border_color = imagecolorallocate($im, 153, 102, 102);
323        $text_color = imagecolorallocate($im, 204, 153, 153);
324        $grid_color = imagecolorallocate($im, 255, 182, 182);
325        $shadow_color = imagecolorallocate($im, 255, 240, 240);
326
327        // -----------------------------------
328        //  Create the rectangle
329
330        // -----------------------------------
331
332        ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
333
334        // -----------------------------------
335        //  Create the spiral pattern
336
337        // -----------------------------------
338
339        $theta = 1;
340        $thetac = 7;
341        $radius = 16;
342        $circles = 20;
343        $points = 32;
344        for ($i = 0; $i < ($circles * $points) - 1; $i++) {
345            $theta = $theta + $thetac;
346            $rad = $radius * ($i / $points);
347            $x = ($rad * cos($theta)) + $x_axis;
348            $y = ($rad * sin($theta)) + $y_axis;
349            $theta = $theta + $thetac;
350            $rad1 = $radius * (($i + 1) / $points);
351            $x1 = ($rad1 * cos($theta)) + $x_axis;
352            $y1 = ($rad1 * sin($theta)) + $y_axis;
353            imageline($im, $x, $y, $x1, $y1, $grid_color);
354            $theta = $theta - $thetac;
355        }
356
357        // -----------------------------------
358        //  Write the text
359
360        // -----------------------------------
361
362        //get random font
363
364        $fn = explode(',', '4,5,6,8,16,17,19,24,26');
365        $f = mt_rand(0, count($fn) - 1);
366        $font_path = './static/fonts/font' . $fn[$f] . '.ttf';
367        $use_font = ($font_path != '' and file_exists($font_path) and function_exists('imagettftext')) ? true : false;
368
369        if ($use_font == false) {
370            $font_size = 5;
371            $x = mt_rand(0, $img_width / ($length / 3));
372            $y = 0;
373        } else {
374            $font_size = 16;
375            $x = mt_rand(0, $img_width / ($length / 1.5));
376            $y = $font_size + 2;
377        }
378        for ($i = 0; $i < strlen($word); $i++) {
379
380            if ($use_font == false) {
381                $y = mt_rand(0, $img_height / 2);
382                imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
383                $x += ($font_size * 2);
384            } else {
385                $y = mt_rand($img_height / 2, $img_height - 3);
386                imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
387                $x += $font_size;
388            }
389        }
390
391        // -----------------------------------
392        //  Create the border
393
394        // -----------------------------------
395
396        imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color);
397
398        // -----------------------------------
399        //  Generate the image
400
401        // -----------------------------------
402
403        $img_name = $now . '.jpg';
404        echo ImageJPEG($im);
405        ImageDestroy($im);
406    }
407}
408
409// ------------------------------------------------------------------------
410/* End of file captcha_helper.php */
411
412/* Location: ./system/heleprs/captcha_helper.php */