Guest User

Untitled

a guest
Aug 19th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.98 KB | None | 0 0
  1. <?php
  2.     class captcha {
  3.         private $nombre_max = 10;
  4.         private $mot_max = 7;
  5.    
  6.         public function __construct() {
  7.         }
  8.        
  9.         public function set() {
  10.             $random = rand(0,3);
  11.             switch($random) {
  12.                 case 0 :
  13.                     $captcha = $this->addition();
  14.                     break;
  15.                 case 1 :
  16.                     $captcha = $this->soustraction();
  17.                     break;
  18.                 case 2 :
  19.                     $captcha = $this->multiplication();
  20.                     break;
  21.                 case 3 :
  22.                     $captcha = $this->quelle_lettre();
  23.                     break;
  24.             }
  25.             return $captcha;
  26.         }
  27.        
  28.         public function check($code_captcha,$reponse) {
  29.             return (hash('whirlpool',$reponse)===$code_captcha);
  30.         }
  31.        
  32.         private function addition()    {
  33.             while(($c = ($a = rand(0,$this->nombre_max)) + ($b = rand(0,$this->nombre_max)) ) > $this->nombre_max);
  34.             return array('question' => sprintf('Combien font %s plus %s ? (chiffres)',$this->nombre($a),$this->nombre($b)), 'code' => hash('whirlpool',$c));
  35.         }
  36.        
  37.         private function soustraction()    {
  38.             while(($c = ($a = rand(0,$this->nombre_max)) - ($b = rand(0,$this->nombre_max)) ) < 0);
  39.             return array('question' => sprintf('Que donne %s moins %s ? (chiffres)',$this->nombre($a),$this->nombre($b)), 'code' => hash('whirlpool',$c));
  40.         }
  41.    
  42.         private function multiplication() {
  43.             while(($c = ($a = rand(0,$this->nombre_max)) * ($b = rand(0,$this->nombre_max)) ) > $this->nombre_max);
  44.             return array('question' => sprintf('Que donne %s fois %s ? (chiffres)',$this->nombre($a),$this->nombre($b)), 'code' => hash('whirlpool',$c));
  45.         }
  46.        
  47.         private function quelle_lettre() {
  48.             $mot = $this->mot(rand(1,$this->mot_max));
  49.             while(($position = rand(0,strlen($mot)-1))>11);
  50.             $lettre = substr($mot,$position,1);
  51.             return array('question' => sprintf('Quelle est %s lettre dans le mot %s ?',$this->position_lettre($mot, $position),$mot), 'code' => hash('whirlpool',$lettre));
  52.         }
  53.        
  54.        
  55.         private function mot($i) {
  56.             $arr = array(
  57.                 'tomate','banane','orange','ciel','charbon','herbe','pierre'
  58.             );
  59.             return $i > 0 ? $arr[$i-1] : array_values($arr);
  60.         }
  61.        
  62.         private function nombre($i) {
  63.             $arr = array(
  64.                 'z&eacute;ro','un','deux','trois','quatre','cinq','six','sept','huit','neuf','dix','onze','douze','treize','quatorze','quinze',
  65.                 'seize','dix-sept','dix-huit','dix-neuf','vingt','vingt et un','vingt-deux','vingt-trois','vingt-quatre','vingt-cinq','vingt-six',
  66.                 'vingt-sept','vingt-huit','vingt-neuf','trente','trente et un','trente-deux','trente-trois','trente-quatre',
  67.                 'trente-cinq','trente-six','trente-sept','trente-huit','trente-neuf','quarante','quarante et un','quarante-deux','quarante-trois',
  68.                 'quarante-quatre','quarante-cinq','quarante-six','quarante-sept','quarante-huit','quarante-neuf','cinquante'
  69.             );
  70.             return $i >= 0 ? $arr[$i] : array_values($arr);
  71.         }
  72.        
  73.         private function position_lettre($mot,$n=0) {
  74.             $size = strlen($mot);
  75.             $res = '';
  76.             $arr = array(
  77.                 'premi&egrave;re','deuxi&egrave;me','troisi&egrave;me','quatri&egrave;me','cinqui&egrave;me','sixi&egrave;me',
  78.                 'huiti&egrave;me','neuvi&egrave;me','dixi&egrave;me','onzi&egrave;me'
  79.             );
  80.             if(1+$n == $size)
  81.                 $res= 'la derni&egrave;re';
  82.             else
  83.                 if($n == $size)
  84.                     $res='l\'avant-derni&egrave;re';
  85.                 else
  86.                     $res = 'la '.$arr[$n];
  87.             return $res;
  88.         }
  89.     }
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment