PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/captcha_helper.php

https://gitlab.com/sittipongwork/impro_dashboard
PHP | 336 lines | 200 code | 53 blank | 83 comment | 39 complexity | e9ea588ed90abdddeec9f74c1b7c00dd 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.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter CAPTCHA Helper
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Helpers
  22. * @category Helpers
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/helpers/xml_helper.html
  25. */
  26. // ------------------------------------------------------------------------
  27. /**
  28. * Create CAPTCHA
  29. *
  30. * @access public
  31. * @param array array of data for the CAPTCHA
  32. * @param string path to create the image in
  33. * @param string URL to the CAPTCHA image folder
  34. * @param string server path to font
  35. * @return string
  36. */
  37. if ( ! function_exists('create_captcha'))
  38. {
  39. function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  40. {
  41. $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
  42. foreach ($defaults as $key => $val)
  43. {
  44. if ( ! is_array($data))
  45. {
  46. if ( ! isset($$key) OR $$key == '')
  47. {
  48. $$key = $val;
  49. }
  50. }
  51. else
  52. {
  53. $$key = ( ! isset($data[$key])) ? $val : $data[$key];
  54. }
  55. }
  56. if ($img_path == '' OR $img_url == '')
  57. {
  58. return FALSE;
  59. }
  60. if ( ! @is_dir($img_path))
  61. {
  62. return FALSE;
  63. }
  64. if ( ! is_writable($img_path))
  65. {
  66. return FALSE;
  67. }
  68. if ( ! extension_loaded('gd'))
  69. {
  70. return FALSE;
  71. }
  72. // -----------------------------------
  73. // Remove old images
  74. // -----------------------------------
  75. list($usec, $sec) = explode(" ", microtime());
  76. $now = ((float)$usec + (float)$sec);
  77. $current_dir = @opendir($img_path);
  78. while ($filename = @readdir($current_dir))
  79. {
  80. if ($filename != "." and $filename != ".." and $filename != "index.html")
  81. {
  82. $name = str_replace(".jpg", "", $filename);
  83. if (($name + $expiration) < $now)
  84. {
  85. @unlink($img_path.$filename);
  86. }
  87. }
  88. }
  89. @closedir($current_dir);
  90. // -----------------------------------
  91. // Do we have a "word" yet?
  92. // -----------------------------------
  93. // -----------------------------------
  94. // Do we have a "word" yet?
  95. // -----------------------------------
  96. if (empty($word))
  97. {
  98. $word = '';
  99. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  100. $pool_length = strlen($pool);
  101. $rand_max = $pool_length - 1;
  102. // PHP7 or a suitable polyfill
  103. if (function_exists('random_int'))
  104. {
  105. try
  106. {
  107. for ($i = 0; $i < $word_length; $i++)
  108. {
  109. $word .= $pool[random_int(0, $rand_max)];
  110. }
  111. }
  112. catch (Exception $e)
  113. {
  114. // This means fallback to the next possible
  115. // alternative to random_int()
  116. $word = '';
  117. }
  118. }
  119. }
  120. if (empty($word))
  121. {
  122. // To avoid numerous get_random_bytes() calls, we'll
  123. // just try fetching as much bytes as we need at once.
  124. if (($bytes = _ci_captcha_get_random_bytes($pool_length)) !== FALSE)
  125. {
  126. $byte_index = $word_index = 0;
  127. while ($word_index < $word_length)
  128. {
  129. if (($rand_index = unpack('C', $bytes[$byte_index++])) > $rand_max)
  130. {
  131. // Was this the last byte we have?
  132. // If so, try to fetch more.
  133. if ($byte_index === $pool_length)
  134. {
  135. // No failures should be possible if
  136. // the first get_random_bytes() call
  137. // didn't return FALSE, but still ...
  138. for ($i = 0; $i < 5; $i++)
  139. {
  140. if (($bytes = _ci_captcha_get_random_bytes($pool_length)) === FALSE)
  141. {
  142. continue;
  143. }
  144. $byte_index = 0;
  145. break;
  146. }
  147. if ($bytes === FALSE)
  148. {
  149. // Sadly, this means fallback to mt_rand()
  150. $word = '';
  151. break;
  152. }
  153. }
  154. continue;
  155. }
  156. $word .= $pool[$rand_index];
  157. $word_index++;
  158. }
  159. }
  160. }
  161. if (empty($word))
  162. {
  163. for ($i = 0; $i < $word_length; $i++)
  164. {
  165. $word .= $pool[mt_rand(0, $rand_max)];
  166. }
  167. }
  168. elseif ( ! is_string($word))
  169. {
  170. $word = (string) $word;
  171. }
  172. // -----------------------------------
  173. // Determine angle and position
  174. // -----------------------------------
  175. $length = strlen($word);
  176. $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
  177. $x_axis = rand(6, (360/$length)-16);
  178. $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
  179. // -----------------------------------
  180. // Create image
  181. // -----------------------------------
  182. // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  183. if (function_exists('imagecreatetruecolor'))
  184. {
  185. $im = imagecreatetruecolor($img_width, $img_height);
  186. }
  187. else
  188. {
  189. $im = imagecreate($img_width, $img_height);
  190. }
  191. // -----------------------------------
  192. // Assign colors
  193. // -----------------------------------
  194. $bg_color = imagecolorallocate ($im, 255, 255, 255);
  195. $border_color = imagecolorallocate ($im, 153, 102, 102);
  196. $text_color = imagecolorallocate ($im, 204, 153, 153);
  197. $grid_color = imagecolorallocate($im, 255, 182, 182);
  198. $shadow_color = imagecolorallocate($im, 255, 240, 240);
  199. // -----------------------------------
  200. // Create the rectangle
  201. // -----------------------------------
  202. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
  203. // -----------------------------------
  204. // Create the spiral pattern
  205. // -----------------------------------
  206. $theta = 1;
  207. $thetac = 7;
  208. $radius = 16;
  209. $circles = 20;
  210. $points = 32;
  211. for ($i = 0; $i < ($circles * $points) - 1; $i++)
  212. {
  213. $theta = $theta + $thetac;
  214. $rad = $radius * ($i / $points );
  215. $x = ($rad * cos($theta)) + $x_axis;
  216. $y = ($rad * sin($theta)) + $y_axis;
  217. $theta = $theta + $thetac;
  218. $rad1 = $radius * (($i + 1) / $points);
  219. $x1 = ($rad1 * cos($theta)) + $x_axis;
  220. $y1 = ($rad1 * sin($theta )) + $y_axis;
  221. imageline($im, $x, $y, $x1, $y1, $grid_color);
  222. $theta = $theta - $thetac;
  223. }
  224. // -----------------------------------
  225. // Write the text
  226. // -----------------------------------
  227. $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
  228. if ($use_font == FALSE)
  229. {
  230. $font_size = 5;
  231. $x = rand(0, $img_width/($length/3));
  232. $y = 0;
  233. }
  234. else
  235. {
  236. $font_size = 16;
  237. $x = rand(0, $img_width/($length/1.5));
  238. $y = $font_size+2;
  239. }
  240. for ($i = 0; $i < strlen($word); $i++)
  241. {
  242. if ($use_font == FALSE)
  243. {
  244. $y = rand(0 , $img_height/2);
  245. imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  246. $x += ($font_size*2);
  247. }
  248. else
  249. {
  250. $y = rand($img_height/2, $img_height-3);
  251. imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
  252. $x += $font_size;
  253. }
  254. }
  255. // -----------------------------------
  256. // Create the border
  257. // -----------------------------------
  258. imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
  259. // -----------------------------------
  260. // Generate the image
  261. // -----------------------------------
  262. $img_name = $now.'.jpg';
  263. ImageJPEG($im, $img_path.$img_name);
  264. $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
  265. ImageDestroy($im);
  266. return array('word' => $word, 'time' => $now, 'image' => $img);
  267. }
  268. function _ci_captcha_get_random_bytes($length)
  269. {
  270. if (defined('MCRYPT_DEV_URANDOM'))
  271. {
  272. return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
  273. }
  274. elseif (function_exists('openssl_random_pseudo_bytes'))
  275. {
  276. return openssl_random_pseudo_bytes($length);
  277. }
  278. return FALSE;
  279. }
  280. }
  281. // ------------------------------------------------------------------------
  282. /* End of file captcha_helper.php */
  283. /* Location: ./system/heleprs/captcha_helper.php */