Skip to content

Commit

Permalink
Merge pull request #538 from DiamondLightSource/pre-release/Fix/LIMS-…
Browse files Browse the repository at this point in the history
…645/Make_parse_request_paramters_allow_string_and_other_types

Update string match to be more like original
  • Loading branch information
John-Holt-Tessella authored Apr 25, 2023
2 parents ab6a5bc + c728685 commit af2d012
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions api/src/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ function _parse_args()
$tmp = array();
foreach ($r->$k as $val)
{
if ($this->_string_match('/^' . $v . '$/m', $val))
if ($this->_match_pattern_to_value_unsafe('/^' . $v . '$/m', $val))
{
array_push($tmp, $v == '.*' ? $purifier->purify($val) : $val);
}
Expand All @@ -527,7 +527,7 @@ function _parse_args()
}
else
{
if ($this->_string_match('/^' . $v . '$/m', $r->$k))
if ($this->_match_pattern_to_value_unsafe('/^' . $v . '$/m', $r->$k))
{
$par[$k] = $v == '.*' ? $purifier->purify($r->$k) : $r->$k;
if ($k == 'prop')
Expand All @@ -554,7 +554,7 @@ function _parse_args()
$tmp = array();
foreach ($request[$k] as $val)
{
if ($this->_string_match('/^' . $v . '$/m', $val))
if ($this->_match_pattern_to_value_unsafe('/^' . $v . '$/m', $val))
{
array_push($tmp, $v == '.*' ? $purifier->purify($val) : $val);
}
Expand All @@ -572,7 +572,7 @@ function _parse_args()
$tmp = array();
foreach ($value as $value2)
{
if ($this->_string_match('/^' . $v . '$/m', $value2))
if ($this->_match_pattern_to_value_unsafe('/^' . $v . '$/m', $value2))
{
array_push($tmp, $v == '.*' ? $purifier->purify($value2) : $value2);
}
Expand All @@ -581,7 +581,7 @@ function _parse_args()
}
else
{
if ($this->_string_match('/^' . $v . '$/m', $value))
if ($this->_match_pattern_to_value_unsafe('/^' . $v . '$/m', $value))
{
$request[$k]->$key = $v == '.*' ? $purifier->purify($value) : $value;
}
Expand All @@ -607,7 +607,7 @@ function _parse_args()
$tmp = array();
foreach ($item as $element)
{
if ($this->_string_match('/^' . $v . '$/m', $element))
if ($this->_match_pattern_to_value_unsafe('/^' . $v . '$/m', $element))
{
array_push($tmp, $v == '.*' ? $purifier->purify($element) : $element);
}
Expand All @@ -616,7 +616,7 @@ function _parse_args()
}
else
{
if ($this->_string_match('/^' . $v . '$/m', $item))
if ($this->_match_pattern_to_value_unsafe('/^' . $v . '$/m', $item))
{
$object->$name = $v == '.*' ? $purifier->purify($item) : $item;
}
Expand All @@ -628,7 +628,7 @@ function _parse_args()
}
else
{
if ($this->_string_match('/^' . $v . '$/m', $request[$k]))
if ($this->_match_pattern_to_value_unsafe('/^' . $v . '$/m', $request[$k]))
{
$parsed[$k] = $v == '.*' ? $purifier->purify($request[$k]) : $request[$k];
}
Expand Down Expand Up @@ -673,13 +673,17 @@ function argOrNull($key)
# ------------------------------------------------------------------------
# Misc Helpers

function _string_match($pattern, $input)
/**
* Return 1 if pattern matches the input, 0 if not
* It is unsafe because if the value can not be converted to a string it is assume to match, this is to
* replicate older behaviour (particularly on simple sample page)
*/
function _match_pattern_to_value_unsafe($pattern, $input)
{
if (is_string($input))
{
return preg_match($pattern, $input);
}
return false;
if (is_null($input) || is_scalar($input) || (is_object($input) && method_exists($input, '__toString'))) {
return preg_match($pattern, strval($input));
}
return 1;
}

# Pretty-ish printer
Expand Down

0 comments on commit af2d012

Please sign in to comment.