Skip to content

Commit a88faf2

Browse files
committed
Enhancement: Extract phpweb\Spam\Challenge
1 parent b95f3e7 commit a88faf2

File tree

6 files changed

+202
-92
lines changed

6 files changed

+202
-92
lines changed

manual/add-note.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?php
2+
3+
use phpweb\Spam\Challenge;
4+
25
$ip_spam_lookup_url = 'http://www.dnsbl.info/dnsbl-database-check.php?IP=';
36

47
$_SERVER['BASE_PAGE'] = 'manual/add-note.php';
8+
9+
require_once __DIR__ . '/../autoload.php';
510
include_once __DIR__ . '/../include/prepend.inc';
611
include_once __DIR__ . '/../include/posttohost.inc';
712
include_once __DIR__ . '/../include/shared-manual.inc';
8-
include __DIR__ . '/spam_challenge.php';
913

1014
use phpweb\UserNotes\UserNote;
1115

@@ -55,7 +59,7 @@
5559
}
5660

5761
// SPAM challenge failed
58-
elseif (!test_answer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['answer'])) {
62+
elseif (!Challenge::isValidAnswer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['answer'])) {
5963
$error = 'SPAM challenge failed.';
6064
}
6165

@@ -366,14 +370,14 @@
366370
</tr>
367371
<tr>
368372
<th class="subr"><label for="form-answer">Answer to this simple question (SPAM challenge)</label>:<br>
369-
<?php $c = gen_challenge(); echo $c[3]; ?>?</th>
373+
<?php $c = Challenge::create(); echo $c->question; ?>?</th>
370374
<td><input id="form-answer" type="text" name="answer" size="60" maxlength="10" required> (Example: nine)</td>
371375
</tr>
372376
<tr>
373377
<th colspan="2">
374-
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
375-
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
376-
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
378+
<input type="hidden" name="func" value="<?php echo $c->function; ?>">
379+
<input type="hidden" name="arga" value="<?php echo $c->argumentOne; ?>">
380+
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo; ?>">
377381
<input type="submit" name="action" value="Preview">
378382
<input type="submit" name="action" value="Add Note">
379383
</th>

manual/spam_challenge.php

Lines changed: 0 additions & 66 deletions
This file was deleted.

manual/vote-note.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<?php
2+
3+
use phpweb\Spam\Challenge;
4+
25
$_SERVER['BASE_PAGE'] = 'manual/vote-note.php';
6+
7+
require_once __DIR__ . '/../autoload.php';
38
include_once __DIR__ . '/../include/prepend.inc';
49
include_once __DIR__ . '/../include/posttohost.inc';
510
include_once __DIR__ . '/../include/shared-manual.inc';
6-
include_once __DIR__ . '/spam_challenge.php';
711

