From c59e21b5c706b46159ecc19b33977299754b07b9 Mon Sep 17 00:00:00 2001 From: Renato Botelho Date: Tue, 3 Sep 2013 12:35:13 -0300 Subject: Fix #3004: . Create a function to replace strings on deep associative arrays . Use the recent created function array_replace_values_recursive to fix VIP interface names instead of touch config.xml directly --- etc/inc/upgrade_config.inc | 15 +++++---------- etc/inc/util.inc | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) (limited to 'etc') diff --git a/etc/inc/upgrade_config.inc b/etc/inc/upgrade_config.inc index a78c393..569dd3a 100644 --- a/etc/inc/upgrade_config.inc +++ b/etc/inc/upgrade_config.inc @@ -2898,16 +2898,11 @@ function upgrade_085_to_086() { foreach ($config['virtualip']['vip'] as $vip) { if ($vip['mode'] != "carp") continue; - $vipchg[] = "s/\\([^_]\\)vip{$vip['vhid']}\\([^0-9]\\)/\\1{$vip['interface']}_vip{$vip['vhid']}\\2/g\n"; - } - if (!empty($vipchg)) { - file_put_contents("{$g['tmp_path']}/vipreplace", $vipchg); - write_config(); - mwexec("/usr/bin/sed -I \"\" -f {$g['tmp_path']}/vipreplace {$g['conf_path']}/config.xml"); - require_once("config.lib.inc"); - unset($config); - $config = parse_config(true); - @unlink("{$g['tmp_path']}/vipreplace"); + $config = array_replace_values_recursive( + $config, + '^vip' . $vip['vhid'] . '$', + "{$vip['interface']}_vip{$vip['vhid']}" + ); } } } diff --git a/etc/inc/util.inc b/etc/inc/util.inc index d6d05ac..6a62563 100644 --- a/etc/inc/util.inc +++ b/etc/inc/util.inc @@ -1778,6 +1778,22 @@ function is_file_included($file = "") { } /* + * Replace a value on a deep associative array using regex + */ +function array_replace_values_recursive($data, $match, $replace) { + if (empty($data)) + return $data; + + if (is_string($data)) + $data = preg_replace("/{$match}/", $replace, $data); + else if (is_array($data)) + foreach ($data as $k => $v) + $data[$k] = array_replace_values_recursive($v, $match, $replace); + + return $data; +} + +/* This function was borrowed from a comment on PHP.net at the following URL: http://www.php.net/manual/en/function.array-merge-recursive.php#73843 */ -- cgit v1.1