From 20bbb2da4b742e15ad682f7bdb41b87270430cbb Mon Sep 17 00:00:00 2001
From: loiclau <loic.lau@gmail.com>
Date: Thu, 11 May 2017 13:16:24 +0200
Subject: [PATCH] *fix KB url error and query with ?

---
 .../centreon-knowledge/wikiApi.class.php      | 16 ++++---
 .../configKnowledge/functions.php             |  8 ++--
 www/install/insertBaseConf.sql                |  2 +-
 www/install/php/Update-2.8.7_to_2.8.8.php     | 47 +++++++++++++++++++
 4 files changed, 62 insertions(+), 11 deletions(-)
 create mode 100644 www/install/php/Update-2.8.7_to_2.8.8.php

diff --git a/www/class/centreon-knowledge/wikiApi.class.php b/www/class/centreon-knowledge/wikiApi.class.php
index 9ebc85caed..ef707b3c92 100644
--- a/www/class/centreon-knowledge/wikiApi.class.php
+++ b/www/class/centreon-knowledge/wikiApi.class.php
@@ -390,12 +390,13 @@ class WikiApi
         $resHost = $this->db->execute($stmt, array((string)$hostName));
         $tuple = $resHost->fetchRow();
 
+
         $valueToAdd = './include/configuration/configKnowledge/proxy/proxy.php?host_name=$HOSTNAME$';
         $queryUpdate = "UPDATE extended_host_information "
-            . "SET ehi_notes_url = '" . $valueToAdd . "' "
+            . "SET ehi_notes_url = ? "
             . "WHERE host_host_id = ?";
         $stmt = $this->db->prepare($queryUpdate);
-        $this->db->execute($stmt, array((int)$tuple['host_id']));
+        $this->db->execute($stmt, array((string)$valueToAdd, (int)$tuple['host_id']));
     }
 
     /**
@@ -413,16 +414,17 @@ class WikiApi
 
 
         $stmt = $this->db->prepare($query);
-        $resService =$this->db->execute($stmt, array((string)$hostName, (string)$serviceDescription));
+        $resService = $this->db->execute($stmt, array((string)$hostName, (string)$serviceDescription));
         $tuple = $resService->fetchRow();
 
         $valueToAdd = './include/configuration/configKnowledge/proxy/proxy.php?' .
             'host_name=$HOSTNAME$&service_description=$SERVICEDESC$';
         $queryUpdate = "UPDATE extended_service_information " .
-            "SET esi_notes_url = '" . $valueToAdd . "' " .
+            "SET esi_notes_url = ? " .
             "WHERE service_service_id = ? ";
         $stmt = $this->db->prepare($queryUpdate);
-        $this->db->execute($stmt, array((int)$tuple['service_id']));
+        $this->db->execute($stmt, array((string)$valueToAdd, (int)$tuple['service_id']));
+
     }
 
     /**
@@ -438,10 +440,10 @@ class WikiApi
         $valueToAdd = './include/configuration/configKnowledge/proxy/proxy.php?' .
             'host_name=$HOSTNAME$&service_description=$SERVICEDESC$';
         $queryUpdate = "UPDATE extended_service_information " .
-            "SET esi_notes_url = '" . $valueToAdd . "' " .
+            "SET esi_notes_url = ? " .
             "WHERE service_service_id = ? ";
         $stmt = $this->db->prepare($queryUpdate);
-        $this->db->execute($stmt, array((int)$tuple['service_id']));
+        $this->db->execute($stmt, array((string)$valueToAdd, (int)$tuple['service_id']));
 
     }
 }
diff --git a/www/include/configuration/configKnowledge/functions.php b/www/include/configuration/configKnowledge/functions.php
index 27a854cd20..e7e55fcf27 100644
--- a/www/include/configuration/configKnowledge/functions.php
+++ b/www/include/configuration/configKnowledge/functions.php
@@ -29,10 +29,11 @@ function versionCentreon($pearDB)
 
 function getWikiConfig($pearDB)
 {
-    $errorMsg = '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>';
+    $errorMsg = '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>';
 
-    $mandatoryConfigKey = array('kb_db_name', 'kb_db_host', 'kb_WikiURL');
+    $mandatoryConfigKey = array('kb_db_name', 'kb_db_host', 'kb_wiki_url');
     if (is_null($pearDB)) {
         throw new \Exception($errorMsg);
     }
@@ -45,6 +46,7 @@ function getWikiConfig($pearDB)
 
     $gopt = array();
     while ($opt = $res->fetchRow()) {
+
         if (!empty($opt["value"])) {
             $gopt[$opt["key"]] = html_entity_decode($opt["value"], ENT_QUOTES, "UTF-8");
         } else {
diff --git a/www/install/insertBaseConf.sql b/www/install/insertBaseConf.sql
index c5b985d290..9edbad3873 100644
--- a/www/install/insertBaseConf.sql
+++ b/www/install/insertBaseConf.sql
@@ -1280,4 +1280,4 @@ VALUES ('kb_db_name', ''),
 ('kb_db_password', ''),
 ('kb_db_host', ''),
 ('kb_db_prefix', ''),
-('kb_WikiURL', '');
+('kb_wiki_url', '');
diff --git a/www/install/php/Update-2.8.7_to_2.8.8.php b/www/install/php/Update-2.8.7_to_2.8.8.php
new file mode 100644
index 0000000000..ee46f1519f
--- /dev/null
+++ b/www/install/php/Update-2.8.7_to_2.8.8.php
@@ -0,0 +1,47 @@
+<?php
+/*
+ * Copyright 2005-2016 Centreon
+ * Centreon is developped by : Julien Mathis and Romain Le Merlus under
+ * GPL Licence 2.0.
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation ; either version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, see <http://www.gnu.org/licenses>.
+ *
+ * Linking this program statically or dynamically with other modules is making a
+ * combined work based on this program. Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this program give Centreon
+ * permission to link this program with independent modules to produce an executable,
+ * regardless of the license terms of these independent modules, and to copy and
+ * distribute the resulting executable under terms of Centreon choice, provided that
+ * Centreon also meet, for each linked independent module, the terms  and conditions
+ * of the license of that module. An independent module is a module which is not
+ * derived from this program. If you modify this program, you may extend this
+ * exception to your version of the program, but you are not obliged to do so. If you
+ * do not wish to do so, delete this exception statement from your version.
+ *
+ * For more information : contact@centreon.com
+ *
+ *
+ */
+
+/* Update Wiki url  */
+if (isset($pearDB)) {
+    $res = $pearDB->query('SELECT `kb_wiki_url` FROM `options`');
+    if ($res->numRows() == 0) {
+        $query = 'UPDATE `options` SET `key` = "kb_wiki_url" WHERE `key` = "kb_WikiURL"';
+        $pearDB->query($query);
+    } else {
+        $query = 'DELETE FROM `options` WHERE `key` = "kb_WikiURL"';
+        $pearDB->query($query);
+    }
+}
-- 
GitLab