From 5c713ba53c7d1dc6b180f6a5089aa87ed855eca2 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 10 Aug 2023 13:12:05 +0300 Subject: [PATCH] Fix Closes #45 --- tests/webfiori/tests/http/APIFilterTest.php | 26 +++++++++++++++++++++ webfiori/http/APIFilter.php | 20 +++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/tests/webfiori/tests/http/APIFilterTest.php b/tests/webfiori/tests/http/APIFilterTest.php index 23d8a2a..989e704 100644 --- a/tests/webfiori/tests/http/APIFilterTest.php +++ b/tests/webfiori/tests/http/APIFilterTest.php @@ -464,6 +464,32 @@ public function testFilterGet23() { $this->assertEquals(1,count($filtered)); $this->assertEquals([false, "Hello", null, "World"],$filtered['array']); } + /** + * @test + */ + public function testFilterGet31() { + $this->apiFilter = new APIFilter(); + $param00 = new RequestParameter('array', 'array'); + $this->apiFilter->addRequestParameter($param00); + $_GET['array'] = [false, "Hello", null, "World"]; + $this->apiFilter->filterGET(); + $filtered = $this->apiFilter->getInputs(); + $this->assertEquals(1,count($filtered)); + $this->assertEquals([false, "Hello", null, "World"],$filtered['array']); + } + /** + * @test + */ + public function testFilterGet32() { + $this->apiFilter = new APIFilter(); + $param00 = new RequestParameter('array', 'array'); + $this->apiFilter->addRequestParameter($param00); + $_GET['array'] = [false, ["Hello"], null, "World"]; + $this->apiFilter->filterGET(); + $filtered = $this->apiFilter->getInputs(); + $this->assertEquals(1,count($filtered)); + $this->assertEquals([false, ["Hello"], null, "World"],$filtered['array']); + } /** * @test */ diff --git a/webfiori/http/APIFilter.php b/webfiori/http/APIFilter.php index 32c9710..944b0dc 100644 --- a/webfiori/http/APIFilter.php +++ b/webfiori/http/APIFilter.php @@ -168,7 +168,11 @@ public static function filter(APIFilter $apiFilter, array $arr): array { $defaultVal = $def[$paramIdx]->getDefault(); if (isset($arr[$name])) { - $toBeFiltered = urldecode($arr[$name]); + if (gettype($arr[$name]) != 'array') { + $toBeFiltered = urldecode($arr[$name]); + } else { + $toBeFiltered = self::decodeArray($arr[$name]); + } $retVal[$noFIdx][$name] = $toBeFiltered; if (isset($def[$optIdx]['filter-func'])) { @@ -194,6 +198,17 @@ public static function filter(APIFilter $apiFilter, array $arr): array { return $retVal; } + private static function decodeArray(array $array) { + $retVal = []; + foreach ($array as $arrEl) { + if (gettype($arrEl) == 'array') { + $retVal[] = self::decodeArray($arrEl); + } else { + $retVal[] = urldecode($arrEl.''); + } + } + return $retVal; + } /** * Validate and sanitize GET parameters. * @@ -341,6 +356,9 @@ public function setInputStream($pathOrResource) : bool { return false; } private static function applyBasicFilterOnly($def,$toBeFiltered) { + if (gettype($toBeFiltered) == 'array') { + return $toBeFiltered; + } $toBeFiltered = strip_tags($toBeFiltered); $paramObj = $def['parameter'];