Commit 99147870 authored by Kevin Duret's avatar Kevin Duret
Browse files

fix(kb): fix mediawiki url to templates (#9343)

Refs: MON-6321 MON-6531
parent a6061fdc
......@@ -36,26 +36,22 @@
require_once _CENTREON_PATH_ . '/www/include/configuration/configKnowledge/functions.php';
require_once _CENTREON_PATH_ . '/www/class/centreonHost.class.php';
require_once _CENTREON_PATH_ . '/www/class/centreonService.class.php';
require_once _CENTREON_PATH_ . '/www/class/centreon-knowledge/wikiApi.class.php';
class procedures_Proxy
class ProceduresProxy
{
private $DB;
private $hflag;
private $sflag;
private $proc;
private $wikiUrl;
private $hostObj;
private $serviceObj;
/**
* procedures_Proxy constructor.
* ProceduresProxy constructor.
* @param $pearDB
*/
public function __construct($pearDB)
{
$this->DB = $pearDB;
$this->hflag = 0;
$this->sflag = 0;
$this->hostObj = new CentreonHost($this->DB);
$this->serviceObj = new CentreonService($this->DB);
......@@ -82,11 +78,13 @@ class procedures_Proxy
}
/**
* Get service id from hostname and service description
*
* @param string $hostName
* @param string $serviceDescription
* @return mixed
* @return int|null
*/
private function getServiceId($hostName, $serviceDescription)
private function getServiceId($hostName, $serviceDescription): ?int
{
/*
* Get Services attached to hosts
......@@ -99,10 +97,11 @@ class procedures_Proxy
"AND h.host_name LIKE '" . $hostName . "' " .
"AND s.service_description LIKE '" . $serviceDescription . "' "
);
while ($row = $result->fetch()) {
return $row["service_id"];
if ($row = $result->fetch()) {
return (int) $row["service_id"];
}
$result->closeCursor();
/*
* Get Services attached to hostgroups
*/
......@@ -115,61 +114,96 @@ class procedures_Proxy
"AND service_id = hsr.service_service_id " .
"AND service_description LIKE '" . $serviceDescription . "' "
);
while ($row = $result->fetch()) {
return $row["service_id"];
if ($row = $result->fetch()) {
return (int) $row["service_id"];
}
$result->closeCursor();
return null;
}
/**
* @param string $hostName
* Get service notes url
*
* @param int $serviceId
* @return string|null
*/
public function getHostUrl($hostName)
private function getServiceNotesUrl(int $serviceId): ?string
{
$this->proc->setHostInformations();
$notesUrl = null;
$statement = $this->DB->prepare(
"SELECT esi_notes_url " .
"FROM extended_service_information " .
"WHERE service_service_id = :serviceId"
);
$procList = $this->proc->getProcedures();
$statement->bindValue(':serviceId', $serviceId, \PDO::PARAM_INT);
$statement->execute();
/*
* Check if host has a procedure directly on Host
*/
if (isset($procList["Host_:_" . $hostName])) {
return $this->wikiUrl . "/index.php?title=Host_:_" . $hostName;
if ($row = $statement->fetch()) {
$notesUrl = $row['esi_notes_url'];
}
/*
* Check if host can get a procedure on templates
*/
return $notesUrl;
}
/**
* Get host url
*
* @param string $hostName
* @return string|null
*/
public function getHostUrl($hostName): ?string
{
$hostId = $this->getHostId($hostName);
$templates = $this->hostObj->getTemplateChain($hostId);
foreach ($templates as $template) {
$templateName = $template['host_name'];
if (isset($procList["Host-Template_:_" . $templateName])) {
return $this->wikiUrl . "/index.php?title=Host-Template_:_" . $templateName;
}
$hostProperties = $this->hostObj->getInheritedValues(
$hostId,
[],
1,
['host_name', 'ehi_notes_url']
);
if (isset($hostProperties['ehi_notes_url'])) {
return $this->wikiUrl . "/index.php?title=Host_:_" . $hostProperties['host_name'];
}
$inheritedHostProperties = $this->hostObj->getInheritedValues(
$hostId,
[],
-1,
['host_name', 'ehi_notes_url']
);
if (isset($inheritedHostProperties['ehi_notes_url'])) {
return $this->wikiUrl . "/index.php?title=Host-Template_:_" . $inheritedHostProperties['host_name'];
}
return null;
}
/**
* Get service url
*
* @param string $hostName
* @param string $serviceDescription
* @return string|null
*/
public function getServiceUrl($hostName, $serviceDescription)
public function getServiceUrl($hostName, $serviceDescription): ?string
{
if ($this->hflag != 0) {
$this->proc->setHostInformations();
}
$this->proc->setServiceInformations();
$this->sflag;
$serviceDescription = str_replace(' ', '_', $serviceDescription);
$procList = $this->proc->getProcedures();
$serviceId = $this->getServiceId($hostName, $serviceDescription);
if ($serviceId === null) {
return null;
}
/*
* Check Service
*/
$serviceDescription = str_replace(' ', '_', $serviceDescription);
if (isset($procList["Service_:_" . trim($hostName . "_/_" . $serviceDescription)])) {
$notesUrl = $this->getServiceNotesUrl($serviceId);
if ($notesUrl !== null) {
return $this->wikiUrl . "/index.php?title=Service_:_" . $hostName . "_/_" . $serviceDescription;
}
......@@ -180,7 +214,8 @@ class procedures_Proxy
$templates = $this->serviceObj->getTemplatesChain($serviceId);
foreach ($templates as $templateId) {
$templateDescription = $this->serviceObj->getServiceDesc($templateId);
if (isset($procList["Service-Template_:_" . $templateDescription])) {
$notesUrl = $this->getServiceNotesUrl((int) $templateId);
if ($notesUrl !== null) {
return $this->wikiUrl . "/index.php?title=Service-Template_:_" . $templateDescription;
}
}
......
<?php
/*
* Copyright 2005-2019 CENTREON
* Copyright 2005-2020 CENTREON
* Centreon is developed by : Julien Mathis and Romain Le Merlus under
* GPL Licence 2.0.
*
......@@ -42,34 +43,17 @@ class procedures
private $procList = [];
public $DB;
public $centreon_DB;
public $db_prefix;
public $hostList;
public $hosttplList;
public $serviceList;
public $serviceTplList;
public $hostIconeList;
public $diff;
public $api;
/**
* Constructor
*
* @param int $retry
* @param string $db_name
* @param string $db_user
* @param string $db_host
* @param string $db_password
* @param CentreonDB $pearDB
* @param string $db_prefix
*/
public function __construct($pearDB)
{
$this->api = new WikiApi();
$this->centreon_DB = $pearDB;
$this->hostList = array();
$this->hosttplList = array();
$this->serviceList = array();
$this->serviceTplList = array();
}
/**
......@@ -83,7 +67,7 @@ class procedures
return null;
}
$pages = $this->api->getAllPages();
$pages = $this->api->getAllPages();
//replace space
foreach ($pages as $page) {
$page = str_replace(' ', '_', $page);
......@@ -91,80 +75,6 @@ class procedures
}
}
/**
* Get Procedures
*
* @return array
*/
public function getProcedures()
{
return $this->procList;
}
/**
* Get Icon List
*
* @return array
*/
public function getIconeList()
{
return $this->hostIconeList;
}
/**
*
*/
public function getDiff($selection, $type = null)
{
$wikiContent = $this->getProcedures();
$diff = array();
$prefix = "";
switch ($type) {
case 0:
$prefix = "Host_:_";
break;
case 1:
$prefix = "Service_:_";
break;
case 2:
$prefix = "Host-Template_:_";
break;
case 3:
$prefix = "Service-Template_:_";
break;
}
foreach ($selection as $key => $value) {
if (!isset($wikiContent[$prefix . trim($key)])) {
$diff[$key] = 0;
} else {
$diff[$key] = 1;
}
}
return $diff;
}
/**
* Get Host Id
*
* @param string $host_name
* @param CentreonDB $pearDB
* @return int
*/
public function getMyHostID($host_name = null)
{
$dbResult = $this->centreon_DB->query(
"SELECT host_id FROM host " .
"WHERE host_name = '" . $host_name . "' " .
"LIMIT 1 "
);
$row = $dbResult->fetch();
if ($row["host_id"]) {
return $row["host_id"];
}
}
/**
* Get service template
*
......@@ -214,12 +124,12 @@ class procedures
* Get host template models
*
* @param int $host_id
* @return void
* @return array
*/
public function getMyHostMultipleTemplateModels($host_id = null)
{
if (!$host_id) {
return;
return [];
}
$tplArr = array();
......@@ -240,86 +150,7 @@ class procedures
}
unset($row);
unset($hTpl);
return ($tplArr);
}
/**
* Set host information
*
* @return void
*/
public function setHostInformations()
{
/*
* Get Host Informations
*/
$dbResult = $this->centreon_DB->query(
"SELECT host_name, host_id, host_register, ehi_icon_image " .
"FROM host, extended_host_information ehi " .
"WHERE host.host_id = ehi.host_host_id " .
"ORDER BY host_name"
);
while ($data = $dbResult->fetch()) {
if ($data["host_register"] == 1) {
$this->hostList[$data["host_name"]] = $data["host_id"];
} else {
$this->hostTplList[$data["host_name"]] = $data["host_id"];
}
$this->hostIconeList["Host_:_" . $data["host_name"]]
= "./img/media/" . $this->getImageFilePath($data["ehi_icon_image"]);
}
$dbResult->closeCursor();
unset($data);
}
/**
* Get image file path
*
* @param int $image_id
* @return string
*/
public function getImageFilePath($image_id)
{
if (isset($image_id) && $image_id) {
$dbResult2 = $this->centreon_DB->query(
"SELECT img_path, dir_alias " .
"FROM view_img vi, view_img_dir vid, view_img_dir_relation vidr " .
"WHERE vi.img_id = " . $image_id . " " .
"AND vidr.img_img_id = vi.img_id " .
"AND vid.dir_id = vidr.dir_dir_parent_id LIMIT 1"
);
$row2 = $dbResult2->fetch();
if (isset($row2["dir_alias"])
&& isset($row2["img_path"])
&& $row2["dir_alias"]
&& $row2["img_path"]
) {
return $row2["dir_alias"] . "/" . $row2["img_path"];
}
$dbResult2->closeCursor();
unset($row2);
} else {
return "../icones/16x16/server_network.gif";
}
}
/**
* Set service information
*
* @return void
*/
public function setServiceInformations()
{
$dbResult = $this->centreon_DB->query(
"SELECT service_description, service_id, service_register " .
"FROM service WHERE service_register = '0' " .
"ORDER BY service_description"
);
while ($data = $dbResult->fetch()) {
$this->serviceTplList["Service_:_" . $data["service_description"]] = $data["service_id"];
}
$dbResult->closeCursor();
unset($data);
return $tplArr;
}
/**
......
......@@ -48,9 +48,10 @@ class WikiApi
private $curl;
private $loggedIn;
private $tokens;
private $cookies;
private $noSslCertificate;
public const PROXY_URL = './include/configuration/configKnowledge/proxy/proxy.php';
/**
* WikiApi constructor.
*/
......@@ -409,14 +410,20 @@ class WikiApi
$resHost = $this->db->query(
"SELECT host_id FROM host WHERE host_name LIKE '" . $hostName . "'"
);
$tuple = $resHost->fetch();
$valueToAdd = './include/configuration/configKnowledge/proxy/proxy.php?host_name=$HOSTNAME$';
$this->db->query(
"UPDATE extended_host_information "
. "SET ehi_notes_url = '" . $valueToAdd . "' "
. "WHERE host_host_id = '" . $tuple['host_id'] . "'"
);
$hostRow = $resHost->fetch();
if ($hostRow !== false) {
$url = self::PROXY_URL . '?host_name=$HOSTNAME$';
$statement = $this->db->prepare(
"UPDATE extended_host_information " .
"SET ehi_notes_url = :url " .
"WHERE host_host_id = :hostId"
);
$statement->bindValue(':url', $url, \PDO::PARAM_STR);
$statement->bindValue(':hostId', $hostRow['host_id'], \PDO::PARAM_INT);
$statement->execute();
}
}
/**
......@@ -433,15 +440,19 @@ class WikiApi
"AND host_service_relation.host_host_id = host.host_id " .
"AND host_service_relation.service_service_id = service.service_id "
);
$tuple = $resService->fetch();
$valueToAdd = './include/configuration/configKnowledge/proxy/proxy.php?' .
'host_name=$HOSTNAME$&service_description=$SERVICEDESC$';
$this->db->query(
"UPDATE extended_service_information " .
"SET esi_notes_url = '" . $valueToAdd . "' " .
"WHERE service_service_id = '" . $tuple['service_id'] . "' "
);
$serviceRow = $resService->fetch();
if ($serviceRow !== false) {
$url = self::PROXY_URL . '?host_name=$HOSTNAME$&service_description=$SERVICEDESC$';
$statement = $this->db->prepare(
"UPDATE extended_service_information " .
"SET esi_notes_url = :url " .
"WHERE service_service_id = :serviceId"
);
$statement->bindValue(':url', $url, \PDO::PARAM_STR);
$statement->bindValue(':serviceId', $serviceRow['service_id'], \PDO::PARAM_INT);
$statement->execute();
}
}
/**
......@@ -453,21 +464,25 @@ class WikiApi
"SELECT service_id FROM service " .
"WHERE service_description LIKE '" . $serviceName . "' "
);
$tuple = $resService->fetch();
$valueToAdd = './include/configuration/configKnowledge/proxy/proxy.php?' .
'host_name=$HOSTNAME$&service_description=$SERVICEDESC$';
$this->db->query(
"UPDATE extended_service_information " .
"SET esi_notes_url = '" . $valueToAdd . "' " .
"WHERE service_service_id = '" . $tuple['service_id'] . "' "
);
$serviceTemplateRow = $resService->fetch();
if ($serviceTemplateRow !== false) {
$url = self::PROXY_URL . '?host_name=$HOSTNAME$&service_description=$SERVICEDESC$';
$statement = $this->db->prepare(
"UPDATE extended_service_information " .
"SET esi_notes_url = :url " .
"WHERE service_service_id = :serviceId"
);
$statement->bindValue(':url', $url, \PDO::PARAM_STR);
$statement->bindValue(':serviceId', $serviceTemplateRow['service_id'], \PDO::PARAM_INT);
$statement->execute();
}
}
/**
* make a call to mediawiki api to delete a page
* @param string $title
* @return array
* @return object
*/
private function deleteMWPage($title = '')
{
......
......@@ -1492,8 +1492,8 @@ class CentreonHost
}
}
$query = 'SELECT ' . $queryFields . ' ' .
'FROM host h ' .
'WHERE host_id = :hostId';
'FROM host h, extended_host_information ehi ' .
'WHERE host_id = :hostId AND host_id = ehi.host_host_id';
$stmt = $this->db->prepare($query);
$stmt->bindParam(':hostId', $hostId, PDO::PARAM_INT);
$dbResult = $stmt->execute();
......
......@@ -742,7 +742,7 @@ function getMyHostTemplateModels($host_id = null)
function getMyHostMultipleTemplateModels($host_id = null)
{
if (!$host_id) {
return;
return [];
}
global $pearDB;
......@@ -755,7 +755,7 @@ function getMyHostMultipleTemplateModels($host_id = null)
$hTpl = $DBRESULT2->fetchRow();
$tplArr[$row['host_tpl_id']] = html_entity_decode($hTpl["host_name"], ENT_QUOTES, "UTF-8");
}
return ($tplArr);
return $tplArr;
}
#
......
<?php
/*
* Copyright 2005-2019 CENTREON
* Copyright 2005-2020 CENTREON
* Centreon is developed by : Julien Mathis and Romain Le Merlus under
* GPL Licence 2.0.
*
......@@ -96,8 +97,6 @@ try {
$proc = new procedures($pearDB);
$proc->fetchProcedures();
$proc->setHostInformations();
$proc->setServiceInformations();
$query = "
SELECT SQL_CALC_FOUND_ROWS host_name, host_id, host_register, ehi_icon_image
......@@ -126,7 +125,6 @@ try {
if ($data["host_register"] == 0) {
$selection[$data["host_name"]] = $data["host_id"];
}
$proc->hostIconeList[$data["host_name"]] = "./img/media/" . $proc->getImageFilePath($data["ehi_icon_image"]);
}
$statement->closeCursor();
unset($data);
......@@ -193,7 +191,6 @@ try {
$tpl->assign("content", $diff);
$tpl->assign("status", $status);
$tpl->assign("selection", 2);
$tpl->assign("icone", $proc->getIconeList());
/*
* Send template in order to open
......
<?php
/*
* Copyright 2005-2019 CENTREON
* Copyright 2005-2020 CENTREON
* Centreon is developed by : Julien Mathis and Romain Le Merlus under
* GPL Licence 2.0.
*
......@@ -101,8 +102,6 @@ try {
$line = [0 => "list_one", 1 => "list_two"];
$proc = new procedures($pearDB);