Formulaire avec captcha qui ne fonctionne pas...

Formulaire avec captcha qui ne fonctionne pas... - PHP - Programmation

Marsh Posté le 21-06-2008 à 09:57:30    

Bonjour,
 
J'essaie de faire fonctionner le formulaire suivant :
 

Code :
  1. ******************au début de la page****************
  2. <?php
  3.   session_start();
  4. ?>
  5. *************************************************
  6. <script type="text/javascript" language="Javascript" >
  7.           <!--
  8.           function verification()
  9.           {
  10.            if(document.formulaire.nom.value == "" )  {
  11.              alert("Veuillez entrer votre nom svp" );
  12.              document.formulaire.nom.focus();
  13.              return false;
  14.             }
  15.              else
  16.            if(document.formulaire.prenom.value == "" ) {
  17.              alert("Veuillez entrer votre prenom svp" );
  18.              document.formulaire.prenom.focus();
  19.              return false;
  20.             }
  21.              else
  22.            if(document.formulaire.email.value == "" ) {
  23.              alert("Veuillez entrer votre adresse electronique svp" );
  24.              document.formulaire.email.focus();
  25.              return false;
  26.             }
  27.             else
  28.            if(document.formulaire.email.value.indexOf('@') == -1) {
  29.              alert("Ce n'est pas une adresse electronique, verifiez la svp" );
  30.              document.formulaire.email.focus();
  31.              return false;
  32.             }
  33.             else
  34.            if(document.formulaire.texte.value == "" ) {
  35.              alert("Veuillez entrer le texte de votre message svp" );
  36.              document.formulaire.texte.focus();
  37.              return false;
  38.             }
  39.             else
  40.            if(document.formulaire.code.value == "" ) {
  41.              alert("Veuillez saisir le code de sécurité" );
  42.              document.formulaire.texte.focus();
  43.              return false;
  44.             }
  45.           return true
  46.           }
  47.           //-->
  48.           </script>
  49.           <?php
  50.           if (empty($_POST)) { ?>
  51.                               <form name='formulaire' action='traitement.php' method='POST' onSubmit="return verification()">
  52.                                 <table cellspacing="0">
  53.                                   <tr>
  54.                                     <td >Votre nom : </td>
  55.                                     <td ><input name='nom' type='text' size='40' class="writein"></td>
  56.                                   </tr>
  57.                                   <tr>
  58.                                     <td >Votre pr&eacute;nom : </td>
  59.                                     <td ><input name='prenom' type='text' size='40' class="writein"></td>
  60.                                   </tr>
  61.                                   <tr>
  62.                                     <td >Votre email :</td>
  63.                                     <td ><input name='email' type='text' size='40' class="writein"></td>
  64.                                   </tr>
  65.                                   <tr>
  66.                                     <td >Votre message</td>
  67.                                     <td><textarea name='texte' cols='40' rows='10' class="writein"></textarea></td>
  68.                                   </tr>
  69.          <tr><td></td>
  70.          <td>
  71.          <!-- pass a session id to the query string of the script to prevent ie caching -->
  72. <img src="securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>">
  73.          </td></tr>
  74.          <tr><td>Recopier le code</td>
  75.          <td><input type="text" name="code" /></td></tr>
  76.                                   <tr >
  77.                                     <td ><input name='annuler' type='reset' value='Annuler'></td>
  78.                                     <td> <input name='soumettre' type='submit' value='Envoyer'></td>
  79.                                   </tr>
  80.                                 </table>
  81. Tous les champs du formulaire <strong>doivent</strong> &ecirc;tre remplis.
  82. </form>
  83. <?php
  84. } else { //form is posted
  85.   include("securimage.php" );
  86.   $img = new Securimage();
  87.   $valid = $img->check($_POST['code']);
  88.   if($valid == true) {
  89.     echo "<center>Thanks, you entered the correct code.</center>";
  90.   } else {
  91.     echo "<center>Sorry, the code you entered was invalid.  <a href=\"javascript:history.go(-1)\">Go back</a> to try again.</center>";
  92.   }
  93. }
  94. ?>


Les tests avec javascript sont bien exécutés mais le code du captcha n'est pas vérifié par le script.
 
Avez vous une solution ?
Merci

Reply

Marsh Posté le 21-06-2008 à 09:57:30   

Reply

Marsh Posté le 21-06-2008 à 10:32:16    

Comment veux-tu qu'on puisse t'aider si on sait pas ce que fait la classe Securimage ?

Reply

Marsh Posté le 21-06-2008 à 16:41:37    

