Advertisement
Guest User

Un Captcha accessible pour tous en PHP

a guest
Jan 20th, 2018
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. Original Link :
  2. Un Captcha accessible pour tous en PHP : http://www.c-p-f.org/php-Un_Captcha_accessible_pour_tous-a35.html
  3. ##########################################################################################################
  4.  
  5. <?
  6. /**
  7. * Classe de génération de test de Turing
  8. * Cette classe permet de générer un test de Turing (ou Captcha)
  9. * accessible à tous
  10. * @author CrazyCat <crazycat@c-p-f.org>
  11. * @copyright 20071022
  12. * @version 1.0
  13. * @package Captcha
  14. *
  15. * @TODO Traduction des différents éléments pour avoir les 5 langues
  16. */
  17.  
  18. class Turing {
  19.  
  20. /**
  21. * Tableau des chiffres en littéral (0 -> 50)
  22. * @var mixed
  23. */
  24. var $arrNumbers;
  25.  
  26.  
  27. /**
  28. * Tableau des positions en lettres
  29. * @var mixed
  30. */
  31. var $arrPos;
  32.  
  33. /**
  34. * Tableau des phrases type
  35. * @var mixed
  36. */
  37. var $strSentences;
  38.  
  39. /**
  40. * Type de réponses attendures
  41. * @var mixed
  42. */
  43. var $strTypes;
  44.  
  45. /**
  46. * Langue utilisée par le test de Turing
  47. * @var string
  48. */
  49. var $lang = 'fr';
  50.  
  51.  
  52. /**
  53. * Valeur maximale d'une opération
  54. * @var int
  55. */
  56. var $MAX = 50;
  57.  
  58. /**
  59. * Tableau question / réponse
  60. * @var mixed
  61. */
  62. var $arrTest;
  63.  
  64. /**
  65. * Option de debug
  66. * @var boolean
  67. */
  68. var $Debug = false;
  69.  
  70. /**
  71. * Constructeur PHP5
  72. * @param string $lang
  73. */
  74. function __construct($lang='fr', $Debug=false) {
  75. $this->lang = (string) $lang;
  76. switch($lang) {
  77. case 'en':
  78. break;
  79. case 'de':
  80. break;
  81. case 'it':
  82. break;
  83. case 'es':
  84. break;
  85. default:
  86. $this->arrNumbers = array('zéro', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'neuf', 'dix', 'onze', 'douze', 'treize', 'quatorze', 'quinze', 'seize', 'dix-sept', 'dix-huit', 'dix-neuf', 'vingt', 'vingt et un', 'vingt-deux', 'vingt-trois', 'vingt-quatre', 'vingt-cinq', 'vingt-six', 'vingt-sept', 'vingt-huit', 'vingt-neuf', 'trente', 'trente et un', 'trente-deux', 'trente-trois', 'trente-quatre', 'trente-cinq', 'trente-six', 'trente-sept', 'trente-huit', 'trente-neuf', 'quarante', 'quarante et un', 'quarante-deux', 'quarante-trois', 'quarante-quatre', 'quarante-cinq', 'quarante-six', 'quarante-sept', 'quarante-huit', 'quarante-neuf', 'cinquante');
  87. $this->arrPos = array('premier', 'second', 'troisième', 'quatrième', 'cinquième', 'sixième', 'septième', 'huitième', 'neuvième', 'dixième', 'onzième', 'douzième', 'treizième', 'quatorzième', 'quinzième', 'seizième', 'dernier');
  88. $this->strSentences = array(
  89. 'Quel est le %s caractère de %s ? (%s)',
  90. 'Dans %s, quel caractère vient après %s ? (%s)',
  91. 'Combien font %s et %s ? (%s)',
  92. 'Combien font %s moins %s ? (%s)',
  93. 'Combien font %s fois %s ? (%s)'
  94. );
  95. $this->strTypes = array('lettre', 'chiffre', 'nombre');
  96. }
  97. $choice = rand(0,6);
  98. switch($choice) {
  99. case 1: $this->mot(0, true); break;
  100. case 2: $this->addition(); break;
  101. case 3: $this->soustraction(); break;
  102. case 4: $this->multiplication(); break;
  103. case 5: $this->mot(1, true); break;
  104. case 6: $this->mot(1, false); break;
  105. default: $this->mot(0, false); break;
  106. }
  107. $this->genHtml($Debug);
  108. }
  109.  
  110. /**
  111. * Constructeur PHP4
  112. * @param string $lang
  113. */
  114. function Turing($lang='fr',$Debug=false) {
  115. $this->__construct($lang, $Debug);
  116. }
  117.  
  118. /**
  119. * Renvoit la valeur littérale d'un nombre
  120. * @param int $i
  121. * @return string
  122. */
  123. function numbers($i) {
  124. if (intval($i)>0) {
  125. return $this->arrNumbers[$i];
  126. }
  127. }
  128. /**
  129. * Génère une question sur la position d'un caractère
  130. * @param integer $strType Jeu de caractères à utiliser
  131. * @param boolean $unique Chaine à caractères uniques ou non
  132. */
  133. function mot($strType, $unique=false) {
  134. if ($strType==1) $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  135. else $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
  136. $arrChars = array();
  137. for ($i=0;$i<50;$i++) {
  138. $arrChars[] = $chars[rand(0, count($chars)-1)];
  139. }
  140. if ($unique===true) {
  141. $tmpChars = array_values(array_unique($arrChars));
  142. $arrChars = $tmpChars;
  143. }
  144. $strTuring = implode("", $arrChars);
  145. if (strlen($strTuring)>17) $strTuring = substr($strTuring, 0, rand(8,17));
  146. if ($unique === true) {
  147. $chrSelected = rand(1,strlen($strTuring)-1);
  148. $this->arrTest = array(sprintf($this->strSentences[1], $strTuring, $arrChars[($chrSelected - 1)], $this->strTypes[$strType]), $arrChars[$chrSelected]);
  149. } else {
  150. $chrSelected = rand(0,strlen($strTuring) -1 );
  151. if ($chrSelected == (strlen($strTuring) -1)) {
  152. $this->arrTest = array(sprintf($this->strSentences[0], $this->arrPos[16], $strTuring, $this->strTypes[$strType]), $arrChars[$chrSelected]);
  153. } else {
  154. $this->arrTest = array(sprintf($this->strSentences[0], $this->arrPos[$chrSelected], $strTuring, $this->strTypes[$strType]), $arrChars[$chrSelected]);
  155. }
  156. }
  157. }
  158.  
  159. /**
  160. * Génère une addition
  161. */
  162. function addition() {
  163. while (($c =($a = rand(1, $this->MAX)) + ($b = rand(1, $this->MAX))) > $this->MAX );
  164. $this->arrTest = array(sprintf($this->strSentences[2], $this->numbers($a), $this->numbers($b), $this->strTypes[2]), $c);
  165. }
  166.  
  167. /**
  168. * Génère une soustraction
  169. */
  170. function soustraction() {
  171. while ( ( $c =($a = rand(1, $this->MAX)) - ($b = rand(1, $this->MAX))) < 1 );
  172. $this->arrTest = array(sprintf($this->strSentences[3], $this->numbers($a), $this->numbers($b), $this->strTypes[2]), $c);
  173. }
  174.  
  175. /**
  176. * Génère une multiplication
  177. */
  178. function multiplication() {
  179. while ( ( $c =($a = rand(1, $this->MAX)) * ($b = rand(1, $this->MAX))) > $this->MAX );
  180. $this->arrTest = array(sprintf($this->strSentences[4], $this->numbers($a), $this->numbers($b), $this->strTypes[2]), $c);
  181. }
  182.  
  183. /**
  184. * Génération du formulaire html
  185. * Affiche 3 éléments de formulaire:
  186. * <label> est la question
  187. * <input turing> est le champ de réponse
  188. * <input tutingS> est la valeur codée (MD5) de la réponse
  189. * @return string Eléments du formulaire
  190. */
  191. function genHtml($Debug=false) {
  192. if ($Debug===true) var_dump($this->arrTest);
  193. $strForm = '<label for="turing">'.$this->arrTest[0].'</label><input type="text" name="turing" id="turing" value="" /><input type="hidden" name="turingS" id="turingS" value="'.md5($this->arrTest[1]).'" />';
  194. echo $strForm;
  195. }
  196. }
  197. ?>
  198.  
  199. Utilisation
  200.  
  201. La classe affiche elle-même le formulaire, il suffit donc de l'instancier.
  202. <?$test = &new Turing();?>
  203.  
  204. Les éléments de formulaire seront:
  205. <label for="turing">Combien font trente-neuf et sept ? (nombre)</label>
  206. <input name="turing" id="turing" value="" type="text" />
  207. <input name="turingS" id="turingS" value="d9d4f495e875a2e075a1a4a6e1b9770f" type="hidden" />
  208.  
  209. Lors du traitement du formulaire, il suffit de vérifier que md5($_POST['turing']) == $_POST['turingS'];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement