PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/WeBid/trunk/includes/captcha/securimage.php

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