From 7e119aa8e3539167a9b81952d9f1f9cdb89a4d8a Mon Sep 17 00:00:00 2001 From: datagutten Date: Sat, 11 Apr 2020 01:56:05 +0900 Subject: [PATCH] Use symfony/process instead of shell_exec --- composer.json | 7 +++--- composer.lock | 65 ++++++++++++++++++++++++++++++++++++++++++++++++- src/Chardet.php | 10 +++++--- src/Checks/Checksum.php | 5 +++- 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index fb0ad2e..40f872a 100644 --- a/composer.json +++ b/composer.json @@ -18,9 +18,10 @@ "require": { "php": ">=7.2", "ext-mbstring": "*", - "symfony/console": "^3.4", - "symfony/debug": "^3.4", - "symfony/yaml": "^3.4" + "symfony/console": "^3.4 | ^4.0 | ^5.0", + "symfony/debug": "^3.4 | ^4.0 | ^5.0", + "symfony/yaml": "^3.4 | ^4.0 | ^5.0", + "symfony/process": "^3.4 | ^4.0 | ^5.0" }, "bin": ["bin/logchecker"], "require-dev": { diff --git a/composer.lock b/composer.lock index a664eba..88fc91c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cd99ec05b86608c741f0f4cdf62b27d9", + "content-hash": "2f3702b4303474d7c0f6960a7bb1cdc3", "packages": [ { "name": "psr/log", @@ -354,6 +354,69 @@ ], "time": "2020-03-09T19:04:49+00:00" }, + { + "name": "symfony/process", + "version": "v5.0.7", + "source": { + "type": "git", + "url": "https://github.com/symfony/process.git", + "reference": "c5ca4a0fc16a0c888067d43fbcfe1f8a53d8e70e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/process/zipball/c5ca4a0fc16a0c888067d43fbcfe1f8a53d8e70e", + "reference": "c5ca4a0fc16a0c888067d43fbcfe1f8a53d8e70e", + "shasum": "" + }, + "require": { + "php": "^7.2.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.0-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Process\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Process Component", + "homepage": "https://symfony.com", + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2020-03-27T16:56:45+00:00" + }, { "name": "symfony/yaml", "version": "v3.4.39", diff --git a/src/Chardet.php b/src/Chardet.php index 9d18732..8566e4a 100644 --- a/src/Chardet.php +++ b/src/Chardet.php @@ -3,6 +3,8 @@ namespace OrpheusNET\Logchecker; use OrpheusNET\Logchecker\Exception\FileNotFoundException; +use Symfony\Component\Process\Exception\ProcessFailedException; +use Symfony\Component\Process\Process; class Chardet { @@ -33,13 +35,15 @@ class Chardet throw new FileNotFoundException($filename); } - $output = shell_exec($this->executable . " " . escapeshellarg($filename)); + $process = new Process([$this->executable, $filename]); + $process->run(); + // Following regex: // matches[1] - file path // matches[2] - charset // matches[3] - confidence - - if ((preg_match('/(.+): (.+) .+confidence:? ([^\)]+)/', $output, $matches) === 0)) { + + if ((preg_match('/(.+): (.+) .+confidence:? ([^\)]+)/', $process->getOutput(), $matches) === 0)) { throw new \Exception('This file is not analyzed'); } elseif (isset($matches[2]) && $matches[2] === 'None') { throw new \Exception('Could not determine character set'); diff --git a/src/Checks/Checksum.php b/src/Checks/Checksum.php index 3aa1e0d..f5e00b4 100644 --- a/src/Checks/Checksum.php +++ b/src/Checks/Checksum.php @@ -3,6 +3,7 @@ namespace OrpheusNET\Logchecker\Checks; use OrpheusNET\Logchecker\Util; +use Symfony\Component\Process\Process; class Checksum { @@ -25,7 +26,9 @@ class Checksum } if (static::logcheckerExists($command)) { - $output = shell_exec("{$command} " . escapeshellarg($logPath)); + $process = new Process([$command, $logPath]); + $process->run(); + $output = $process->getOutput(); if (strpos($output, $goodResult) === false) { if ($output == null) { return ChecksumStates::CHECKSUM_MISSING;