PageRenderTime 65ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/system/application/plugins/dx_captcha_pi.php

https://bitbucket.org/snaver/unravel-the-music
PHP | 331 lines | 157 code | 47 blank | 127 comment | 34 complexity | 1e1e4d52bc33d9fc99dfbe64411a0c41 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2006, 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. Instructions:
  18. Load the plugin using:
  19. $this->load->plugin('captcha');
  20. Once loaded you can generate a captcha like this:
  21. $vals = array(
  22. 'word' => 'Random word',
  23. 'img_path' => './captcha/',
  24. 'img_url' => 'http://www.your-site.com/captcha/',
  25. 'font_path' => './system/texb.ttf',
  26. 'img_width' => rand(500, 600),
  27. 'img_height' => rand(80, 120),
  28. 'expiration' => 7200
  29. );
  30. $cap = create_captcha($vals);
  31. echo $cap['image'];
  32. NOTES:
  33. The captcha function requires the GD image library.
  34. Only the img_path and img_url are required.
  35. If a "word" is not supplied, the function will generate a random
  36. ASCII string. You might put together your own word library that
  37. you can draw randomly from.
  38. If you do not specify a path to a TRUE TYPE font, the native ugly GD
  39. font will be used.
  40. The "captcha" folder must be writable (666, or 777)
  41. The "expiration" (in seconds) signifies how long an image will
  42. remain in the captcha folder before it will be deleted. The default
  43. is two hours.
  44. RETURNED DATA
  45. The create_captcha() function returns an associative array with this data:
  46. [array]
  47. (
  48. 'image' => IMAGE TAG
  49. 'time' => TIMESTAMP (in microtime)
  50. 'word' => CAPTCHA WORD
  51. )
  52. The "image" is the actual image tag:
  53. <img src="http://your-site.com/captcha/12345.jpg" width="140" height="50" />
  54. The "time" is the micro timestamp used as the image name without the file
  55. extension. It will be a number like this: 1139612155.3422
  56. The "word" is the word that appears in the captcha image, which if not
  57. supplied to the function, will be a random string.
  58. /**
  59. |==========================================================
  60. | Create Captcha
  61. |==========================================================
  62. |
  63. */
  64. function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  65. {
  66. /**
  67. * Function to create a random color
  68. * Note: We aren't using this outside this function so we will sit it inside
  69. * @auteur mastercode.nl
  70. * @param $type string Mode for the color
  71. * @return int
  72. **/
  73. function color($type)
  74. {
  75. switch($type)
  76. {
  77. case "bg":
  78. $color = rand(253,255);
  79. break;
  80. case "text":
  81. $color = rand(0,23);
  82. break;
  83. case "grid":
  84. $color = rand(0,55);
  85. break;
  86. default:
  87. $color = rand(180,200);
  88. break;
  89. }
  90. return $color;
  91. }
  92. $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_size' => '', 'font_path' => '', 'show_grid' => true, 'skew' => true, 'expiration' => 7200);
  93. foreach ($defaults as $key => $val)
  94. {
  95. if ( ! is_array($data))
  96. {
  97. if ( ! isset($$key) OR $$key == '')
  98. {
  99. $$key = $val;
  100. }
  101. }
  102. else
  103. {
  104. $$key = ( ! isset($data[$key])) ? $val : $data[$key];
  105. }
  106. }
  107. if ($img_path == '' OR $img_url == '')
  108. {
  109. return FALSE;
  110. }
  111. if ( ! @is_dir($img_path))
  112. {
  113. return FALSE;
  114. }
  115. if ( ! is_really_writable($img_path))
  116. {
  117. return FALSE;
  118. }
  119. if ( ! extension_loaded('gd'))
  120. {
  121. return FALSE;
  122. }
  123. // -----------------------------------
  124. // Select random Font from folder
  125. // -----------------------------------
  126. if ( is_dir($font_path) )
  127. {
  128. $handle = opendir($font_path);
  129. while(($file = @readdir($handle)) !== false)
  130. {
  131. if(!in_array($file,array('.','..')) && substr($file, strlen($file)-4, 4) == '.ttf' )
  132. {
  133. $fonts[] = $file;
  134. }
  135. }
  136. $font_file = $font_path. DIRECTORY_SEPARATOR .$fonts[array_rand($fonts)];
  137. }
  138. else
  139. {
  140. $font_file = $font_path;
  141. }
  142. // -----------------------------------
  143. // Remove old images
  144. // -----------------------------------
  145. list($usec, $sec) = explode(" ", microtime());
  146. $now = ((float)$usec + (float)$sec);
  147. $current_dir = @opendir($img_path);
  148. while($filename = @readdir($current_dir))
  149. {
  150. if ($filename != "." and $filename != ".." and $filename != "index.html")
  151. {
  152. $name = str_replace(".png", "", $filename);
  153. if (($name + $expiration) < $now)
  154. {
  155. @unlink($img_path.$filename);
  156. }
  157. }
  158. }
  159. @closedir($current_dir);
  160. // -----------------------------------
  161. // Do we have a "word" yet?
  162. // -----------------------------------
  163. if ($word == '')
  164. {
  165. // No Zero (for user clarity);
  166. $pool = '23456789abcdefghikmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
  167. $str = '';
  168. for ($i = 0; $i < rand(6, 8); $i++)
  169. {
  170. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  171. }
  172. $word = strtoupper($str);
  173. }
  174. // -----------------------------------
  175. // Length of Word
  176. // -----------------------------------
  177. $length = strlen($word);
  178. // -----------------------------------
  179. // Create image
  180. // -----------------------------------
  181. $im = ImageCreateTruecolor($img_width, $img_height);
  182. // -----------------------------------
  183. // Assign colors
  184. // -----------------------------------
  185. $bg_color = imagecolorallocate ($im, color('bg'), color('bg'), color('bg'));
  186. $border_color = imagecolorallocate ($im, 255, 255, 255);
  187. $text_color = imagecolorallocate ($im, color('text'), color('text'), color('text'));
  188. $grid_color[] = imagecolorallocate($im, color('grid'), color('grid'), color('grid'));
  189. $grid_color[] = $grid_color[0] + 150;
  190. $grid_color[] = $grid_color[0] + 180;
  191. $grid_color[] = $grid_color[0] + 210;
  192. $shadow_color = imagecolorallocate($im, 255, 240, 240);
  193. // -----------------------------------
  194. // Create the rectangle
  195. // -----------------------------------
  196. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
  197. if ( $show_grid == TRUE )
  198. {
  199. // X grid
  200. $grid = rand(12,22);
  201. for ($x = 0; $x < $img_width; $x += mt_rand($grid - 2, $grid + 3))
  202. {
  203. $current_colour = $grid_color[array_rand($grid_color)];
  204. imagedashedline($im, mt_rand($x -3, $x + 3), mt_rand(0, 4), mt_rand($x -3, $x + 3), mt_rand($img_height - 5, $img_height), $current_colour);
  205. }
  206. // Y grid
  207. for ($y = 0; $y < $img_height; $y += mt_rand($grid - 2, $grid + 2))
  208. {
  209. $current_colour = $grid_color[array_rand($grid_color)];
  210. imageline($im, mt_rand(4,8), mt_rand($y - 3, $y), mt_rand($img_width - 5, $img_width), mt_rand($y - 3, $y), $current_colour);
  211. }
  212. }
  213. // -----------------------------------
  214. // Write the text
  215. // -----------------------------------
  216. $use_font = ($font_file != '' AND file_exists($font_file) AND function_exists('imagettftext')) ? TRUE : FALSE;
  217. if ($use_font == FALSE)
  218. {
  219. $font_size = rand(8,9);
  220. $x = rand(2, $img_width/($length/3));
  221. // y isnt used here
  222. }
  223. else
  224. {
  225. // Make font proportional to the image size
  226. $font_size = !empty($font_size) ? $font_size : mt_rand(18,20);
  227. $x = rand(4, $img_width - (($font_size + ($font_size >> 1)) * $length));
  228. // y isnt used here
  229. }
  230. for ($i = 0; $i < strlen($word); $i++)
  231. {
  232. if ($use_font == FALSE)
  233. {
  234. $y = rand(0 , $img_height/2);
  235. imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  236. $x += ($font_size*2);
  237. }
  238. else
  239. {
  240. $letter = substr($word, $i, 1);
  241. $less_rotate = array('c', 'N', 'U', 'Z', '7', '6', '9'); //letters that we don't want rotated too much...
  242. $angle = $skew == TRUE ? (in_array($letter, $less_rotate)) ? rand(-5, 5) : rand(-15, 15) : 0;
  243. $y = $img_height/2 + ($font_size >> 1) + ($skew == TRUE ? rand(-9, 9) : 0);
  244. $x += ($font_size >> 1.1);
  245. imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_file, $letter);
  246. $x += $font_size + ($font_size >> 2);
  247. }
  248. }
  249. // -----------------------------------
  250. // Create the border
  251. // -----------------------------------
  252. imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
  253. // -----------------------------------
  254. // Generate the image
  255. // -----------------------------------
  256. $img_name = $now.'.png';
  257. ImagePNG($im, $img_path.$img_name);
  258. $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" class='captchaImage' style=\"border:0;\" alt=\" \" />";
  259. ImageDestroy($im);
  260. return array('word' => $word, 'time' => $now, 'image' => $img);
  261. }
  262. ?>