Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Original Link :
- Un Captcha accessible pour tous en PHP : http://www.c-p-f.org/php-Un_Captcha_accessible_pour_tous-a35.html
- ##########################################################################################################
- <?
- /**
- * Classe de génération de test de Turing
- * Cette classe permet de générer un test de Turing (ou Captcha)
- * accessible à tous
- * @author CrazyCat <crazycat@c-p-f.org>
- * @copyright 20071022
- * @version 1.0
- * @package Captcha
- *
- * @TODO Traduction des différents éléments pour avoir les 5 langues
- */
- class Turing {
- /**
- * Tableau des chiffres en littéral (0 -> 50)
- * @var mixed
- */
- var $arrNumbers;
- /**
- * Tableau des positions en lettres
- * @var mixed
- */
- var $arrPos;
- /**
- * Tableau des phrases type
- * @var mixed
- */
- var $strSentences;
- /**
- * Type de réponses attendures
- * @var mixed
- */
- var $strTypes;
- /**
- * Langue utilisée par le test de Turing
- * @var string
- */
- var $lang = 'fr';
- /**
- * Valeur maximale d'une opération
- * @var int
- */
- var $MAX = 50;
- /**
- * Tableau question / réponse
- * @var mixed
- */
- var $arrTest;
- /**
- * Option de debug
- * @var boolean
- */
- var $Debug = false;
- /**
- * Constructeur PHP5
- * @param string $lang
- */
- function __construct($lang='fr', $Debug=false) {
- $this->lang = (string) $lang;
- switch($lang) {
- case 'en':
- break;
- case 'de':
- break;
- case 'it':
- break;
- case 'es':
- break;
- default:
- $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');
- $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');
- $this->strSentences = array(
- 'Quel est le %s caractère de %s ? (%s)',
- 'Dans %s, quel caractère vient après %s ? (%s)',
- 'Combien font %s et %s ? (%s)',
- 'Combien font %s moins %s ? (%s)',
- 'Combien font %s fois %s ? (%s)'
- );
- $this->strTypes = array('lettre', 'chiffre', 'nombre');
- }
- $choice = rand(0,6);
- switch($choice) {
- case 1: $this->mot(0, true); break;
- case 2: $this->addition(); break;
- case 3: $this->soustraction(); break;
- case 4: $this->multiplication(); break;
- case 5: $this->mot(1, true); break;
- case 6: $this->mot(1, false); break;
- default: $this->mot(0, false); break;
- }
- $this->genHtml($Debug);
- }
- /**
- * Constructeur PHP4
- * @param string $lang
- */
- function Turing($lang='fr',$Debug=false) {
- $this->__construct($lang, $Debug);
- }
- /**
- * Renvoit la valeur littérale d'un nombre
- * @param int $i
- * @return string
- */
- function numbers($i) {
- if (intval($i)>0) {
- return $this->arrNumbers[$i];
- }
- }
- /**
- * Génère une question sur la position d'un caractère
- * @param integer $strType Jeu de caractères à utiliser
- * @param boolean $unique Chaine à caractères uniques ou non
- */
- function mot($strType, $unique=false) {
- if ($strType==1) $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
- 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');
- $arrChars = array();
- for ($i=0;$i<50;$i++) {
- $arrChars[] = $chars[rand(0, count($chars)-1)];
- }
- if ($unique===true) {
- $tmpChars = array_values(array_unique($arrChars));
- $arrChars = $tmpChars;
- }
- $strTuring = implode("", $arrChars);
- if (strlen($strTuring)>17) $strTuring = substr($strTuring, 0, rand(8,17));
- if ($unique === true) {
- $chrSelected = rand(1,strlen($strTuring)-1);
- $this->arrTest = array(sprintf($this->strSentences[1], $strTuring, $arrChars[($chrSelected - 1)], $this->strTypes[$strType]), $arrChars[$chrSelected]);
- } else {
- $chrSelected = rand(0,strlen($strTuring) -1 );
- if ($chrSelected == (strlen($strTuring) -1)) {
- $this->arrTest = array(sprintf($this->strSentences[0], $this->arrPos[16], $strTuring, $this->strTypes[$strType]), $arrChars[$chrSelected]);
- } else {
- $this->arrTest = array(sprintf($this->strSentences[0], $this->arrPos[$chrSelected], $strTuring, $this->strTypes[$strType]), $arrChars[$chrSelected]);
- }
- }
- }
- /**
- * Génère une addition
- */
- function addition() {
- while (($c =($a = rand(1, $this->MAX)) + ($b = rand(1, $this->MAX))) > $this->MAX );
- $this->arrTest = array(sprintf($this->strSentences[2], $this->numbers($a), $this->numbers($b), $this->strTypes[2]), $c);
- }
- /**
- * Génère une soustraction
- */
- function soustraction() {
- while ( ( $c =($a = rand(1, $this->MAX)) - ($b = rand(1, $this->MAX))) < 1 );
- $this->arrTest = array(sprintf($this->strSentences[3], $this->numbers($a), $this->numbers($b), $this->strTypes[2]), $c);
- }
- /**
- * Génère une multiplication
- */
- function multiplication() {
- while ( ( $c =($a = rand(1, $this->MAX)) * ($b = rand(1, $this->MAX))) > $this->MAX );
- $this->arrTest = array(sprintf($this->strSentences[4], $this->numbers($a), $this->numbers($b), $this->strTypes[2]), $c);
- }
- /**
- * Génération du formulaire html
- * Affiche 3 éléments de formulaire:
- * <label> est la question
- * <input turing> est le champ de réponse
- * <input tutingS> est la valeur codée (MD5) de la réponse
- * @return string Eléments du formulaire
- */
- function genHtml($Debug=false) {
- if ($Debug===true) var_dump($this->arrTest);
- $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]).'" />';
- echo $strForm;
- }
- }
- ?>
- Utilisation
- La classe affiche elle-même le formulaire, il suffit donc de l'instancier.
- <?$test = &new Turing();?>
- Les éléments de formulaire seront:
- <label for="turing">Combien font trente-neuf et sept ? (nombre)</label>
- <input name="turing" id="turing" value="" type="text" />
- <input name="turingS" id="turingS" value="d9d4f495e875a2e075a1a4a6e1b9770f" type="hidden" />
- 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