You can use file_get_content(), update the file content, then push the new data with file_put_content().
What we need is some comments where you need to insert new line :
view "internal"
{
match-clients { localnets; };
match-destinations { localnets; };
recursion yes;
include "/etc/named.root.hints";
#INTERNAL
zone "my.internal.zone" {
type master;
file "my.internal.zone.db";
};
};
view "external"
{
match-clients { !localnets; !localhost; };
match-destinations { !localnets; !localhost; };
recursion no;
include "/etc/named.root.hints";
#EXTERNAL
zone "my.external.zone" {
type master;
file "my.external.zone.db";
};
};
So you can catch your comments with PHP and add content after :
$confText = file_get_contents('your_file_path') ;
$newExternalZone = PHP_EOL.
'zone "my.new.external.zone" {
type master;
file "my.new.external.zone.db";
};'.PHP_EOL ;
preg_replace("/(#EXTERNAL)/", "$1".$newExternalZone, $confText) ;
file_put_contents('your_file_path', $confText) ;
The code is quite simple here, catch the #External and put $newExternalZone after ! You can update and use data comming from POST,GET or other for newExternalZone.
I was able to do it using the below code. The code accepts arguments using PHP CLI and creates a new file with the values.
$file = file('named.conf');
$arg = getopt("", array('zone:', 'type:', 'file:'));
$internal_end = 0;
$external_end = 0;
$flag = false;
$count = 0;
foreach ($file as $index => $line) {
if (preg_match('/view\s*"internal"\s*{/i', $line) !== 1 && !$flag) {
continue;
}
$flag = true;
$ob = substr_count($line, '{');
$count += $ob;
$cb = substr_count($line, '}');
$count -= $cb;
if ($count == 0) {
$internal_end = $index;
break;
}
}
array_splice($file, $internal_end, 0, array(
"\n",
"zone \"".$arg['zone']."\" {\n",
"\ttype ".$arg['type'].";\n",
"\tfile \"".$arg['file']."\";\n",
"};\n",
"\n"
));
$flag = false;
$count = 0;
foreach ($file as $index => $line) {
if (preg_match('/view\s*"external"\s*{/i', $line) !== 1 && !$flag) {
continue;
}
$flag = true;
$ob = substr_count($line, '{');
$count += $ob;
$cb = substr_count($line, '}');
$count -= $cb;
if ($count == 0) {
$external_end = $index;
break;
}
}
array_splice($file, $external_end, 0, array(
"\n",
"zone \"".$arg['zone']."\" {\n",
"\ttype ".$arg['type'].";\n",
"\tfile \"".$arg['file']."\";\n",
"};\n",
"\n"
));
file_put_contents('named_new.conf', implode('', $file));
To execute the code callphp script.php --zone=my.new.external.zone --type=master --file=my.new.external.zone.db
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.