Skip to content

Commit a076df1

Browse files
committed
Enhancement: Extract phpweb\Spam\Challenge
1 parent 77a0620 commit a076df1

File tree

6 files changed

+241
-91
lines changed

6 files changed

+241
-91
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
site_header("Add Manual Note", array('css' => 'add-note.css'));
1115

@@ -53,7 +57,7 @@
5357
}
5458

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

@@ -377,14 +381,14 @@
377381
</tr>
378382
<tr>
379383
<th class="subr"><label for="form-answer">Answer to this simple question (SPAM challenge)</label>:<br>
380-
<?php $c = gen_challenge(); echo $c[3]; ?>?</th>
384+
<?php $c = Challenge::create(); echo $c->question(); ?>?</th>
381385
<td><input id="form-answer" type="text" name="answer" size="60" maxlength="10" required> (Example: nine)</td>
382386
</tr>
383387
<tr>
384388
<th colspan="2">
385-
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
386-
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
387-
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
389+
<input type="hidden" name="func" value="<?php echo $c->function(); ?>">
390+
<input type="hidden" name="arga" value="<?php echo $c->argumentOne(); ?>">
391+
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo(); ?>">
388392
<input type="submit" name="action" value="Preview">
389393
<input type="submit" name="action" value="Add Note">
390394
</th>

manual/spam_challenge.php

Lines changed: 0 additions & 65 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;
@@ -55,7 +59,7 @@
5559
}
5660
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')) {
5761
if (!empty($_POST['challenge']) && !empty($_POST['func']) || empty($_POST['arga']) || empty($_POST['argb'])) {
58-
if (!test_answer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['challenge'])) {
62+
if (!Challenge::isValidAnswer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['challenge'])) {
5963
$error = "Incorrect answer! Please try again.";
6064
}
6165
else {
@@ -113,13 +117,13 @@
113117
<div style="background-color: #f5f5ff; border: 1px solid black; padding: 15px; width: 90%; margin: auto;">
114118
<form action="" method="post">
115119
<div>
116-
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = gen_challenge(); echo $c[3]; ?></strong>?<br>
120+
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = Challenge::create(); echo $c->question(); ?></strong>?<br>
117121
<input id="form-challenge" type="text" name="challenge" maxlength="10" required> (Example: nine)</p>
118122
<p><input type="submit" value="Vote" name="votenote"></p>
119123
</div>
120-
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
121-
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
122-
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
124+
<input type="hidden" name="func" value="<?php echo $c->function(); ?>">
125+
<input type="hidden" name="arga" value="<?php echo $c->argumentOne(); ?>">
126+
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo(); ?>">
123127
</form>
124128
</div>
125129
</div>
@@ -165,13 +169,13 @@
165169
<div style="background-color: #f5f5ff; border: 1px solid black; padding: 15px; width: 90%; margin: auto;">
166170
<form action="" method="post">
167171
<div>
168-
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = gen_challenge(); echo $c[3]; ?></strong>?<br>
172+
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = Challenge::create(); echo $c->question(); ?></strong>?<br>
169173
<input id="form-challenge" type="text" name="challenge" maxlength="10" required> (Example: nine)</p>
170174
<p><input type="submit" value="Vote" name="votenote"></p>
171175
</div>
172-
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
173-
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
174-
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
176+
<input type="hidden" name="func" value="<?php echo $c->function(); ?>">
177+
<input type="hidden" name="arga" value="<?php echo $c->argumentOne(); ?>">
178+
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo(); ?>">
175179
</form>
176180
<?php echo $error_div; ?>
177181
</div>

src/Spam/Challenge.php

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpweb\Spam;
6+
7+
final class Challenge
8+
{
9+
private const NUM = array(
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 = array(
23+
// name, print, generator
24+
'max' => array(
25+
'max',
26+
[
27+
self::class,
28+
'print_prefix'
29+
],
30+
),
31+
'min' => array(
32+
'min',
33+
[
34+
self::class,
35+
'print_prefix'
36+
],
37+
),
38+
'minus' => array(
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' => array(
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+
/**
69+
* @var string
70+
*/
71+
private $function;
72+
73+
/**
74+
* @var string
75+
*/
76+
private $argumentOne;
77+
78+
/**
79+
* @var string
80+
*/
81+
private $argumentTwo;
82+
83+
/**
84+
* @var string
85+
*/
86+
private $question;
87+
88+
private function __construct(
89+
string $function,
90+
string $argumentOne,
91+
string $argumentTwo,
92+
string $question
93+
) {
94+
$this->function = $function;
95+
$this->argumentOne = $argumentOne;
96+
$this->argumentTwo = $argumentTwo;
97+
$this->question = $question;
98+
}
99+
100+
public static function create(): self
101+
{
102+
$function = array_rand(self::CHALLENGES);
103+
104+
$challenge = self::CHALLENGES[$function];
105+
106+
$a = rand(0, 9);
107+
$argumentOne = self::NUM[$a];
108+
$b = isset($challenge[2]) ? $challenge[2]($a) : rand(0, 9);
109+
$argumentTwo = self::NUM[$b];
110+
111+
$question = $challenge[1](
112+
$function,
113+
$argumentOne,
114+
$argumentTwo
115+
);
116+
117+
return new self(
118+
$function,
119+
$argumentOne,
120+
$argumentTwo,
121+
$question
122+
);
123+
}
124+
125+
public static function isValidAnswer(
126+
$function,
127+
$argumentOne,
128+
$argumentTwo,
129+
$answer
130+
): bool {
131+
if (!is_string($function)) {
132+
return false;
133+
}
134+
135+
if (!array_key_exists($function, self::CHALLENGES)) {
136+
return false;
137+
}
138+
139+
$challenge = self::CHALLENGES[$function];
140+
141+
if (!is_array($challenge)) {
142+
return false;
143+
}
144+
145+
$a = array_search($argumentOne, self::NUM, false);
146+
147+
if (!is_int($a)) {
148+
return false;
149+
}
150+
151+
$b = array_search($argumentTwo, self::NUM, false);
152+
153+
if (!is_int($b)) {
154+
return false;
155+
}
156+
157+
return self::NUM[$challenge[0]($a, $b)] === $answer;
158+
}
159+
160+
public static function plus($a, $b) {
161+
return $a + $b;
162+
}
163+
164+
public static function gen_plus($a) {
165+
return rand(0, 9 - $a);
166+
}
167+
168+
public static function minus($a, $b) {
169+
return $a - $b;
170+
}
171+
172+
public static function gen_minus($a) {
173+
return rand(0, $a);
174+
}
175+
176+
public static function print_infix($name, $a, $b) {
177+
return "$a $name $b";
178+
}
179+
180+
public static function print_prefix($name, $a, $b) {
181+
return "$name($a, $b)";
182+
}
183+
184+
public function function(): string
185+
{
186+
return $this->function;
187+
}
188+
189+
public function argumentOne(): string
190+
{
191+
return $this->argumentOne;
192+
}
193+
194+
public function argumentTwo(): string
195+
{
196+
return $this->argumentTwo;
197+
}
198+
199+
public function question(): string
200+
{
201+
return $this->question;
202+
}
203+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
--TEST--
2-
test_answer() returns true when answer to spam challenge is valid
2+
phpweb\Spam\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+
use phpweb\Spam\Challenge;
7+
8+
require_once __DIR__ . '/../../../autoload.php';
79

810
$answers = [
911
[
@@ -73,7 +75,7 @@ $answers = [
7375
];
7476

7577
$results = array_map(static function (array $answer): array {
76-
$answer['isValid'] = test_answer(
78+
$answer['isValid'] = Challenge::isValidAnswer(
7779
$answer['function'],
7880
$answer['argumentOne'],
7981
$answer['argumentTwo'],

0 commit comments

Comments
 (0)