diff options
| author | 2019-03-19 20:27:06 +0100 | |
|---|---|---|
| committer | 2019-03-19 20:27:06 +0100 | |
| commit | 71b4226dc721bc0f23cc594760329f29e51defac (patch) | |
| tree | 7a90f25cd21e882b64cd6ed31f646226d80307b3 /cli/_cli.php | |
| parent | 834ffacce22ff6a2c0f1459476dc4a45e8ea06f9 (diff) | |
Add an option validation on cli commands (#2278)
If an option used on cli is not recognized, the command
aborts and displays an error message.
If the typed option is similar to one of the recognized
options, a hint is displayed.
At the moment, there is a limitation on long options.
Short options are not validated at the moment.
See #2046
Diffstat (limited to 'cli/_cli.php')
| -rw-r--r-- | cli/_cli.php | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/cli/_cli.php b/cli/_cli.php index e8fb6ae42..dec244bc3 100644 --- a/cli/_cli.php +++ b/cli/_cli.php @@ -3,6 +3,9 @@ if (php_sapi_name() !== 'cli') { die('FreshRSS error: This PHP script may only be invoked from command line!'); } +const REGEX_INPUT_OPTIONS = '/^--/'; +const REGEX_PARAM_OPTIONS = '/:*$/'; + require(__DIR__ . '/../constants.php'); require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader require(LIB_PATH . '/lib_install.php'); @@ -64,3 +67,25 @@ function performRequirementCheck($databaseType) { fail($message); } } + +function getLongOptions($options, $regex) { + $longOptions = array_filter($options, function($a) use ($regex) { + return preg_match($regex, $a); + }); + return array_map(function($a) use ($regex) { + return preg_replace($regex, '', $a); + }, $longOptions); +} + +function validateOptions($input, $params) { + $sanitizeInput = getLongOptions($input, REGEX_INPUT_OPTIONS); + $sanitizeParams = getLongOptions($params, REGEX_PARAM_OPTIONS); + $unknownOptions = array_diff($sanitizeInput, $sanitizeParams); + + if (0 === count($unknownOptions)) { + return true; + } + + fwrite(STDERR, sprintf("FreshRSS error: unknown options: %s\n", implode (', ', $unknownOptions))); + return false; +} |
