PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/plugins/captcha_pi.php

http://bitstopdev-hostedqueuesys.googlecode.com/
PHP | 356 lines | 130 code | 46 blank | 180 comment | 26 complexity | 2f5491003b50b5a330c4b2a8c0e94916 MD5 | raw file
Possible License(s): Unlicense
  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) 2008 - 2010, 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://example.com/captcha/',
  25. 'font_path' => './system/fonts/texb.ttf',
  26. 'img_width' => '150',
  27. 'img_height' => 30,
  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://example.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. ADDING A DATABASE
  59. In order for the captcha function to prevent someone from posting, you will need
  60. to add the information returned from create_captcha() function to your database.
  61. Then, when the data from the form is submitted by the user you will need to verify
  62. that the data exists in the database and has not expired.
  63. Here is a table prototype:
  64. CREATE TABLE captcha (
  65. captcha_id bigint(13) unsigned NOT NULL auto_increment,
  66. captcha_time int(10) unsigned NOT NULL,
  67. ip_address varchar(16) default '0' NOT NULL,
  68. word varchar(20) NOT NULL,
  69. PRIMARY KEY `captcha_id` (`captcha_id`),
  70. KEY `word` (`word`)
  71. )
  72. Here is an example of usage with a DB.
  73. On the page where the captcha will be shown you'll have something like this:
  74. $this->load->plugin('captcha');
  75. $vals = array(
  76. 'img_path' => './captcha/',
  77. 'img_url' => 'http://example.com/captcha/'
  78. );
  79. $cap = create_captcha($vals);
  80. $data = array(
  81. 'captcha_id' => '',
  82. 'captcha_time' => $cap['time'],
  83. 'ip_address' => $this->input->ip_address(),
  84. 'word' => $cap['word']
  85. );
  86. $query = $this->db->insert_string('captcha', $data);
  87. $this->db->query($query);
  88. echo 'Submit the word you see below:';
  89. echo $cap['image'];
  90. echo '<input type="text" name="captcha" value="" />';
  91. Then, on the page that accepts the submission you'll have something like this:
  92. // First, delete old captchas
  93. $expiration = time()-7200; // Two hour limit
  94. $DB->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);
  95. // Then see if a captcha exists:
  96. $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND date > ?";
  97. $binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
  98. $query = $this->db->query($sql, $binds);
  99. $row = $query->row();
  100. if ($row->count == 0)
  101. {
  102. echo "You must submit the word that appears in the image";
  103. }
  104. */
  105. /**
  106. |==========================================================
  107. | Create Captcha
  108. |==========================================================
  109. |
  110. */
  111. function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '')
  112. {
  113. $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_path' => '', 'expiration' => 7200);
  114. foreach ($defaults as $key => $val)
  115. {
  116. if ( ! is_array($data))
  117. {
  118. if ( ! isset($$key) OR $$key == '')
  119. {
  120. $$key = $val;
  121. }
  122. }
  123. else
  124. {
  125. $$key = ( ! isset($data[$key])) ? $val : $data[$key];
  126. }
  127. }
  128. if ($img_path == '' OR $img_url == '')
  129. {
  130. return FALSE;
  131. }
  132. if ( ! @is_dir($img_path))
  133. {
  134. return FALSE;
  135. }
  136. if ( ! is_really_writable($img_path))
  137. {
  138. return FALSE;
  139. }
  140. if ( ! extension_loaded('gd'))
  141. {
  142. return FALSE;
  143. }
  144. // -----------------------------------
  145. // Remove old images
  146. // -----------------------------------
  147. list($usec, $sec) = explode(" ", microtime());
  148. $now = ((float)$usec + (float)$sec);
  149. $current_dir = @opendir($img_path);
  150. while($filename = @readdir($current_dir))
  151. {
  152. if ($filename != "." and $filename != ".." and $filename != "index.html")
  153. {
  154. $name = str_replace(".jpg", "", $filename);
  155. if (($name + $expiration) < $now)
  156. {
  157. @unlink($img_path.$filename);
  158. }
  159. }
  160. }
  161. @closedir($current_dir);
  162. // -----------------------------------
  163. // Do we have a "word" yet?
  164. // -----------------------------------
  165. if ($word == '')
  166. {
  167. $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  168. $str = '';
  169. for ($i = 0; $i < 8; $i++)
  170. {
  171. $str .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  172. }
  173. $word = $str;
  174. }
  175. // -----------------------------------
  176. // Determine angle and position
  177. // -----------------------------------
  178. $length = strlen($word);
  179. $angle = ($length >= 6) ? rand(-($length-6), ($length-6)) : 0;
  180. $x_axis = rand(6, (360/$length)-16);
  181. $y_axis = ($angle >= 0 ) ? rand($img_height, $img_width) : rand(6, $img_height);
  182. // -----------------------------------
  183. // Create image
  184. // -----------------------------------
  185. // PHP.net recommends imagecreatetruecolor(), but it isn't always available
  186. if (function_exists('imagecreatetruecolor'))
  187. {
  188. $im = imagecreatetruecolor($img_width, $img_height);
  189. }
  190. else
  191. {
  192. $im = imagecreate($img_width, $img_height);
  193. }
  194. // -----------------------------------
  195. // Assign colors
  196. // -----------------------------------
  197. $bg_color = imagecolorallocate ($im, 255, 255, 255);
  198. $border_color = imagecolorallocate ($im, 153, 102, 102);
  199. $text_color = imagecolorallocate ($im, 204, 153, 153);
  200. $grid_color = imagecolorallocate($im, 255, 182, 182);
  201. $shadow_color = imagecolorallocate($im, 255, 240, 240);
  202. // -----------------------------------
  203. // Create the rectangle
  204. // -----------------------------------
  205. ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color);
  206. // -----------------------------------
  207. // Create the spiral pattern
  208. // -----------------------------------
  209. $theta = 1;
  210. $thetac = 7;
  211. $radius = 16;
  212. $circles = 20;
  213. $points = 32;
  214. for ($i = 0; $i < ($circles * $points) - 1; $i++)
  215. {
  216. $theta = $theta + $thetac;
  217. $rad = $radius * ($i / $points );
  218. $x = ($rad * cos($theta)) + $x_axis;
  219. $y = ($rad * sin($theta)) + $y_axis;
  220. $theta = $theta + $thetac;
  221. $rad1 = $radius * (($i + 1) / $points);
  222. $x1 = ($rad1 * cos($theta)) + $x_axis;
  223. $y1 = ($rad1 * sin($theta )) + $y_axis;
  224. imageline($im, $x, $y, $x1, $y1, $grid_color);
  225. $theta = $theta - $thetac;
  226. }
  227. // -----------------------------------
  228. // Write the text
  229. // -----------------------------------
  230. $use_font = ($font_path != '' AND file_exists($font_path) AND function_exists('imagettftext')) ? TRUE : FALSE;
  231. if ($use_font == FALSE)
  232. {
  233. $font_size = 5;
  234. $x = rand(0, $img_width/($length/3));
  235. $y = 0;
  236. }
  237. else
  238. {
  239. $font_size = 16;
  240. $x = rand(0, $img_width/($length/1.5));
  241. $y = $font_size+2;
  242. }
  243. for ($i = 0; $i < strlen($word); $i++)
  244. {
  245. if ($use_font == FALSE)
  246. {
  247. $y = rand(0 , $img_height/2);
  248. imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color);
  249. $x += ($font_size*2);
  250. }
  251. else
  252. {
  253. $y = rand($img_height/2, $img_height-3);
  254. imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_path, substr($word, $i, 1));
  255. $x += $font_size;
  256. }
  257. }
  258. // -----------------------------------
  259. // Create the border
  260. // -----------------------------------
  261. imagerectangle($im, 0, 0, $img_width-1, $img_height-1, $border_color);
  262. // -----------------------------------
  263. // Generate the image
  264. // -----------------------------------
  265. $img_name = $now.'.jpg';
  266. ImageJPEG($im, $img_path.$img_name);
  267. $img = "<img src=\"$img_url$img_name\" width=\"$img_width\" height=\"$img_height\" style=\"border:0;\" alt=\" \" />";
  268. ImageDestroy($im);
  269. return array('word' => $word, 'time' => $now, 'image' => $img);
  270. }
  271. /* End of file captcha_pi.php */
  272. /* Location: ./system/plugins/captcha_pi.php */