a very big request

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aolex
Forum Newbie
Posts: 2
Joined: Sat Jan 22, 2005 2:18 pm
Location: Dongen, Nederlands
Contact:

a very big request

Post 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 :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
aolex
Forum Newbie
Posts: 2
Joined: Sat Jan 22, 2005 2:18 pm
Location: Dongen, Nederlands
Contact:

Post 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 :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

note: file_put_contents is PHP5 only.
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post 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
CyberSpatium
Forum Newbie
Posts: 21
Joined: Thu Mar 20, 2003 12:23 pm
Contact:

Post 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
User avatar
Trenchant
Forum Contributor
Posts: 291
Joined: Mon Nov 29, 2004 6:04 pm
Location: Web Dummy IS

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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 :))
Post Reply