PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/application/helpers/captcha_helper.php

http://github.com/claudehohl/Stikked
PHP | 412 lines | 223 code | 86 blank | 103 comment | 52 complexity | 239a325ec1a9f2111dd67bf5b12a62c7 MD5 | raw file
Possible License(s): LGPL-3.0, MIT, BSD-3-Clause
  1. <?php if (!defined('BASEPATH')) {
  2. exit('No direct script access allowed');
  3. }
  4. /**
  5. * CodeIgniter
  6. *
  7. * An open source application development framework for PHP 5.1.6 or newer
  8. *
  9. * @package CodeIgniter
  10. * @author ExpressionEngine Dev Team
  11. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  12. * @license http://codeigniter.com/user_guide/license.html
  13. * @link http://codeigniter.com
  14. * @since Version 1.0
  15. * @filesource
  16. */
  17. // ------------------------------------------------------------------------
  18. /**
  19. * CodeIgniter CAPTCHA Helper
  20. *
  21. * @package CodeIgniter
  22. * @subpackage Helpers
  23. * @category Helpers
  24. * @author ExpressionEngine Dev Team
  25. * @link http://codeigniter.com/user_guide/helpers/xml_helper.html
  26. */
  27. // ------------------------------------------------------------------------
  28. /**
  29. * Create CAPTCHA
  30. *
  31. * @access public
  32. * @param array array of data for the CAPTCHA
  33. * @param string path to create the image in
  34. * @param string URL to the CAPTCHA image folder
  35. * @param string server path to font
  36. * @return string
  37. */
  38. if (!function_exists('create_captcha')) {
  39. function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  40. {
  41. $defaults = array(
  42. 'word' => '',
  43. 'img_path' => '',
  44. 'img_url' => '',
  45. 'img_width' => '150',
  46. 'img_height' => '30',
  47. 'font_path' => '',
  48. 'expiration' => 7200,
  49. );
  50. foreach ($defaults as $key => $val) {
  51. if (!is_array($data)) {
  52. if (!isset($$key) or $$key == '') {
  53. $$key = $val;
  54. }
  55. } else {
  56. $$key = (!isset($data[$key])) ? $val : $data[$key];
  57. }
  58. }
  59. if ($img_path == '' or $img_url == '') {
  60. return false;
  61. }
  62. if (!@is_dir($img_path)) {
  63. return false;
  64. }
  65. if (!is_writable($img_path)) {
  66. return false;
  67. }
  68. if (!extension_loaded('gd')) {
  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. if ($filename != "." and $filename != ".." and $filename != "index.html") {
  79. $name = str_replace(".jpg", "", $filename);
  80. if (($name + $expiration) < $now) {
  81. @unlink($img_path . $filename);
  82. }
  83. }
  84. }
  85. @closedir($current_dir);
  86. // -----------------------------------
  87. // Do we have a "word" yet?
  88. // -----------------------------------
  89. if ($word == '') {
  90. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  91. $str = '';
  92. for ($i = 0; $i < 8; $i++) {
  93. $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
  94. }
  95. $word = $str;
  96. }
  97. // -----------------------------------
  98. // Determine angle and position
  99. // -----------------------------------
  100. $length = strlen($word);
  101. $angle = ($length >= 6) ? rand(-($length - 6), ($length - 6)) : 0;
  102. $x_axis = rand(6, (360 / $length) - 16);
  103. $y_axis = ($angle >= 0) ? rand($img_height, $img_width) : rand(6, $img_height);
  104. // -----------------------------------
  105. // Create image
  106. // -----------------------------------
  107. // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  108. if (function_exists('imagecreatetruecolor')) {
  109. $im = imagecreatetruecolor($img_width, $img_height);
  110. } else {
  111. $im = imagecreate($img_width, $img_height);
  112. }
  113. // -----------------------------------
  114. // Assign colors
  115. // -----------------------------------
  116. $bg_color = imagecolorallocate($im, 255, 255, 255);
  117. $border_color = imagecolorallocate($im, 153, 102, 102);
  118. $text_color = imagecolorallocate($im, 204, 153, 153);
  119. $grid_color = imagecolorallocate($im, 255, 182, 182);
  120. $shadow_color = imagecolorallocate($im, 255, 240, 240);
  121. // -----------------------------------
  122. // Create the rectangle
  123. // -----------------------------------
  124. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
  125. // -----------------------------------
  126. // Create the spiral pattern
  127. // -----------------------------------
  128. $theta = 1;
  129. $thetac = 7;
  130. $radius = 16;
  131. $circles = 20;
  132. $points = 32;
  133. for ($i = 0; $i < ($circles * $points) - 1; $i++) {
  134. $theta = $theta + $thetac;
  135. $rad = $radius * ($i / $points);
  136. $x = ($rad * cos($theta)) + $x_axis;
  137. $y = ($rad * sin($theta)) + $y_axis;
  138. $theta = $theta + $thetac;
  139. $rad1 = $radius * (($i + 1) / $points);
  140. $x1 = ($rad1 * cos($theta)) + $x_axis;
  141. $y1 = ($rad1 * sin($theta)) + $y_axis;
  142. imageline($im, $x, $y, $x1, $y1, $grid_color);
  143. $theta = $theta - $thetac;
  144. }
  145. // -----------------------------------
  146. // Write the text
  147. // -----------------------------------
  148. $use_font = ($font_path != '' and file_exists($font_path) and function_exists('imagettftext')) ? true : false;
  149. if ($use_font == false) {
  150. $font_size = 5;
  151. $x = rand(0, $img_width / ($length / 3));
  152. $y = 0;
  153. } else {
  154. $font_size = 16;
  155. $x = rand(0, $img_width / ($length / 1.5));
  156. $y = $font_size + 2;
  157. }
  158. for ($i = 0; $i < strlen($word); $i++) {
  159. if ($use_font == false) {
  160. $y = rand(0, $img_height / 2);
  161. imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  162. $x += ($font_size * 2);
  163. } else {
  164. $y = rand($img_height / 2, $img_height - 3);
  165. imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
  166. $x += $font_size;
  167. }
  168. }
  169. // -----------------------------------
  170. // Create the border
  171. // -----------------------------------
  172. imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color);
  173. // -----------------------------------
  174. // Generate the image
  175. // -----------------------------------
  176. $img_name = $now . '.jpg';
  177. ImageJPEG($im, $img_path . $img_name);
  178. $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
  179. ImageDestroy($im);
  180. return array(
  181. 'word' => $word,
  182. 'time' => $now,
  183. 'image' => $img,
  184. );
  185. }
  186. }
  187. /**
  188. * Display CAPTCHA
  189. *
  190. * @access public
  191. * @return string
  192. */
  193. if (!function_exists('display_captcha')) {
  194. function display_captcha($word = '')
  195. {
  196. $data = '';
  197. list($usec, $sec) = explode(" ", microtime());
  198. $now = ((float) $usec + (float) $sec);
  199. $defaults = array(
  200. 'word' => $word,
  201. 'img_path' => '',
  202. 'img_url' => '',
  203. 'img_width' => '180',
  204. 'img_height' => '40',
  205. 'font_path' => '',
  206. 'expiration' => 7200,
  207. );
  208. foreach ($defaults as $key => $val) {
  209. if (!is_array($data)) {
  210. if (!isset($$key) or $$key == '') {
  211. $$key = $val;
  212. }
  213. } else {
  214. $$key = (!isset($data[$key])) ? $val : $data[$key];
  215. }
  216. }
  217. if (!extension_loaded('gd')) {
  218. return false;
  219. }
  220. // -----------------------------------
  221. // Do we have a "word" yet?
  222. // -----------------------------------
  223. if ($word == '') {
  224. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  225. $str = '';
  226. for ($i = 0; $i < 4; $i++) {
  227. $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1);
  228. }
  229. $word = $str;
  230. }
  231. // -----------------------------------
  232. // Determine angle and position
  233. // -----------------------------------
  234. $length = strlen($word);
  235. $angle = ($length >= 6) ? mt_rand(-($length - 6), ($length - 6)) : 0;
  236. $angle = 10;
  237. $x_axis = mt_rand(6, (360 / $length) - 16);
  238. $y_axis = ($angle >= 0) ? mt_rand($img_height, $img_width) : mt_rand(6, $img_height);
  239. // -----------------------------------
  240. // Create image
  241. // -----------------------------------
  242. // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  243. if (function_exists('imagecreatetruecolor')) {
  244. $im = imagecreatetruecolor($img_width, $img_height);
  245. } else {
  246. $im = imagecreate($img_width, $img_height);
  247. }
  248. // -----------------------------------
  249. // Assign colors
  250. // -----------------------------------
  251. function b()
  252. {
  253. return mt_rand(240, 255);
  254. }
  255. $bg_color = imagecolorallocate($im, b(), b(), b());
  256. $border_color = imagecolorallocate($im, 153, 102, 102);
  257. $text_color = imagecolorallocate($im, 204, 153, 153);
  258. $grid_color = imagecolorallocate($im, 255, 182, 182);
  259. $shadow_color = imagecolorallocate($im, 255, 240, 240);
  260. // -----------------------------------
  261. // Create the rectangle
  262. // -----------------------------------
  263. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
  264. // -----------------------------------
  265. // Create the spiral pattern
  266. // -----------------------------------
  267. $theta = 1;
  268. $thetac = 7;
  269. $radius = 16;
  270. $circles = 20;
  271. $points = 32;
  272. for ($i = 0; $i < ($circles * $points) - 1; $i++) {
  273. $theta = $theta + $thetac;
  274. $rad = $radius * ($i / $points);
  275. $x = ($rad * cos($theta)) + $x_axis;
  276. $y = ($rad * sin($theta)) + $y_axis;
  277. $theta = $theta + $thetac;
  278. $rad1 = $radius * (($i + 1) / $points);
  279. $x1 = ($rad1 * cos($theta)) + $x_axis;
  280. $y1 = ($rad1 * sin($theta)) + $y_axis;
  281. imageline($im, $x, $y, $x1, $y1, $grid_color);
  282. $theta = $theta - $thetac;
  283. }
  284. // -----------------------------------
  285. // Write the text
  286. // -----------------------------------
  287. //get random font
  288. $fn = explode(',', '4,5,6,8,16,17,19,24,26');
  289. $f = mt_rand(0, count($fn) - 1);
  290. $font_path = './static/fonts/font' . $fn[$f] . '.ttf';
  291. $use_font = ($font_path != '' and file_exists($font_path) and function_exists('imagettftext')) ? true : false;
  292. if ($use_font == false) {
  293. $font_size = 5;
  294. $x = mt_rand(0, $img_width / ($length / 3));
  295. $y = 0;
  296. } else {
  297. $font_size = 16;
  298. $x = mt_rand(0, $img_width / ($length / 1.5));
  299. $y = $font_size + 2;
  300. }
  301. for ($i = 0; $i < strlen($word); $i++) {
  302. if ($use_font == false) {
  303. $y = mt_rand(0, $img_height / 2);
  304. imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  305. $x += ($font_size * 2);
  306. } else {
  307. $y = mt_rand($img_height / 2, $img_height - 3);
  308. imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
  309. $x += $font_size;
  310. }
  311. }
  312. // -----------------------------------
  313. // Create the border
  314. // -----------------------------------
  315. imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color);
  316. // -----------------------------------
  317. // Generate the image
  318. // -----------------------------------
  319. $img_name = $now . '.jpg';
  320. echo ImageJPEG($im);
  321. ImageDestroy($im);
  322. }
  323. }
  324. // ------------------------------------------------------------------------
  325. /* End of file captcha_helper.php */
  326. /* Location: ./system/heleprs/captcha_helper.php */