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

/system/helpers/captcha_helper.php

https://gitlab.com/nadeermalangadan/CodeIgniter
PHP | 253 lines | 145 code | 27 blank | 81 comment | 18 complexity | fc3b83839ff52c1c6bcb6477408e9e27 MD5 | raw file
  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. *
  9. * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
  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. (http://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link http://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter CAPTCHA Helper
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link http://codeigniter.com/user_guide/helpers/captcha_helper.html
  47. */
  48. // ------------------------------------------------------------------------
  49. if ( ! function_exists('create_captcha'))
  50. {
  51. /**
  52. * Create CAPTCHA
  53. *
  54. * @param array $data data for the CAPTCHA
  55. * @param string $img_path path to create the image in
  56. * @param string $img_url URL to the CAPTCHA image folder
  57. * @param string $font_path server path to font
  58. * @return string
  59. */
  60. function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  61. {
  62. $defaults = array(
  63. 'word' => '',
  64. 'img_path' => '',
  65. 'img_url' => '',
  66. 'img_width' => '150',
  67. 'img_height' => '30',
  68. 'font_path' => '',
  69. 'expiration' => 7200,
  70. 'word_length' => 8,
  71. 'font_size' => 16,
  72. 'img_id' => '',
  73. 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  74. 'colors' => array(
  75. 'background' => array(255,255,255),
  76. 'border' => array(153,102,102),
  77. 'text' => array(204,153,153),
  78. 'grid' => array(255,182,182)
  79. )
  80. );
  81. foreach ($defaults as $key => $val)
  82. {
  83. if ( ! is_array($data) && empty($$key))
  84. {
  85. $$key = $val;
  86. }
  87. else
  88. {
  89. $$key = isset($data[$key]) ? $data[$key] : $val;
  90. }
  91. }
  92. if ($img_path === '' OR $img_url === ''
  93. OR ! is_dir($img_path) OR ! is_really_writable($img_path)
  94. OR ! extension_loaded('gd'))
  95. {
  96. return FALSE;
  97. }
  98. // -----------------------------------
  99. // Remove old images
  100. // -----------------------------------
  101. $now = microtime(TRUE);
  102. $current_dir = @opendir($img_path);
  103. while ($filename = @readdir($current_dir))
  104. {
  105. if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now)
  106. {
  107. @unlink($img_path.$filename);
  108. }
  109. }
  110. @closedir($current_dir);
  111. // -----------------------------------
  112. // Do we have a "word" yet?
  113. // -----------------------------------
  114. if (empty($word))
  115. {
  116. $word = '';
  117. for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++)
  118. {
  119. $word .= $pool[mt_rand(0, $mt_rand_max)];
  120. }
  121. }
  122. elseif ( ! is_string($word))
  123. {
  124. $word = (string) $word;
  125. }
  126. // -----------------------------------
  127. // Determine angle and position
  128. // -----------------------------------
  129. $length = strlen($word);
  130. $angle = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
  131. $x_axis = mt_rand(6, (360/$length)-16);
  132. $y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
  133. // Create image
  134. // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  135. $im = function_exists('imagecreatetruecolor')
  136. ? imagecreatetruecolor($img_width, $img_height)
  137. : imagecreate($img_width, $img_height);
  138. // -----------------------------------
  139. // Assign colors
  140. // ----------------------------------
  141. is_array($colors) OR $colors = $defaults['colors'];
  142. foreach (array_keys($defaults['colors']) as $key)
  143. {
  144. // Check for a possible missing value
  145. is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key];
  146. $colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
  147. }
  148. // Create the rectangle
  149. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);
  150. // -----------------------------------
  151. // Create the spiral pattern
  152. // -----------------------------------
  153. $theta = 1;
  154. $thetac = 7;
  155. $radius = 16;
  156. $circles = 20;
  157. $points = 32;
  158. for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
  159. {
  160. $theta += $thetac;
  161. $rad = $radius * ($i / $points);
  162. $x = ($rad * cos($theta)) + $x_axis;
  163. $y = ($rad * sin($theta)) + $y_axis;
  164. $theta += $thetac;
  165. $rad1 = $radius * (($i + 1) / $points);
  166. $x1 = ($rad1 * cos($theta)) + $x_axis;
  167. $y1 = ($rad1 * sin($theta)) + $y_axis;
  168. imageline($im, $x, $y, $x1, $y1, $colors['grid']);
  169. $theta -= $thetac;
  170. }
  171. // -----------------------------------
  172. // Write the text
  173. // -----------------------------------
  174. $use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext'));
  175. if ($use_font === FALSE)
  176. {
  177. ($font_size > 5) && $font_size = 5;
  178. $x = mt_rand(0, $img_width / ($length / 3));
  179. $y = 0;
  180. }
  181. else
  182. {
  183. ($font_size > 30) && $font_size = 30;
  184. $x = mt_rand(0, $img_width / ($length / 1.5));
  185. $y = $font_size + 2;
  186. }
  187. for ($i = 0; $i < $length; $i++)
  188. {
  189. if ($use_font === FALSE)
  190. {
  191. $y = mt_rand(0 , $img_height / 2);
  192. imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']);
  193. $x += ($font_size * 2);
  194. }
  195. else
  196. {
  197. $y = mt_rand($img_height / 2, $img_height - 3);
  198. imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
  199. $x += $font_size;
  200. }
  201. }
  202. // Create the border
  203. imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);
  204. // -----------------------------------
  205. // Generate the image
  206. // -----------------------------------
  207. $img_url = rtrim($img_url, '/').'/';
  208. if (function_exists('imagejpeg'))
  209. {
  210. $img_filename = $now.'.jpg';
  211. imagejpeg($im, $img_path.$img_filename);
  212. }
  213. elseif (function_exists('imagepng'))
  214. {
  215. $img_filename = $now.'.png';
  216. imagepng($im, $img_path.$img_filename);
  217. }
  218. else
  219. {
  220. return FALSE;
  221. }
  222. $img = '<img '.($img_id === '' ? '' : 'id="'.$img_id.'"').' src="'.$img_url.$img_filename.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';
  223. ImageDestroy($im);
  224. return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);
  225. }
  226. }