From 4e48b2b9455c3d194f59a1b0645f678295919d22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Marigot?= Date: Thu, 28 Jul 2022 21:06:38 +0200 Subject: [PATCH] fix(clapi): Implode method that escape potential delimiter char in values (#11451) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apply this new method to escape on export (used in all export methods on each objects). Refs: #11451 Signed-off-by: Grégory Marigot --- .../CentreonLDAPContactRelation.class.php | 13 +- .../centreonACLAction.class.php | 37 +++--- .../centreon-clapi/centreonACLGroup.class.php | 40 +++--- .../centreon-clapi/centreonACLMenu.class.php | 41 ++++--- .../centreonACLResource.class.php | 40 +++--- .../centreonCentbrokerCfg.class.php | 116 +++++++++++------- .../centreon-clapi/centreonCommand.class.php | 43 +++---- .../centreon-clapi/centreonContact.class.php | 32 ++--- .../centreonContactGroup.class.php | 10 +- .../centreonDependency.class.php | 66 ++++------ .../centreon-clapi/centreonDowntime.class.php | 6 +- .../centreonEngineCfg.class.php | 32 ++--- .../centreon-clapi/centreonHost.class.php | 103 +++++++++------- .../centreonHostCategory.class.php | 10 +- .../centreonHostGroup.class.php | 10 +- .../centreonHostGroupService.class.php | 74 ++++++----- .../centreon-clapi/centreonLDAP.class.php | 50 +++++--- .../centreon-clapi/centreonObject.class.php | 46 ++++--- .../centreonResourceCfg.class.php | 20 +-- .../centreon-clapi/centreonService.class.php | 86 +++++++------ .../centreonServiceCategory.class.php | 20 +-- .../centreonServiceGroup.class.php | 20 +-- .../centreonServiceTemplate.class.php | 100 ++++++++------- .../centreon-clapi/centreonTrap.class.php | 34 ++--- 24 files changed, 581 insertions(+), 468 deletions(-) diff --git a/www/class/centreon-clapi/CentreonLDAPContactRelation.class.php b/www/class/centreon-clapi/CentreonLDAPContactRelation.class.php index 5f7752e3f4f..f8b422ccc4e 100644 --- a/www/class/centreon-clapi/CentreonLDAPContactRelation.class.php +++ b/www/class/centreon-clapi/CentreonLDAPContactRelation.class.php @@ -106,12 +106,13 @@ public function export($filterName = null): bool foreach ($contact as $parameter => $value) { if (!empty($value) && !in_array($parameter, $this->exportExcludedParams) && $parameter === "ar_id") { $value = $this->ldap->getObjectName($value); - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $contact[$this->contact->getUniqueLabelField()] . $this->delim - . self::LDAP_PARAMETER_NAME . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $contact[$this->contact->getUniqueLabelField()], + self::LDAP_PARAMETER_NAME, + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonACLAction.class.php b/www/class/centreon-clapi/centreonACLAction.class.php index 18500658678..c7d07c5860f 100644 --- a/www/class/centreon-clapi/centreonACLAction.class.php +++ b/www/class/centreon-clapi/centreonACLAction.class.php @@ -307,24 +307,24 @@ public function export($filterName = null) $filters ); - $exportLine = ''; foreach ($aclActionRuleList as $aclActionRule) { - $exportLine .= $this->action . $this->delim . 'ADD' . $this->delim - . $aclActionRule['acl_action_name'] . $this->delim - . $aclActionRule['acl_action_description'] . $this->delim . "\n"; - - $exportLine .= $this->action . $this->delim . 'SETPARAM' . $this->delim - . $aclActionRule['acl_action_name'] . $this->delim; - - $exportLine .= 'activate' . $this->delim . $aclActionRule['acl_action_activate'] . $this->delim . "\n"; - - $exportLine .= $this->exportGrantActions( + echo $this->implodeDelimEscaped(array( + $this->action, + 'ADD', + $aclActionRule['acl_action_name'], + $aclActionRule['acl_action_description'] + )) . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + 'SETPARAM', + $aclActionRule['acl_action_name'], + 'activate', + $aclActionRule['acl_action_activate'] + )) . "\n"; + echo $this->exportGrantActions( $aclActionRule['acl_action_id'], $aclActionRule['acl_action_name'] ); - - echo $exportLine; - $exportLine = ''; } } @@ -343,9 +343,12 @@ private function exportGrantActions($aclActionRuleId, $aclActionName) $aclActionList = $stmt->fetchAll(); foreach ($aclActionList as $aclAction) { - $grantActions .= $this->action . $this->delim . 'GRANT' . $this->delim . - $aclActionName . $this->delim . - $aclAction['acl_action_name'] . $this->delim . "\n"; + $grantActions .= $this->implodeDelimEscaped(array( + $this->action, + 'GRANT', + $aclActionName, + $aclAction['acl_action_name'] + )) . "\n"; } return $grantActions; diff --git a/www/class/centreon-clapi/centreonACLGroup.class.php b/www/class/centreon-clapi/centreonACLGroup.class.php index cc8c06f1391..9babba3a1d7 100644 --- a/www/class/centreon-clapi/centreonACLGroup.class.php +++ b/www/class/centreon-clapi/centreonACLGroup.class.php @@ -283,21 +283,24 @@ public function export($filterName = null) $filters ); - $exportLine = ''; foreach ($aclGroupList as $aclGroup) { - $exportLine .= $this->action . $this->delim . "ADD" . $this->delim - . $aclGroup['acl_group_name'] . $this->delim - . $aclGroup['acl_group_alias'] . $this->delim . "\n"; - - $exportLine .= $this->action . $this->delim . "SETPARAM" . $this->delim - . $aclGroup['acl_group_name'] . $this->delim - . 'activate' . $this->delim - . $aclGroup['acl_group_activate'] . $this->delim . "\n"; - - $exportLine .= $this->exportLinkedObjects($aclGroup['acl_group_id'], $aclGroup['acl_group_name']); - - echo $exportLine; - $exportLine = ''; + echo $this->implodeDelimEscaped(array( + $this->action, + "ADD", + $aclGroup['acl_group_name'], + $aclGroup['acl_group_alias'] + )) . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "SETPARAM", + $aclGroup['acl_group_name'], + 'activate', + $aclGroup['acl_group_activate'] + )) . "\n"; + echo $this->exportLinkedObjects( + $aclGroup['acl_group_id'], + $aclGroup['acl_group_name'] + ); } } @@ -336,9 +339,12 @@ private function exportLinkedObjects($aclGroupId, $aclGroupName) ), ); - $linkedObjectsSetter = $this->action . $this->delim . 'SET%s' . $this->delim . - $aclGroupName . $this->delim . - '%s' . $this->delim . "\n"; + $linkedObjectsSetter = $this->implodeDelimEscaped(array( + $this->action, + 'SET%s', + $aclGroupName, + '%s' + )) . "\n"; $linkedObjectsStr = ''; diff --git a/www/class/centreon-clapi/centreonACLMenu.class.php b/www/class/centreon-clapi/centreonACLMenu.class.php index 77fa1161825..26929052e38 100644 --- a/www/class/centreon-clapi/centreonACLMenu.class.php +++ b/www/class/centreon-clapi/centreonACLMenu.class.php @@ -407,25 +407,26 @@ public function export($filterName = null) $filters ); - $exportLine = ''; foreach ($aclMenuList as $aclMenu) { - $exportLine .= $this->action . $this->delim . "ADD" . $this->delim - . $aclMenu['acl_topo_name'] . $this->delim - . $aclMenu['acl_topo_alias'] . $this->delim . "\n"; - - $exportLine .= $this->action . $this->delim . - "SETPARAM" . $this->delim . - $aclMenu['acl_topo_name'] . $this->delim; + echo $this->implodeDelimEscaped(array( + $this->action, + "ADD", + $aclMenu['acl_topo_name'], + $aclMenu['acl_topo_alias'] + )) . "\n"; + $setTab = array( + $this->action, + "SETPARAM", + $aclMenu['acl_topo_name'], + ); if (!empty($aclMenu['acl_comments'])) { - $exportLine .= 'comment' . $this->delim . $aclMenu['acl_comments'] . $this->delim; + $setTab = array_merge($setTab, array('comment', $aclMenu['acl_comments'])); } + $setTab = array_merge($setTab, array('activate', $aclMenu['acl_topo_activate'])); + echo $this->implodeDelimEscaped($setTab) . "\n"; - $exportLine .= 'activate' . $this->delim . $aclMenu['acl_topo_activate'] . $this->delim . "\n"; - $exportLine .= $this->grantMenu($aclMenu['acl_topo_id'], $aclMenu['acl_topo_name']); - - echo $exportLine; - $exportLine = ''; + echo $this->grantMenu($aclMenu['acl_topo_id'], $aclMenu['acl_topo_name']); } } @@ -438,11 +439,13 @@ private function grantMenu($aclTopoId, $aclTopoName) { $grantedMenu = ''; - $grantedMenuTpl = $this->action . $this->delim . - '%s' . $this->delim . - $aclTopoName . $this->delim . - '%s' . $this->delim . - '%s' . $this->delim . "\n"; + $grantedMenuTpl = $this->implodeDelimEscaped(array( + $this->action, + '%s', + $aclTopoName, + '%s', + '%s' + )) . "\n"; $grantedPossibilities = array( '1' => 'GRANTRW', diff --git a/www/class/centreon-clapi/centreonACLResource.class.php b/www/class/centreon-clapi/centreonACLResource.class.php index 2355b69622f..0187a1374fc 100644 --- a/www/class/centreon-clapi/centreonACLResource.class.php +++ b/www/class/centreon-clapi/centreonACLResource.class.php @@ -454,25 +454,26 @@ public function export($filterName = null) $filters ); - $exportLine = ''; foreach ($aclResourceList as $aclResource) { - $exportLine .= $this->action . $this->delim . "ADD" . $this->delim - . $aclResource['acl_res_name'] . $this->delim - . $aclResource['acl_res_alias'] . $this->delim . "\n"; - - $exportLine .= $this->action . $this->delim . "SETPARAM" . $this->delim - . $aclResource['acl_res_name'] . $this->delim; - + echo $this->implodeDelimEscaped(array( + $this->action, + "ADD", + $aclResource['acl_res_name'], + $aclResource['acl_res_alias'] + )) . "\n"; + + $setTab = array( + $this->action, + "SETPARAM", + $aclResource['acl_res_name'] + ); if (!empty($aclResource['acl_res_comment'])) { - $exportLine .= 'comment' . $this->delim . $aclResource['acl_res_comment'] . $this->delim; + $setTab = array_merge($setTab, array('comment', $aclResource['acl_res_comment'])); } + $setTab = array_merge($setTab, array('activate', $aclResource['acl_res_activate'])); + echo $this->implodeDelimEscaped($setTab) . "\n"; - $exportLine .= 'activate' . $this->delim . $aclResource['acl_res_activate'] . $this->delim . "\n"; - - $exportLine .= $this->exportGrantResources($aclResource); - - echo $exportLine; - $exportLine = ''; + echo $this->exportGrantResources($aclResource); } } @@ -742,9 +743,12 @@ private function exportGrantObject($grantedResourceItems, $grantCommand, $aclRes $grantObject = ''; // Template for object export command - $grantedCommandTpl = $this->action . $this->delim . $grantCommand . $this->delim . - $aclResName . $this->delim . - '%s' . $this->delim . "\n"; + $grantedCommandTpl = $this->implodeDelimEscaped(array( + $this->action, + $grantCommand, + $aclResName, + '%s' + )) . "\n"; $grantedObjectList = ''; if (is_array($grantedResourceItems)) { // Non wildcard mode diff --git a/www/class/centreon-clapi/centreonCentbrokerCfg.class.php b/www/class/centreon-clapi/centreonCentbrokerCfg.class.php index 7c8b86d1bd1..b7cf2be44ce 100644 --- a/www/class/centreon-clapi/centreonCentbrokerCfg.class.php +++ b/www/class/centreon-clapi/centreonCentbrokerCfg.class.php @@ -721,36 +721,50 @@ public function export($filterName = null) 'AND' ); foreach ($elements as $element) { - $addStr = $this->action . $this->delim . "ADD" . - $this->delim . $element['config_name'] . - $this->delim . $this->instanceObj->getInstanceName($element['ns_nagios_server']); - echo $addStr . "\n"; - echo $this->action . $this->delim - . "SETPARAM" . $this->delim - . $element['config_name'] . $this->delim - . "filename" . $this->delim - . $element['config_filename'] . "\n"; - echo $this->action . $this->delim - . "SETPARAM" . $this->delim - . $element['config_name'] . $this->delim - . "cache_directory" . $this->delim - . $element['cache_directory'] . "\n"; - echo $this->action . $this->delim - . "SETPARAM" . $this->delim - . $element['config_name'] . $this->delim - . "stats_activate" . $this->delim - . $element['stats_activate'] . "\n"; - echo $this->action . $this->delim - . "SETPARAM" . $this->delim - . $element['config_name'] . $this->delim - . "daemon" . $this->delim - . $element['daemon'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "ADD", + $element['config_name'], + $this->instanceObj->getInstanceName($element['ns_nagios_server']) + )) . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "SETPARAM", + $element['config_name'], + "filename", + $element['config_filename'] + )) . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "SETPARAM", + $element['config_name'], + "cache_directory", + $element['cache_directory'] + )) . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "SETPARAM", + $element['config_name'], + "stats_activate", + $element['stats_activate'] + )) . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "SETPARAM", + $element['config_name'], + "daemon", + $element['daemon'] + )) . "\n"; + $poolSize = empty($element['pool_size']) ? '' : $element['pool_size']; - echo $this->action . $this->delim - . "SETPARAM" . $this->delim - . $element['config_name'] . $this->delim - . "pool_size" . $this->delim - . $poolSize . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "SETPARAM", + $element['config_name'], + "pool_size", + $poolSize + )) . "\n"; + $sql = "SELECT config_key, config_value, config_group, config_group_id FROM cfg_centreonbroker_info WHERE config_id = ? @@ -758,7 +772,7 @@ public function export($filterName = null) $res = $this->db->query($sql, array($element['config_id'])); $blockId = array(); $categories = array(); - $addParamStr = array(); + $addParamTab = array(); $setParamStr = array(); $resultSet = $res->fetchAll(); unset($res); @@ -775,24 +789,30 @@ public function export($filterName = null) $row['config_value'] = CentreonUtils::convertLineBreak($row['config_value']); if ($row['config_value'] != '') { $setParamStr[$row['config_group'] . '_' . $row['config_group_id']] .= - $this->action . $this->delim . "SET" . strtoupper($row['config_group']) . - $this->delim . $element['config_name'] . - $this->delim . $row['config_group_id'] . - $this->delim . $row['config_key'] . - $this->delim . $row['config_value'] . "\n"; + $this->implodeDelimEscaped(array( + $this->action, + "SET" . strtoupper($row['config_group']), + $element['config_name'], + $row['config_group_id'], + $row['config_key'], + $row['config_value'] + )) . "\n" + ; } } elseif ($row['config_key'] == 'name') { - $addParamStr[$row['config_group'] . '_' . $row['config_group_id']] = - $this->action . $this->delim . "ADD" . strtoupper($row['config_group']) . - $this->delim . $element['config_name'] . - $this->delim . $row['config_value']; + $addParamTab[$row['config_group'] . '_' . $row['config_group_id']] = array( + $this->action, + "ADD" . strtoupper($row['config_group']), + $element['config_name'], + $row['config_value'] + ); } elseif ($row['config_key'] == 'blockId') { $blockId[$row['config_group'] . '_' . $row['config_group_id']] = $row['config_value']; } elseif ($row['config_key'] == 'category') { $categories[$row['config_group'] . '_' . $row['config_group_id']][] = $row['config_value']; } } - foreach ($addParamStr as $id => $add) { + foreach ($addParamTab as $id => $addTab) { if (isset($blockId[$id]) && isset($setParamStr[$id])) { list($tag, $type) = explode('_', $blockId[$id]); $resType = $this->db->query( @@ -801,18 +821,22 @@ public function export($filterName = null) ); $rowType = $resType->fetch(); if (isset($rowType['type_shortname'])) { - echo $add . $this->delim . $rowType['type_shortname'] . "\n"; + $addTab[] = $rowType['type_shortname']; + echo $this->implodeDelimEscaped($addTab) . "\n"; echo $setParamStr[$id]; } unset($resType); } if (isset($categories[$id])) { list($configGroup, $configGroupId) = explode('_', $id); - echo $this->action . $this->delim . "SET" . strtoupper($configGroup) - . $this->delim . $element['config_name'] - . $this->delim . $configGroupId - . $this->delim . 'category' - . $this->delim . implode(',', $categories[$id]) . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "SET" . strtoupper($configGroup), + $element['config_name'], + $configGroupId, + 'category', + implode(',', $categories[$id]) + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonCommand.class.php b/www/class/centreon-clapi/centreonCommand.class.php index b5fd1e53041..aedaea89ab7 100644 --- a/www/class/centreon-clapi/centreonCommand.class.php +++ b/www/class/centreon-clapi/centreonCommand.class.php @@ -360,22 +360,22 @@ public function export($filterName = null) $filters ); foreach ($elements as $element) { - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { - $addStr .= $this->delim . $element[$param]; + $addTab[] = $element[$param]; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; foreach ($element as $parameter => $value) { if (!in_array($parameter, $this->exportExcludedParams)) { if (!is_null($value) && $value != "") { - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $parameter . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + $parameter, + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } if ($parameter == "graph_id" && !empty($value)) { @@ -387,22 +387,23 @@ public function export($filterName = null) } $v = $tmp[$graphObject->getUniqueLabelField()]; - $v = CentreonUtils::convertLineBreak($v); - - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $this->getClapiActionName($parameter) . $this->delim - . $v . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + $this->getClapiActionName($parameter), + CentreonUtils::convertLineBreak($v) + )) . "\n"; } } $argDescriptions = $this->getArgsDescriptions($element['command_id']); if (sizeof($argDescriptions) > 0) { - echo $this->action . $this->delim - . "setargumentdescr" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . implode(';', $argDescriptions) . "\n"; + echo $this->implodeDelimEscaped(array_merge(array( + $this->action, + "setargumentdescr", + $element[$this->object->getUniqueLabelField()] + ), $argDescriptions)) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonContact.class.php b/www/class/centreon-clapi/centreonContact.class.php index c44794accff..71254823034 100644 --- a/www/class/centreon-clapi/centreonContact.class.php +++ b/www/class/centreon-clapi/centreonContact.class.php @@ -559,11 +559,13 @@ private function exportNotifCommands($objType, $contactId, $contactName) $str .= $element[$commandObj->getUniqueLabelField()]; } if ($str) { - echo $this->action . $this->delim - . "setparam" . $this->delim - . $contactName . $this->delim - . $objType . $this->delim - . $str . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $contactName, + $objType, + $str + )) . "\n"; } } @@ -594,12 +596,11 @@ public function export($filterName = null) "AND" ); foreach ($elements as $element) { - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { - $addStr .= $this->delim . $element[$param]; + $addTab[] = $element[$param]; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; foreach ($element as $parameter => $value) { if (!is_null($value) && $value != "" && !in_array($parameter, $this->exportExcludedParams)) { if ($parameter == "timeperiod_tp_id") { @@ -631,12 +632,13 @@ public function export($filterName = null) $value = $result[$this->timezoneObject->getUniqueLabelField()]; } } - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $parameter . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + $parameter, + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } $objId = $element[$this->object->getPrimaryKey()]; diff --git a/www/class/centreon-clapi/centreonContactGroup.class.php b/www/class/centreon-clapi/centreonContactGroup.class.php index 445e1275459..00b76c6a62f 100644 --- a/www/class/centreon-clapi/centreonContactGroup.class.php +++ b/www/class/centreon-clapi/centreonContactGroup.class.php @@ -256,9 +256,13 @@ public function export($filterName = null) ); foreach ($elements as $element) { CentreonContact::getInstance()->export($element[$cFieldName]); - echo $this->action . $this->delim . "addcontact" . - $this->delim . $element[$cgFieldName] . $this->delim . $element[$cFieldName] . - $this->delim . $element['contact_alias'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontact", + $element[$cgFieldName], + $element[$cFieldName], + $element['contact_alias'] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonDependency.class.php b/www/class/centreon-clapi/centreonDependency.class.php index 09dcc558470..9b81c58a066 100644 --- a/www/class/centreon-clapi/centreonDependency.class.php +++ b/www/class/centreon-clapi/centreonDependency.class.php @@ -1254,8 +1254,7 @@ protected function exportHostDep() foreach ($rows as $row) { if ($row['dep_id'] != $previous) { // add dependency echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADD', @@ -1271,8 +1270,7 @@ protected function exportHostDep() } // setparam echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'SETPARAM', @@ -1291,8 +1289,7 @@ protected function exportHostDep() $childRows = $res->fetchAll(); foreach ($childRows as $childRow) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDCHILD', @@ -1312,8 +1309,7 @@ protected function exportHostDep() $childRows = $res->fetchAll(); foreach ($childRows as $childRow) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDCHILD', @@ -1325,8 +1321,7 @@ protected function exportHostDep() } else { // addparent echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDPARENT', @@ -1363,8 +1358,7 @@ protected function exportServiceDep() foreach ($rows as $row) { if ($row['dep_id'] != $previous) { // add dependency echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADD', @@ -1381,8 +1375,7 @@ protected function exportServiceDep() // setparam $v = CentreonUtils::convertLineBreak($v); echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'SETPARAM', @@ -1401,8 +1394,7 @@ protected function exportServiceDep() $childRows = $res->fetchAll(); foreach ($childRows as $childRow) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDCHILD', @@ -1422,8 +1414,7 @@ protected function exportServiceDep() $childRows = $res->fetchAll(); foreach ($childRows as $childRow) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDCHILD', @@ -1435,8 +1426,7 @@ protected function exportServiceDep() } else { // addparent echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDPARENT', @@ -1472,8 +1462,7 @@ protected function exportHostgroupDep() foreach ($rows as $row) { if ($row['dep_id'] != $previous) { // add dependency echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADD', @@ -1489,8 +1478,7 @@ protected function exportHostgroupDep() } // setparam echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'SETPARAM', @@ -1509,8 +1497,7 @@ protected function exportHostgroupDep() $childRows = $res->fetchAll(); foreach ($childRows as $childRow) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDCHILD', @@ -1522,8 +1509,7 @@ protected function exportHostgroupDep() } else { // addparent echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDPARENT', @@ -1559,8 +1545,7 @@ protected function exportServicegroupDep() foreach ($rows as $row) { if ($row['dep_id'] != $previous) { // add dependency echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADD', @@ -1576,8 +1561,7 @@ protected function exportServicegroupDep() } // setparam echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'SETPARAM', @@ -1596,8 +1580,7 @@ protected function exportServicegroupDep() $childRows = $res->fetchAll(); foreach ($childRows as $childRow) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDCHILD', @@ -1609,8 +1592,7 @@ protected function exportServicegroupDep() } else { // addparent echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDPARENT', @@ -1646,8 +1628,7 @@ protected function exportMetaDep() foreach ($rows as $row) { if ($row['dep_id'] != $previous) { // add dependency echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADD', @@ -1663,8 +1644,7 @@ protected function exportMetaDep() } // setparam echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'SETPARAM', @@ -1683,8 +1663,7 @@ protected function exportMetaDep() $childRows = $res->fetchAll(); foreach ($childRows as $childRow) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDCHILD', @@ -1696,8 +1675,7 @@ protected function exportMetaDep() } else { // addparent echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, 'ADDPARENT', diff --git a/www/class/centreon-clapi/centreonDowntime.class.php b/www/class/centreon-clapi/centreonDowntime.class.php index 937f4cde0a8..1f33ddad9ab 100644 --- a/www/class/centreon-clapi/centreonDowntime.class.php +++ b/www/class/centreon-clapi/centreonDowntime.class.php @@ -891,8 +891,7 @@ protected function exportPeriods() } if (!is_null($periodType)) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array_merge( array( $this->action, @@ -970,8 +969,7 @@ protected function exportGenericRel($actionType, $sql) $rows = $stmt->fetchAll(); foreach ($rows as $row) { echo - implode( - $this->delim, + $this->implodeDelimEscaped( array( $this->action, $actionType, diff --git a/www/class/centreon-clapi/centreonEngineCfg.class.php b/www/class/centreon-clapi/centreonEngineCfg.class.php index 326df2ab0d3..969627c09fb 100644 --- a/www/class/centreon-clapi/centreonEngineCfg.class.php +++ b/www/class/centreon-clapi/centreonEngineCfg.class.php @@ -331,15 +331,14 @@ public function export($filterName = null) $element = array_merge($element, $this->getLoggerV2Cfg($element['nagios_id'])); /* ADD action */ - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { if ($param == 'nagios_server_id') { $element[$param] = $this->instanceObj->getInstanceName($element[$param]); } - $addStr .= $this->delim . $element[$param]; + $addTab[] = $element[$param]; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; /* SETPARAM action */ foreach ($element as $parameter => $value) { @@ -362,12 +361,13 @@ public function export($filterName = null) } $value = str_replace("\n", "
", $value); - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $parameter . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + $parameter, + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } $modules = $this->brokerModuleObj->getList( @@ -383,11 +383,13 @@ public function export($filterName = null) foreach ($modules as $module) { array_push($moduleList, $module['broker_module']); } - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . 'broker_module' . $this->delim - . implode('|', $moduleList) . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + 'broker_module', + implode('|', $moduleList) + )) . "\n"; } } diff --git a/www/class/centreon-clapi/centreonHost.class.php b/www/class/centreon-clapi/centreonHost.class.php index c19aaff93ec..ec66e11dc91 100644 --- a/www/class/centreon-clapi/centreonHost.class.php +++ b/www/class/centreon-clapi/centreonHost.class.php @@ -1297,24 +1297,24 @@ public function export($filterName = null) } foreach ($elements as $element) { - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { - $addStr .= $this->delim; + $value = ''; if ($param == 'instance') { if ($this->register) { foreach ($instElements as $instElem) { if ($element['host_name'] == $instElem['host_name']) { - $addStr .= $instElem['name']; + $value = $instElem['name']; } } } } if (isset($element[$param]) && $param != "hostgroup" && $param != "template") { - $addStr .= $element[$param]; + $value = $element[$param]; } + $addTab[] = $value; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; foreach ($element as $parameter => $value) { if (!in_array($parameter, $this->exportExcludedParams) && !is_null($value) && $value != "") { $action_tmp = null; @@ -1338,30 +1338,32 @@ public function export($filterName = null) } unset($tmpObj); } - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $this->getClapiActionName($parameter) . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + $this->getClapiActionName($parameter), + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } // Set parentship if (isset($parentShip[$element[$this->object->getPrimaryKey()]])) { foreach ($parentShip[$element[$this->object->getPrimaryKey()]] as $parentId) { - echo $this->action . $this->delim - . "addparent" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . ( + echo $this->implodeDelimEscaped(array( + $this->action, + "addparent", + $element[$this->object->getUniqueLabelField()], ( - isset($elements[$parentId]) - && isset($elements[$parentId][$this->object->getUniqueLabelField()]) - ) + ( + isset($elements[$parentId]) + && isset($elements[$parentId][$this->object->getUniqueLabelField()]) + ) ? $elements[$parentId][$this->object->getUniqueLabelField()] : '' - ) - . "\n"; + ) + )) . "\n"; } } @@ -1382,12 +1384,13 @@ public function export($filterName = null) if (isset($params) && is_array($params)) { foreach ($params as $k => $v) { if (!is_null($v) && $v != "") { - $v = CentreonUtils::convertLineBreak($v); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $this->getClapiActionName($k) . $this->delim - . $v . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + $this->getClapiActionName($k), + CentreonUtils::convertLineBreak($v) + )) . "\n"; } } } @@ -1410,13 +1413,15 @@ public function export($filterName = null) $description = "'" . $description . "'"; } - echo $this->action . $this->delim - . "setmacro" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $this->stripMacro($macro['host_macro_name']) . $this->delim - . $macro['host_macro_value'] . $this->delim - . ((strlen($macro['is_password']) === 0) ? 0 : (int) $macro['is_password']) . $this->delim - . $description . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setmacro", + $element[$this->object->getUniqueLabelField()], + $this->stripMacro($macro['host_macro_name']), + $macro['host_macro_value'], + ((strlen($macro['is_password']) === 0) ? 0 : (int) $macro['is_password']), + $description + )) . "\n"; } } $cgRel = new \Centreon_Object_Relation_Contact_Group_Host($this->dependencyInjector); @@ -1436,10 +1441,12 @@ public function export($filterName = null) ); foreach ($elements as $element) { CentreonContactGroup::getInstance()->export($element['cg_name']); - echo $this->action . $this->delim - . "addcontactgroup" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $element['cg_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontactgroup", + $element[$this->object->getUniqueLabelField()], + $element['cg_name'] + )) . "\n"; } $contactRel = new \Centreon_Object_Relation_Contact_Host($this->dependencyInjector); $filters_contactRel = array("host_register" => $this->register); @@ -1458,10 +1465,12 @@ public function export($filterName = null) ); foreach ($elements as $element) { CentreonContact::getInstance()->export($element['contact_alias']); - echo $this->action . $this->delim - . "addcontact" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $element['contact_alias'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontact", + $element[$this->object->getUniqueLabelField()], + $element['contact_alias'] + )) . "\n"; } $htplRel = new \Centreon_Object_Relation_Host_Template_Host($this->dependencyInjector); $filters_htplRel = array("h.host_register" => $this->register); @@ -1480,10 +1489,12 @@ public function export($filterName = null) ); foreach ($elements as $element) { CentreonHostTemplate::getInstance()->export($element['template']); - echo $this->action . $this->delim - . "addtemplate" . $this->delim - . $element['host'] . $this->delim - . $element['template'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addtemplate", + $element['host'], + $element['template'] + )) . "\n"; } // Filter only diff --git a/www/class/centreon-clapi/centreonHostCategory.class.php b/www/class/centreon-clapi/centreonHostCategory.class.php index 558075276d5..779edb4611b 100644 --- a/www/class/centreon-clapi/centreonHostCategory.class.php +++ b/www/class/centreon-clapi/centreonHostCategory.class.php @@ -268,10 +268,12 @@ public function export($filterName = null) 'AND' ); foreach ($elements as $element) { - echo $this->action . $this->delim - . "addmember" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $element['host_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addmember", + $element[$this->object->getUniqueLabelField()], + $element['host_name'] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonHostGroup.class.php b/www/class/centreon-clapi/centreonHostGroup.class.php index 02da2e0c01d..e940969fd97 100644 --- a/www/class/centreon-clapi/centreonHostGroup.class.php +++ b/www/class/centreon-clapi/centreonHostGroup.class.php @@ -385,10 +385,12 @@ public function export($filterName = null) 'AND' ); foreach ($elements as $element) { - echo $this->action . $this->delim - . "addhost" . $this->delim - . $element[$labelField] . $this->delim - . $element[$hFieldName] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addhost", + $element[$labelField], + $element[$hFieldName] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonHostGroupService.class.php b/www/class/centreon-clapi/centreonHostGroupService.class.php index 87711df4246..b38667ae807 100644 --- a/www/class/centreon-clapi/centreonHostGroupService.class.php +++ b/www/class/centreon-clapi/centreonHostGroupService.class.php @@ -944,9 +944,8 @@ public function export($filterName = null) $tpObj = new \Centreon_Object_Timeperiod($this->dependencyInjector); $macroObj = new \Centreon_Object_Service_Macro_Custom($this->dependencyInjector); foreach ($elements as $element) { - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { - $addStr .= $this->delim; if ($param == "service_template_model_stm_id") { $tmp = $this->object->getParameters($element[$param], 'service_description'); if (isset($tmp) && isset($tmp['service_description']) && $tmp['service_description']) { @@ -957,10 +956,9 @@ public function export($filterName = null) $element[$param] = ""; } } - $addStr .= $element[$param]; + $addTab[] = $element[$param]; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; foreach ($element as $parameter => $value) { if (!in_array($parameter, $this->exportExcludedParams) && !is_null($value) && $value != "") { $action_tmp = null; @@ -983,12 +981,14 @@ public function export($filterName = null) } unset($tmpObj); } - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element['hg_name'] . $this->delim - . $element['service_description'] . $this->delim - . $this->getClapiActionName($parameter) . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element['hg_name'], + $element['service_description'], + $this->getClapiActionName($parameter), + $value + )) . "\n"; } } $params = $extendedObj->getParameters( @@ -1004,12 +1004,14 @@ public function export($filterName = null) if (isset($params) && is_array($params)) { foreach ($params as $k => $v) { if (!is_null($v) && $v != "") { - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element['hg_name'] . $this->delim - . $element['service_description'] . $this->delim - . $this->getClapiActionName($k) . $this->delim - . $v . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element['hg_name'], + $element['service_description'], + $this->getClapiActionName($k), + $v + )) . "\n"; } } } @@ -1023,12 +1025,14 @@ public function export($filterName = null) "AND" ); foreach ($macros as $macro) { - echo $this->action . $this->delim - . "setmacro" . $this->delim - . $element['hg_name'] . $this->delim - . $element['service_description'] . $this->delim - . $this->stripMacro($macro['svc_macro_name']) . $this->delim - . $macro['svc_macro_value'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setmacro", + $element['hg_name'], + $element['service_description'], + $this->stripMacro($macro['svc_macro_name']), + $macro['svc_macro_value'] + )) . "\n"; } $cgRel = new \Centreon_Object_Relation_Contact_Group_Service($this->dependencyInjector); $cgelements = $cgRel->getMergedParameters( @@ -1046,11 +1050,13 @@ public function export($filterName = null) ); foreach ($cgelements as $cgelement) { CentreonContactGroup::getInstance()->export($element['cg_name']); - echo $this->action . $this->delim - . "addcontactgroup" . $this->delim - . $element['hg_name'] . $this->delim - . $cgelement['service_description'] . $this->delim - . $cgelement['cg_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontactgroup", + $element['hg_name'], + $cgelement['service_description'], + $cgelement['cg_name'] + )) . "\n"; } $contactRel = new \Centreon_Object_Relation_Contact_Service($this->dependencyInjector); $celements = $contactRel->getMergedParameters( @@ -1068,11 +1074,13 @@ public function export($filterName = null) ); foreach ($celements as $celement) { CentreonContact::getInstance()->export($celement['contact_alias']); - echo $this->action . $this->delim - . "addcontact" . $this->delim - . $element['hg_name'] . $this->delim - . $celement['service_description'] . $this->delim - . $celement['contact_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontact", + $element['hg_name'], + $celement['service_description'], + $celement['contact_name'] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonLDAP.class.php b/www/class/centreon-clapi/centreonLDAP.class.php index 23f34edca61..6c1b2de76e9 100644 --- a/www/class/centreon-clapi/centreonLDAP.class.php +++ b/www/class/centreon-clapi/centreonLDAP.class.php @@ -423,15 +423,20 @@ public function export($filterName = null) ); foreach ($ldapList as $ldap) { - echo $this->action . $this->delim . "ADD" . $this->delim - . $ldap['ar_name'] . $this->delim - . $ldap['ar_description'] . $this->delim . "\n"; - - echo $this->action . $this->delim . "SETPARAM" . $this->delim - . $ldap['ar_name'] . $this->delim - . 'enable' . $this->delim - . $ldap['ar_enable'] . $this->delim . "\n"; - + echo $this->implodeDelimEscaped(array( + $this->action, + "ADD", + $ldap['ar_name'], + $ldap['ar_description'] + )) . "\n"; + + echo $this->implodeDelimEscaped(array( + $this->action, + "SETPARAM", + $ldap['ar_name'], + 'enable', + $ldap['ar_enable'] + )) . "\n"; $filters = array('`auth_ressource_id`' => $ldap['ar_id']); @@ -446,15 +451,17 @@ public function export($filterName = null) ); foreach ($ldapServerList as $server) { - echo $this->action . $this->delim . "ADDSERVER" . $this->delim - . $ldap['ar_name'] . $this->delim - . $server['host_address'] . $this->delim - . $server['host_port'] . $this->delim - . $server['use_ssl'] . $this->delim - . $server['use_tls'] . $this->delim . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "ADDSERVER", + $ldap['ar_name'], + $server['host_address'], + $server['host_port'], + $server['use_ssl'], + $server['use_tls'] + )) . "\n"; } - $filters = array('`ar_id`' => $ldap['ar_id']); $ldapConfigurationLabelField = $configurationLdapObj->getUniqueLabelField(); @@ -483,10 +490,13 @@ public function export($filterName = null) ? $contactGroupName['cg_name'] : null; } - echo $this->action . $this->delim . "SETPARAM" . $this->delim - . $ldap['ar_name'] . $this->delim - . $configuration['ari_name'] . $this->delim - . $configuration['ari_value'] . $this->delim . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "SETPARAM", + $ldap['ar_name'], + $configuration['ari_name'], + $configuration['ari_value'] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonObject.class.php b/www/class/centreon-clapi/centreonObject.class.php index 3aaa3275295..fa2702de5f5 100644 --- a/www/class/centreon-clapi/centreonObject.class.php +++ b/www/class/centreon-clapi/centreonObject.class.php @@ -517,22 +517,22 @@ public function export($filterName = null) "AND" ); foreach ($elements as $element) { - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { $element[$param] = CentreonUtils::convertLineBreak($element[$param]); - $addStr .= $this->delim . $element[$param]; + $addTab[] = $element[$param]; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; foreach ($element as $parameter => $value) { if (!in_array($parameter, $this->exportExcludedParams)) { if (!is_null($value) && $value != "") { - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $parameter . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + $parameter, + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } } @@ -679,10 +679,28 @@ protected function explodeDelimEscaped($values) $tmptok = '##I_AM_NOT_A_DELIM##'; // then on each values the token is replaced by the unescaped char - return array_map(function($v) use($tmptok) { - return str_replace($tmptok, $this->delim, $v); - }, explode($this->delim, - str_replace("\\".$this->delim, $tmptok, $values) + return array_map( + function ($v) use ($tmptok) { + return str_replace($tmptok, $this->delim, $v); + }, + explode($this->delim, str_replace("\\" . $this->delim, $tmptok, $values)) + ); + } + + /** + * Concat a list of values into string (with delimiter escapement support) + * + * @param array $values + * @return string + */ + protected function implodeDelimEscaped($values) + { + // add a backslash before the delimiter char inside each values + return implode($this->delim, array_map( + function ($v) { + return str_replace($this->delim, "\\" . $this->delim, $v); + }, + $values )); } } diff --git a/www/class/centreon-clapi/centreonResourceCfg.class.php b/www/class/centreon-clapi/centreonResourceCfg.class.php index 8dc6db29e92..6c3d8f77774 100644 --- a/www/class/centreon-clapi/centreonResourceCfg.class.php +++ b/www/class/centreon-clapi/centreonResourceCfg.class.php @@ -357,7 +357,7 @@ public function export($filterName = null) ); /* ADD action */ - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { if ($param == 'instance_id') { $instances = array(); @@ -366,22 +366,22 @@ public function export($filterName = null) } $element[$param] = implode('|', $instances); } - $addStr .= $this->delim . $element[$param]; + $addTab[] = $element[$param]; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; /* SETPARAM action */ foreach ($element as $parameter => $value) { if (!in_array($parameter, $this->exportExcludedParams) && !is_null($value) && $value != "") { $parameter = str_replace("resource_", "", $parameter); $value = str_replace("\n", "
", $value); - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getPrimaryKey()] . $this->delim - . $parameter . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getPrimaryKey()], + $parameter, + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonService.class.php b/www/class/centreon-clapi/centreonService.class.php index 7ae980ea0c4..1ffb8ca0c3d 100644 --- a/www/class/centreon-clapi/centreonService.class.php +++ b/www/class/centreon-clapi/centreonService.class.php @@ -1307,9 +1307,8 @@ public function export($filterName = null) $extendedObj = new \Centreon_Object_Service_Extended($this->dependencyInjector); $macroObj = new \Centreon_Object_Service_Macro_Custom($this->dependencyInjector); foreach ($elements as $element) { - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { - $addStr .= $this->delim; if ($param == "service_template_model_stm_id") { $tmp = $this->object->getParameters($element[$param], 'service_description'); if (isset($tmp) && isset($tmp['service_description']) && $tmp['service_description']) { @@ -1320,11 +1319,10 @@ public function export($filterName = null) $element[$param] = ""; } } - $addStr .= $element[$param]; + $addTab[] = $element[$param]; } + echo $this->implodeDelimEscaped($addTab) . "\n"; - $addStr .= "\n"; - echo $addStr; foreach ($element as $parameter => $value) { if (!in_array($parameter, $this->exportExcludedParams) && !is_null($value) && $value != "") { $action_tmp = null; @@ -1344,14 +1342,15 @@ public function export($filterName = null) } unset($tmpObj); } - $value = CentreonUtils::convertLineBreak($value); - if ($this->getClapiActionName($parameter) != "host_id") { - echo $this->action . $this->delim . "setparam" . $this->delim - . $element['host_name'] . $this->delim - . $element['service_description'] . $this->delim - . $this->getClapiActionName($parameter) . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element['host_name'], + $element['service_description'], + $this->getClapiActionName($parameter), + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } } @@ -1368,11 +1367,14 @@ public function export($filterName = null) if (isset($params) && is_array($params)) { foreach ($params as $k => $v) { if (!is_null($v) && $v != "") { - $v = CentreonUtils::convertLineBreak($v); - echo $this->action . $this->delim . "setparam" . $this->delim - . $element['host_name'] . $this->delim - . $element['service_description'] . $this->delim - . $this->getClapiActionName($k) . $this->delim . $v . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element['host_name'], + $element['service_description'], + $this->getClapiActionName($k), + CentreonUtils::convertLineBreak($v) + )) . "\n"; } } } @@ -1397,13 +1399,16 @@ public function export($filterName = null) $description = "'" . $description . "'"; } - echo $this->action . $this->delim . "setmacro" . $this->delim - . $element['host_name'] . $this->delim - . $element['service_description'] . $this->delim - . $this->stripMacro($macro['svc_macro_name']) . $this->delim - . $macro['svc_macro_value'] . $this->delim - . ((strlen($macro['is_password']) === 0) ? 0 : (int) $macro['is_password']) . $this->delim - . $description . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setmacro", + $element['host_name'], + $element['service_description'], + $this->stripMacro($macro['svc_macro_name']), + $macro['svc_macro_value'], + ((strlen($macro['is_password']) === 0) ? 0 : (int) $macro['is_password']), + $description + )) . "\n"; } $cgRel = new \Centreon_Object_Relation_Contact_Group_Service($this->dependencyInjector); $cgelements = $cgRel->getMergedParameters( @@ -1421,10 +1426,13 @@ public function export($filterName = null) ); foreach ($cgelements as $cgelement) { CentreonContactGroup::getInstance()->export($cgelement['cg_name']); - echo $this->action . $this->delim . "addcontactgroup" . $this->delim - . $element['host_name'] . $this->delim - . $cgelement['service_description'] . $this->delim - . $cgelement['cg_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontactgroup", + $element['host_name'], + $cgelement['service_description'], + $cgelement['cg_name'] + )) . "\n"; } $contactRel = new \Centreon_Object_Relation_Contact_Service($this->dependencyInjector); $celements = $contactRel->getMergedParameters( @@ -1442,10 +1450,13 @@ public function export($filterName = null) ); foreach ($celements as $celement) { CentreonContact::getInstance()->export($celement['contact_alias']); - echo $this->action . $this->delim . "addcontact" . $this->delim - . $element['host_name'] . $this->delim - . $celement['service_description'] . $this->delim - . $celement['contact_alias'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontact", + $element['host_name'], + $celement['service_description'], + $celement['contact_alias'] + )) . "\n"; } $trapRel = new \Centreon_Object_Relation_Trap_Service($this->dependencyInjector); $telements = $trapRel->getMergedParameters( @@ -1463,10 +1474,13 @@ public function export($filterName = null) ); foreach ($telements as $telement) { CentreonTrap::getInstance()->export($telement['traps_name']); - echo $this->action . $this->delim . "addtrap" . $this->delim - . $element['host_name'] . $this->delim - . $telement['service_description'] . $this->delim - . $telement['traps_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addtrap", + $element['host_name'], + $telement['service_description'], + $telement['traps_name'] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonServiceCategory.class.php b/www/class/centreon-clapi/centreonServiceCategory.class.php index bbefeb38220..c4def2bebe5 100644 --- a/www/class/centreon-clapi/centreonServiceCategory.class.php +++ b/www/class/centreon-clapi/centreonServiceCategory.class.php @@ -428,16 +428,20 @@ public function export($filterName = null) "AND" ); foreach ($elements as $element) { - echo $this->action . $this->delim - . "addservice" . $this->delim - . $scName . $this->delim - . $element['host_name'] . "," . $element['service_description'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addservice", + $scName, + $element['host_name'] . "," . $element['service_description'] + )) . "\n"; } } else { - echo $this->action . $this->delim - . "addservicetemplate" . $this->delim - . $scName . $this->delim - . $svcParam['service_description'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addservicetemplate", + $scName, + $svcParam['service_description'] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonServiceGroup.class.php b/www/class/centreon-clapi/centreonServiceGroup.class.php index b40ac0395f3..fb2f831eb5d 100644 --- a/www/class/centreon-clapi/centreonServiceGroup.class.php +++ b/www/class/centreon-clapi/centreonServiceGroup.class.php @@ -461,10 +461,12 @@ public function export($filterName = null) "AND" ); foreach ($elements as $element) { - echo $this->action . $this->delim - . "addservice" . $this->delim - . $sgName . $this->delim - . $element['host_name'] . "," . $element['service_description'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addservice", + $sgName, + $element['host_name'] . "," . $element['service_description'] + )) . "\n"; } } $existingRelationIds = $relobjHgSvc->getHostGroupIdServiceIdFromServicegroupId($sgId); @@ -483,10 +485,12 @@ public function export($filterName = null) "AND" ); foreach ($elements as $element) { - echo $this->action . $this->delim - . "addhostgroupservice" . $this->delim - . $sgName . $this->delim - . $element['hg_name'] . "," . $element['service_description'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addhostgroupservice", + $sgName, + $element['hg_name'] . "," . $element['service_description'] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonServiceTemplate.class.php b/www/class/centreon-clapi/centreonServiceTemplate.class.php index 97f63b47abc..2f6512767ee 100644 --- a/www/class/centreon-clapi/centreonServiceTemplate.class.php +++ b/www/class/centreon-clapi/centreonServiceTemplate.class.php @@ -1013,9 +1013,8 @@ protected function parseTemplateTree($tree, $filter_id = null) $extendedObj = new \Centreon_Object_Service_Extended($this->dependencyInjector); $macroObj = new \Centreon_Object_Service_Macro_Custom($this->dependencyInjector); foreach ($tree as $element) { - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { - $addStr .= $this->delim; if ($param == "service_template_model_stm_id") { $tmp = $this->object->getParameters($element[$param], 'service_description'); if (isset($tmp) && isset($tmp['service_description']) && $tmp['service_description']) { @@ -1026,10 +1025,9 @@ protected function parseTemplateTree($tree, $filter_id = null) $element[$param] = ""; } } - $addStr .= $element[$param]; + $addTab[] = $element[$param]; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; foreach ($element as $parameter => $value) { if (!in_array($parameter, $this->exportExcludedParams) && !is_null($value) && $value != "") { $action_tmp = null; @@ -1047,12 +1045,13 @@ protected function parseTemplateTree($tree, $filter_id = null) } unset($tmpObj); } - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element['service_description'] - . $this->delim . $this->getClapiActionName($parameter) . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element['service_description'], + $this->getClapiActionName($parameter), + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } $params = $extendedObj->getParameters( @@ -1068,12 +1067,13 @@ protected function parseTemplateTree($tree, $filter_id = null) if (isset($params) && is_array($params)) { foreach ($params as $k => $v) { if (!is_null($v) && $v != "") { - $v = CentreonUtils::convertLineBreak($v); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element['service_description'] . $this->delim - . $this->getClapiActionName($k) . $this->delim - . $v . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element['service_description'], + $this->getClapiActionName($k), + CentreonUtils::convertLineBreak($v) + )) . "\n"; } } } @@ -1087,11 +1087,13 @@ protected function parseTemplateTree($tree, $filter_id = null) "AND" ); foreach ($macros as $macro) { - echo $this->action . $this->delim - . "setmacro" . $this->delim - . $element['service_description'] . $this->delim - . $this->stripMacro($macro['svc_macro_name']) . $this->delim - . $macro['svc_macro_value'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setmacro", + $element['service_description'], + $this->stripMacro($macro['svc_macro_name']), + $macro['svc_macro_value'] + )) . "\n"; } if (isset($element['children']) && count($element['children'])) { $this->parseTemplateTree($element['children'], $extendedObj); @@ -1155,10 +1157,12 @@ public function export($filterName = null) ); foreach ($elements as $element) { CentreonContactGroup::getInstance()->export($element['cg_name']); - echo $this->action . $this->delim - . "addcontactgroup" . $this->delim - . $element['service_description'] . $this->delim - . $element['cg_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontactgroup", + $element['service_description'], + $element['cg_name'] + )) . "\n"; } // contacts @@ -1179,10 +1183,12 @@ public function export($filterName = null) ); foreach ($elements as $element) { CentreonContact::getInstance()->export($element['contact_alias']); - echo $this->action . $this->delim - . "addcontact" . $this->delim - . $element['service_description'] . $this->delim - . $element['contact_alias'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addcontact", + $element['service_description'], + $element['contact_alias'] + )) . "\n"; } // macros @@ -1200,12 +1206,14 @@ public function export($filterName = null) "AND" ); foreach ($macros as $macro) { - echo $this->action . $this->delim - . "setmacro" . $this->delim - . $element['service_description'] . $this->delim - . $this->stripMacro($macro['svc_macro_name']) . $this->delim - . $macro['svc_macro_value'] . $this->delim - . "'" . $macro['description'] . "'" . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setmacro", + $element['service_description'], + $this->stripMacro($macro['svc_macro_name']), + $macro['svc_macro_value'], + "'" . $macro['description'] . "'" + )) . "\n"; } // traps @@ -1226,10 +1234,12 @@ public function export($filterName = null) ); foreach ($telements as $telement) { CentreonTrap::getInstance()->export($telement['traps_name']); - echo $this->action . $this->delim - . "addtrap" . $this->delim - . $telement['service_description'] . $this->delim - . $telement['traps_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addtrap", + $telement['service_description'], + $telement['traps_name'] + )) . "\n"; } // hosts @@ -1249,10 +1259,12 @@ public function export($filterName = null) "AND" ); foreach ($helements as $helement) { - echo $this->action . $this->delim - . "addhosttemplate" . $this->delim - . $helement['service_description'] . $this->delim - . $helement['host_name'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addhosttemplate", + $helement['service_description'], + $helement['host_name'] + )) . "\n"; } } } diff --git a/www/class/centreon-clapi/centreonTrap.class.php b/www/class/centreon-clapi/centreonTrap.class.php index 712ca53319b..b3c7673672e 100644 --- a/www/class/centreon-clapi/centreonTrap.class.php +++ b/www/class/centreon-clapi/centreonTrap.class.php @@ -346,12 +346,11 @@ public function export($filterName = null) "AND" ); foreach ($elements as $element) { - $addStr = $this->action . $this->delim . "ADD"; + $addTab = array($this->action, "ADD"); foreach ($this->insertParams as $param) { - $addStr .= $this->delim . $element[$param]; + $addTab[] = $element[$param]; } - $addStr .= "\n"; - echo $addStr; + echo $this->implodeDelimEscaped($addTab) . "\n"; foreach ($element as $parameter => $value) { if ($parameter != 'traps_id') { if (!is_null($value) && $value != "") { @@ -360,12 +359,13 @@ public function export($filterName = null) $parameter = 'vendor'; $value = $this->manufacturerObj->getName($value); } - $value = CentreonUtils::convertLineBreak($value); - echo $this->action . $this->delim - . "setparam" . $this->delim - . $element[$this->object->getUniqueLabelField()] . $this->delim - . $parameter . $this->delim - . $value . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "setparam", + $element[$this->object->getUniqueLabelField()], + $parameter, + CentreonUtils::convertLineBreak($value) + )) . "\n"; } } } @@ -379,12 +379,14 @@ public function export($filterName = null) array('trap_id' => $element['traps_id']) ); foreach ($matchingProps as $prop) { - echo $this->action . $this->delim . - "addmatching" . $this->delim . - $element['traps_name'] . $this->delim . - $prop['tmo_string'] . $this->delim . - $prop['tmo_regexp'] . $this->delim . - $prop['tmo_status'] . "\n"; + echo $this->implodeDelimEscaped(array( + $this->action, + "addmatching", + $element['traps_name'], + $prop['tmo_string'], + $prop['tmo_regexp'], + $prop['tmo_status'] + )) . "\n"; } } }