Advertisement
Guest User

Untitled

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