Questions anti flood

Description

c'est le genre de source qui demande aux utilisateurs combien font 2+2 pour leur permettre de passer, c'est pas parfait comme système, j'aime pas trop mon brouillage de sources html, mais j'ai pas le temps de faire mieux, et ça reste potable comparé à ce qui existe à coté...

Source / Exemple :


<?php
header('content-type: text/html; charset=utf8');
?>
<html>
<head>
</head>
<body>
<?php

function int2str($a){
	if ($a<0) return 'moins '.int2str(-$a);
	if ($a<17){
		switch ($a){
			case 0: return 'zero';
			case 1: return 'un';
			case 2: return 'deux';
			case 3: return 'trois';
			case 4: return 'quatre';
			case 5: return 'cinq';
			case 6: return 'six';
			case 7: return 'sept';
			case 8: return 'huit';
			case 9: return 'neuf';
			case 10: return 'dix';
			case 11: return 'onze';
			case 12: return 'douze';
			case 13: return 'treize';
			case 14: return 'quatorze';
			case 15: return 'quinze';
			case 16: return 'seize';
		}
	}else if ($a<20){
		return 'dix-'.int2str($a-10);
	}else if ($a<100){
		if ($a%10==0){
			switch ($a){
				case 20: return 'vingt';
				case 30: return 'trente';
				case 40: return 'quarente';
				case 50: return 'cinquante';
				case 60: return 'soixante';
				case 70: return 'soixante-dix';
				case 80: return 'quatrevingt';
				case 90: return 'quatrexingt-dix';
			}
		}else if ($a<70){
			return int2str($a-$a%10).' '.int2str($a%10);
		}else if ($a<80){
			return int2str(60).' '.int2str($a%20);
		}else{
			return int2str(80).' '.int2str($a%20);
		}
	}else if ($a==100){
		return 'cent';
	}else if ($a<200){
		return int2str(100).' '.int2str($a%100);
	}else if ($a<1000){
		return int2str((int)($a/100)).' '.int2str(100).' '.int2str($a%100);
	}
	//on pourrait pousser pour aller plus loin, mais c'est sans interret pour ce projet, et pas interessant, c'est pas non plus compliqué...
}

function egal($a){
	foreach ($a as $b)
		return $b;
}

function fois($a){
	$s=1;
	foreach ($a as $b)
		$s*=$b;
	return $s;
}

function plus($a){
	$s=0;
	foreach ($a as $b)
		$s+=$b;
	return $s;
}
function randomInt($l){
	return mt_rand(0, $l);
}

function randomString($length=5){
	return substr(str_replace('0', 'o', strtolower(md5(microtime()))), 0, $length);
}
function randomVarName(){
	$a=substr(str_replace('0', 'o', strtolower(md5(microtime()))), 0, 10);
	while (intval($a)){
		$a=substr($a, 1);
	}
	if ($a=='') return randomVarName();
	return $a;
}
function randomCouleur(){
	$r=mt_rand(1, 52);
	$v=mt_rand(1, 52);
	$b=mt_rand(1, 52);

	$v1=mt_rand(0, 2);
	$v2=mt_rand(0, 2);

	$v3=mt_rand(0, 2);
	$v4=mt_rand(0, 2);
	$v5=mt_rand(0, 2);
	if ($v1){
		$r+=200;
	}
	if ($v2){
		$b+=200;
	}
	if (!$v1 && !$v2){
		$v+=200;
	}
	if ($v3){
		$a=$b;
		$b=$v;
		$v=$a;
	}
	if ($v4){
		$a=$b;
		$b=$r;
		$r=$a;
	}
	if ($v5){
		$a=$r;
		$r=$v;
		$v=$a;
	}
	return $r.', '.$v.', '.$b;
}
function couleurproche($r, $v, $b){
	$r+=mt_rand(-5, 10);
	$v+=mt_rand(-5, 10);
	$b+=mt_rand(-5, 10);
	return $r.', '.$v.', '.$b;
}

