PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/manage/codeigniter/system/helpers/captcha_helper.php

https://bitbucket.org/myockey/clearcreek-chapel-website
PHP | 246 lines | 133 code | 45 blank | 68 comment | 27 complexity | 619b88e27ec54d20080db432c4ada3f0 MD5 | raw file
  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.2.4 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2012, 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. * CodeIgniter CAPTCHA Helper
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Helpers
  21. * @category Helpers
  22. * @author EllisLab Dev Team
  23. * @link http://codeigniter.com/user_guide/helpers/xml_helper.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Create CAPTCHA
  28. *
  29. * @access public
  30. * @param array array of data for the CAPTCHA
  31. * @param string path to create the image in
  32. * @param string URL to the CAPTCHA image folder
  33. * @param string server path to font
  34. * @return string
  35. */
  36. if ( ! function_exists('create_captcha'))
  37. {
  38. function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  39. {
  40. $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
  41. foreach ($defaults as $key => $val)
  42. {
  43. if ( ! is_array($data))
  44. {
  45. if ( ! isset($$key) OR $$key == '')
  46. {
  47. $$key = $val;
  48. }
  49. }
  50. else
  51. {
  52. $$key = ( ! isset($data[$key])) ? $val : $data[$key];
  53. }
  54. }
  55. if ($img_path == '' OR $img_url == '')
  56. {
  57. return FALSE;
  58. }
  59. if ( ! @is_dir($img_path))
  60. {
  61. return FALSE;
  62. }
  63. if ( ! is_writable($img_path))
  64. {
  65. return FALSE;
  66. }
  67. if ( ! extension_loaded('gd'))
  68. {
  69. return FALSE;
  70. }
  71. // -----------------------------------
  72. // Remove old images
  73. // -----------------------------------
  74. list($usec, $sec) = explode(" ", microtime());
  75. $now = ((float)$usec + (float)$sec);
  76. $current_dir = @opendir($img_path);
  77. while($filename = @readdir($current_dir))
  78. {
  79. if ($filename != "." and $filename != ".." and $filename != "index.html")
  80. {
  81. $name = str_replace(".jpg", "", $filename);
  82. if (($name + $expiration) < $now)
  83. {
  84. @unlink($img_path.$filename);
  85. }
  86. }
  87. }
  88. @closedir($current_dir);
  89. // -----------------------------------
  90. // Do we have a "word" yet?
  91. // -----------------------------------
  92. if ($word == '')
  93. {
  94. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  95. $str = '';
  96. for ($i = 0; $i < 8; $i++)
  97. {
  98. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  99. }
  100. $word = $str;
  101. }
  102. // -----------------------------------
  103. // Determine angle and position
  104. // -----------------------------------
  105. $length = strlen($word);
  106. $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
  107. $x_axis = rand(6, (360/$length)-16);
  108. $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
  109. // -----------------------------------
  110. // Create image
  111. // -----------------------------------
  112. // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  113. if (function_exists('imagecreatetruecolor'))
  114. {
  115. $im = imagecreatetruecolor($img_width, $img_height);
  116. }
  117. else
  118. {
  119. $im = imagecreate($img_width, $img_height);
  120. }
  121. // -----------------------------------
  122. // Assign colors
  123. // -----------------------------------
  124. $bg_color = imagecolorallocate ($im, 255, 255, 255);
  125. $border_color = imagecolorallocate ($im, 153, 102, 102);
  126. $text_color = imagecolorallocate ($im, 204, 153, 153);
  127. $grid_color = imagecolorallocate($im, 255, 182, 182);
  128. $shadow_color = imagecolorallocate($im, 255, 240, 240);
  129. // -----------------------------------
  130. // Create the rectangle
  131. // -----------------------------------
  132. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
  133. // -----------------------------------
  134. // Create the spiral pattern
  135. // -----------------------------------
  136. $theta = 1;
  137. $thetac = 7;
  138. $radius = 16;
  139. $circles = 20;
  140. $points = 32;
  141. for ($i = 0; $i < ($circles * $points) - 1; $i++)
  142. {
  143. $theta = $theta + $thetac;
  144. $rad = $radius * ($i / $points );
  145. $x = ($rad * cos($theta)) + $x_axis;
  146. $y = ($rad * sin($theta)) + $y_axis;
  147. $theta = $theta + $thetac;
  148. $rad1 = $radius * (($i + 1) / $points);
  149. $x1 = ($rad1 * cos($theta)) + $x_axis;
  150. $y1 = ($rad1 * sin($theta )) + $y_axis;
  151. imageline($im, $x, $y, $x1, $y1, $grid_color);
  152. $theta = $theta - $thetac;
  153. }
  154. // -----------------------------------
  155. // Write the text
  156. // -----------------------------------
  157. $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
  158. if ($use_font == FALSE)
  159. {
  160. $font_size = 5;
  161. $x = rand(0, $img_width/($length/3));
  162. $y = 0;
  163. }
  164. else
  165. {
  166. $font_size = 16;
  167. $x = rand(0, $img_width/($length/1.5));
  168. $y = $font_size+2;
  169. }
  170. for ($i = 0; $i < strlen($word); $i++)
  171. {
  172. if ($use_font == FALSE)
  173. {
  174. $y = rand(0 , $img_height/2);
  175. imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  176. $x += ($font_size*2);
  177. }
  178. else
  179. {
  180. $y = rand($img_height/2, $img_height-3);
  181. imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
  182. $x += $font_size;
  183. }
  184. }
  185. // -----------------------------------
  186. // Create the border
  187. // -----------------------------------
  188. imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
  189. // -----------------------------------
  190. // Generate the image
  191. // -----------------------------------
  192. $img_name = $now.'.jpg';
  193. ImageJPEG($im, $img_path.$img_name);
  194. $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
  195. ImageDestroy($im);
  196. return array('word' => $word, 'time' => $now, 'image' => $img);
  197. }
  198. }
  199. // ------------------------------------------------------------------------
  200. /* End of file captcha_helper.php */
  201. /* Location: ./system/heleprs/captcha_helper.php */