2018-09-22 17:42:04 +03:00
|
|
|
<?php
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
|
|
class ParameterValidator
|
|
|
|
{
|
|
|
|
/**
|
2024-01-25 15:03:00 +03:00
|
|
|
* Validate and sanitize user inputs against configured bridge parameters (contexts)
|
2022-07-01 16:10:30 +03:00
|
|
|
*/
|
2024-01-25 15:03:00 +03:00
|
|
|
public function validateInput(array &$input, $contexts): array
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2024-01-25 15:03:00 +03:00
|
|
|
$errors = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-10-01 20:23:30 +03:00
|
|
|
foreach ($input as $name => $value) {
|
2022-07-01 16:10:30 +03:00
|
|
|
$registered = false;
|
2024-01-25 15:03:00 +03:00
|
|
|
foreach ($contexts as $contextName => $contextParameters) {
|
|
|
|
if (!array_key_exists($name, $contextParameters)) {
|
2023-10-01 20:23:30 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$registered = true;
|
2024-01-25 15:03:00 +03:00
|
|
|
if (!isset($contextParameters[$name]['type'])) {
|
2023-10-01 20:23:30 +03:00
|
|
|
// Default type is text
|
2024-01-25 15:03:00 +03:00
|
|
|
$contextParameters[$name]['type'] = 'text';
|
2023-10-01 20:23:30 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-01-25 15:03:00 +03:00
|
|
|
switch ($contextParameters[$name]['type']) {
|
2023-10-01 20:23:30 +03:00
|
|
|
case 'number':
|
|
|
|
$input[$name] = $this->validateNumberValue($value);
|
|
|
|
break;
|
|
|
|
case 'checkbox':
|
|
|
|
$input[$name] = $this->validateCheckboxValue($value);
|
|
|
|
break;
|
|
|
|
case 'list':
|
2024-01-25 15:03:00 +03:00
|
|
|
$input[$name] = $this->validateListValue($value, $contextParameters[$name]['values']);
|
2023-10-01 20:23:30 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 'text':
|
2024-01-25 15:03:00 +03:00
|
|
|
if (isset($contextParameters[$name]['pattern'])) {
|
|
|
|
$input[$name] = $this->validateTextValue($value, $contextParameters[$name]['pattern']);
|
2023-10-01 20:23:30 +03:00
|
|
|
} else {
|
|
|
|
$input[$name] = $this->validateTextValue($value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-10-01 20:23:30 +03:00
|
|
|
if (
|
|
|
|
is_null($input[$name])
|
2024-01-25 15:03:00 +03:00
|
|
|
&& isset($contextParameters[$name]['required'])
|
|
|
|
&& $contextParameters[$name]['required']
|
2023-10-01 20:23:30 +03:00
|
|
|
) {
|
2024-01-25 15:03:00 +03:00
|
|
|
$errors[] = ['name' => $name, 'reason' => 'Parameter is invalid!'];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$registered) {
|
2024-01-25 15:03:00 +03:00
|
|
|
$errors[] = ['name' => $name, 'reason' => 'Parameter is not registered!'];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-25 15:03:00 +03:00
|
|
|
return $errors;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the context matching the provided inputs
|
|
|
|
*
|
2023-10-01 20:23:30 +03:00
|
|
|
* @param array $input Associative array of user data
|
2024-01-25 15:03:00 +03:00
|
|
|
* @param array $contexts Array of bridge parameters
|
2022-07-01 16:10:30 +03:00
|
|
|
* @return string|null Returns the context name or null if no match was found
|
|
|
|
*/
|
2024-01-25 15:03:00 +03:00
|
|
|
public function getQueriedContext(array $input, array $contexts)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
|
|
|
$queriedContexts = [];
|
|
|
|
|
|
|
|
// Detect matching context
|
2024-01-25 15:03:00 +03:00
|
|
|
foreach ($contexts as $contextName => $contextParameters) {
|
|
|
|
$queriedContexts[$contextName] = null;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
|
|
// Ensure all user data exist in the current context
|
2024-01-25 15:03:00 +03:00
|
|
|
$notInContext = array_diff_key($input, $contextParameters);
|
|
|
|
if (array_key_exists('global', $contexts)) {
|
|
|
|
$notInContext = array_diff_key($notInContext, $contexts['global']);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2022-08-06 23:46:28 +03:00
|
|
|
if (count($notInContext) > 0) {
|
2022-07-01 16:10:30 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if all parameters of the context are satisfied
|
2024-01-25 15:03:00 +03:00
|
|
|
foreach ($contextParameters as $id => $properties) {
|
|
|
|
if (!empty($input[$id])) {
|
|
|
|
$queriedContexts[$contextName] = true;
|
2022-07-01 16:10:30 +03:00
|
|
|
} elseif (
|
|
|
|
isset($properties['type'])
|
|
|
|
&& ($properties['type'] === 'checkbox' || $properties['type'] === 'list')
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
} elseif (isset($properties['required']) && $properties['required'] === true) {
|
2024-01-25 15:03:00 +03:00
|
|
|
$queriedContexts[$contextName] = false;
|
2022-07-01 16:10:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort if one of the globally required parameters is not satisfied
|
|
|
|
if (
|
2024-01-25 15:03:00 +03:00
|
|
|
array_key_exists('global', $contexts)
|
2022-07-01 16:10:30 +03:00
|
|
|
&& $queriedContexts['global'] === false
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
unset($queriedContexts['global']);
|
|
|
|
|
|
|
|
switch (array_sum($queriedContexts)) {
|
2022-08-06 23:46:28 +03:00
|
|
|
case 0:
|
|
|
|
// Found no match, is there a context without parameters?
|
2023-10-01 20:23:30 +03:00
|
|
|
if (isset($input['context'])) {
|
|
|
|
return $input['context'];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
2024-01-25 15:03:00 +03:00
|
|
|
foreach ($queriedContexts as $context2 => $queried) {
|
2022-07-01 16:10:30 +03:00
|
|
|
if (is_null($queried)) {
|
2024-01-25 15:03:00 +03:00
|
|
|
return $context2;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2022-08-06 23:46:28 +03:00
|
|
|
case 1:
|
|
|
|
// Found unique match
|
2022-07-01 16:10:30 +03:00
|
|
|
return array_search(true, $queriedContexts);
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 20:23:30 +03:00
|
|
|
|
|
|
|
private function validateTextValue($value, $pattern = null)
|
|
|
|
{
|
|
|
|
if (is_null($pattern)) {
|
|
|
|
// No filtering taking place
|
|
|
|
$filteredValue = filter_var($value);
|
|
|
|
} else {
|
|
|
|
$filteredValue = filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^' . $pattern . '$/']]);
|
|
|
|
}
|
|
|
|
if ($filteredValue === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validateNumberValue($value)
|
|
|
|
{
|
|
|
|
$filteredValue = filter_var($value, FILTER_VALIDATE_INT);
|
|
|
|
if ($filteredValue === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validateCheckboxValue($value)
|
|
|
|
{
|
|
|
|
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validateListValue($value, $expectedValues)
|
|
|
|
{
|
|
|
|
$filteredValue = filter_var($value);
|
|
|
|
if ($filteredValue === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!in_array($filteredValue, $expectedValues)) {
|
|
|
|
// Check sub-values?
|
|
|
|
foreach ($expectedValues as $subName => $subValue) {
|
|
|
|
if (is_array($subValue) && in_array($filteredValue, $subValue)) {
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $filteredValue;
|
|
|
|
}
|
2018-09-22 17:42:04 +03:00
|
|
|
}
|