Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an initial (and basic) challenge to avoid spam (at least to try)
- Loading branch information
1 parent
d8cdce1
commit 377dc8d
Showing
2 changed files
with
96 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
// $Id$ | ||
|
||
// simple and stupid SPAM protection (using little challenges) | ||
|
||
$nums = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'); | ||
|
||
function plus($a, $b) { | ||
return $a + $b; | ||
} | ||
|
||
function gen_plus($a) { | ||
return rand(0, 9 - $a); | ||
} | ||
|
||
function minus($a, $b) { | ||
return $a - $b; | ||
} | ||
|
||
function gen_minus($a) { | ||
return rand(0, $a); | ||
} | ||
|
||
function print_infix($name, $a, $b) { | ||
return "$a $name $b"; | ||
} | ||
|
||
function print_prefix($name, $a, $b) { | ||
return "$name($a, $b)"; | ||
} | ||
|
||
$challenges = array( | ||
// name, print, generator | ||
array('max', 'print_prefix'), | ||
array('min', 'print_prefix'), | ||
array('minus', 'print_infix', 'gen_minus'), | ||
array('plus', 'print_infix', 'gen_plus'), | ||
); | ||
|
||
|
||
// generate a challenge | ||
function gen_challenge() { | ||
global $challenges, $nums; | ||
$c = $challenges[rand(0, sizeof($challenges)-1)]; | ||
|
||
$a = rand(0, 9); | ||
$an = $nums[$a]; | ||
$b = isset($c[2]) ? $c[2]($a) : rand(0, 9); | ||
$bn = $nums[$b]; | ||
|
||
return array($c[0], $an, $bn, $c[1]($c[0], $an, $bn)); | ||
} | ||
|
||
|
||
// test an answer for validity | ||
function test_answer($name, $an, $bn, $answer) { | ||
global $challenges, $nums; | ||
|
||
foreach ($challenges as $x) { | ||
if ($x[0] === $name) { | ||
$c = $x; | ||
break; | ||
} | ||
} | ||
|
||
$a = array_search($an, $nums); | ||
$b = array_search($bn, $nums); | ||
|
||
if (empty($c) || $a === false || $b === false) return false; | ||
|
||
return ($nums[$c[0]($a, $b)] === $answer); | ||
} | ||
|
||
|
||
?> |