From cc0aef7b2c6ba7205329b93fb95f0bdceaa89d1c Mon Sep 17 00:00:00 2001 From: Sam Wilson Date: Thu, 23 May 2024 10:24:25 +0200 Subject: [PATCH] Use replacement placeholders in the render command (#767) * Switch to using process command placeholders rather than environment variables in constructing the rsvg-convert command. * Only pass required placeholders. * Use Symfony's AcceptHeader class to reformat the lang value. Bug: T365644 --- src/Service/Renderer.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Service/Renderer.php b/src/Service/Renderer.php index cd750a3a..f128edc6 100644 --- a/src/Service/Renderer.php +++ b/src/Service/Renderer.php @@ -4,6 +4,7 @@ namespace App\Service; +use Symfony\Component\HttpFoundation\AcceptHeader; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; @@ -35,19 +36,23 @@ public function __construct(string $rsvgCommand) public function render(string $file, string $lang, ?string $outFile = null) : string { // Construct the command, using variables that will be escaped when it's run. - $command = $this->rsvgCommand.' "$SVG"'; + $command = $this->rsvgCommand.' "${:SVG}"'; + $env = ['SVG' => $file]; if ('fallback' !== $lang) { // Set the language to use from the SVG systemLanguage. // If the fallback language is being requested, the OS's default will be // used instead (as is done in MediaWiki). - $command .= " --accept-language=$lang"; + $command .= ' --accept-language="${:LANG}"'; + // Reformat the lang as an accept header. + $env['LANG'] = (string)AcceptHeader::fromString($lang); } if ($outFile) { // Redirect to output file if required. - $command .= ' > "$PNG"'; + $command .= ' > "${:PNG}"'; + $env['PNG'] = $outFile; } $process = Process::fromShellCommandline($command); - $process->mustRun(null, ['SVG' => $file, 'PNG' => $outFile]); + $process->mustRun(null, $env); return $process->getOutput(); } }