Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

fix(BE): better handling PNG export failure #7823

Merged
merged 4 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions www/class/centreonGraph.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1244,22 +1244,38 @@ public function addArgument($arg)
}

/**
* Geneate image...
* Instead of crashing, a message should be send when the PNG fail to be generated
*
* @param string $message The message to be displayed if the other cases didn't match.
* Optional : to avoid border effects of legacy code
*/
public static function displayError()
public static function displayError(string $message = "")
{
$image = imagecreate(250, 100);
$fond = imagecolorallocate($image, 0xEF, 0xF2, 0xFB);
$image = imagecreate(500, 100);

// background color
imagecolorallocate($image, 0xEF, 0xF2, 0xFB);

// font color
$textcolor = imagecolorallocate($image, 0, 0, 255);
// imagestring($image, 5, 0, 0, "Session: ".$_GET['session_id']."svc_id: ".$_GET["index"], $textcolor);

/*
* Send Header
*/
header("Content-Type: image/gif");
$str = "Error : ";
if (!empty($_GET['session_id'])) {
$str .= "Session = " . $_GET['session_id'];
} elseif (!empty($_GET['index'])) {
$str .= "SVC_Id = " . $_GET['index'];
} elseif (!empty($message)) {
$str .= $message;
} else {
$str .= "An undefined error occurred";
}

// generating the PNG with the error message
imagestring($image, 5, 0, 0, $str, $textcolor);
header("Content-Type: image/png");

imagegif($image);
exit;
// making the PNG to be saved instead of the graph
imagepng($image);
loiclau marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
71 changes: 31 additions & 40 deletions www/include/views/graphs/generateGraphs/generateMetricImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,68 +50,67 @@
$pearDB = new CentreonDB();
$pearDBO = new CentreonDB('centstorage');

/*
* If an error occurs in this block, the PNG won't be generated and may crash the browser
* To avoid it, we'll generate a PNG with the error message to be displayed using displayError() method
*/
if (!CentreonSession::checkSession($sid, $pearDB)) {
CentreonGraph::displayError();
CentreonGraph::displayError("Wrong sessionId or missing");
}

if (!isset($_GET['index']) && !isset($_GET['svcId'])) {
CentreonGraph::displayError();
CentreonGraph::displayError("Index and svcId arguments are missing");
}

if (isset($_GET['index'])) {
if (false === is_numeric($_GET['index'])) {
CentreonGraph::displayError();
if (!is_numeric($_GET['index'])) {
CentreonGraph::displayError("Index is not an integer");
}
$index = $_GET['index'];
} else {
list($hostId, $svcId) = explode('_', $_GET['svcId']);
if (!is_numeric($hostId) || !is_numeric($svcId)) {
CentreonGraph::displayError();
CentreonGraph::displayError("Host or service Id is not an integer");
}
$res = $pearDBO->prepare(
'SELECT id FROM index_data
WHERE host_id = :hostId AND service_id = :svcId'
);
$res->bindValue(':hostId', $hostId, \PDO::PARAM_INT);
$res->bindValue(':svcId', $svcId, \PDO::PARAM_INT);
$res->execute();

if (!$res) {
CentreonGraph::displayError();
}
$row = $res->fetch();
if (!$row) {
CentreonGraph::displayError();
try {
$res->bindValue(':hostId', $hostId, \PDO::PARAM_INT);
$res->bindValue(':svcId', $svcId, \PDO::PARAM_INT);
$res->execute();
$row = $res->fetch();
$index = $row['id'];
} catch (\PDOException $e) {
CentreonGraph::displayError("SQL request thrown an error");
}
$index = $row['id'];
}

if (!$index) {
CentreonGraph::displayError("Index not found");
}

/*
* As everything went fine, we should be able to generate a PNG from the RRD data
*/
require_once _CENTREON_PATH_ . "www/include/common/common-Func.php";
$contactId = CentreonSession::getUser($sid, $pearDB);
$obj = new CentreonGraph($contactId, $index, 0, 1);

/**
* Set One curve
**/
// Set One curve
$obj->onecurve = true;

/**
* Set metric id
*/
// Set metric id
if (isset($_GET["metric"])) {
$obj->setMetricList($_GET["metric"]);
}

/**
* Set arguments from GET
*/
// Set arguments from GET
$obj->setRRDOption("start", $obj->checkArgument("start", $_GET, time() - (60 * 60 * 48)));
$obj->setRRDOption("end", $obj->checkArgument("end", $_GET, time()));

/**
* Template Management
*/
// Template Management
if (isset($_GET["template_id"])) {
$obj->setTemplate($_GET["template_id"]);
} else {
Expand All @@ -125,26 +124,18 @@

$obj->initCurveList();

/**
* Comment time
*/
// Comment time
$obj->setOption("comment_time");

/**
* Create Legende
*/
// Create Legend
$obj->createLegend();

/**
* Set Colors
*/
// Set Colors
$obj->setColor('BACK', '#FFFFFF');
$obj->setColor('FRAME', '#FFFFFF');
$obj->setColor('SHADEA', '#EFEFEF');
$obj->setColor('SHADEB', '#EFEFEF');
$obj->setColor('ARROW', '#FF0000');

/**
* Display Images Binary Data
*/
// Display Images Binary Data
$obj->displayImageFlow();