function couleurde($couleur){
	$couleur=egal($couleur);
	list($r, $v, $b)=explode(', ', $couleur);
	if ($r>200 && $v<200 && $b<200) return 'rouge';
	if ($r<200 && $v>200 && $b<200) return 'vert';
	if ($r<200 && $v<200 && $b>200) return 'bleu';
	if ($r>200 && $v>200 && $b<200) return 'jaune';
	if ($r>200 && $v<200 && $b>200) return 'violet';
	if ($r<200 && $v>200 && $b>200) return 'bleu ciel';
	return 'aucune idée';
}
function genquestion($tab, &$reponse){
	$str=$tab[0];
	$fnc=$tab[1];
	unset($tab[1]);
	unset($tab[0]);
	$reponse=$fnc($tab);
	$str=explode('%', $str);
	$i=0;
	$s='';
	foreach ($tab as $a){
		if (gettype($a)=='integer') $a=int2str($a);
		if (!isset($str[$i+1])) return $s;
		$s.=$str[$i].$a;
		$i++;
	}
	return $s.$str[$i];
}

function leet5p34k($in){
	return str_replace(array('o', 'O', 'i', 'I', 'l'), array('0', '0', '1', '1', '1'), $in);
}
function substringcount($str, $sub){
	return count(explode($sub, ' '.$str.' '));
}
function upper_lower_1337_random($in){
	$out='';
	$length=strlen($in);
	$pos=0;
	$fnc=array(1=>'strtolower', 2=>'strtoupper', 3=>'leet5p34k');
	$fncpos=1;
	$inbalise=0;
	$options=false;
	while ($pos!==$length){
		$add=mt_rand(0, 2);
		$fncpos++;
		if (!isset($fnc[$fncpos])) $fncpos=1;
		if ($pos+$add>$length) $add=$length-$pos;
		$part=substr($in, $pos, $add);
		$inbalise+=substringcount($part, '<')+substringcount($part, '>');
		if ($fncpos!=3 || $inbalise%2==0)$out.=$fnc[$fncpos]($part);
		else $out.=$part;
		$options=(strpos(substr($in, 0, $pos), '<option')!==false);
		if ($inbalise%2==0 && mt_rand(0, 2) && !$options){
			switch (mt_rand(0, 2)){
				case 0:
					$out.='<!--'.randomString().'-->';
				break;
				case 1:
					$out.='<span style="color:rgb('.couleurproche(255, 255, 255).');display:none;">'.randomString().'</span>';
				break;
				case 2:
					$out.='<span style="color:rgb('.couleurproche(255, 255, 255).');position:absolute;margin-top:-10em;">'.randomString(2).'</span>';
				break;
			}
		}
		$pos+=$add;
	}
	return $out;
}
function brouille($in, $brouille=false){
	if ($brouille)$in=utf8_encode(upper_lower_1337_random(utf8_decode($in)));
	$nom1=randomVarName();
	$nom2=randomVarName();
	$out='<div id="'.$nom2.'"></div><script type="text/javascript">
		'.$nom1.'=\''.str_replace('\'', '\\\'', $in).'\';		//on pourrait même s\'amuser à crypter cette chaine, mais bon, il sest 02:42 heures du matin, donc, je vous laisses améliorer ce code si vous en avez besoin, mais moi je vais me coucher...
		document.getElementById(\''.$nom2.'\').innerHTML='.$nom1.';
	</script>';
	return $out;
}

function questionrand(&$questions){
	$a=$questions[array_rand($questions)];
	$r=array();
	foreach($a as $b=>$c){
		if ($b<2){
			$r[]=$c;
		}else{
			$r[]=$c[0]($c[1]);	//Forme de pointeur sur fonction...
		}
	}
	return $r;
}
function randomASSOC(&$assoc){
	$keys=array();
	$vals=array();
	foreach($assoc as $key=>$val){
		$keys[]=$key;
		$vals[]=$val;
	}
	$i=count($keys);
	for ($a=1;$a<$i;$a++){
		$b=mt_rand(0,$a);
		$c=$keys[$a];$keys[$a]=$keys[$b];$keys[$b]=$c;
		$c=$vals[$a];$vals[$a]=$vals[$b];$vals[$b]=$c;
	}
	$assoc=array();
	foreach ($keys as $k=>$key){
		$assoc[$key]=$vals[$k];
	}
	$rand=mt_rand(0,3);
	$assoc['result']=$vals[$rand];
	return $keys[$rand];
}
function Fpos(&$a){
	foreach($a as $b=>$c){
		unset($a[$b]);
		return '<option value="'.$c.'">'.$c.'</option>';
	}
}
function associations($tab){
	foreach ($tab as $v){
	}
	return $v['result'];
}
//DOIT comporter AU MOINS 4 personnes
$array_perso=array(
	'Claude François'=>'Un chanteur',
	'Jaque Chirac'=>'Un président de la république française',
	'Georges W. Bush'=>'Un président américain',
	'Bill Gates'=>'L\'homme le plus riche au monde',
	'Linus Torvald'=>'Le programmeur qui a commencé le kernel linux',
	'Larry Wall'=>'Le créateur du langage PERL');

$array_marques=array(
	'citroen'=>'voitures',
	'oncle ben\'s'=>'riz',
	'nescafé'=>'café',
	'intel'=>'processeurs',
	'optic 2000'=>'lunettes',
	'SFR'=>'téléphonie',
	'wanadoo'=>'internet');

$array_utilite=array(
	'casserolle'=>'faire cuire des aliments',
	'une souris'=>'Ã  rien (sauf parfois, quand on invite des amis, faut qu\'ils puisse se servire du pc...)',
	'les lunettes'=>'Ã  voir devant soi',
	'un appareil photo'=>'Ã  imprimer sur du papier une trace de chaque moment',
	'les chaussures'=>'Ã  ne pas avoir mal aux pieds',
	'un lit'=>'Ã  dormir',
	'une écharpe'=>'à ne pas attraper froid');

$questions=array(		//Pour plus de simplicité, des updates online simplifiées, on peut mettre ceci en sql...
	array('Tapez en chiffres, le nombre suivant : <u><b>%</b></u> ?<input type="text" name="reponse">',
		'egal',array('randomInt', 1000)),

	array('Combien font <u><b>%</b> plus <b>%</b></u> ?<input type="text" name="reponse">',
		'plus',array('randomInt', 200), array('randomInt', 200)),

	array('Combien font <u><b>%</b> fois <b>%</b></u> ?<input type="text" name="reponse">',
		'fois', array('randomInt', 10), array('randomInt',10)),

	array('retapez la chaine suivante : "%"<input type="text" name="reponse">', 'egal', array('randomString', 5)),

	array('Quelle est cette <span style="background-color:rgb(%)">couleur</span> ?<select name="reponse"><option value="rouge">rouge</option><option value="vert">vert</option><option value="jaune">jaune</option><option value="bleu">bleu</option><option value="violet">violet (ou rose)</option><option value="bleu ciel">bleu ciel</option></select>',
		'couleurde', array('randomCouleur', 0)),

	array('Qui (est/était) % ?<select name="reponse"> % % % %</select>',
		'associations',array('randomASSOC', &$array_perso), array('Fpos', &$array_perso),array('Fpos', &$array_perso), array('Fpos', &$array_perso), array('Fpos', &$array_perso), array('egal', array(&$array_perso))),

	array('La marque % produit quelle type de chose ?<select name="reponse"> % % % %</select>',
		'associations',array('randomASSOC', &$array_marques), array('Fpos', &$array_marques),array('Fpos', &$array_marques), array('Fpos', &$array_marques), array('Fpos', &$array_marques), array('egal', array(&$array_marques))),

	array('Quelle est l\'utilité d\'un(e) % ?<select name="reponse"> % % % %</select>',
		'associations',array('randomASSOC', &$array_utilite), array('Fpos', &$array_utilite),array('Fpos', &$array_utilite), array('Fpos', &$array_utilite), array('Fpos', &$array_utilite), array('egal', array(&$array_utilite)))
);

echo brouille(genquestion(questionrand($questions), $reponse));
echo '<hr />'.$reponse;

?>
</body>
</html>

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.