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

/local_food_include/securimage.php

https://github.com/traceyambrose/foodcoop
PHP | 934 lines | 349 code | 128 blank | 457 comment | 74 complexity | f30c4d3f49120290555f0e044bf92ef2 MD5 | raw file
  1. <?php
  2. /**
  3. * Project: Securimage: A PHP class for creating and managing form CAPTCHA images<br />
  4. * File: securimage.php<br />
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or any later version.<br /><br />
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.<br /><br />
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA<br /><br />
  19. *
  20. * Any modifications to the library should be indicated clearly in the source code
  21. * to inform users that the changes are not a part of the original software.<br /><br />
  22. *
  23. * If you found this script useful, please take a quick moment to rate it.<br />
  24. * http://www.hotscripts.com/rate/49400.html Thanks.
  25. *
  26. * @link http://www.phpcaptcha.org Securimage PHP CAPTCHA
  27. * @link http://www.phpcaptcha.org/latest.zip Download Latest Version
  28. * @link http://www.phpcaptcha.org/Securimage_Docs/ Online Documentation
  29. * @copyright 2007 Drew Phillips
  30. * @author drew010 <drew@drew-phillips.com>
  31. * @version 1.0.3.1 (March 24, 2008)
  32. * @package Securimage
  33. *
  34. */
  35. /**
  36. ChangeLog
  37. 1.0.3.1
  38. - Error reading from wordlist in some cases caused words to be cut off 1 letter short
  39. 1.0.3
  40. - Removed shadow_text from code which could cause an undefined property error due to removal from previous version
  41. 1.0.2
  42. - Audible CAPTCHA Code wav files
  43. - Create codes from a word list instead of random strings
  44. 1.0
  45. - Added the ability to use a selected character set, rather than a-z0-9 only.
  46. - Added the multi-color text option to use different colors for each letter.
  47. - Switched to automatic session handling instead of using files for code storage
  48. - Added GD Font support if ttf support is not available. Can use internal GD fonts or load new ones.
  49. - Added the ability to set line thickness
  50. - Added option for drawing arced lines over letters
  51. - Added ability to choose image type for output
  52. */
  53. /**
  54. * Output images in JPEG format
  55. */
  56. define('SI_IMAGE_JPEG', 1);
  57. /**
  58. * Output images in PNG format
  59. */
  60. define('SI_IMAGE_PNG', 2);
  61. /**
  62. * Output images in GIF format
  63. * Must have GD >= 2.0.28!
  64. */
  65. define('SI_IMAGE_GIF', 3);
  66. /**
  67. * Securimage CAPTCHA Class.
  68. *
  69. * @package Securimage
  70. * @subpackage classes
  71. *
  72. */
  73. class Securimage {
  74. /**
  75. * The desired width of the CAPTCHA image.
  76. *
  77. * @var int
  78. */
  79. var $image_width = 250;
  80. /**
  81. * The desired height of the CAPTCHA image.
  82. *
  83. * @var int
  84. */
  85. var $image_height = 65;
  86. /**
  87. * The image format for output.<br />
  88. * Valid options: SI_IMAGE_PNG, SI_IMAGE_JPG, SI_IMAGE_GIF
  89. *
  90. * @var int
  91. */
  92. var $image_type = SI_IMAGE_PNG;
  93. /**
  94. * The length of the code to generate.
  95. *
  96. * @var int
  97. */
  98. var $code_length = 4;
  99. /**
  100. * The character set for individual characters in the image.<br />
  101. * Letters are converted to uppercase.<br />
  102. * The font must support the letters or there may be problematic substitutions.
  103. *
  104. * @var string
  105. */
  106. var $charset = 'ABCDEFGHKLMNPRSTUVWYZ23456789';
  107. //var $charset = '0123456789';
  108. /**
  109. * Create codes using this word list
  110. *
  111. * @var string The path to the word list to use for creating CAPTCHA codes
  112. */
  113. var $wordlist_file = 'words.txt';
  114. /**
  115. * True to use a word list file instead of a random code
  116. *
  117. * @var bool
  118. */
  119. var $use_wordlist = true;
  120. /**
  121. * Whether to use a GD font instead of a TTF font.<br />
  122. * TTF offers more support and options, but use this if your PHP doesn't support TTF.<br />
  123. *
  124. * @var boolean
  125. */
  126. var $use_gd_font = false;
  127. /**
  128. * The GD font to use.<br />
  129. * Internal gd fonts can be loaded by their number.<br />
  130. * Alternatively, a file path can be given and the font will be loaded from file.
  131. *
  132. * @var mixed
  133. */
  134. var $gd_font_file = 'gdfonts/bubblebath.gdf';
  135. /**
  136. * The approximate size of the font in pixels.<br />
  137. * This does not control the size of the font because that is determined by the GD font itself.<br />
  138. * This is used to aid the calculations of positioning used by this class.<br />
  139. *
  140. * @var int
  141. */
  142. var $gd_font_size = 20;
  143. // Note: These font options below do not apply if you set $use_gd_font to true with the exception of $text_color
  144. /**
  145. * The path to the TTF font file to load.
  146. *
  147. * @var string
  148. */
  149. // var $ttf_file = "./PenguinAttack.ttf";
  150. var $ttf_file = "./elephant.ttf";
  151. /**
  152. * The font size.<br />
  153. * Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2)<br />
  154. *
  155. * @var int
  156. */
  157. var $font_size = 35;
  158. /**
  159. * The minimum angle in degrees, with 0 degrees being left-to-right reading text.<br />
  160. * Higher values represent a counter-clockwise rotation.<br />
  161. * For example, a value of 90 would result in bottom-to-top reading text.
  162. *
  163. * @var int
  164. */
  165. var $text_angle_minimum = -15;
  166. /**
  167. * The minimum angle in degrees, with 0 degrees being left-to-right reading text.<br />
  168. * Higher values represent a counter-clockwise rotation.<br />
  169. * For example, a value of 90 would result in bottom-to-top reading text.
  170. *
  171. * @var int
  172. */
  173. var $text_angle_maximum = 35;
  174. /**
  175. * The X-Position on the image where letter drawing will begin.<br />
  176. * This value is in pixels from the left side of the image.
  177. *
  178. * @var int
  179. */
  180. var $text_x_start = 18;
  181. /**
  182. * Letters can be spaced apart at random distances.<br />
  183. * This is the minimum distance between two letters.<br />
  184. * This should be <i>at least</i> as wide as a font character.<br />
  185. * Small values can cause letters to be drawn over eachother.<br />
  186. *
  187. * @var int
  188. */
  189. var $text_minimum_distance = 40;
  190. /**
  191. * Letters can be spaced apart at random distances.<br />
  192. * This is the maximum distance between two letters.<br />
  193. * This should be <i>at least</i> as wide as a font character.<br />
  194. * Small values can cause letters to be drawn over eachother.<br />
  195. *
  196. * @var int
  197. */
  198. var $text_maximum_distance = 42;
  199. /**
  200. * The background color for the image.<br />
  201. * This should be specified in HTML hex format.<br />
  202. * Make sure to include the preceding # sign!
  203. *
  204. * @var string
  205. */
  206. var $image_bg_color = "#ffffff";
  207. /**
  208. * The text color to use for drawing characters.<br />
  209. * This value is ignored if $use_multi_text is set to true.<br />
  210. * Make sure this contrasts well with the background color.<br />
  211. * Specify the color in HTML hex format with preceding # sign
  212. *
  213. * @see Securimage::$use_multi_text
  214. * @var string
  215. */
  216. // var $text_color = "#ff0000";
  217. var $text_color = "#448800";
  218. /**
  219. * Set to true to use multiple colors for each character.
  220. *
  221. * @see Securimage::$multi_text_color
  222. * @var boolean
  223. */
  224. var $use_multi_text = false;
  225. /**
  226. * String of HTML hex colors to use.<br />
  227. * Separate each possible color with commas.<br />
  228. * Be sure to precede each value with the # sign.
  229. *
  230. * @var string
  231. */
  232. var $multi_text_color = "#0a68dd,#f65c47,#8d32fd";
  233. /**
  234. * Set to true to make the characters appear transparent.
  235. *
  236. * @see Securimage::$text_transparency_percentage
  237. * @var boolean
  238. */
  239. var $use_transparent_text = false;
  240. /**
  241. * The percentage of transparency, 0 to 100.<br />
  242. * A value of 0 is completely opaque, 100 is completely transparent (invisble)
  243. *
  244. * @see Securimage::$use_transparent_text
  245. * @var int
  246. */
  247. var $text_transparency_percentage = 15;
  248. // Line options
  249. /**
  250. * Draw vertical and horizontal lines on the image.
  251. *
  252. * @see Securimage::$line_color
  253. * @see Securimage::$line_distance
  254. * @see Securimage::$line_thickness
  255. * @see Securimage::$draw_lines_over_text
  256. * @var boolean
  257. */
  258. var $draw_lines = true;
  259. /**
  260. * The color of the lines drawn on the image.<br />
  261. * Use HTML hex format with preceding # sign.
  262. *
  263. * @see Securimage::$draw_lines
  264. * @var string
  265. */
  266. var $line_color = "#ffffff";
  267. /**
  268. * How far apart to space the lines from eachother in pixels.
  269. *
  270. * @see Securimage::$draw_lines
  271. * @var int
  272. */
  273. var $line_distance = 4;
  274. /**
  275. * How thick to draw the lines in pixels.<br />
  276. * 1-3 is ideal depending on distance
  277. *
  278. * @see Securimage::$draw_lines
  279. * @see Securimage::$line_distance
  280. * @var int
  281. */
  282. var $line_thickness = 1;
  283. /**
  284. * Set to true to draw angled lines on the image in addition to the horizontal and vertical lines.
  285. *
  286. * @see Securimage::$draw_lines
  287. * @var boolean
  288. */
  289. var $draw_angled_lines = true;
  290. /**
  291. * Draw the lines over the text.<br />
  292. * If fales lines will be drawn before putting the text on the image.<br />
  293. * This can make the image hard for humans to read depending on the line thickness and distance.
  294. *
  295. * @var boolean
  296. */
  297. var $draw_lines_over_text = true;
  298. /**
  299. * For added security, it is a good idea to draw arced lines over the letters to make it harder for bots to segment the letters.<br />
  300. * Two arced lines will be drawn over the text on each side of the image.<br />
  301. * This is currently expirimental and may be off in certain configurations.
  302. *
  303. * @var boolean
  304. */
  305. var $arc_linethrough = false;
  306. /**
  307. * The colors or color of the arced lines.<br />
  308. * Use HTML hex notation with preceding # sign, and separate each value with a comma.<br />
  309. * This should be similar to your font color for single color images.
  310. *
  311. * @var string
  312. */
  313. var $arc_line_colors = "#8080ff";
  314. /**
  315. * Full path to the WAV files to use to make the audio files, include trailing /.<br />
  316. * Name Files [A-Z0-9].wav
  317. *
  318. * @since 1.0.1
  319. * @var string
  320. */
  321. var $audio_path = './audio/';
  322. //END USER CONFIGURATION
  323. //There should be no need to edit below unless you really know what you are doing.
  324. /**
  325. * The gd image resource.
  326. *
  327. * @access private
  328. * @var resource
  329. */
  330. var $im;
  331. /**
  332. * The background image resource
  333. *
  334. * @access private
  335. * @var resource
  336. */
  337. var $bgimg;
  338. /**
  339. * The code generated by the script
  340. *
  341. * @access private
  342. * @var string
  343. */
  344. var $code;
  345. /**
  346. * The code that was entered by the user
  347. *
  348. * @access private
  349. * @var string
  350. */
  351. var $code_entered;
  352. /**
  353. * Whether or not the correct code was entered
  354. *
  355. * @access private
  356. * @var boolean
  357. */
  358. var $correct_code;
  359. /**
  360. * Class constructor.<br />
  361. * Because the class uses sessions, this will attempt to start a session if there is no previous one.<br />
  362. * If you do not start a session before calling the class, the constructor must be called before any
  363. * output is sent to the browser.
  364. *
  365. * <code>
  366. * $securimage = new Securimage();
  367. * </code>
  368. *
  369. */
  370. function Securimage()
  371. {
  372. if ( session_id() == '' ) { // no session has been started yet, which is needed for validation
  373. session_start();
  374. }
  375. }
  376. /**
  377. * Generate a code and output the image to the browser.
  378. *
  379. * <code>
  380. * <?php
  381. * include 'securimage.php';
  382. * $securimage = new Securimage();
  383. * $securimage->show('bg.jpg');
  384. * ?>
  385. * </code>
  386. *
  387. * @param string $background_image The path to an image to use as the background for the CAPTCHA
  388. */
  389. function show($background_image = "")
  390. {
  391. if($background_image != "" && is_readable($background_image)) {
  392. $this->bgimg = $background_image;
  393. }
  394. $this->doImage();
  395. }
  396. /**
  397. * Validate the code entered by the user.
  398. *
  399. * <code>
  400. * $code = $_POST['code'];
  401. * if ($securimage->check($code) == false) {
  402. * die("Sorry, the code entered did not match.");
  403. * } else {
  404. * $valid = true;
  405. * }
  406. * </code>
  407. * @param string $code The code the user entered
  408. * @return boolean true if the code was correct, false if not
  409. */
  410. function check($code)
  411. {
  412. $this->code_entered = $code;
  413. $this->validate();
  414. return $this->correct_code;
  415. }
  416. /**
  417. * Generate and output the image
  418. *
  419. * @access private
  420. *
  421. */
  422. function doImage()
  423. {
  424. if($this->use_transparent_text == true || $this->bgimg != "") {
  425. $this->im = imagecreatetruecolor($this->image_width, $this->image_height);
  426. $bgcolor = imagecolorallocate($this->im, hexdec(substr($this->image_bg_color, 1, 2)), hexdec(substr($this->image_bg_color, 3, 2)), hexdec(substr($this->image_bg_color, 5, 2)));
  427. imagefilledrectangle($this->im, 0, 0, imagesx($this->im), imagesy($this->im), $bgcolor);
  428. } else { //no transparency
  429. $this->im = imagecreate($this->image_width, $this->image_height);
  430. $bgcolor = imagecolorallocate($this->im, hexdec(substr($this->image_bg_color, 1, 2)), hexdec(substr($this->image_bg_color, 3, 2)), hexdec(substr($this->image_bg_color, 5, 2)));
  431. }
  432. if($this->bgimg != "") { $this->setBackground(); }
  433. $this->createCode();
  434. if (!$this->draw_lines_over_text && $this->draw_lines) $this->drawLines();
  435. $this->drawWord();
  436. if ($this->arc_linethrough == true) $this->arcLines();
  437. if ($this->draw_lines_over_text && $this->draw_lines) $this->drawLines();
  438. $this->output();
  439. }
  440. /**
  441. * Set the background of the CAPTCHA image
  442. *
  443. * @access private
  444. *
  445. */
  446. function setBackground()
  447. {
  448. $dat = @getimagesize($this->bgimg);
  449. if($dat == false) { return; }
  450. switch($dat[2]) {
  451. case 1: $newim = @imagecreatefromgif($this->bgimg); break;
  452. case 2: $newim = @imagecreatefromjpeg($this->bgimg); break;
  453. case 3: $newim = @imagecreatefrompng($this->bgimg); break;
  454. case 15: $newim = @imagecreatefromwbmp($this->bgimg); break;
  455. case 16: $newim = @imagecreatefromxbm($this->bgimg); break;
  456. default: return;
  457. }
  458. if(!$newim) return;
  459. imagecopy($this->im, $newim, 0, 0, 0, 0, $this->image_width, $this->image_height);
  460. }
  461. /**
  462. * Draw arced lines over the text
  463. *
  464. * @access private
  465. *
  466. */
  467. function arcLines()
  468. {
  469. $colors = explode(',', $this->arc_line_colors);
  470. imagesetthickness($this->im, 3);
  471. $color = $colors[rand(0, sizeof($colors) - 1)];
  472. $linecolor = imagecolorallocate($this->im, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
  473. $xpos = $this->text_x_start + ($this->font_size * 2) + rand(-5, 5);
  474. $width = $this->image_width / 2.66 + rand(3, 10);
  475. $height = $this->font_size * 2.14 - rand(3, 10);
  476. if ( rand(0,100) % 2 == 0 ) {
  477. $start = rand(0,66);
  478. $ypos = $this->image_height / 2 - rand(5, 15);
  479. $xpos += rand(5, 15);
  480. } else {
  481. $start = rand(180, 246);
  482. $ypos = $this->image_height / 2 + rand(5, 15);
  483. }
  484. $end = $start + rand(75, 110);
  485. imagearc($this->im, $xpos, $ypos, $width, $height, $start, $end, $linecolor);
  486. $color = $colors[rand(0, sizeof($colors) - 1)];
  487. $linecolor = imagecolorallocate($this->im, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
  488. if ( rand(1,75) % 2 == 0 ) {
  489. $start = rand(45, 111);
  490. $ypos = $this->image_height / 2 - rand(5, 15);
  491. $xpos += rand(5, 15);
  492. } else {
  493. $start = rand(200, 250);
  494. $ypos = $this->image_height / 2 + rand(5, 15);
  495. }
  496. $end = $start + rand(75, 100);
  497. imagearc($this->im, $this->image_width * .75, $ypos, $width, $height, $start, $end, $linecolor);
  498. }
  499. /**
  500. * Draw lines on the image
  501. *
  502. * @access private
  503. *
  504. */
  505. function drawLines()
  506. {
  507. $linecolor = imagecolorallocate($this->im, hexdec(substr($this->line_color, 1, 2)), hexdec(substr($this->line_color, 3, 2)), hexdec(substr($this->line_color, 5, 2)));
  508. imagesetthickness($this->im, $this->line_thickness);
  509. //vertical lines
  510. for($x = 1; $x < $this->image_width; $x += $this->line_distance) {
  511. imageline($this->im, $x, 0, $x, $this->image_height, $linecolor);
  512. }
  513. //horizontal lines
  514. for($y = 11; $y < $this->image_height; $y += $this->line_distance) {
  515. imageline($this->im, 0, $y, $this->image_width, $y, $linecolor);
  516. }
  517. if ($this->draw_angled_lines == true) {
  518. for ($x = -($this->image_height); $x < $this->image_width; $x += $this->line_distance) {
  519. imageline($this->im, $x, 0, $x + $this->image_height, $this->image_height, $linecolor);
  520. }
  521. for ($x = $this->image_width + $this->image_height; $x > 0; $x -= $this->line_distance) {
  522. imageline($this->im, $x, 0, $x - $this->image_height, $this->image_height, $linecolor);
  523. }
  524. }
  525. }
  526. /**
  527. * Draw the CAPTCHA code over the image
  528. *
  529. * @access private
  530. *
  531. */
  532. function drawWord()
  533. {
  534. if ($this->use_gd_font == true) {
  535. if (!is_int($this->gd_font_file)) { //is a file name
  536. $font = @imageloadfont($this->gd_font_file);
  537. if ($font == false) {
  538. trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING);
  539. return;
  540. }
  541. } else { //gd font identifier
  542. $font = $this->gd_font_file;
  543. }
  544. $color = imagecolorallocate($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2)));
  545. imagestring($this->im, $font, $this->text_x_start, ($this->image_height / 2) - ($this->gd_font_size / 2), $this->code, $color);
  546. } else { //ttf font
  547. if($this->use_transparent_text == true) {
  548. $alpha = intval($this->text_transparency_percentage / 100 * 127);
  549. $font_color = imagecolorallocatealpha($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2)), $alpha);
  550. } else { //no transparency
  551. $font_color = imagecolorallocate($this->im, hexdec(substr($this->text_color, 1, 2)), hexdec(substr($this->text_color, 3, 2)), hexdec(substr($this->text_color, 5, 2)));
  552. }
  553. $x = $this->text_x_start;
  554. $strlen = strlen($this->code);
  555. $y_min = ($this->image_height / 2) + ($this->font_size / 2) - 2;
  556. $y_max = ($this->image_height / 2) + ($this->font_size / 2) + 2;
  557. $colors = explode(',', $this->multi_text_color);
  558. for($i = 0; $i < $strlen; ++$i) {
  559. $angle = rand($this->text_angle_minimum, $this->text_angle_maximum);
  560. $y = rand($y_min, $y_max);
  561. if ($this->use_multi_text == true) {
  562. $idx = rand(0, sizeof($colors) - 1);
  563. $r = substr($colors[$idx], 1, 2);
  564. $g = substr($colors[$idx], 3, 2);
  565. $b = substr($colors[$idx], 5, 2);
  566. if($this->use_transparent_text == true) {
  567. $font_color = imagecolorallocatealpha($this->im, "0x$r", "0x$g", "0x$b", $alpha);
  568. } else {
  569. $font_color = imagecolorallocate($this->im, "0x$r", "0x$g", "0x$b");
  570. }
  571. }
  572. imagettftext($this->im, $this->font_size, $angle, $x, $y, $font_color, $this->ttf_file, $this->code{$i});
  573. $x += rand($this->text_minimum_distance, $this->text_maximum_distance);
  574. } //for loop
  575. } //else ttf font
  576. } //function
  577. /**
  578. * Create a code and save to the session
  579. *
  580. * @since 1.0.1
  581. *
  582. */
  583. function createCode()
  584. {
  585. $this->code = false;
  586. if ($this->use_wordlist && is_readable($this->wordlist_file)) {
  587. $this->code = $this->readCodeFromFile();
  588. }
  589. if ($this->code == false) {
  590. $this->code = $this->generateCode($this->code_length);
  591. }
  592. $this->saveData();
  593. }
  594. /**
  595. * Generate a code
  596. *
  597. * @access private
  598. * @param int $len The code length
  599. * @return string
  600. */
  601. function generateCode($len)
  602. {
  603. $code = '';
  604. for($i = 1, $cslen = strlen($this->charset); $i <= $len; ++$i) {
  605. $code .= strtoupper( $this->charset{rand(0, $cslen - 1)} );
  606. }
  607. return $code;
  608. }
  609. /**
  610. * Reads a word list file to get a code
  611. *
  612. * @access private
  613. * @since 1.0.2
  614. * @return mixed false on failure, a word on success
  615. */
  616. function readCodeFromFile()
  617. {
  618. $fp = @fopen($this->wordlist_file, 'rb');
  619. if (!$fp) return false;
  620. $fsize = filesize($this->wordlist_file);
  621. if ($fsize < 32) return false; // too small of a list to be effective
  622. if ($fsize < 128) {
  623. $max = $fsize; // still pretty small but changes the range of seeking
  624. } else {
  625. $max = 128;
  626. }
  627. fseek($fp, rand(0, $fsize - $max), SEEK_SET);
  628. $data = fread($fp, 128); // read a random 128 bytes from file
  629. fclose($fp);
  630. $data = preg_replace("/\r?\n/", "\n", $data);
  631. $start = strpos($data, "\n", rand(0, 100)) + 1; // random start position
  632. $end = strpos($data, "\n", $start); // find end of word
  633. return strtolower(substr($data, $start, $end - $start)); // return substring in 128 bytes
  634. }
  635. /**
  636. * Output image to the browser
  637. *
  638. * @access private
  639. *
  640. */
  641. function output()
  642. {
  643. header("Expires: Sun, 1 Jan 2000 12:00:00 GMT");
  644. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
  645. header("Cache-Control: no-store, no-cache, must-revalidate");
  646. header("Cache-Control: post-check=0, pre-check=0", false);
  647. header("Pragma: no-cache");
  648. switch($this->image_type)
  649. {
  650. case SI_IMAGE_JPEG:
  651. header("Content-Type: image/jpeg");
  652. imagejpeg($this->im, null, 90);
  653. break;
  654. case SI_IMAGE_GIF:
  655. header("Content-Type: image/gif");
  656. imagegif($this->im);
  657. break;
  658. default:
  659. header("Content-Type: image/png");
  660. imagepng($this->im);
  661. break;
  662. }
  663. imagedestroy($this->im);
  664. }
  665. /**
  666. * Get WAV file data of the spoken code.<br />
  667. * This is appropriate for output to the browser as audio/x-wav
  668. *
  669. * @since 1.0.1
  670. * @return string WAV data
  671. *
  672. */
  673. function getAudibleCode()
  674. {
  675. $letters = array();
  676. $code = $this->getCode();
  677. if ($code == '') {
  678. $this->createCode();
  679. $code = $this->getCode();
  680. }
  681. for($i = 0; $i < strlen($code); ++$i) {
  682. $letters[] = $code{$i};
  683. }
  684. return $this->generateWAV($letters);
  685. }
  686. /**
  687. * Save the code in the session
  688. *
  689. * @access private
  690. *
  691. */
  692. function saveData()
  693. {
  694. $_SESSION['securimage_code_value'] = strtolower($this->code);
  695. }
  696. /**
  697. * Validate the code to the user code
  698. *
  699. * @access private
  700. *
  701. */
  702. function validate()
  703. {
  704. if ( isset($_SESSION['securimage_code_value']) && !empty($_SESSION['securimage_code_value']) ) {
  705. if ( $_SESSION['securimage_code_value'] == strtolower(trim($this->code_entered)) ) {
  706. $this->correct_code = true;
  707. $_SESSION['securimage_code_value'] = '';
  708. } else {
  709. $this->correct_code = false;
  710. }
  711. } else {
  712. $this->correct_code = false;
  713. }
  714. }
  715. /**
  716. * Get the captcha code
  717. *
  718. * @since 1.0.1
  719. * @return string
  720. */
  721. function getCode()
  722. {
  723. if (isset($_SESSION['securimage_code_value']) && !empty($_SESSION['securimage_code_value'])) {
  724. return $_SESSION['securimage_code_value'];
  725. } else {
  726. return '';
  727. }
  728. }
  729. /**
  730. * Check if the user entered code was correct
  731. *
  732. * @access private
  733. * @return boolean
  734. */
  735. function checkCode()
  736. {
  737. return $this->correct_code;
  738. }
  739. /**
  740. * Generate a wav file by concatenating individual files
  741. * @since 1.0.1
  742. * @access private
  743. * @param array $letters Array of letters to build a file from
  744. * @return string WAV file data
  745. */
  746. function generateWAV($letters)
  747. {
  748. $first = true; // use first file to write the header...
  749. $data_len = 0;
  750. $files = array();
  751. $out_data = '';
  752. foreach ($letters as $letter) {
  753. $filename = $this->audio_path . strtoupper($letter) . '.wav';
  754. $fp = fopen($filename, 'rb');
  755. $file = array();
  756. $data = fread($fp, filesize($filename)); // read file in
  757. $header = substr($data, 0, 36);
  758. $body = substr($data, 44);
  759. $data = unpack('NChunkID/VChunkSize/NFormat/NSubChunk1ID/VSubChunk1Size/vAudioFormat/vNumChannels/VSampleRate/VByteRate/vBlockAlign/vBitsPerSample', $header);
  760. $file['sub_chunk1_id'] = $data['SubChunk1ID'];
  761. $file['bits_per_sample'] = $data['BitsPerSample'];
  762. $file['channels'] = $data['NumChannels'];
  763. $file['format'] = $data['AudioFormat'];
  764. $file['sample_rate'] = $data['SampleRate'];
  765. $file['size'] = $data['ChunkSize'] + 8;
  766. $file['data'] = $body;
  767. if ( ($p = strpos($file['data'], 'LIST')) !== false) {
  768. // If the LIST data is not at the end of the file, this will probably break your sound file
  769. $info = substr($file['data'], $p + 4, 8);
  770. $data = unpack('Vlength/Vjunk', $info);
  771. $file['data'] = substr($file['data'], 0, $p);
  772. $file['size'] = $file['size'] - (strlen($file['data']) - $p);
  773. }
  774. $files[] = $file;
  775. $data = null;
  776. $header = null;
  777. $body = null;
  778. $data_len += strlen($file['data']);
  779. fclose($fp);
  780. }
  781. $out_data = '';
  782. for($i = 0; $i < sizeof($files); ++$i) {
  783. if ($i == 0) { // output header
  784. $out_data .= pack('C4VC8', ord('R'), ord('I'), ord('F'), ord('F'), $data_len + 36, ord('W'), ord('A'), ord('V'), ord('E'), ord('f'), ord('m'), ord('t'), ord(' '));
  785. $out_data .= pack('VvvVVvv',
  786. 16,
  787. $files[$i]['format'],
  788. $files[$i]['channels'],
  789. $files[$i]['sample_rate'],
  790. $files[$i]['sample_rate'] * (($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8),
  791. ($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8,
  792. $files[$i]['bits_per_sample'] );
  793. $out_data .= pack('C4', ord('d'), ord('a'), ord('t'), ord('a'));
  794. $out_data .= pack('V', $data_len);
  795. }
  796. $out_data .= $files[$i]['data'];
  797. }
  798. return $out_data;
  799. }
  800. } /* class Securimage */
  801. ?>