PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/api/system/helpers/captcha_helper.php

https://bitbucket.org/hoytei/emervet
PHP | 254 lines | 143 code | 28 blank | 83 comment | 16 complexity | 0cef87218187625a6d1bcaccd5a0a9b5 MD5 | raw file
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014, 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, 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. 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  72. 'colors' => array(
  73. 'background' => array(255,255,255),
  74. 'border' => array(153,102,102),
  75. 'text' => array(204,153,153),
  76. 'grid' => array(255,182,182)
  77. )
  78. );
  79. foreach ($defaults as $key => $val)
  80. {
  81. if ( ! is_array($data) && empty($$key))
  82. {
  83. $$key = $val;
  84. }
  85. else
  86. {
  87. $$key = isset($data[$key]) ? $data[$key] : $val;
  88. }
  89. }
  90. if ($img_path === '' OR $img_url === ''
  91. OR ! is_dir($img_path) OR ! is_really_writable($img_path)
  92. OR ! extension_loaded('gd'))
  93. {
  94. return FALSE;
  95. }
  96. // -----------------------------------
  97. // Remove old images
  98. // -----------------------------------
  99. $now = microtime(TRUE);
  100. $current_dir = @opendir($img_path);
  101. while ($filename = @readdir($current_dir))
  102. {
  103. if (substr($filename, -4) === '.jpg' && (str_replace('.jpg', '', $filename) + $expiration) < $now)
  104. {
  105. @unlink($img_path.$filename);
  106. }
  107. }
  108. @closedir($current_dir);
  109. // -----------------------------------
  110. // Do we have a "word" yet?
  111. // -----------------------------------
  112. if (empty($word))
  113. {
  114. $word = '';
  115. for ($i = 0, $mt_rand_max = strlen($pool) - 1; $i < $word_length; $i++)
  116. {
  117. $word .= $pool[mt_rand(0, $mt_rand_max)];
  118. }
  119. }
  120. elseif ( ! is_string($word))
  121. {
  122. $word = (string) $word;
  123. }
  124. // -----------------------------------
  125. // Determine angle and position
  126. // -----------------------------------
  127. $length = strlen($word);
  128. $angle = ($length >= 6) ? mt_rand(-($length-6), ($length-6)) : 0;
  129. $x_axis = mt_rand(6, (360/$length)-16);
  130. $y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
  131. // Create image
  132. // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  133. $im = function_exists('imagecreatetruecolor')
  134. ? imagecreatetruecolor($img_width, $img_height)
  135. : imagecreate($img_width, $img_height);
  136. // -----------------------------------
  137. // Assign colors
  138. // ----------------------------------
  139. is_array($colors) OR $colors = $defaults['colors'];
  140. foreach (array_keys($defaults['colors']) as $key)
  141. {
  142. // Check for a possible missing value
  143. is_array($colors[$key]) OR $colors[$key] = $defaults['colors'][$key];
  144. $colors[$key] = imagecolorallocate($im, $colors[$key][0], $colors[$key][1], $colors[$key][2]);
  145. }
  146. // Create the rectangle
  147. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $colors['background']);
  148. // -----------------------------------
  149. // Create the spiral pattern
  150. // -----------------------------------
  151. $theta = 1;
  152. $thetac = 7;
  153. $radius = 16;
  154. $circles = 20;
  155. $points = 32;
  156. for ($i = 0, $cp = ($circles * $points) - 1; $i < $cp; $i++)
  157. {
  158. $theta += $thetac;
  159. $rad = $radius * ($i / $points);
  160. $x = ($rad * cos($theta)) + $x_axis;
  161. $y = ($rad * sin($theta)) + $y_axis;
  162. $theta += $thetac;
  163. $rad1 = $radius * (($i + 1) / $points);
  164. $x1 = ($rad1 * cos($theta)) + $x_axis;
  165. $y1 = ($rad1 * sin($theta)) + $y_axis;
  166. imageline($im, $x, $y, $x1, $y1, $colors['grid']);
  167. $theta -= $thetac;
  168. }
  169. // -----------------------------------
  170. // Write the text
  171. // -----------------------------------
  172. $use_font = ($font_path !== '' && file_exists($font_path) && function_exists('imagettftext'));
  173. if ($use_font === FALSE)
  174. {
  175. $font_size = 5;
  176. $x = mt_rand(0, $img_width / ($length / 3));
  177. $y = 0;
  178. }
  179. else
  180. {
  181. $font_size = 16;
  182. $x = mt_rand(0, $img_width / ($length / 1.5));
  183. $y = $font_size + 2;
  184. }
  185. for ($i = 0; $i < $length; $i++)
  186. {
  187. if ($use_font === FALSE)
  188. {
  189. $y = mt_rand(0 , $img_height / 2);
  190. imagestring($im, $font_size, $x, $y, $word[$i], $colors['text']);
  191. $x += ($font_size * 2);
  192. }
  193. else
  194. {
  195. $y = mt_rand($img_height / 2, $img_height - 3);
  196. imagettftext($im, $font_size, $angle, $x, $y, $colors['text'], $font_path, $word[$i]);
  197. $x += $font_size;
  198. }
  199. }
  200. // Create the border
  201. imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $colors['border']);
  202. // -----------------------------------
  203. // Generate the image
  204. // -----------------------------------
  205. $img_url = rtrim($img_url, '/').'/';
  206. if (function_exists('imagejpeg'))
  207. {
  208. $img_filename = $now.'.jpg';
  209. imagejpeg($im, $img_path.$img_filename);
  210. }
  211. elseif (function_exists('imagepng'))
  212. {
  213. $img_filename = $now.'.png';
  214. imagepng($im, $img_path.$img_filename);
  215. }
  216. else
  217. {
  218. return FALSE;
  219. }
  220. $img = '<img src="'.$img_url.$img_filename.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';
  221. ImageDestroy($im);
  222. return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);
  223. }
  224. }
  225. /* End of file captcha_helper.php */
  226. /* Location: ./system/helpers/captcha_helper.php */