the upload tree, and public form submissions are size-bounded and rate-limited
262 lines
10 KiB
PHP
262 lines
10 KiB
PHP
<?php
|
|
|
|
namespace Plugi\Extensions\Form;
|
|
|
|
use Plugi\Extensions\Form\Finishers\DefaultFinisher;
|
|
use Plugi\Extensions\Form\Finishers\FinishersRepository;
|
|
|
|
class SubmissionHandler
|
|
{
|
|
public const MAX_REQUEST_BYTES = 65536;
|
|
public const MAX_FIELD_BYTES = 16384;
|
|
public const MAX_FIELDS = 100;
|
|
|
|
private $formsRepository;
|
|
private $finishersRepository;
|
|
private $rateLimiter;
|
|
|
|
public function __construct($formsRepository = null, $finishersRepository = null, $rateLimiter = null)
|
|
{
|
|
$this->formsRepository = $formsRepository ?? new DefinitionsRepository();
|
|
$this->finishersRepository = $finishersRepository ?? new FinishersRepository();
|
|
$this->rateLimiter = $rateLimiter ?? new SubmissionRateLimiter();
|
|
}
|
|
|
|
public function handle($formId, string $rawData): array
|
|
{
|
|
if (strlen($rawData) > self::MAX_REQUEST_BYTES) {
|
|
return $this->failure(413, ['The submission is too large.']);
|
|
}
|
|
|
|
$formId = filter_var($formId, FILTER_VALIDATE_INT);
|
|
if ($formId === false || $formId < 1) {
|
|
return $this->failure(400, ['A valid form id is required.']);
|
|
}
|
|
|
|
$clientIp = $_SERVER['REMOTE_ADDR'] ?? '';
|
|
if (!is_string($clientIp) || filter_var($clientIp, FILTER_VALIDATE_IP) === false) {
|
|
$clientIp = 'unknown';
|
|
}
|
|
try {
|
|
if (!$this->rateLimiter->consume($formId, $clientIp)) {
|
|
return $this->failure(429, ['Too many submissions. Please try again later.']);
|
|
}
|
|
} catch (\Throwable $exception) {
|
|
return $this->failure(503, ['Submissions are temporarily unavailable.']);
|
|
}
|
|
|
|
try {
|
|
$form = $this->formsRepository->findWithId($formId);
|
|
} catch (\Throwable $exception) {
|
|
return $this->failure(500, ['The form could not be loaded.']);
|
|
}
|
|
if (!$form) {
|
|
return $this->failure(404, ['The requested form was not found.']);
|
|
}
|
|
$form = (array) $form;
|
|
|
|
$payload = json_decode($rawData, true);
|
|
if (json_last_error() !== JSON_ERROR_NONE || !is_array($payload) || !isset($payload['fields']) || !is_array($payload['fields'])) {
|
|
return $this->failure(400, ['The submission payload is invalid.']);
|
|
}
|
|
foreach (array_keys($payload) as $key) {
|
|
if ($key !== 'fields') {
|
|
return $this->failure(400, ['The submission payload contains unsupported data.']);
|
|
}
|
|
}
|
|
|
|
$formFields = json_decode($form['fields'] ?? '', true);
|
|
if (json_last_error() !== JSON_ERROR_NONE || !is_array($formFields) || !$this->isList($formFields)) {
|
|
return $this->failure(422, ['The form fields configuration is invalid.']);
|
|
}
|
|
$fieldErrors = $this->validateFormFields($formFields, $payload['fields']);
|
|
if ($fieldErrors) {
|
|
return $this->failure(422, $fieldErrors);
|
|
}
|
|
|
|
$finisherEntries = json_decode($form['finishers'] ?? '', true);
|
|
if (json_last_error() !== JSON_ERROR_NONE || !is_array($finisherEntries) || !$this->isList($finisherEntries)) {
|
|
return $this->failure(422, ['The form actions configuration is invalid.']);
|
|
}
|
|
|
|
$configuredFinishers = [];
|
|
foreach ($finisherEntries as $entry) {
|
|
if (!is_array($entry) || !isset($entry['type']) || !is_string($entry['type']) || $entry['type'] === '') {
|
|
return $this->failure(422, ['The form contains an invalid action entry.']);
|
|
}
|
|
foreach (array_keys($entry) as $key) {
|
|
if ($key !== 'type' && $key !== 'options') {
|
|
return $this->failure(422, ['The form contains unsupported action settings.']);
|
|
}
|
|
}
|
|
$options = array_key_exists('options', $entry) ? $entry['options'] : [];
|
|
if (!is_array($options) || !$this->finishersRepository->finisherExists($entry['type'])) {
|
|
return $this->failure(422, ['The form contains an unknown or invalid action.']);
|
|
}
|
|
|
|
$config = $this->finishersRepository->getFinisherConfig($entry['type']);
|
|
$class = $config['class'] ?? null;
|
|
if (!is_string($class) || !is_subclass_of($class, DefaultFinisher::class)) {
|
|
return $this->failure(422, ['The form contains an invalid action configuration.']);
|
|
}
|
|
$configuredFinishers[] = [
|
|
'type' => $entry['type'],
|
|
'class' => $class,
|
|
'options' => $options,
|
|
];
|
|
}
|
|
|
|
$form['formFields'] = $formFields;
|
|
$context = [
|
|
'formId' => $formId,
|
|
'form' => $form,
|
|
'rawData' => $rawData,
|
|
'submission' => $payload,
|
|
];
|
|
$actions = [];
|
|
foreach ($configuredFinishers as $finisher) {
|
|
$success = false;
|
|
try {
|
|
$instance = new $finisher['class']($context, $finisher['options']);
|
|
$success = $instance->execute() === true;
|
|
} catch (\Throwable $exception) {
|
|
$success = false;
|
|
}
|
|
$actions[] = [
|
|
'type' => $finisher['type'],
|
|
'success' => $success,
|
|
];
|
|
}
|
|
|
|
$success = !in_array(false, array_column($actions, 'success'), true);
|
|
$body = [
|
|
'success' => $success,
|
|
'actions' => $actions,
|
|
'errors' => $success ? [] : ['One or more actions could not be completed.'],
|
|
];
|
|
|
|
return [
|
|
'status' => $success ? 200 : 500,
|
|
'body' => $body,
|
|
];
|
|
}
|
|
|
|
private function validateFormFields(array $formFields, array $submittedFields): array
|
|
{
|
|
$errors = [];
|
|
$knownFields = [];
|
|
$supportedTypes = ['text', 'email', 'textarea', 'checkbox', 'radio', 'select'];
|
|
|
|
if (count($submittedFields) > self::MAX_FIELDS) {
|
|
return ['The submission contains too many fields.'];
|
|
}
|
|
|
|
foreach ($formFields as $field) {
|
|
if (!is_array($field) || !isset($field['name']) || !is_string($field['name']) || trim($field['name']) === '' || !isset($field['label']) || !is_string($field['label']) || !isset($field['type']) || !is_string($field['type']) || !in_array($field['type'], $supportedTypes, true)) {
|
|
return ['The form contains an invalid field configuration.'];
|
|
}
|
|
$name = $field['name'];
|
|
if (array_key_exists($name, $knownFields)) {
|
|
return ['The form contains duplicate field names.'];
|
|
}
|
|
if (array_key_exists('required', $field) && !is_bool($field['required'])) {
|
|
return ['The form contains an invalid required-field setting.'];
|
|
}
|
|
|
|
$options = array_key_exists('options', $field) ? $field['options'] : [];
|
|
if (!is_array($options) || !$this->isList($options)) {
|
|
return ['The form contains invalid field options.'];
|
|
}
|
|
$optionValues = [];
|
|
foreach ($options as $option) {
|
|
if (!is_array($option) || !isset($option['label']) || !is_string($option['label']) || !isset($option['value']) || !is_scalar($option['value'])) {
|
|
return ['The form contains invalid field options.'];
|
|
}
|
|
$optionValues[] = (string) $option['value'];
|
|
}
|
|
|
|
$knownFields[$name] = [
|
|
'type' => $field['type'],
|
|
'required' => $field['required'] ?? false,
|
|
'options' => $optionValues,
|
|
];
|
|
}
|
|
|
|
foreach (array_keys($submittedFields) as $name) {
|
|
if (!is_string($name) || !array_key_exists($name, $knownFields)) {
|
|
return ['The submission contains an unknown field.'];
|
|
}
|
|
$value = $submittedFields[$name];
|
|
if (is_string($value) && strlen($value) > self::MAX_FIELD_BYTES) {
|
|
return ['A submitted field is too large.'];
|
|
}
|
|
if (is_array($value)) {
|
|
if (count($value) > self::MAX_FIELDS) {
|
|
return ['A submitted field contains too many values.'];
|
|
}
|
|
foreach ($value as $item) {
|
|
if (is_string($item) && strlen($item) > self::MAX_FIELD_BYTES) {
|
|
return ['A submitted field is too large.'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($knownFields as $name => $field) {
|
|
$value = $submittedFields[$name] ?? null;
|
|
$empty = $value === null || $value === false || (is_string($value) && trim($value) === '') || (is_array($value) && count($value) === 0);
|
|
if ($field['required'] && $empty) {
|
|
$errors[] = 'A value is required for ' . $name . '.';
|
|
continue;
|
|
}
|
|
if ($empty) {
|
|
continue;
|
|
}
|
|
|
|
if ($field['type'] === 'checkbox') {
|
|
$values = is_array($value) ? $value : [$value];
|
|
foreach ($values as $selectedValue) {
|
|
if (!is_string($selectedValue) && !is_int($selectedValue)) {
|
|
$errors[] = 'The value for ' . $name . ' is invalid.';
|
|
break;
|
|
}
|
|
if (!in_array((string) $selectedValue, $field['options'], true)) {
|
|
$errors[] = 'The value for ' . $name . ' is invalid.';
|
|
break;
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (!is_string($value) && !is_int($value) && !is_float($value)) {
|
|
$errors[] = 'The value for ' . $name . ' is invalid.';
|
|
continue;
|
|
}
|
|
if (($field['type'] === 'select' || $field['type'] === 'radio') && !in_array((string) $value, $field['options'], true)) {
|
|
$errors[] = 'The value for ' . $name . ' is invalid.';
|
|
}
|
|
if ($field['type'] === 'email' && filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
|
|
$errors[] = 'The email value for ' . $name . ' is invalid.';
|
|
}
|
|
}
|
|
|
|
return $errors;
|
|
}
|
|
|
|
private function failure(int $status, array $errors): array
|
|
{
|
|
return [
|
|
'status' => $status,
|
|
'body' => [
|
|
'success' => false,
|
|
'actions' => [],
|
|
'errors' => $errors,
|
|
],
|
|
];
|
|
}
|
|
|
|
private function isList(array $values): bool
|
|
{
|
|
return $values === [] || array_keys($values) === range(0, count($values) - 1);
|
|
}
|
|
} |