From 72475b16ef2221986acf72c82b539d9b8c95ba6c Mon Sep 17 00:00:00 2001 From: billz Date: Tue, 26 Nov 2024 14:30:14 -0800 Subject: [PATCH] Remove ANSI escape sequences w/ stripArtifacts() --- includes/provider.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/includes/provider.php b/includes/provider.php index 38292e78..e7bc5905 100755 --- a/includes/provider.php +++ b/includes/provider.php @@ -129,16 +129,33 @@ function saveProviderConfig($status, $binPath, $country, $id = null) } /** - * Removes artifacts from shell_exec string values + * Removes artifacts from shell_exec string values and lines with ANSI escape sequences * - * @param string $output - * @param string $pattern - * @return string $result + * @param string|array $output + * @param string|null $pattern + * @return string|array $result */ function stripArtifacts($output, $pattern = null) { - $result = preg_replace('/[-\/\n\t\\\\'.$pattern.'|]/', '', $output); - return $result; + if (is_array($output)) { + return array_map(function ($line) use ($pattern) { + return stripArtifacts($line, $pattern); + }, $output); + } + if (!is_string($output)) { + return $output; + } + + $lines = explode("\n", $output); + $lines = array_filter($lines, function ($line) use ($pattern) { + // remove ANSI escape sequences + if (preg_match('/\x1b\[[0-9;]*[a-zA-Z]/', $line)) { + return false; + } + $line = preg_replace('/[-\/\t\\\\' . preg_quote($pattern, '/') . '|]/', '', $line); + return trim($line) !== ''; + }); + return implode("\n", $lines); } /**