812
// Initialize global variables
913
$error = false;
@@ -53,7 +57,7 @@
5357
}
5458
if (!empty($_REQUEST['id']) && !empty($_REQUEST['page']) && ($N = manual_notes_load($_REQUEST['page'])) && array_key_exists($_REQUEST['id'], $N) && !empty($_REQUEST['vote']) && ($_REQUEST['vote'] === 'up' || $_REQUEST['vote'] === 'down')) {
5559
if (!empty($_POST['challenge']) && !empty($_POST['func']) || empty($_POST['arga']) || empty($_POST['argb'])) {
56-
if (!test_answer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['challenge'])) {
60+
if (!Challenge::isValidAnswer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['challenge'])) {
5761
$error = "Incorrect answer! Please try again.";
5862
}
5963
else {
@@ -109,13 +113,13 @@
109113
<div style="background-color: #f5f5ff; border: 1px solid black; padding: 15px; width: 90%; margin: auto;">
110114
<form action="" method="post">
111115
<div>
112-
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = gen_challenge(); echo $c[3]; ?></strong>?<br>
116+
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = Challenge::create(); echo $c->question; ?></strong>?<br>
113117
<input id="form-challenge" type="text" name="challenge" maxlength="10" required> (Example: nine)</p>
114118
<p><input type="submit" value="Vote" name="votenote"></p>
115119
</div>
116-
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
117-
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
118-
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
120+
<input type="hidden" name="func" value="<?php echo $c->function; ?>">
121+
<input type="hidden" name="arga" value="<?php echo $c->argumentOne; ?>">
122+
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo; ?>">
119123
</form>
120124
</div>
121125
</div>
@@ -158,13 +162,13 @@
158162
<div style="background-color: #f5f5ff; border: 1px solid black; padding: 15px; width: 90%; margin: auto;">
159163
<form action="" method="post">
160164
<div>
161-
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = gen_challenge(); echo $c[3]; ?></strong>?<br>
165+
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = Challenge::create(); echo $c->question; ?></strong>?<br>
162166
<input id="form-challenge" type="text" name="challenge" maxlength="10" required> (Example: nine)</p>
163167
<p><input type="submit" value="Vote" name="votenote"></p>
164168
</div>
165-
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
166-
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
167-
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
169+
<input type="hidden" name="func" value="<?php echo $c->function; ?>">
170+
<input type="hidden" name="arga" value="<?php echo $c->argumentOne; ?>">
171+
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo; ?>">
168172
</form>
169173
<?php echo $error_div; ?>
170174
</div>

src/Spam/Challenge.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpweb\Spam;
6+
7+
final readonly class Challenge
8+
{
9+
private const NUMBERS = [
10+
0 => 'zero',
11+
1 => 'one',
12+
2 => 'two',
13+
3 => 'three',
14+
4 => 'four',
15+
5 => 'five',
16+
6 => 'six',
17+
7 => 'seven',
18+
8 => 'eight',
19+
9 => 'nine',
20+
];
21+
22+
private const CHALLENGES = [
23+
// name, print, generator
24+
'max' => [
25+
'max',
26+
[
27+
self::class,
28+
'print_prefix',
29+
],
30+
],
31+
'min' => [
32+
'min',
33+
[
34+
self::class,
35+
'print_prefix',
36+
],
37+
],
38+
'minus' => [
39+
[
40+
self::class,
41+
'minus',
42+
],
43+
[
44+
self::class,
45+
'print_infix',
46+
],
47+
[
48+
self::class,
49+
'gen_minus',
50+
],
51+
],
52+
'plus' => [
53+
[
54+
self::class,
55+
'plus',
56+
],
57+
[
58+
self::class,
59+
'print_infix',
60+
],
61+
[
62+
self::class,
63+
'gen_plus',
64+
],
65+
],
66+
];
67+
68+
private function __construct(
69+
public string $function,
70+
public string $argumentOne,
71+
public string $argumentTwo,
72+
public string $question,
73+
) {
74+
}
75+
76+
public static function create(): self
77+
{
78+
$function = array_rand(self::CHALLENGES);
79+
80+
$challenge = self::CHALLENGES[$function];
81+
82+
$a = mt_rand(0, 9);
83+
$argumentOne = self::NUMBERS[$a];
84+
85+
$b = isset($challenge[2]) ? $challenge[2]($a) : mt_rand(0, 9);
86+
$argumentTwo = self::NUMBERS[$b];
87+
88+
$question = $challenge[1](
89+
$function,
90+
$argumentOne,
91+
$argumentTwo,
92+
);
93+
94+
return new self(
95+
$function,
96+
$argumentOne,
97+
$argumentTwo,
98+
$question,
99+
);
100+
}
101+
102+
public static function isValidAnswer(
103+
string $function,
104+
string $argumentOne,
105+
string $argumentTwo,
106+
string $answer,
107+
): bool {
108+
if (!array_key_exists($function, self::CHALLENGES)) {
109+
return false;
110+
}
111+
112+
$challenge = self::CHALLENGES[$function];
113+
114+
$a = array_search($argumentOne, self::NUMBERS, true);
115+
116+
if (!is_int($a)) {
117+
return false;
118+
}
119+
120+
$b = array_search($argumentTwo, self::NUMBERS, true);
121+
122+
if (!is_int($b)) {
123+
return false;
124+
}
125+
126+
$expected = self::NUMBERS[$challenge[0]($a, $b)];
127+
128+
return $expected === $answer;
129+
}
130+
131+
private static function plus(int $a, int $b): int
132+
{
133+
return $a + $b;
134+
}
135+
136+
private static function gen_plus(int $a): int
137+
{
138+
return mt_rand(0, 9 - $a);
139+
}
140+
141+
private static function minus(int $a, int $b): int
142+
{
143+
return $a - $b;
144+
}
145+
146+
private static function gen_minus(int $a): int
147+
{
148+
return mt_rand(0, $a);
149+
}
150+
151+
private static function print_infix(string $name, string $a, string $b): string
152+
{
153+
return "$a $name $b";
154+
}
155+
156+
private static function print_prefix(string $name, string $a, string $b): string
157+
{
158+
return "$name($a, $b)";
159+
}
160+
}

tests/test-answer.phpt renamed to tests/Spam/Challenge/is-valid-answer.phpt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
--TEST--
2-
test_answer() returns true when answer to spam challenge is valid
2+
Challenge::isValidAnswer() returns true when answer to spam challenge is valid
33
--FILE--
44
<?php
55

6-
require_once __DIR__ . '/../manual/spam_challenge.php';
6+
declare(strict_types=1);
7+
8+
use phpweb\Spam\Challenge;
9+
10+
require_once __DIR__ . '/../../../src/autoload.php';
711

812
$answers = [
913
[
@@ -73,7 +77,7 @@ $answers = [
7377
];
7478

7579
$results = array_map(static function (array $answer): array {
76-
$answer['isValid'] = test_answer(
80+
$answer['isValid'] = Challenge::isValidAnswer(
7781
$answer['function'],
7882
$answer['argumentOne'],
7983
$answer['argumentTwo'],

tests/gen-challenge.phpt renamed to tests/Spam/Challenge/random.phpt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
--TEST--
2-
gen_challenge() generates a spam challenge
2+
Challenge::random() generates a spam challenge
33
--FILE--
44
<?php
55

6-
require_once __DIR__ . '/../manual/spam_challenge.php';
6+
declare(strict_types=1);
7+
8+
use phpweb\Spam\Challenge;
9+
10+
require_once __DIR__ . '/../../../src/autoload.php';
711

812
mt_srand(9001);
913

1014
$challenges = array_map(static function (): array {
11-
[$function, $argumentOne, $argumentTwo, $question] = gen_challenge();
15+
$challenge = Challenge::create();
1216

1317
return [
14-
'function' => $function,
15-
'argumentOne' => $argumentOne,
16-
'argumentTwo' => $argumentTwo,
17-
'question' => $question,
18+
'function' => $challenge->function,
19+
'argumentOne' => $challenge->argumentOne,
20+
'argumentTwo' => $challenge->argumentTwo,
21+
'question' => $challenge->question,
1822
];
1923
}, range(1, 20));
2024

0 commit comments

Comments
 (0)