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

Commit

Permalink
fix(BE): exported PNG should display the error
Browse files Browse the repository at this point in the history
  • Loading branch information
sc979 committed Aug 30, 2019
1 parent f78fec0 commit 492f5f0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 41 deletions.
36 changes: 26 additions & 10 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
*/
$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/gif");

imagegif($image);
exit;
// making the PNG to be saved instead of the graph
imagepng($image);
}

/**
Expand Down
56 changes: 25 additions & 31 deletions www/include/views/graphs/generateGraphs/generateMetricImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,27 @@
$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
Expand All @@ -77,38 +81,36 @@
$res->bindValue(':svcId', $svcId, \PDO::PARAM_INT);
$res->execute();
$row = $res->fetch();
//$index = $row['id'];
$index = $row['id'];
} catch (\PDOException $e) {
CentreonGraph::displayError();
CentreonGraph::displayError("SQL request thrown an error");
}
}

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 @@ -122,26 +124,18 @@

$obj->initCurveList();

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

/**
* Create Legend
*/
// 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();

0 comments on commit 492f5f0

Please sign in to comment.