From ccb86383aa16309e5406cb343c958459ab1154b2 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Thu, 21 Oct 2021 16:12:51 +0200 Subject: [PATCH] fix(pendo): use api to get pendo information (#10349) Refs: MON-10926 --- config/packages/Centreon.yaml | 5 +- src/Centreon/Domain/Log/LegacyLogger.php | 115 +++++++++ www/api/class/centreon_ceip.class.php | 267 ++++++++++++++++++++ www/front_src/public/index.html | 19 +- www/include/common/javascript/pendo.js | 61 +++++ www/include/core/footer/footerPart.php | 200 +-------------- www/include/core/login/template/login.ihtml | 5 + 7 files changed, 459 insertions(+), 213 deletions(-) create mode 100644 src/Centreon/Domain/Log/LegacyLogger.php create mode 100644 www/api/class/centreon_ceip.class.php create mode 100644 www/include/common/javascript/pendo.js diff --git a/config/packages/Centreon.yaml b/config/packages/Centreon.yaml index e9066f3c485..7b7141aa03e 100644 --- a/config/packages/Centreon.yaml +++ b/config/packages/Centreon.yaml @@ -336,4 +336,7 @@ services: Centreon\Domain\Log\ContactForDebug: class: Centreon\Domain\Log\ContactForDebug - arguments: ['%env(DEBUG_CONTACT)%'] \ No newline at end of file + arguments: ['%env(DEBUG_CONTACT)%'] + + Centreon\Domain\Log\LegacyLogger: + public: true \ No newline at end of file diff --git a/src/Centreon/Domain/Log/LegacyLogger.php b/src/Centreon/Domain/Log/LegacyLogger.php new file mode 100644 index 00000000000..0162038429e --- /dev/null +++ b/src/Centreon/Domain/Log/LegacyLogger.php @@ -0,0 +1,115 @@ +traitEmergency($message, $context); + } + + /** + * @inheritDoc + */ + public function alert($message, array $context = []) + { + $this->traitAlert($message, $context); + } + + /** + * @inheritDoc + */ + public function critical($message, array $context = []) + { + $this->traitCritical($message, $context); + } + + /** + * @inheritDoc + */ + public function error($message, array $context = []) + { + $this->traitError($message, $context); + } + + /** + * @inheritDoc + */ + public function warning($message, array $context = []) + { + $this->traitWarning($message, $context); + } + + /** + * @inheritDoc + */ + public function notice($message, array $context = []) + { + $this->traitNotice($message, $context); + } + + /** + * @inheritDoc + */ + public function info($message, array $context = []) + { + $this->traitInfo($message, $context); + } + + /** + * @inheritDoc + */ + public function debug($message, array $context = []) + { + $this->traitDebug($message, $context); + } + + /** + * @inheritDoc + */ + public function log($level, $message, array $context = []) + { + $this->traitLog($level, $message, $context); + } +} diff --git a/www/api/class/centreon_ceip.class.php b/www/api/class/centreon_ceip.class.php new file mode 100644 index 00000000000..e73af20f382 --- /dev/null +++ b/www/api/class/centreon_ceip.class.php @@ -0,0 +1,267 @@ +user = $centreon->user; + + // Generate UUID + $this->uuid = (new CentreonUUID($this->pearDB))->getUUID(); + + $kernel = \App\Kernel::createForWeb(); + $this->logger = $kernel->getContainer()->get( + \Centreon\Domain\Log\LegacyLogger::class + ); + } + + /** + * Get CEIP Account and User info + * + * @return array with Account/User info + */ + public function getCeipInfo(): array + { + // Don't compute data is CEIP is disabled + if (!$this->isCeipActive()) { + return [ + 'ceip' => false + ]; + } + + return [ + 'visitor' => $this->getVisitorInformation(), + 'account' => $this->getAccountInformation(), + 'excludeAllText' => true, + 'ceip' => true + ]; + } + + /** + * Get the type of the Centreon server + * + * @return string the type of the server (central|remote) + */ + private function getServerType(): string + { + $instanceType = 'central'; + + $result = $this->pearDB->query( + "SELECT `value` FROM `informations` WHERE `key` = 'isRemote'" + ); + if ($row = $result->fetch()) { + $instanceType = $row['value'] === 'yes' ? 'remote' : 'central'; + } + + return $instanceType; + } + + /** + * Get visitor information + * + * @return array with visitor information + * @throws \PDOException + */ + private function getVisitorInformation(): array + { + $locale = $this->user->lang === 'browser' + ? null + : $this->user->lang; + + $role = $this->user->admin + ? "admin" + : "user"; + + if (strcmp($role, 'admin') != 0) { + $stmt = $this->pearDB->prepare(' + SELECT COUNT(*) AS countAcl + FROM acl_actions_rules AS aar + INNER JOIN acl_actions AS aa ON (aa.acl_action_id = aar.acl_action_rule_id) + INNER JOIN acl_group_actions_relations AS agar ON (agar.acl_action_id = aar.acl_action_rule_id) + INNER JOIN acl_group_contacts_relations AS agcr ON (agcr.acl_group_id = agar.acl_group_id) + INNER JOIN acl_group_contactgroups_relations AS agcgr ON (agcgr.acl_group_id = agar.acl_group_id) + WHERE aar.acl_action_name LIKE "service\_%" OR aar.acl_action_name LIKE "host\_%" + AND agcr.contact_contact_id = :contact_id + OR agcgr.acl_group_id IN ( + SELECT contactgroup_cg_id + FROM contactgroup_contact_relation + WHERE contact_contact_id = :contact_id + ) + '); + $stmt->bindValue(':contact_id', $this->user->user_id, PDO::PARAM_INT); + $stmt->execute(); + if (($row = $stmt->fetch()) && $row['countAcl'] > 0) { + $role = 'operator'; + } + } + + return [ + 'id' => substr($this->uuid, 0, 6) . '-' . $this->user->user_id, + 'locale' => $locale, + 'role' => $role + ]; + } + + /** + * Get account information + * + * @return array with account information + */ + private function getAccountInformation(): array + { + // Get Centreon statistics + $centreonStats = new CentreonStatistics($this->logger); + $configUsage = $centreonStats->getPlatformInfo(); + + // Get Licences information + $licenseInfo = $this->getLicenseInformation(); + + // Get Version of Centreon + $centreonVersion = $this->getCentreonVersion(); + + return [ + 'id' => $this->uuid, + 'name' => $licenseInfo['companyName'], + 'serverType' => $this->getServerType(), + 'licenseType' => $licenseInfo['licenseType'], + 'versionMajor' => $centreonVersion['major'], + 'versionMinor' => $centreonVersion['minor'], + 'nb_hosts' => (int) $configUsage['nb_hosts'], + 'nb_services' => (int) $configUsage['nb_services'], + 'nb_servers' => $configUsage['nb_central'] + $configUsage['nb_remotes'] + $configUsage['nb_pollers'] + ]; + } + + /** + * Get license information such as company name and license type + * + * @return array with license info + */ + private function getLicenseInformation(): array + { + /** + * Getting License informations. + */ + $dependencyInjector = \Centreon\LegacyContainer::getInstance(); + $productLicense = 'Open Source'; + $licenseClientName = ''; + try { + $centreonModules = ['epp', 'bam', 'map', 'mbi']; + $licenseObject = $dependencyInjector['lm.license']; + $licenseInformation = []; + foreach ($centreonModules as $module) { + $licenseObject->setProduct($module); + $isLicenseValid = $licenseObject->validate(false); + if ($isLicenseValid && !empty($licenseObject->getData())) { + $licenseInformation[$module] = $licenseObject->getData(); + $licenseClientName = $licenseInformation[$module]['client']['name']; + if ($module === 'epp') { + $productLicense = 'IT Edition'; + } + if (in_array($module, ['mbi', 'bam', 'map'])) { + $productLicense = 'Business Edition'; + break; + } + } + } + } catch (\Exception $exception) { + $this->logger->error($exception->getMessage, ['context' => $exception]); + } + + return [ + 'companyName' => $licenseClientName, + 'licenseType' => $productLicense + ]; + } + + /** + * Get the major and minor versions of Centreon web + * + * @return array with major and minor versions + * @throws \PDOException + */ + private function getCentreonVersion(): array + { + $major = null; + $minor = null; + + $result = $this->pearDB->query( + "SELECT informations.value FROM informations WHERE informations.key = 'version'" + ); + if ($row = $result->fetch()) { + $minor = $row['value']; + $major = substr($minor, 0, strrpos($minor, '.', 0)); + } + + return [ + 'major' => $major, + 'minor' => $minor + ]; + } + + /** + * Get CEIP status + * + * @return bool the status of CEIP + * @throws \PDOException + */ + private function isCeipActive(): bool + { + $result = $this->pearDB->query( + "SELECT `value` FROM `options` WHERE `key` = 'send_statistics' LIMIT 1" + ); + + if (($sendStatisticsResult = $result->fetch()) && $sendStatisticsResult["value"] === "1") { + return true; + } else { + return false; + } + } +} diff --git a/www/front_src/public/index.html b/www/front_src/public/index.html index d42e290e769..17304dd2a13 100755 --- a/www/front_src/public/index.html +++ b/www/front_src/public/index.html @@ -33,22 +33,5 @@ To create a production bundle, use `npm run build` or `yarn build`. --> - + diff --git a/www/include/common/javascript/pendo.js b/www/include/common/javascript/pendo.js new file mode 100644 index 00000000000..8455cdaad20 --- /dev/null +++ b/www/include/common/javascript/pendo.js @@ -0,0 +1,61 @@ +// Pendo.io +const initPendo = (data) => { + (function(apiKey, platformData){ + (function(p,e,n,d,o){var v,w,x,y,z;o=p[d]=p[d]||{};o._q=o._q||[]; + v=['initialize','identify','updateOptions','pageLoad','track'];for(w=0,x=v.length;w { + const contentType = response.headers.get('content-type'); + if (contentType && contentType.indexOf("application/json") !== -1) { + response.json().then(function(data) { + if (data.ceip === true) { + initPendo(data); + + // Create localStorage cache + const platformData = { + cacheGenerationDate: Date.now(), + visitor: data.visitor, + account: data.account, + excludeAllText: data.excludeAllText, + ceip: true + }; + localStorage.setItem('centreonPlatformData', JSON.stringify(platformData)); + } else { + localStorage.setItem('centreonPlatformData', JSON.stringify({ ceip: false })); + } + }); + } + }); + } +} \ No newline at end of file diff --git a/www/include/core/footer/footerPart.php b/www/include/core/footer/footerPart.php index 03f4429aebf..88f8c2a0cae 100644 --- a/www/include/core/footer/footerPart.php +++ b/www/include/core/footer/footerPart.php @@ -1,7 +1,6 @@ query("SELECT `value` FROM `options` WHERE `key` = 'send_statistics' LIMIT 1"); - -if (($sendStatisticsResult = $result->fetch()) && $sendStatisticsResult["value"] == "1") { - $sendStatistics = '1'; -} else { - $sendStatistics = '0'; -} - -/** - * Getting Platform Type. - */ -$result = $pearDB->query("SELECT `value` FROM `informations` WHERE `key` = 'isRemote'"); -$isRemote = '0'; -if ($row = $result->fetch()) { - $isRemote = $row['value'] === 'yes' ? '1' : '0'; -} - -/** - * Getting Centreon UUID. - */ -$centreonUUID = new CentreonUUID($pearDB); -$uuid = $centreonUUID->getUUID(); - -/** - * Getting Platform statistics. - */ -$output = new ConsoleOutput(); -$logger = new ConsoleLogger($output); -$centreonStats = new CentreonStatistics($logger); -$stats = $centreonStats->getPlatformInfo(); - -/** - * Getting License informations. - */ -$productLicense = 'Open Source'; -$licenseClientName = null; -try { - $centreonModules = ['epp', 'bam', 'map', 'mbi']; - $licenseObject = $dependencyInjector['lm.license']; - $licenseInformation = []; - foreach ($centreonModules as $module) { - $licenseObject->setProduct($module); - $isLicenseValid = $licenseObject->validate(false); - if ($isLicenseValid && !empty($licenseObject->getData())) { - $licenseInformation[$module] = $licenseObject->getData(); - $licenseClientName = $licenseInformation[$module]['client']['name']; - if ($module === 'epp') { - $productLicense = 'IT Edition'; - } - if (in_array($module, ['mbi', 'bam', 'map'])) { - $productLicense = 'Business Edition'; - } - } - } -} catch (\Exception $ex) { - error_log($ex->getMessage()); -} - - if (!$min) { ?> @@ -218,8 +152,8 @@ function exitHandler() { } /* -* Create Data Flow -*/ + * Create Data Flow + */ $cdata = CentreonData::getInstance(); $jsdata = $cdata->getJsData(); foreach ($jsdata as $k => $val) { @@ -228,131 +162,8 @@ function exitHandler() { ?> +