1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-16 03:34:33 +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

@@ -1031,6 +1031,26 @@ $config->wireInputOrder = 'get post';
*/ */
$config->wireInputLazy = false; $config->wireInputLazy = false;
/**
* Maximum depth for input variables accessed from $input
*
* A value of 1 (or less) means only single dimensional arrays are allowed. For instance, `$a['foo']`
* would be allowed (since it is one dimension), but `$a['foo']['bar']` would not because it is
* multi-dimensional to a depth of 2.
*
* A value of 2 or higher enables multi-dimensional arrays to that depth. For instance, a value of 2
* would allow `$a['foo']['bar']` and a value of 3 or higher would enable `$a['foo']['bar']['baz']`.
*
* Note: if your use case requires multi-dimensional input arrays and you do not have control over
* this setting (like if you are a 3rd party module author), or are using a version of PW prior to
* 3.0.178 you can always still access multi-dimensional arrays directly from `$_GET` or `$_POST`.
*
* @var int
* @since 3.0.178
*
*/
$config->wireInputArrayDepth = 1;
/** /**
* Options for setting cookies from $input->cookie()->set(...) * Options for setting cookies from $input->cookie()->set(...)
* *

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 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 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 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 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 * @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 * 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 * @param array $a
* @return array * @return array
* *
*/ */
protected function cleanArray(array $a) { protected function cleanArray(array $a) {
static $depth = 1;
$maxDepth = (int) $this->wire()->config->wireInputArrayDepth;
if($maxDepth < 1) $maxDepth = 1;
$clean = array(); $clean = array();
foreach($a as $key => $value) { foreach($a as $key => $value) {
if(is_array($value)) continue; // we only allow one dimensional arrays if(is_array($value)) {
if(is_string($value) && $this->stripSlashes) $value = stripslashes($value); if($depth >= $maxDepth) {
$clean[$key] = $value; // 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; return $clean;
} }