Page 1 of 1

a very big request

Posted: Sat Jan 22, 2005 2:28 pm
by aolex
hello there,

i have a very big problem . i have a server and i want to make a script
to allow create/edit/remove vhosts . i am using apache so the chosts
file is something like this :

Code: Select all

<VirtualHost *>
          bla1
          bla
          bla
</VirtualHost>

<VirtualHost *>
          bla2
          bla
          bla
</VirtualHost>

<VirtualHost *>
          bla3
          bla
          bla
</VirtualHost>
now what i would like is a script that reads the file and echo's me the
vhosts like this :

Code: Select all

bla1                                        Delete | Modify
bla2                                        Delete | Modify
bla3                                        Delete | Modify
if i press delete i want the script to delete this from the file :

Code: Select all

<VirtualHost *>
          bla1
          bla
          bla
</VirtualHost>
so that the vhost with the name bla1 removes . if i press delete , i want
to have some fileds with the values in the directive . like this :

Code: Select all

|&#1111;u]bla     &#1111;/u]|
|&#1111;u]bla     &#1111;/u]|
|&#1111;u]bla     &#1111;/u]|
and if i change bla to huh to save the file like this :

Code: Select all

<VirtualHost *>
          bla1
          huh
          huh
</VirtualHost>
i know it's very hard to do it , but please someone , help me ... i've been
trying to get this done but i can't . it's too dificult for me . tell me at least
if it's posiblle . please , help me !

Thank you very very much :)

Posted: Sat Jan 22, 2005 6:53 pm
by timvw
and what exactly are you failing to do?

open a file with http://www.php.net/fopen
read with http://www.php.net/fread
write with http://www.php.net/fwrite
close with http://www.php.net/fclose

the first "real" thing you could experience is that the user that is running the script has no rights to modify the vhosts file.

Posted: Sun Jan 23, 2005 2:59 am
by aolex
you definetely didn't understand . i am the server admin so the permission
stuff is solved :) . i know how to open and write in the file , but how does
the script with subdomain i want to edit ? it's very hard man , ik denk niet
dat het kan :)

Posted: Sun Jan 23, 2005 4:00 pm
by timvw
as i asked before what have you tried already? i'm affraid you haven't tried hard enough...

read the file into an array (each line is a row)

Code: Select all

$lines = explode("\n", file_get_contents('vhosts.txt'));
find all the lines that have ServerName

Code: Select all

foreach($lines as $line)
&#123;
     if (preg_match("#\s+ServerName (.*?)#", $line, $matches))
     &#123;
              echo "$matches&#1111;1] <br>";
      &#125;
&#125;
delete a given <virtualhost> servername $servername </virtualhost> section.

Code: Select all

$file = file_get_contents('vhosts.txt');
$file = str_replace("#<VirtualHost>\n\s+ServerName $servername(.*?)<\/Virtualhost>#", $file);
file_put_contents('vhosts.txt', $file);
etc...

Posted: Sun Jan 23, 2005 4:59 pm
by John Cartwright
note: file_put_contents is PHP5 only.

Posted: Sun Jan 23, 2005 5:00 pm
by Trenchant
An alternative way of doing this is using MySQL. For the CP Web Dummy is designing we use mysql to reference all information.

So everytime you create a new entry save a copy in a mysql database. It makes it a little easyer for certain things like backups. Depending on the application this is for it may not be practical however.

What kind of an application is this for? Who will use this system?

Many People/Few People
Public/Private
Local Access/Global Access

Posted: Sun Jan 23, 2005 5:32 pm
by CyberSpatium
another problem about editing/adding/deleting vhost entries to your httpd.conf file is every time you make changes, you will have to restart apache to get the new chages to take effect.

CyberSpatium

Posted: Sun Jan 23, 2005 6:06 pm
by Trenchant
Thats where the big question about whats the application comes back.

If its public I would set a cron to restart apache every 2 hours only if theres a new entry waiting.

Posted: Sun Jan 23, 2005 7:27 pm
by timvw
Btw, the cron could run every minute... Because it only has to restart apache when fstat on the file changes ;) But then again, i prefer real-time action through ldap and mod_cfg_ldap :) (forgetting about nscd :))