Voici le fichier securimage.php attention, c'est long...
 

Code :
  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  = 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 = "./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.     if ( session_id() == '' ) { // no session has been started yet, which is needed for validation
  371.       session_start();
  372.     }
  373.   }
  374.   /**
  375.    * Generate a code and output the image to the browser.
  376.    *
  377.    * <code>
  378.    *   <?php
  379.    *   include 'securimage.php';
  380.    *   $securimage = new Securimage();
  381.    *   $securimage->show('bg.jpg');
  382.    *   ?>
  383.    * </code>
  384.    *
  385.    * @param string $background_image  The path to an image to use as the background for the CAPTCHA
  386.    */
  387.   function show($background_image = "" )
  388.   {
  389.     if($background_image != "" && is_readable($background_image)) {
  390.       $this->bgimg = $background_image;
  391.     }
  392.     $this->doImage();
  393.   }
  394.   /**
  395.    * Validate the code entered by the user.
  396.    *
  397.    * <code>
  398.    *   $code = $_POST['code'];
  399.    *   if ($securimage->check($code) == false) {
  400.    *     die("Sorry, the code entered did not match." );
  401.    *   } else {
  402.    *     $valid = true;
  403.    *   }
  404.    * </code>
  405.    * @param string $code  The code the user entered
  406.    * @return boolean  true if the code was correct, false if not
  407.    */
  408.   function check($code)
  409.   {
  410.     $this->code_entered = $code;
  411.     $this->validate();
  412.     return $this->correct_code;
  413.   }
  414.   /**
  415.    * Generate and output the image
  416.    *
  417.    * @access private
  418.    *
  419.    */
  420.   function doImage()
  421.   {
  422.     if($this->use_transparent_text == true || $this->bgimg != "" ) {
  423.       $this->im = imagecreatetruecolor($this->image_width, $this->image_height);
  424.       $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)));
  425.       imagefilledrectangle($this->im, 0, 0, imagesx($this->im), imagesy($this->im), $bgcolor);
  426.     } else { //no transparency
  427.       $this->im = imagecreate($this->image_width, $this->image_height);
  428.       $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)));
  429.     }
  430.     if($this->bgimg != "" ) { $this->setBackground(); }
  431.     $this->createCode();
  432.     if (!$this->draw_lines_over_text && $this->draw_lines) $this->drawLines();
  433.     $this->drawWord();
  434.     if ($this->arc_linethrough == true) $this->arcLines();
  435.     if ($this->draw_lines_over_text && $this->draw_lines) $this->drawLines();
  436.     $this->output();
  437.   }
  438.   /**
  439.    * Set the background of the CAPTCHA image
  440.    *
  441.    * @access private
  442.    *
  443.    */
  444.   function setBackground()
  445.   {
  446.     $dat = @getimagesize($this->bgimg);
  447.     if($dat == false) { return; }
  448.     switch($dat[2]) {
  449.       case 1:  $newim = @imagecreatefromgif($this->bgimg); break;
  450.       case 2:  $newim = @imagecreatefromjpeg($this->bgimg); break;
  451.       case 3:  $newim = @imagecreatefrompng($this->bgimg); break;
  452.       case 15: $newim = @imagecreatefromwbmp($this->bgimg); break;
  453.       case 16: $newim = @imagecreatefromxbm($this->bgimg); break;
  454.       default: return;
  455.     }
  456.     if(!$newim) return;
  457.     imagecopy($this->im, $newim, 0, 0, 0, 0, $this->image_width, $this->image_height);
  458.   }
  459.   /**
  460.    * Draw arced lines over the text
  461.    *
  462.    * @access private
  463.    *
  464.    */
  465.   function arcLines()
  466.   {
  467.     $colors = explode(',', $this->arc_line_colors);
  468.     imagesetthickness($this->im, 3);
  469.     $color = $colors[rand(0, sizeof($colors) - 1)];
  470.     $linecolor = imagecolorallocate($this->im, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
  471.     $xpos   = $this->text_x_start + ($this->font_size * 2) + rand(-5, 5);
  472.     $width  = $this->image_width / 2.66 + rand(3, 10);
  473.     $height = $this->font_size * 2.14 - rand(3, 10);
  474.     if ( rand(0,100) % 2 == 0 ) {
  475.       $start = rand(0,66);
  476.       $ypos  = $this->image_height / 2 - rand(5, 15);
  477.       $xpos += rand(5, 15);
  478.     } else {
  479.       $start = rand(180, 246);
  480.       $ypos  = $this->image_height / 2 + rand(5, 15);
  481.     }
  482.     $end = $start + rand(75, 110);
  483.     imagearc($this->im, $xpos, $ypos, $width, $height, $start, $end, $linecolor);
  484.     $color = $colors[rand(0, sizeof($colors) - 1)];
  485.     $linecolor = imagecolorallocate($this->im, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
  486.     if ( rand(1,75) % 2 == 0 ) {
  487.       $start = rand(45, 111);
  488.       $ypos  = $this->image_height / 2 - rand(5, 15);
  489.       $xpos += rand(5, 15);
  490.     } else {
  491.       $start = rand(200, 250);
  492.       $ypos  = $this->image_height / 2 + rand(5, 15);
  493.     }
  494.     $end = $start + rand(75, 100);
  495.     imagearc($this->im, $this->image_width * .75, $ypos, $width, $height, $start, $end, $linecolor);
  496.   }
  497.   /**
  498.    * Draw lines on the image
  499.    *
  500.    * @access private
  501.    *
  502.    */
  503.   function drawLines()
  504.   {
  505.     $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)));
  506.     imagesetthickness($this->im, $this->line_thickness);
  507.     //vertical lines
  508.     for($x = 1; $x < $this->image_width; $x += $this->line_distance) {
  509.       imageline($this->im, $x, 0, $x, $this->image_height, $linecolor);
  510.     }
  511.     //horizontal lines
  512.     for($y = 11; $y < $this->image_height; $y += $this->line_distance) {
  513.       imageline($this->im, 0, $y, $this->image_width, $y, $linecolor);
  514.     }
  515.     if ($this->draw_angled_lines == true) {
  516.       for ($x = -($this->image_height); $x < $this->image_width; $x += $this->line_distance) {
  517.         imageline($this->im, $x, 0, $x + $this->image_height, $this->image_height, $linecolor);
  518.       }
  519.       for ($x = $this->image_width + $this->image_height; $x > 0; $x -= $this->line_distance) {
  520.         imageline($this->im, $x, 0, $x - $this->image_height, $this->image_height, $linecolor);
  521.       }
  522.     }
  523.   }
  524.   /**
  525.    * Draw the CAPTCHA code over the image
  526.    *
  527.    * @access private
  528.    *
  529.    */
  530.   function drawWord()
  531.   {
  532.     if ($this->use_gd_font == true) {
  533.       if (!is_int($this->gd_font_file)) { //is a file name
  534.         $font = @imageloadfont($this->gd_font_file);
  535.         if ($font == false) {
  536.           trigger_error("Failed to load GD Font file {$this->gd_font_file} ", E_USER_WARNING);
  537.           return;
  538.         }
  539.       } else { //gd font identifier
  540.         $font = $this->gd_font_file;
  541.       }
  542.       $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)));
  543.       imagestring($this->im, $font, $this->text_x_start, ($this->image_height / 2) - ($this->gd_font_size / 2), $this->code, $color);
  544.     } else { //ttf font
  545.       if($this->use_transparent_text == true) {
  546.         $alpha = intval($this->text_transparency_percentage / 100 * 127);
  547.         $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);
  548.       } else { //no transparency
  549.         $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)));
  550.       }
  551.       $x = $this->text_x_start;
  552.       $strlen = strlen($this->code);
  553.       $y_min = ($this->image_height / 2) + ($this->font_size / 2) - 2;
  554.       $y_max = ($this->image_height / 2) + ($this->font_size / 2) + 2;
  555.       $colors = explode(',', $this->multi_text_color);
  556.       for($i = 0; $i < $strlen; ++$i) {
  557.         $angle = rand($this->text_angle_minimum, $this->text_angle_maximum);
  558.         $y = rand($y_min, $y_max);
  559.         if ($this->use_multi_text == true) {
  560.           $idx = rand(0, sizeof($colors) - 1);
  561.           $r = substr($colors[$idx], 1, 2);
  562.           $g = substr($colors[$idx], 3, 2);
  563.           $b = substr($colors[$idx], 5, 2);
  564.           if($this->use_transparent_text == true) {
  565.             $font_color = imagecolorallocatealpha($this->im, "0x$r", "0x$g", "0x$b", $alpha);
  566.           } else {
  567.             $font_color = imagecolorallocate($this->im, "0x$r", "0x$g", "0x$b" );
  568.           }
  569.         }
  570.         @imagettftext($this->im, $this->font_size, $angle, $x, $y, $font_color, $this->ttf_file, $this->code{$i});
  571.         $x += rand($this->text_minimum_distance, $this->text_maximum_distance);
  572.       } //for loop
  573.     } //else ttf font
  574.   } //function
  575.   /**
  576.    * Create a code and save to the session
  577.    *
  578.    * @since 1.0.1
  579.    *
  580.    */
  581.   function createCode()
  582.   {
  583.     $this->code = false;
  584.     if ($this->use_wordlist && is_readable($this->wordlist_file)) {
  585.       $this->code = $this->readCodeFromFile();
  586.     }
  587.     if ($this->code == false) {
  588.       $this->code = $this->generateCode($this->code_length);
  589.     }
  590.     $this->saveData();
  591.   }
  592.   /**
  593.    * Generate a code
  594.    *
  595.    * @access private
  596.    * @param int $len  The code length
  597.    * @return string
  598.    */
  599.   function generateCode($len)
  600.   {
  601.     $code = '';
  602.     for($i = 1, $cslen = strlen($this->charset); $i <= $len; ++$i) {
  603.       $code .= strtoupper( $this->charset{rand(0, $cslen - 1)} );
  604.     }
  605.     return $code;
  606.   }
  607.   /**
  608.    * Reads a word list file to get a code
  609.    *
  610.    * @access private
  611.    * @since 1.0.2
  612.    * @return mixed  false on failure, a word on success
  613.    */
  614.   function readCodeFromFile()
  615.   {
  616.     $fp = @fopen($this->wordlist_file, 'rb');
  617.     if (!$fp) return false;
  618.     $fsize = filesize($this->wordlist_file);
  619.     if ($fsize < 32) return false; // too small of a list to be effective
  620.     if ($fsize < 128) {
  621.       $max = $fsize; // still pretty small but changes the range of seeking
  622.     } else {
  623.       $max = 128;
  624.     }
  625.     fseek($fp, rand(0, $fsize - $max), SEEK_SET);
  626.     $data = fread($fp, 128); // read a random 128 bytes from file
  627.     fclose($fp);
  628.     $data = preg_replace("/\r?\n/", "\n", $data);
  629.     $start = strpos($data, "\n", rand(0, 100)) + 1; // random start position
  630.     $end   = strpos($data, "\n", $start);           // find end of word
  631.     return strtolower(substr($data, $start, $end - $start)); // return substring in 128 bytes
  632.   }
  633.   /**
  634.    * Output image to the browser
  635.    *
  636.    * @access private
  637.    *
  638.    */
  639.   function output()
  640.   {
  641.     header("Expires: Sun, 1 Jan 2000 12:00:00 GMT" );
  642.     header("Last-Modified: " . gmdate("D, d M Y H:i:s" ) . "GMT" );
  643.     header("Cache-Control: no-store, no-cache, must-revalidate" );
  644.     header("Cache-Control: post-check=0, pre-check=0", false);
  645.     header("Pragma: no-cache" );
  646.     switch($this->image_type)
  647.     {
  648.       case SI_IMAGE_JPEG:
  649.         header("Content-Type: image/jpeg" );
  650.         imagejpeg($this->im, null, 90);
  651.         break;
  652.       case SI_IMAGE_GIF:
  653.         header("Content-Type: image/gif" );
  654.         imagegif($this->im);
  655.         break;
  656.       default:
  657.         header("Content-Type: image/png" );
  658.         imagepng($this->im);
  659.         break;
  660.     }
  661.     imagedestroy($this->im);
  662.   }
  663.   /**
  664.    * Get WAV file data of the spoken code.<br />
  665.    * This is appropriate for output to the browser as audio/x-wav
  666.    *
  667.    * @since 1.0.1
  668.    * @return string  WAV data
  669.    *
  670.    */
  671.   function getAudibleCode()
  672.   {
  673.     $letters = array();
  674.     $code    = $this->getCode();
  675.     if ($code == '') {
  676.       $this->createCode();
  677.       $code = $this->getCode();
  678.     }
  679.     for($i = 0; $i < strlen($code); ++$i) {
  680.       $letters[] = $code{$i};
  681.     }
  682.     return $this->generateWAV($letters);
  683.   }
  684.   /**
  685.    * Save the code in the session
  686.    *
  687.    * @access private
  688.    *
  689.    */
  690.   function saveData()
  691.   {
  692.     $_SESSION['securimage_code_value'] = strtolower($this->code);
  693.   }
  694.   /**
  695.    * Validate the code to the user code
  696.    *
  697.    * @access private
  698.    *
  699.    */
  700.   function validate()
  701.   {
  702.     if ( isset($_SESSION['securimage_code_value']) && !empty($_SESSION['securimage_code_value']) ) {
  703.       if ( $_SESSION['securimage_code_value'] == strtolower(trim($this->code_entered)) ) {
  704.         $this->correct_code = true;
  705.         $_SESSION['securimage_code_value'] = '';
  706.       } else {
  707.         $this->correct_code = false;
  708.       }
  709.     } else {
  710.       $this->correct_code = false;
  711.     }
  712.   }
  713.   /**
  714.    * Get the captcha code
  715.    *
  716.    * @since 1.0.1
  717.    * @return string
  718.    */
  719.   function getCode()
  720.   {
  721.     if (isset($_SESSION['securimage_code_value']) && !empty($_SESSION['securimage_code_value'])) {
  722.       return $_SESSION['securimage_code_value'];
  723.     } else {
  724.       return '';
  725.     }
  726.   }
  727.   /**
  728.    * Check if the user entered code was correct
  729.    *
  730.    * @access private
  731.    * @return boolean
  732.    */
  733.   function checkCode()
  734.   {
  735.     return $this->correct_code;
  736.   }
  737.   /**
  738.    * Generate a wav file by concatenating individual files
  739.    * @since 1.0.1
  740.    * @access private
  741.    * @param array $letters  Array of letters to build a file from
  742.    * @return string  WAV file data
  743.    */
  744.   function generateWAV($letters)
  745.   {
  746.     $first = true; // use first file to write the header...
  747.     $data_len    = 0;
  748.     $files       = array();
  749.     $out_data    = '';
  750.     foreach ($letters as $letter) {
  751.       $filename = $this->audio_path . strtoupper($letter) . '.wav';
  752.       $fp = fopen($filename, 'rb');
  753.       $file = array();
  754.       $data = fread($fp, filesize($filename)); // read file in
  755.       $header = substr($data, 0, 36);
  756.       $body   = substr($data, 44);
  757.       $data = unpack('NChunkID/VChunkSize/NFormat/NSubChunk1ID/VSubChunk1Size/vAudioFormat/vNumChannels/VSampleRate/VByteRate/vBlockAlign/vBitsPerSample', $header);
  758.       $file['sub_chunk1_id']   = $data['SubChunk1ID'];
  759.       $file['bits_per_sample'] = $data['BitsPerSample'];
  760.       $file['channels']        = $data['NumChannels'];
  761.       $file['format']          = $data['AudioFormat'];
  762.       $file['sample_rate']     = $data['SampleRate'];
  763.       $file['size']            = $data['ChunkSize'] + 8;
  764.       $file['data']            = $body;
  765.       if ( ($p = strpos($file['data'], 'LIST')) !== false) {
  766.         // If the LIST data is not at the end of the file, this will probably break your sound file
  767.         $info         = substr($file['data'], $p + 4, 8);
  768.         $data         = unpack('Vlength/Vjunk', $info);
  769.         $file['data'] = substr($file['data'], 0, $p);
  770.         $file['size'] = $file['size'] - (strlen($file['data']) - $p);
  771.       }
  772.       $files[] = $file;
  773.       $data    = null;
  774.       $header  = null;
  775.       $body    = null;
  776.       $data_len += strlen($file['data']);
  777.       fclose($fp);
  778.     }
  779.     $out_data = '';
  780.     for($i = 0; $i < sizeof($files); ++$i) {
  781.       if ($i == 0) { // output header
  782.         $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(' '));
  783.         $out_data .= pack('VvvVVvv',
  784.                           16,
  785.                           $files[$i]['format'],
  786.                           $files[$i]['channels'],
  787.                           $files[$i]['sample_rate'],
  788.                           $files[$i]['sample_rate'] * (($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8),
  789.                           ($files[$i]['bits_per_sample'] * $files[$i]['channels']) / 8,
  790.                           $files[$i]['bits_per_sample'] );
  791.         $out_data .= pack('C4', ord('d'), ord('a'), ord('t'), ord('a'));
  792.         $out_data .= pack('V', $data_len);
  793.       }
  794.       $out_data .= $files[$i]['data'];
  795.     }
  796.     return $out_data;
  797.   }
  798. } /* class Securimage */
  799. ?>


Voilà si vous avez une idée, c'est sympa

Reply

Marsh Posté le 21-06-2008 à 19:15:13    

Attend attend ... c'est pas toi l'auteur du code ... Désolé on fait pas le support de codes pompés à droite et a gauche

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed