Skip to content

Commit

Permalink
Enhancement: Extract phpweb\Spam\Challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jul 15, 2022
1 parent 77a0620 commit a076df1
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 91 deletions.
16 changes: 10 additions & 6 deletions manual/add-note.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php

use phpweb\Spam\Challenge;

$ip_spam_lookup_url = 'http://www.dnsbl.info/dnsbl-database-check.php?IP=';

$_SERVER['BASE_PAGE'] = 'manual/add-note.php';

require_once __DIR__ . '/../autoload.php';
include_once __DIR__ . '/../include/prepend.inc';
include_once __DIR__ . '/../include/posttohost.inc';
include_once __DIR__ . '/../include/shared-manual.inc';
include __DIR__ . '/spam_challenge.php';

site_header("Add Manual Note", array('css' => 'add-note.css'));

Expand Down Expand Up @@ -53,7 +57,7 @@
}

// SPAM challenge failed
elseif (!test_answer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['answer'])) {
elseif (!Challenge::isValidAnswer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['answer'])) {
$error = 'SPAM challenge failed.';
}

Expand Down Expand Up @@ -377,14 +381,14 @@
</tr>
<tr>
<th class="subr"><label for="form-answer">Answer to this simple question (SPAM challenge)</label>:<br>
<?php $c = gen_challenge(); echo $c[3]; ?>?</th>
<?php $c = Challenge::create(); echo $c->question(); ?>?</th>
<td><input id="form-answer" type="text" name="answer" size="60" maxlength="10" required> (Example: nine)</td>
</tr>
<tr>
<th colspan="2">
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
<input type="hidden" name="func" value="<?php echo $c->function(); ?>">
<input type="hidden" name="arga" value="<?php echo $c->argumentOne(); ?>">
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo(); ?>">
<input type="submit" name="action" value="Preview">
<input type="submit" name="action" value="Add Note">
</th>
Expand Down
65 changes: 0 additions & 65 deletions manual/spam_challenge.php

This file was deleted.

24 changes: 14 additions & 10 deletions manual/vote-note.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

use phpweb\Spam\Challenge;

$_SERVER['BASE_PAGE'] = 'manual/vote-note.php';

require_once __DIR__ . '/../autoload.php';
include_once __DIR__ . '/../include/prepend.inc';
include_once __DIR__ . '/../include/posttohost.inc';
include_once __DIR__ . '/../include/shared-manual.inc';
include_once __DIR__ . '/spam_challenge.php';

// Initialize global variables
$error = false;
Expand Down Expand Up @@ -55,7 +59,7 @@
}
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')) {
if (!empty($_POST['challenge']) && !empty($_POST['func']) || empty($_POST['arga']) || empty($_POST['argb'])) {
if (!test_answer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['challenge'])) {
if (!Challenge::isValidAnswer($_POST['func'], $_POST['arga'], $_POST['argb'], $_POST['challenge'])) {
$error = "Incorrect answer! Please try again.";
}
else {
Expand Down Expand Up @@ -113,13 +117,13 @@
<div style="background-color: #f5f5ff; border: 1px solid black; padding: 15px; width: 90%; margin: auto;">
<form action="" method="post">
<div>
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = gen_challenge(); echo $c[3]; ?></strong>?<br>
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = Challenge::create(); echo $c->question(); ?></strong>?<br>
<input id="form-challenge" type="text" name="challenge" maxlength="10" required> (Example: nine)</p>
<p><input type="submit" value="Vote" name="votenote"></p>
</div>
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
<input type="hidden" name="func" value="<?php echo $c->function(); ?>">
<input type="hidden" name="arga" value="<?php echo $c->argumentOne(); ?>">
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo(); ?>">
</form>
</div>
</div>
Expand Down Expand Up @@ -165,13 +169,13 @@
<div style="background-color: #f5f5ff; border: 1px solid black; padding: 15px; width: 90%; margin: auto;">
<form action="" method="post">
<div>
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = gen_challenge(); echo $c[3]; ?></strong>?<br>
<p><label for="form-challenge">Please answer this simple SPAM challenge</label>: <strong><?php $c = Challenge::create(); echo $c->question(); ?></strong>?<br>
<input id="form-challenge" type="text" name="challenge" maxlength="10" required> (Example: nine)</p>
<p><input type="submit" value="Vote" name="votenote"></p>
</div>
<input type="hidden" name="func" value="<?php echo $c[0]; ?>">
<input type="hidden" name="arga" value="<?php echo $c[1]; ?>">
<input type="hidden" name="argb" value="<?php echo $c[2]; ?>">
<input type="hidden" name="func" value="<?php echo $c->function(); ?>">
<input type="hidden" name="arga" value="<?php echo $c->argumentOne(); ?>">
<input type="hidden" name="argb" value="<?php echo $c->argumentTwo(); ?>">
</form>
<?php echo $error_div; ?>
</div>
Expand Down
203 changes: 203 additions & 0 deletions src/Spam/Challenge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
<?php

declare(strict_types=1);

namespace phpweb\Spam;

final class Challenge
{
private const NUM = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
);

private const CHALLENGES = array(
// name, print, generator
'max' => array(
'max',
[
self::class,
'print_prefix'
],
),
'min' => array(
'min',
[
self::class,
'print_prefix'
],
),
'minus' => array(
[
self::class,
'minus'
],
[
self::class,
'print_infix'
],
[
self::class,
'gen_minus'
],
),
'plus' => array(
[
self::class,
'plus'
],
[
self::class,
'print_infix'
],
[
self::class,
'gen_plus'
],
),
);

/**
* @var string
*/
private $function;

/**
* @var string
*/
private $argumentOne;

/**
* @var string
*/
private $argumentTwo;

/**
* @var string
*/
private $question;

private function __construct(
string $function,
string $argumentOne,
string $argumentTwo,
string $question
) {
$this->function = $function;
$this->argumentOne = $argumentOne;
$this->argumentTwo = $argumentTwo;
$this->question = $question;
}

public static function create(): self
{
$function = array_rand(self::CHALLENGES);

$challenge = self::CHALLENGES[$function];

$a = rand(0, 9);
$argumentOne = self::NUM[$a];
$b = isset($challenge[2]) ? $challenge[2]($a) : rand(0, 9);
$argumentTwo = self::NUM[$b];

$question = $challenge[1](
$function,
$argumentOne,
$argumentTwo
);

return new self(
$function,
$argumentOne,
$argumentTwo,
$question
);
}

public static function isValidAnswer(
$function,
$argumentOne,
$argumentTwo,
$answer
): bool {
if (!is_string($function)) {
return false;
}

if (!array_key_exists($function, self::CHALLENGES)) {
return false;
}

$challenge = self::CHALLENGES[$function];

if (!is_array($challenge)) {
return false;
}

$a = array_search($argumentOne, self::NUM, false);

if (!is_int($a)) {
return false;
}

$b = array_search($argumentTwo, self::NUM, false);

if (!is_int($b)) {
return false;
}

return self::NUM[$challenge[0]($a, $b)] === $answer;
}

public static function plus($a, $b) {
return $a + $b;
}

public static function gen_plus($a) {
return rand(0, 9 - $a);
}

public static function minus($a, $b) {
return $a - $b;
}

public static function gen_minus($a) {
return rand(0, $a);
}

public static function print_infix($name, $a, $b) {
return "$a $name $b";
}

public static function print_prefix($name, $a, $b) {
return "$name($a, $b)";
}

public function function(): string
{
return $this->function;
}

public function argumentOne(): string
{
return $this->argumentOne;
}

public function argumentTwo(): string
{
return $this->argumentTwo;
}

public function question(): string
{
return $this->question;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
--TEST--
test_answer() returns true when answer to spam challenge is valid
phpweb\Spam\Challenge::isValidAnswer() returns true when answer to spam challenge is valid
--FILE--
<?php

require_once __DIR__ . '/../manual/spam_challenge.php';
use phpweb\Spam\Challenge;

require_once __DIR__ . '/../../../autoload.php';

$answers = [
[
Expand Down Expand Up @@ -73,7 +75,7 @@ $answers = [
];

$results = array_map(static function (array $answer): array {
$answer['isValid'] = test_answer(
$answer['isValid'] = Challenge::isValidAnswer(
$answer['function'],
$answer['argumentOne'],
$answer['argumentTwo'],
Expand Down
Loading

0 comments on commit a076df1

Please sign in to comment.