diff --git a/www/class/centreonGraph.class.php b/www/class/centreonGraph.class.php index 3123bb472e2..e760fcf674c 100644 --- a/www/class/centreonGraph.class.php +++ b/www/class/centreonGraph.class.php @@ -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); } /** diff --git a/www/include/views/graphs/generateGraphs/generateMetricImage.php b/www/include/views/graphs/generateGraphs/generateMetricImage.php index 0f8fdadc07a..e56d1475d0c 100644 --- a/www/include/views/graphs/generateGraphs/generateMetricImage.php +++ b/www/include/views/graphs/generateGraphs/generateMetricImage.php @@ -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 @@ -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 { @@ -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();