Jump to content

Recommended Posts

I am attempting to create a script that, when I purchase a new domain or add a subdomain, will update several files. I need to be able edit a specific line in a file.

here is an example of what I have to write a zonefile

[code=php:0]<?php
$filename = 'C:\\windows\\system32\\dns\\etc\mysite1.com.zone';
$handle = fopen($filename, "x+");
$somecontent = "\$TTL 900

@ SOA     ns1 (
    postmaster
    2004041700
    21600
    1800
    604800
    900

@ NS     ns1
@ NS     ns2

@ A     xx.xx.xxx.xx

@ MX     10 mail

ns1 A     xx.xx.xxx.xx
ns2 A     xx.xx.xxx.xx

mail             A     xx.xx.xxx.xx

www A     xx.xx.xxx.xx";
fwrite($handle, $somecontent);
fclose($handle);
?> [/code]

Now if I want to add a cname I will need to open this file and edit a specific line. I would also want to do this with my named.conf and httpd.conf  here is an example:

say I want to add a domain, I will need to create a zone file and add 4 lines to my named.conf

lines 89 through 97
[code]zone "mysite1.com" IN {
type master;
file "mysite1.com.zone";
};

zone "mysite2.com" IN {
type master;
file "mysite2.com.zone";
};[/code]

So when I create the zone file for my new domain(mysite3.com) I will need to add 4 more lines after line 97. Line 97 is not the last line in the file so I need to be able to add the info to a specific line. The same thing applies.

Any suggestions would be great.

Link to comment
https://forums.phpfreaks.com/topic/14267-editing-a-specific-line-in-a-file/
Share on other sites

I tried this

[code=php:0]<?php
$file = file('C:\\windows\\system32\\dns\\etc\\test_named.conf');
$filename = 'C:\\windows\\system32\\dns\\etc\\test_named.conf';
$lastsite = "mysite2.com";
$lastzone = "mysite2.com.zone";
$layout = "zone \"$lastsite\" IN {
type master;
file \"$lastzone\";
};";
$handle = fopen($filename, "w+b");
$newdomain = "mysite3.com";
$newzone = "mysite3.com.zone";
$somecontent = "
zone \"$newdomain\" IN {
type master;
file \"$newzone\";
};";
foreach($file as $line_number => $content) {
    if (trim($content) == $layout) {
        fwrite($handle, $somecontent);
        fclose($handle);
    }
}
?>[/code]

here is the contents of test_named.conf

[code]zone "mysite.com" IN {
type master;
file "mysite.com.zone";
};

zone "mysite2.com" IN {
type master;
file "mysite2.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
type master;
file "xx.xx.xxx.rev";
};[/code]


it is not writing, it is [b]erasing[/b] all of the content in test_named.conf . This only happens if I use "w",  "w+", "wb",  or "w+b" . If I use any variation of "r" nothing happens.

Anyone have any suggestions as to why this is happening or how I can fix it?
I think the best thing to do is read the whole file into a string/array then edit it and then write it back out to the file.

W,W+,WB etc. all truncate the data before writing. [b]a[/b] is for appending, but because you are editing certain lines you can't use it.
Actually, you just need one modification to your code ... if the line doesn't match the one you're looking for, just write it out as is. Also move the fclose() to after the foreach loop.
[code]<?php
foreach($file as $line_number => $content) {
    if (trim($content) == $layout) {
        fwrite($handle, $somecontent);
    }
    fwrite($handle,$content."\n");
}
fclose($handle);
?>[/code]

Ken
ken,

    I tired what you suggested and it just repeats mysite and mysite2

Here is test_named.conf before

[code=php:0]
zone "mysite.com" IN {
            type master;
            file "mysite.com.zone";
};

zone "mysite2.com" IN {
type master;
file "mysite2.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
type master;
file "xx.xx.xxx.rev";
};[/code]



and after



[code=php:0]
zone "mysite.com" IN {
type master;
file "mysite.com.zone";
};

zone "mysite2.com" IN {
type master;
file "mysite2.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
type master;
file "xx.xx.xxx.rev";
};zone "mysite.com" IN {

type master;

file "mysite.com.zone";

};



zone "mysite2.com" IN {

type master;

file "mysite2.com.zone";

};



zone "xxx.xx.xx.in-addr.arpa" {

type master;

file "xx.xx.xxx.rev";

};[/code]

now I tried it this way

[code=php:0]<?php
$file = file('C:\\windows\\system32\\dns\\etc\\test_named.conf');
$filename = 'C:\\windows\\system32\\dns\\etc\\test_named.conf';
$lastsite = "mysite2.com";
$lastzone = "mysite2.com.zone";
$layout = "zone \"$lastsite\" IN {
type master;
file \"$lastzone\";
};";
$handle = fopen($filename, "a");
$newdomain = "mysite3.com";
$newzone = "mysite3.com.zone";
$somecontent = "
zone \"$newdomain\" IN {
type master;
file \"$newzone\";
};";
foreach($file as $line_number => $content) {
    if (trim($content) == $layout) {
        fwrite($handle, $somecontent);
    }
    fwrite($handle,$somecontent."\n");
}
fclose($handle);
?>[/code]


[b] The result[/b]


[code=php:0]zone "mysite.com" IN {
type master;
file "mysite.com.zone";
};

zone "mysite2.com" IN {
type master;
file "mysite2.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
type master;
file "xx.xx.xxx.rev";
};
zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};

zone "mysite3.com" IN {
type master;
file "mysite3.com.zone";
};[/code]

as you cab see it just repeats it's self over and over again
[b]The Fix[/b]

I added a line in the test_named.php

[code=php:0]
#BEGIN AUTO RECORDS
zone "mysite2.com" IN {
type master;
file "mysite2.com.zone";
};

zone "mysite.com" IN {
type master;
file "mysite.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
type master;
file "xx.xx.xxx.rev";
};[/code]



Then I edited the update.php


[code=php:0]<?php
$file = 'C:/windows/system32/dns/etc/test_named.conf';
$fp = fopen($file, 'r+');
$newdomain = "mysite3.com";
$newzone = "mysite3.com.zone";
$layout = "
zone \"$newdomain\" IN {
    type master;
    file \"$newzone\";
};
";

while (!feof($fp)){
    $line = trim(fgets($fp, 1024));
    if ($line == '#BEGIN AUTO RECORDS'){
        //Find where we are so we can come back.
        $fpos = ftell($fp);
        //First, read the rest of the file, so we don't overwrite it.
        $rest = fread($fp, filesize($file));

        //Go back
        fseek($fp, $fpos, SEEK_SET);
        //Write the new data after the marker.
        fwrite($fp, "\n\n");
        fwrite($fp, $layout);
        //Write back the rest of the data.
        fwrite($fp, $rest);
    }
}
fclose($fp);
?>[/code]

This will add the new domain like this

[code=php:0]
#BEGIN AUTO RECORDS
zone "mysite3.com" IN { //the new domain
type master;
file "mysite3.com.zone";
};

zone "mysite2.com" IN {
type master;
file "mysite2.com.zone";
};

zone "mysite.com" IN {
type master;
file "mysite.com.zone";
};

zone "xxx.xx.xx.in-addr.arpa" {
type master;
file "xx.xx.xxx.rev";
};[/code]

The only issue that I see that maybe a problem is that there is a large space after the #BEGIN AUTO RECORDS . I have created a basic form to add a new domain(this is just a test) You can see it here
[url=http://www.owpt.biz/home/test/open.php]http://www.owpt.biz/home/test/open.php[/url]
Add a domain to see what I mean

Any suggestions would be great.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.