From 0270aa05f3cb7fe0f9b3e3c3a1474df75bf34b0e Mon Sep 17 00:00:00 2001
From: Sylvestre Ho <sho@merethis.com>
Date: Thu, 13 Feb 2014 11:45:00 +0100
Subject: [PATCH] fixes #5201; fixes macro massive change

---
 www/class/centreonHost.class.php              | 24 +++++++++++++++---
 www/class/centreonService.class.php           | 25 ++++++++++++++++---
 .../configObject/host/DB-Func.php             |  3 ++-
 .../configObject/service/DB-Func.php          | 25 +++++++++----------
 4 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/www/class/centreonHost.class.php b/www/class/centreonHost.class.php
index 7618039f5c..32ccf74f60 100644
--- a/www/class/centreonHost.class.php
+++ b/www/class/centreonHost.class.php
@@ -484,11 +484,27 @@ class CentreonHost
      * @param int $hostId
      * @param array $macroInput
      * @param array $macroValue
+     * @param array $macroPassword
+     * @param bool $isMassiveChange
      * @return void
      */
-    public function insertMacro($hostId, $macroInput = array(), $macroValue = array(), $macroPassword = array()) {
-        $this->db->query("DELETE FROM on_demand_macro_host 
+    public function insertMacro($hostId, $macroInput = array(), $macroValue = array(), $macroPassword = array(), $isMassiveChange = false) {
+        if (false === $isMassiveChange) {
+            $this->db->query("DELETE FROM on_demand_macro_host 
                 WHERE host_host_id = ".$this->db->escape($hostId));
+        } else {
+            $macroList = "";
+            foreach ($macroInput as $v) {
+               $macroList .= "'\$_HOST".strtoupper($this->db->escape($v))."\$',";
+            }
+            if ($macroList) {
+                $macroList = rtrim($macroList, ",");
+                $this->db->query("DELETE FROM on_demand_macro_host
+                    WHERE host_host_id = ".$this->db->escape($hostId)."
+                    AND host_macro_name IN ({$macroList})"
+                );          
+            }
+        }
             
         $macros = $macroInput;
         $macrovalues = $macroValue;
@@ -497,7 +513,7 @@ class CentreonHost
             if ($value != "" && 
                 !isset($stored[strtolower($value)])) {
                 $this->db->query("INSERT INTO on_demand_macro_host (`host_macro_name`, `host_macro_value`, `is_password`, `host_host_id`) 
-                                VALUES ('\$_HOST". strtoupper($value) ."\$', '". $this->db->escape($macrovalues[$key]) ."', ".(isset($macroPassword[$key]) ? 1 : 'NULL') .", ". $hostId .")");
+                                VALUES ('\$_HOST". strtoupper($this->db->escape($value)) ."\$', '". $this->db->escape($macrovalues[$key]) ."', ".(isset($macroPassword[$key]) ? 1 : 'NULL') .", ". $this->db->escape($hostId) .")");
                 $stored[strtolower($value)] = true;
             }
         }
@@ -639,4 +655,4 @@ class CentreonHost
     
 }
 
-?>
\ No newline at end of file
+?>
diff --git a/www/class/centreonService.class.php b/www/class/centreonService.class.php
index bfb240ffb0..dd09c361da 100644
--- a/www/class/centreonService.class.php
+++ b/www/class/centreonService.class.php
@@ -303,11 +303,28 @@
          * @param int $serviceId
          * @param array $macroInput
          * @param array $macroValue
+         * @param array $macroPassword
+         * @param bool $isMassiveChange
          * @return void
          */
-        public function insertMacro($serviceId, $macroInput = array(), $macroValue = array(), $macroPassword = array()) {
-            $this->db->query("DELETE FROM on_demand_macro_service
-                WHERE svc_svc_id = ".$this->db->escape($serviceId));
+        public function insertMacro($serviceId, $macroInput = array(), $macroValue = array(), $macroPassword = array(), $isMassiveChange = false) {
+            if (false === $isMassiveChange) {
+                 $this->db->query("DELETE FROM on_demand_macro_service
+                    WHERE svc_svc_id = ".$this->db->escape($serviceId)
+                 );
+            } else {
+                 $macroList = "";
+                 foreach ($macroInput as $v) {
+                    $macroList .= "'\$_SERVICE".strtoupper($this->db->escape($v))."\$',";
+                 }
+                 if ($macroList) {
+                      $macroList = rtrim($macroList, ",");
+                      $this->db->query("DELETE FROM on_demand_macro_service
+                         WHERE svc_svc_id = ".$this->db->escape($serviceId)."
+                         AND svc_macro_name IN ({$macroList})"
+                      );
+                 }
+            }
             
             $macros = $macroInput;
             $macrovalues = $macroValue;
@@ -316,7 +333,7 @@
                 if ($value != "" && 
                     !isset($stored[strtolower($value)])) {
                         $this->db->query("INSERT INTO on_demand_macro_service (`svc_macro_name`, `svc_macro_value`, `is_password`, `svc_svc_id`) 
-                                VALUES ('\$_SERVICE".strtoupper($value)."\$', '".$this->db->escape($macrovalues[$key])."', ".(isset($macroPassword[$key]) ? 1 : 'NULL').", ".$serviceId .")");
+                                VALUES ('\$_SERVICE".strtoupper($this->db->escape($value))."\$', '".$this->db->escape($macrovalues[$key])."', ".(isset($macroPassword[$key]) ? 1 : 'NULL').", ".$this->db->escape($serviceId) .")");
                         $stored[strtolower($value)] = true;
                 }
             }
diff --git a/www/include/configuration/configObject/host/DB-Func.php b/www/include/configuration/configObject/host/DB-Func.php
index 5e2921f78d..3474435242 100644
--- a/www/include/configuration/configObject/host/DB-Func.php
+++ b/www/include/configuration/configObject/host/DB-Func.php
@@ -1502,7 +1502,8 @@ function updateHost_MC($host_id = null)	{
                               $host_id,
                               $_REQUEST['macroInput'],
                               $_REQUEST['macroValue'],
-                              $_REQUEST['macroPassword']
+                              $_REQUEST['macroPassword'],
+                              true
                               );
     }
 
diff --git a/www/include/configuration/configObject/service/DB-Func.php b/www/include/configuration/configObject/service/DB-Func.php
index bea7585602..07ab57c706 100644
--- a/www/include/configuration/configObject/service/DB-Func.php
+++ b/www/include/configuration/configObject/service/DB-Func.php
@@ -1479,19 +1479,18 @@ function divideHostsToHost($service_id) {
 		/*
 		 *  Update on demand macros
 		 */
-		if (isset($_REQUEST['macroInput']) && 
-                        isset($_REQUEST['macroValue'])) {
-                    $service->insertMacro(
-                            $service_id,
-                            $_REQUEST['macroInput'],
-                            $_REQUEST['macroValue'],
-                            $_REQUEST['macroPassword']
-                            );
-                }
-
-                if (isset($ret['criticality_id']) && $ret['criticality_id']) {
-                    setServiceCriticality($service_id, $ret['criticality_id']);
-                }
+		if (isset($_REQUEST['macroInput']) && isset($_REQUEST['macroValue'])) {
+            $service->insertMacro(
+   	    	    $service_id,
+    	        $_REQUEST['macroInput'],
+            	$_REQUEST['macroValue'],
+				$_REQUEST['macroPassword'],
+				true
+    	    );
+        }
+        if (isset($ret['criticality_id']) && $ret['criticality_id']) {
+ 	       setServiceCriticality($service_id, $ret['criticality_id']);
+        }
 
 		$centreon->CentreonLogAction->insertLog("service", $service_id, getMyServiceName($service_id), "mc", $fields);
 	}
-- 
GitLab