Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class captcha {
- private $nombre_max = 10;
- private $mot_max = 7;
- public function __construct() {
- }
- public function set() {
- $random = rand(0,3);
- switch($random) {
- case 0 :
- $captcha = $this->addition();
- break;
- case 1 :
- $captcha = $this->soustraction();
- break;
- case 2 :
- $captcha = $this->multiplication();
- break;
- case 3 :
- $captcha = $this->quelle_lettre();
- break;
- }
- return $captcha;
- }
- public function check($code_captcha,$reponse) {
- return (hash('whirlpool',$reponse)===$code_captcha);
- }
- private function addition() {
- while(($c = ($a = rand(0,$this->nombre_max)) + ($b = rand(0,$this->nombre_max)) ) > $this->nombre_max);
- return array('question' => sprintf('Combien font %s plus %s ? (chiffres)',$this->nombre($a),$this->nombre($b)), 'code' => hash('whirlpool',$c));
- }
- private function soustraction() {
- while(($c = ($a = rand(0,$this->nombre_max)) - ($b = rand(0,$this->nombre_max)) ) < 0);
- return array('question' => sprintf('Que donne %s moins %s ? (chiffres)',$this->nombre($a),$this->nombre($b)), 'code' => hash('whirlpool',$c));
- }
- private function multiplication() {
- while(($c = ($a = rand(0,$this->nombre_max)) * ($b = rand(0,$this->nombre_max)) ) > $this->nombre_max);
- return array('question' => sprintf('Que donne %s fois %s ? (chiffres)',$this->nombre($a),$this->nombre($b)), 'code' => hash('whirlpool',$c));
- }
- private function quelle_lettre() {
- $mot = $this->mot(rand(1,$this->mot_max));
- while(($position = rand(0,strlen($mot)-1))>11);
- $lettre = substr($mot,$position,1);
- return array('question' => sprintf('Quelle est %s lettre dans le mot %s ?',$this->position_lettre($mot, $position),$mot), 'code' => hash('whirlpool',$lettre));
- }
- private function mot($i) {
- $arr = array(
- 'tomate','banane','orange','ciel','charbon','herbe','pierre'
- );
- return $i > 0 ? $arr[$i-1] : array_values($arr);
- }
- private function nombre($i) {
- $arr = 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'
- );
- return $i >= 0 ? $arr[$i] : array_values($arr);
- }
- private function position_lettre($mot,$n=0) {
- $size = strlen($mot);
- $res = '';
- $arr = array(
- 'première','deuxième','troisième','quatrième','cinquième','sixième',
- 'huitième','neuvième','dixième','onzième'
- );
- if(1+$n == $size)
- $res= 'la dernière';
- else
- if($n == $size)
- $res='l\'avant-dernière';
- else
- $res = 'la '.$arr[$n];
- return $res;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment