1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Add support for multi-dimensional arrays in $input with a $config->wireInputArrayDepth setting. This was requested in prior feature requests and in PR #161 but ended up writing it in a different way than the PR to allow for control over the allowed max dimension and consistency with multidimensional arrays in $_GET or $_POST. To enable multi-dimensional array support add $config->wireInputArrayDepth = 2; to /site/config.php and use a value of 2 (or higher) to indicate the number of dimensions you want to support.

Co-authored-by: porl <porl42@gmail.com>
This commit is contained in:
Ryan Cramer
2021-05-12 09:05:28 -04:00
parent e7b71d796f
commit 143ae7535b
3 changed files with 49 additions and 4 deletions

View File

@@ -92,6 +92,7 @@
* @property int $maxUrlDepth Maximum URL/path slashes (depth) for request URLs. (Min=10, Max=60) #pw-group-URLs
* @property string $wireInputOrder Order that variables with the $input API var are handled when you access $input->var. #pw-group-HTTP-and-input
* @property bool $wireInputLazy Specify true for $input API var to load input data in a lazy fashion and potentially use less memory. Default is false. #pw-group-HTTP-and-input
* @property int $wireInputArrayDepth Maximum multi-dimensional array depth for input variables accessed from $input or 1 to only allow single dimension arrays. (3.0.178+). #pw-group-HTTP-and-input
* @property array $cookieOptions Options for setting cookies from $input->cookie #pw-group-HTTP-and-input
*
* @property bool $advanced Special mode for ProcessWire system development. Not recommended for regular site development or production use. #pw-group-system

View File

@@ -330,19 +330,43 @@ class WireInputData extends Wire implements \ArrayAccess, \IteratorAggregate, \C
/**
* Clean an array of data
*
* Removes multi-dimensional arrays and slashes (if applicable)
* Support multi-dimensional arrays consistent with `$config->wireInputArrayDepth`
* setting (3.0.178+) and remove slashes if applicable/necessary.
*
* @param array $a
* @return array
*
*/
protected function cleanArray(array $a) {
static $depth = 1;
$maxDepth = (int) $this->wire()->config->wireInputArrayDepth;
if($maxDepth < 1) $maxDepth = 1;
$clean = array();
foreach($a as $key => $value) {
if(is_array($value)) continue; // we only allow one dimensional arrays
if(is_string($value) && $this->stripSlashes) $value = stripslashes($value);
$clean[$key] = $value;
if(is_array($value)) {
if($depth >= $maxDepth) {
// max dimension reached
$value = null;
} else {
// allow another dimension
$depth++;
$value = $this->cleanArray($value);
$depth--;
// empty arrays not possible in input vars past 1st dimension
if(!count($value)) $value = null;
}
} else if(is_string($value)) {
if($this->stripSlashes) $value = stripslashes($value);
}
if($value !== null) {
$clean[$key] = $value;
}
}
return $clean;
}