From fee03668f5217552fecee2230c3c708000c00b99 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Wed, 20 Nov 2024 16:59:07 +0100 Subject: [PATCH] Improve test_form_rule command to accept JSON formatted parameters Example: ldapsaisie \ -L LSformRule test_form_rule zxcvbn \ -p customDictionaries="$( jo dict1=/path/to/dict1.json dict2=/path/to/dict2.json )" \ -p minScore=3 \ S3cRet2TeST --- src/includes/class/class.LScli.php | 61 +++++++++++++++++++++++++ src/includes/class/class.LSformRule.php | 4 +- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/includes/class/class.LScli.php b/src/includes/class/class.LScli.php index a9d9cb23..c66c1fc8 100644 --- a/src/includes/class/class.LScli.php +++ b/src/includes/class/class.LScli.php @@ -43,6 +43,16 @@ class LScli extends LSlog_staticLoggerClass { */ private static $current_command = null; + /** + * Array of custom CLI argument value with them CLI value as key and PHP value as value + * @var array + */ + public static $custom_cli_arg_values = [ + "null" => null, + "true" => true, + "false" => false, + ]; + /** * Add a CLI command * @@ -843,6 +853,57 @@ class LScli extends LSlog_staticLoggerClass { return $quote_char . str_replace($quote_char, "\\$quote_char", $word) . $quote_char; } + /** + * Parse CLI argument value + * @param string $value + * @param array|null $custom_values Extra custom CLI argument value + * (optional, will be merged with self::$custom_cli_arg_values, + * CLI value (=key), must be passed in lowercase) + * @return mixed + */ + public static function parse_arg_value($value, $custom_values=null) { + $custom_values = array_merge( + self :: $custom_cli_arg_values, + ensureIsArray($custom_values) + ); + if ($value && in_array($value[0], ["{", "["])) { + // With explicit type specified (format: "[type]value") + if (preg_match("/^\[(string|str|bool|boolean|int|integer|float|array)\](.+)$/i", $value, $m)) { + switch(strtolower($m[1])) { + case "string": + case "str": + return strval($m[2]); + case "boolean": + case "bool": + return boolval($m[2]); + case "integer": + case "int": + return intval($m[2]); + case "float": + return floatval($m[2]); + case "array": + return ensureIsArray( + self :: parse_arg_value($m[2]) + ); + } + } + // Otherwise, consider as JSON encoded value + try { + $value = json_decode($value, true, 512, JSON_THROW_ON_ERROR); + } + catch (\JsonException $ex) { + self :: usage("Fail to decode JSON argument '$value': ".$ex->getMessage()); + } + } + else if (array_key_exists(strtolower($value), $custom_values)) { + $value = $custom_values[strtolower($value)]; + } + else if (is_numeric($value)) { + $value = floatval($value); + } + return $value; + } + } /* diff --git a/src/includes/class/class.LSformRule.php b/src/includes/class/class.LSformRule.php index 8ff16147..3e1f1983 100644 --- a/src/includes/class/class.LSformRule.php +++ b/src/includes/class/class.LSformRule.php @@ -126,12 +126,12 @@ class LSformRule extends LSlog_staticLoggerClass { if (in_array($command_args[$i], array('-p', '--param'))) { $i++; LScli :: unquote_word($command_args[$i]); - $param_parts = explode('=', $command_args[$i]); + $param_parts = explode('=', $command_args[$i], 2); if (count($param_parts) != 2) LScli :: usage('Invalid parameter string ('.$command_args[$i].').'); if (array_key_exists($param_parts[0], $params)) LScli :: usage('Parameter "'.$param_parts[0].'" already specified.'); - $params[$param_parts[0]] = $param_parts[1]; + $params[$param_parts[0]] = LScli :: parse_arg_value($param_parts[1]); } else if (is_null($rule_name)) { $rule_name = $command_args[$i];