PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/captcha_helper.php

https://github.com/betchi/CodeIgniter
PHP | 229 lines | 131 code | 26 blank | 72 comment | 15 complexity | 922a977f77f89e4f81fbb1df8bbf01c4 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. defined('BASEPATH') OR exit('No direct script access allowed');
  28. /**
  29. * CodeIgniter CAPTCHA Helper
  30. *
  31. * @package CodeIgniter
  32. * @subpackage Helpers
  33. * @category Helpers
  34. * @author EllisLab Dev Team
  35. * @link http://codeigniter.com/user_guide/helpers/captcha_helper.html
  36. */
  37. // ------------------------------------------------------------------------
  38. if ( ! function_exists('create_captcha'))
  39. {
  40. /**
  41. * Create CAPTCHA
  42. *
  43. * @param array $data data for the CAPTCHA
  44. * @param string $img_path path to create the image in
  45. * @param string $img_url URL to the CAPTCHA image folder
  46. * @param string $font_path server path to font
  47. * @return string
  48. */
  49. function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  50. {
  51. $defaults = array(
  52. 'word' => '',
  53. 'img_path' => '',
  54. 'img_url' => '',
  55. 'img_width' => '150',
  56. 'img_height' => '30',
  57. 'font_path' => '',
  58. 'expiration' => 7200,
  59. 'word_length' => 8,
  60. 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  61. 'colors' => array(
  62. 'background' => array(255,255,255),
  63. 'border' => array(153,102,102),
  64. 'text' => array(204,153,153),
  65. 'grid' => array(255,182,182)
  66. )
  67. );
  68. foreach ($defaults as $key => $val)
  69. {
  70. if ( ! is_array($data) && empty($$key))
  71. {
  72. $$key = $val;
  73. }
  74. else
  75. {
  76. $$key = isset($data[$key]) ? $data[$key] : $val;
  77. }
  78. }
  79. if ($img_path === '' OR $img_url === ''
  80. OR ! is_dir($img_path) OR ! is_really_writable($img_path)
  81. OR ! extension_loaded('gd'))
  82. {
  83. return FALSE;
  84. }
  85. // -----------------------------------
  86. // Remove old images
  87. // -----------------------------------
  88. $now = microtime(TRUE);
  89. $current_dir = @opendir($img_path);
  90. while ($filename = @readdir($current_dir))
  91. {
  92. if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now)
  93. {
  94. @unlink($img_path.$filename);
  95. }
  96. }
  97. @closedir($current_dir);
  98. // -----------------------------------
  99. // Do we have a "word" yet?
  100. // -----------------------------------
  101. if (empty($word))
  102. {
  103. $word = '';
  104. for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++)
  105. {
  106. $word .= $pool[mt_rand(0, $mt_rand_max)];
  107. }
  108. }
  109. elseif ( ! is_string($word))
  110. {
  111. $word = (string) $word;
  112. }
  113. // -----------------------------------
  114. // Determine angle and position
  115. // -----------------------------------
  116. $length = strlen($word);
  117. $angle = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
  118. $x_axis = mt_rand(6, (360/$length)-16);
  119. $y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
  120. // Create image
  121. // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  122. $im = function_exists('imagecreatetruecolor')
  123. ? imagecreatetruecolor($img_width, $img_height)
  124. : imagecreate($img_width, $img_height);
  125. // -----------------------------------
  126. // Assign colors
  127. // ----------------------------------
  128. is_array($colors) OR $colors = $defaults['colors'];
  129. foreach (array_keys($defaults['colors']) as $key)
  130. {
  131. // Check for a possible missing value
  132. is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key];
  133. $colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
  134. }
  135. // Create the rectangle
  136. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);
  137. // -----------------------------------
  138. // Create the spiral pattern
  139. // -----------------------------------
  140. $theta = 1;
  141. $thetac = 7;
  142. $radius = 16;
  143. $circles = 20;
  144. $points = 32;
  145. for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
  146. {
  147. $theta += $thetac;
  148. $rad = $radius * ($i / $points);
  149. $x = ($rad * cos($theta)) + $x_axis;
  150. $y = ($rad * sin($theta)) + $y_axis;
  151. $theta += $thetac;
  152. $rad1 = $radius * (($i + 1) / $points);
  153. $x1 = ($rad1 * cos($theta)) + $x_axis;
  154. $y1 = ($rad1 * sin($theta)) + $y_axis;
  155. imageline($im, $x, $y, $x1, $y1, $colors['grid']);
  156. $theta -= $thetac;
  157. }
  158. // -----------------------------------
  159. // Write the text
  160. // -----------------------------------
  161. $use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext'));
  162. if ($use_font === FALSE)
  163. {
  164. $font_size = 5;
  165. $x = mt_rand(0, $img_width / ($length / 3));
  166. $y = 0;
  167. }
  168. else
  169. {
  170. $font_size = 16;
  171. $x = mt_rand(0, $img_width / ($length / 1.5));
  172. $y = $font_size + 2;
  173. }
  174. for ($i = 0; $i < $length; $i++)
  175. {
  176. if ($use_font === FALSE)
  177. {
  178. $y = mt_rand(0 , $img_height / 2);
  179. imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']);
  180. $x += ($font_size * 2);
  181. }
  182. else
  183. {
  184. $y = mt_rand($img_height / 2, $img_height - 3);
  185. imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
  186. $x += $font_size;
  187. }
  188. }
  189. // Create the border
  190. imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);
  191. // -----------------------------------
  192. // Generate the image
  193. // -----------------------------------
  194. $img_url = rtrim($img_url, '/').'/';
  195. $img_filename = $now.'.jpg';
  196. ImageJPEG($im, $img_path.$img_filename);
  197. $img = '<img src="'.$img_url.$img_filename.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';
  198. ImageDestroy($im);
  199. return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);
  200. }
  201. }
  202. /* End of file captcha_helper.php */
  203. /* Location: ./system/helpers/captcha_helper.php */