From 5a6f3ca0afa353260c9911458faf03f91d16b65b Mon Sep 17 00:00:00 2001 From: Scott Ullrich Date: Wed, 2 May 2007 22:36:26 +0000 Subject: * Do not allow duplicate entries by default in add_text_to_file() * Add option which allows duplicates Submitted-by: Uranellus via IRC --- etc/inc/pfsense-utils.inc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'etc/inc/pfsense-utils.inc') diff --git a/etc/inc/pfsense-utils.inc b/etc/inc/pfsense-utils.inc index 14f8b93..d8b8b98 100644 --- a/etc/inc/pfsense-utils.inc +++ b/etc/inc/pfsense-utils.inc @@ -1536,17 +1536,18 @@ function remove_text_from_file($file, $text) { * add_text_to_file($file, $text): adds $text to $file. * replaces the text if it already exists. */ -function add_text_to_file($file, $text) { +function add_text_to_file($file, $text, $replace = false) { if(file_exists($file) and is_writable($file)) { - $filecontents = split("\n", file_get_contents($file)); + $filecontents = file($file); $fout = fopen($file, "w"); - $new_file_text = ""; - foreach($filecontents as $line) { - if($line) - $new_file_text .= rtrim($line) . "\n"; - } - $new_file_text .= $text . "\n"; - $file_text = str_replace("\n\n", "\n", $new_file_text); + + $filecontents = array_map('rtrim', $filecontents); + array_push($filecontents, $text); + if ($replace) + $filecontents = array_unique($filecontents); + + $file_text = implode("\n", $filecontents); + fwrite($fout, $file_text); fclose($fout); return true; -- cgit v1.1