Fix graph_type variable (svg / png) (#15972)

This commit is contained in:
Warren Ashcroft 2024-04-26 14:33:08 +01:00 committed by GitHub
parent 443bfdb3b7
commit 1aa712e189
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 20 additions and 16 deletions

View File

@ -27,6 +27,7 @@ namespace LibreNMS\Data\Store;
use App\Polling\Measure\Measurement; use App\Polling\Measure\Measurement;
use LibreNMS\Config; use LibreNMS\Config;
use LibreNMS\Enum\ImageFormat;
use LibreNMS\Exceptions\FileExistsException; use LibreNMS\Exceptions\FileExistsException;
use LibreNMS\Exceptions\RrdGraphException; use LibreNMS\Exceptions\RrdGraphException;
use LibreNMS\Proc; use LibreNMS\Proc;
@ -596,8 +597,11 @@ class Rrd extends BaseDatastore
} }
// if valid image is returned with error, extract image and feedback // if valid image is returned with error, extract image and feedback
$image_type = Config::get('webui.graph_type', 'svg'); // rrdtool defaults to png if imgformat not specified
$search = $this->getImageEnd($image_type); $graph_type = preg_match('/--imgformat=([^\s]+)/', $options, $matches) ? strtolower($matches[1]) : 'png';
$imageFormat = ImageFormat::forGraph($graph_type);
$search = $imageFormat->getImageEnd();
if (($position = strrpos($process->getOutput(), $search)) !== false) { if (($position = strrpos($process->getOutput(), $search)) !== false) {
$position += strlen($search); $position += strlen($search);
throw new RrdGraphException( throw new RrdGraphException(
@ -615,16 +619,6 @@ class Rrd extends BaseDatastore
throw new RrdGraphException($error, null, null, null, $process->getExitCode()); throw new RrdGraphException($error, null, null, null, $process->getExitCode());
} }
private function getImageEnd(string $type): string
{
$image_suffixes = [
'png' => hex2bin('0000000049454e44ae426082'),
'svg' => '</svg>',
];
return $image_suffixes[$type] ?? '';
}
public function __destruct() public function __destruct()
{ {
$this->close(); $this->close();

View File

@ -41,4 +41,14 @@ enum ImageFormat: string
{ {
return $this->value == 'svg' ? 'image/svg+xml' : 'image/png'; return $this->value == 'svg' ? 'image/svg+xml' : 'image/png';
} }
public function getImageEnd(): string
{
$image_suffixes = [
'png' => hex2bin('0000000049454e44ae426082'),
'svg' => '</svg>',
];
return $image_suffixes[$this->value] ?? '';
}
} }

View File

@ -87,7 +87,7 @@ class Graph
throw $e; throw $e;
} }
return new GraphImage(ImageFormat::forGraph(), 'Error', $e->generateErrorImage()); return new GraphImage(ImageFormat::forGraph($vars['graph_type'] ?? null), 'Error', $e->generateErrorImage());
} }
} }

View File

@ -45,7 +45,7 @@ class GraphController extends Controller
throw $e; throw $e;
} }
return response($e->generateErrorImage(), 500, ['Content-type' => ImageFormat::forGraph()->contentType()]); return response($e->generateErrorImage(), 500, ['Content-type' => ImageFormat::forGraph($vars['graph_type'] ?? null)->contentType()]);
} }
} }
} }

View File

@ -96,9 +96,9 @@ try {
// output the graph // output the graph
if (\LibreNMS\Util\Debug::isEnabled()) { if (\LibreNMS\Util\Debug::isEnabled()) {
echo '<img src="data:' . ImageFormat::forGraph()->contentType() . ';base64,' . base64_encode($image_data) . '" alt="graph" />'; echo '<img src="data:' . ImageFormat::forGraph($vars['graph_type'] ?? null)->contentType() . ';base64,' . base64_encode($image_data) . '" alt="graph" />';
} else { } else {
header('Content-type: ' . ImageFormat::forGraph()->contentType()); header('Content-type: ' . ImageFormat::forGraph($vars['graph_type'] ?? null)->contentType());
echo (isset($vars['output']) && $vars['output'] === 'base64') ? base64_encode($image_data) : $image_data; echo (isset($vars['output']) && $vars['output'] === 'base64') ? base64_encode($image_data) : $image_data;
} }
} catch (\LibreNMS\Exceptions\RrdGraphException $e) { } catch (\LibreNMS\Exceptions\RrdGraphException $e) {