From d3c9352a81dc7d8c3d5a0220aa449530a27fc486 Mon Sep 17 00:00:00 2001
From: Kevin Duret <kduret@centreon.com>
Date: Thu, 30 Mar 2017 17:08:59 +0200
Subject: [PATCH] improve centreon kb

---
 config/wiki.conf.php                          |  20 ----
 .../procedures_Proxy.class.php                | 108 ++++++++++--------
 .../configKnowledge/functions.php             |   1 +
 .../configuration/configKnowledge/popup.php   |   1 -
 .../configKnowledge/proxy/proxy.php           |   2 +-
 .../configKnowledge/templates/NoWiki.ihtml    |   5 +-
 .../configKnowledge/templates/display.ihtml   |   2 +-
 7 files changed, 65 insertions(+), 74 deletions(-)
 delete mode 100644 config/wiki.conf.php

diff --git a/config/wiki.conf.php b/config/wiki.conf.php
deleted file mode 100644
index bca6c5bc33..0000000000
--- a/config/wiki.conf.php
+++ /dev/null
@@ -1,20 +0,0 @@
-<?php
-
-	/*
-	 * MySQL Database Connexion
-	 */
-	$db_name = "wikidb";
-	$db_user = "centreon";
-	$db_password = "password";
-	$db_host = "localhost";
-	$db_prefix = "";
-
-	/*
-	 * Wiki URL without a / at the end
-	 */
-	$WikiURL = "http://wiki.localhost/mediawiki";
-    $CentreonURL = "http://localhost/centreon";
-
-	$etc_centreon = "/etc/centreon/";
-	$log_centreon = "/var/log/centreon/";
-?>
\ No newline at end of file
diff --git a/www/class/centreon-knowledge/procedures_Proxy.class.php b/www/class/centreon-knowledge/procedures_Proxy.class.php
index b88d316bf5..0b31758c7e 100644
--- a/www/class/centreon-knowledge/procedures_Proxy.class.php
+++ b/www/class/centreon-knowledge/procedures_Proxy.class.php
@@ -11,43 +11,45 @@
  *
  */
 
