1
0
mirror of https://github.com/php/web-php.git synced 2026-03-23 23:02:13 +01:00
Files
archived-web-php/manual/spam_challenge.php
Andreas Möller 11c5706c39 Enhancement: Enable random_api_migration fixer
Co-authored-by: Mathias Reker <mathias@reker.dk>

Closes GH-699.
2022-09-16 13:51:25 +02:00

66 lines
1.4 KiB
PHP

<?php
// simple and stupid SPAM protection (using little challenges)
const NUMS = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
function plus($a, $b) {
return $a + $b;
}
function gen_plus($a) {
return mt_rand(0, 9 - $a);
}
function minus($a, $b) {
return $a - $b;
}
function gen_minus($a) {
return mt_rand(0, $a);
}
function print_infix($name, $a, $b) {
return "$a $name $b";
}
function print_prefix($name, $a, $b) {
return "$name($a, $b)";
}
const CHALLENGES = [
// name, print, generator
['max', 'print_prefix'],
['min', 'print_prefix'],
['minus', 'print_infix', 'gen_minus'],
['plus', 'print_infix', 'gen_plus'],
];
// generate a challenge
function gen_challenge() {
$c = CHALLENGES[mt_rand(0, count(CHALLENGES) - 1)];
$a = mt_rand(0, 9);
$an = NUMS[$a];
$b = isset($c[2]) ? $c[2]($a) : mt_rand(0, 9);
$bn = NUMS[$b];
return [$c[0], $an, $bn, $c[1]($c[0], $an, $bn)];
}
// test an answer for validity
function test_answer($name, $an, $bn, $answer) {
foreach (CHALLENGES as $x) {
if ($x[0] === $name) {
$c = $x;
break;
}
}
$a = array_search($an, NUMS, false);
$b = array_search($bn, NUMS, false);
if (empty($c) || $a === false || $b === false) return false;
return (NUMS[$c[0]($a, $b)] === $answer);
}