-class procedures_Proxy  {
-	var $DB;
-	var $hflag;
-	var $sflag;
-	var $proc;
-	var $url;
-	var $wikiUrl;
-
-	public function procedures_Proxy($pearDB, $db_prefix, $host_name, $service_description = NULL) {
-		global $etc_centreon, $db_name, $db_user, $db_host, $db_password, $WikiURL;
+require_once _CENTREON_PATH_ . '/www/include/configuration/configKnowledge/functions.php';
 
+class procedures_Proxy  {
+	private $DB;
+    private $hflag;
+    private $sflag;
+    private $proc;
+    public $url;
+    private $wikiUrl;
+
+	public function __construct($pearDB, $db_prefix, $host_name, $service_description = null)
+    {
 		$this->DB = $pearDB;
 		$this->hflag = 0;
 		$this->sflag = 0;
 
-        $centreon_path = realpath(dirname(__FILE__) . '/../../../');
-		require_once $centreon_path."/config/centreon.config.php";
-
-		$modules_path = $centreon_path . "/www/include/configuration/configKnowledge/";
-		require_once $modules_path . 'functions.php';
-
 		$conf = getWikiConfig($this->DB);
-		$WikiURL = $conf['kb_wiki_url'];
-
-		$this->wikiUrl = $WikiURL;
-		$this->proc = new procedures(3, $conf['kb_db_name'], $conf['kb_db_user'], $conf['kb_db_host'], $conf['kb_db_password'], $this->DB, $conf['kb_db_prefix']);
+		$this->wikiUrl = $conf['kb_wiki_url'];
+		$this->proc = new procedures(
+		    3,
+            $conf['kb_db_name'],
+            $conf['kb_db_user'],
+            $conf['kb_db_host'],
+            $conf['kb_db_password'],
+            $this->DB,
+            $conf['kb_db_prefix']
+        );
 
 		if (isset($host_name)) {
-			if (isset($service_description))
-				$this->returnServiceWikiUrl($this->DB->escape($host_name), $this->DB->escape($service_description));
-			else
-				$this->returnHostWikiUrl($this->DB->escape($host_name));
+			if (isset($service_description)) {
+                $this->returnServiceWikiUrl($this->DB->escape($host_name), $this->DB->escape($service_description));
+            } else {
+                $this->returnHostWikiUrl($this->DB->escape($host_name));
+            }
 		}
-		return;
 	}
 
-	private function returnHostWikiUrl($host_name) {
+	private function returnHostWikiUrl($host_name)
+    {
 		$this->proc->setHostInformations();
 
 		$procList = $this->proc->getProcedures();
@@ -55,7 +57,7 @@ class procedures_Proxy  {
 		/*
 		 * Check if host has a procedure directly on Host
 		 */
-		if (isset($procList["Host:".$host_name])) {
+		if (isset($procList["Host:" . $host_name])) {
 			$this->url = $this->wikiUrl."/index.php?title=Host:".$host_name;
 			return ;
 		}
@@ -65,16 +67,18 @@ class procedures_Proxy  {
 		 */
 		$templates = $this->getHostTemplateList($host_name);
 		foreach ($templates as $tpl) {
-			if (isset($procList["Host-Template:".$tpl])) {
-				$this->url = $this->wikiURL."/index.php?title=Host-Template:".$tpl;
-				return ;
+			if (isset($procList["Host-Template:" . $tpl])) {
+				$this->url = $this->wikiUrl . "/index.php?title=Host-Template:".$tpl;
+				return;
 			}
 		}
 	}
 
-	private function returnServiceWikiUrl($host_name, $service_description) {
-		if ($this->hflag != 0)
-			$this->proc->setHostInformations();
+	private function returnServiceWikiUrl($host_name, $service_description)
+    {
+		if ($this->hflag != 0) {
+            $this->proc->setHostInformations();
+        }
 		$this->proc->setServiceInformations();
 		$this->sflag;
 
@@ -83,8 +87,8 @@ class procedures_Proxy  {
 		/*
 		 * Check Service
 		 */
-		if (isset($procList["Service:".trim($host_name."_".$service_description)])) {
-			$this->url = $this->wikiURL."/index.php?title=Service:".$host_name."_".$service_description;
+		if (isset($procList["Service:" . trim($host_name."_".$service_description)])) {
+			$this->url = $this->wikiUrl . "/index.php?title=Service:".$host_name."_".$service_description;
 			return;
 		}
 
@@ -95,25 +99,32 @@ class procedures_Proxy  {
 		$templates = $this->getMyServiceTemplateModels($this->getMyServicesID($host_id, $service_description));
 		foreach ($templates as $key => $value) {
 			if (isset($procList["Service-Template:".trim($value)])) {
-				$this->url = $this->wikiURL."/index.php?title=Service-Template:".$value;
+				$this->url = $this->wikiUrl . "/index.php?title=Service-Template:".$value;
 				return ;
 			}
 		}
 		$this->returnHostWikiUrl($host_name);
 	}
 
-	function getMyHostID($host_name = NULL)	{
+	function getMyHostID($host_name = NULL)
+    {
 		$DBRESULT =& $this->DB->query("SELECT host_id FROM host WHERE host_name = '".$host_name."' LIMIT 1");
 		$row =& $DBRESULT->fetchRow();
 		if ($row["host_id"])
 			return $row["host_id"];
 	}
 
-	function getMyServicesID($host_id, $service_description) {
+	function getMyServicesID($host_id, $service_description)
+    {
 		/*
 		 * Get Services attached to hosts
 		 */
-		$DBRESULT =& $this->DB->query("SELECT service_id, service_description FROM service, host_service_relation hsr WHERE hsr.host_host_id = '".$host_id."' AND hsr.service_service_id = service_id AND service_description = '$service_description'");
+		$query = "SELECT service_id, service_description " .
+            "FROM service, host_service_relation hsr " .
+            "WHERE hsr.host_host_id = '" . $host_id . "' " .
+            "AND hsr.service_service_id = service_id " .
+            "AND service_description = '" . $service_description . "' ";
+		$DBRESULT =& $this->DB->query($query);
 		while ($elem =& $DBRESULT->fetchRow())	{
 			return $elem["service_id"];
 		}
@@ -122,10 +133,12 @@ class procedures_Proxy  {
 		/*
 		 * Get Services attached to hostgroups
 		 */
-		$DBRESULT =& $this->DB->query("SELECT service_id, service_description FROM hostgroup_relation hgr, service, host_service_relation hsr" .
-				" WHERE hgr.host_host_id = '".$host_id."' AND hsr.hostgroup_hg_id = hgr.hostgroup_hg_id" .
-				" AND service_id = hsr.service_service_id " .
-				" AND service_description = '$service_description'");
+		$query = "SELECT service_id, service_description " .
+            "FROM hostgroup_relation hgr, service, host_service_relation hsr " .
+            "WHERE hgr.host_host_id = '" . $host_id . "' AND hsr.hostgroup_hg_id = hgr.hostgroup_hg_id " .
+            "AND service_id = hsr.service_service_id " .
+            "AND service_description = '" . $service_description . "' ";
+		$DBRESULT =& $this->DB->query($query);
 		while ($elem =& $DBRESULT->fetchRow()){
 			return $elem["service_id"];
 		}
@@ -133,7 +146,8 @@ class procedures_Proxy  {
 	}
 
 
-	private function getHostTemplateList($host_name) {
+	private function getHostTemplateList($host_name)
+    {
 		$templates = array();
 
 		$DBRESULT =& $this->DB->query("SELECT host_tpl_id FROM `host_template_relation`, `host` WHERE host_host_id = host_id AND host_name = '".$host_name."' ORDER BY `order`");
@@ -145,8 +159,8 @@ class procedures_Proxy  {
 		return $templates;
 	}
 
-	private function getMyServiceTemplateModels($service_id)	{
-
+	private function getMyServiceTemplateModels($service_id)
+    {
 		$tplArr = array();
 
 		$DBRESULT =& $this->DB->query("SELECT service_description, service_template_model_stm_id FROM service WHERE service_id = '".$service_id."' LIMIT 1");
@@ -171,5 +185,3 @@ class procedures_Proxy  {
 		return ($tplArr);
 	}
 }
-
-?>
\ No newline at end of file
diff --git a/www/include/configuration/configKnowledge/functions.php b/www/include/configuration/configKnowledge/functions.php
index 3df3e567c7..8ae725b4f3 100644
--- a/www/include/configuration/configKnowledge/functions.php
+++ b/www/include/configuration/configKnowledge/functions.php
@@ -29,6 +29,7 @@ function versionCentreon($pearDB)
 
 function Mediawikiconfigexist($url)
 {
+    return true;
     $file_headers = @get_headers($url);
     if ($file_headers[0] == 'HTTP/1.1 404 Not Found') {
         return false;
diff --git a/www/include/configuration/configKnowledge/popup.php b/www/include/configuration/configKnowledge/popup.php
index c7a4b4fede..f7f3bf30d3 100644
--- a/www/include/configuration/configKnowledge/popup.php
+++ b/www/include/configuration/configKnowledge/popup.php
@@ -54,7 +54,6 @@ set_include_path(
 
 require_once "DB.php";
 
-//require_once "wiki.conf.php";
 require_once "centreon-knowledge/procedures_DB_Connector.class.php";
 require_once "centreon-knowledge/procedures.class.php";
 require_once "centreonLog.class.php";
diff --git a/www/include/configuration/configKnowledge/proxy/proxy.php b/www/include/configuration/configKnowledge/proxy/proxy.php
index 4acdb7787f..f1570ae131 100644
--- a/www/include/configuration/configKnowledge/proxy/proxy.php
+++ b/www/include/configuration/configKnowledge/proxy/proxy.php
@@ -78,7 +78,7 @@ if (isset($_GET["host_name"]) && isset($_GET["service_description"])) {
 }
 
 if ($proxy->url != "") {
-    header("Location: ".$proxy->url);
+    header("Location: " . $proxy->url);
 } else {
     if (isset($_GET["host_name"]) && isset($_GET["service_description"])) {
         header("Location: $WikiURL/?title=Service:".$_GET["host_name"]."_".$_GET["service_description"]);
diff --git a/www/include/configuration/configKnowledge/templates/NoWiki.ihtml b/www/include/configuration/configKnowledge/templates/NoWiki.ihtml
index 4047395bff..67a4e85118 100644
--- a/www/include/configuration/configKnowledge/templates/NoWiki.ihtml
+++ b/www/include/configuration/configKnowledge/templates/NoWiki.ihtml
@@ -1,6 +1,5 @@
 <div>
     <br/>
-    <br/>
-    Le MediaWiki n'est pas installé ou pas configuré, pour plus d'information veuillez vous référe
-    à la <a href="https://documentation-fr.centreon.com/docs/centreon-knowledge-base/en/latest/" target="_blank" >documentation.</a>
+    MediaWiki is not installed or configured. Please refer to the
+    <a href="https://documentation-fr.centreon.com/docs/centreon-knowledge-base/en/latest/" target="_blank" >documentation.</a>
 </div>
\ No newline at end of file
diff --git a/www/include/configuration/configKnowledge/templates/display.ihtml b/www/include/configuration/configKnowledge/templates/display.ihtml
index 03bc86e02b..3a65cf9c03 100644
--- a/www/include/configuration/configKnowledge/templates/display.ihtml
+++ b/www/include/configuration/configKnowledge/templates/display.ihtml
@@ -232,7 +232,7 @@
 				title: title
 			}),
 			success : function(data) {
-				//location.reload()
+				location.reload()
 			}
 		});
 	}
-- 
GitLab