Jump to content


[PHP] Write to new line of apache conf file


12 replies to this topic

#1 timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 11 September 2009 - 02:41 PM

I have searched google for a few hours regarding this issue. I'm using WAMP which uses php 5.3.0 under the command line, since I'm modifying apache via script/php cli, I'm stuck with this version.

So far, everthing I have researched has not worked under windows xp or this version of php.

nr2bl()
\n
\n\r

or
/n
/n/r

Using these special characters appears in the conf file which would ultimately break apache.

Also note the title, I'm not formatting for web. So <pre> tags and storing this in a database are out of the question.

fwrite breaks under php 5.3.0. Can't close the file. I keep getting a resource error. Apprently more research has concluded this to be a bug under this version <_<

// ip based virtual host
$conf  = '<VirtualHost '.$ip.'>';
$conf  .= '	ServerAdmin '.$sadmin;
$conf  .= '	DocumentRoot '.$install_dir . $NewVhostDomain.'/www/';
$conf  .= '	ServerName '.$NewVhostDomain;
$conf  .= '	ErrorLog '.$install_dir . $NewVhostDomain.'/errors/';
$conf  .= '	TransferLog '.$install_dir . $NewVhostDomain.'/logs/';
$conf  .= '</VirtualHost>';

$conf_file = $install_dir .'/vhostconf/'.$NewVhostDomain.'.conf';

file_put_contents($conf_file, $conf);

Everything works, it just appears in one line.

Conf Input:
<VirtualHost >	ServerAdmin email@gmail.com	DocumentRoot c:/wamp/domain.net/www/	ServerName tim1.net	ErrorLog c:/wamp/domain.net/errors/	TransferLog c:/wamp/domain.net/logs/</VirtualHost>

Edited by timdav83, 11 September 2009 - 02:44 PM.


#2 NDBoost

    Young Padawan

  • Members
  • Pip
  • 27 posts
  • Gender:Male
  • Location:Tempe, AZ

Posted 11 September 2009 - 03:11 PM

Your telling me something like this wont work? If its not working its an issue with your php.ini config...

Seeing as \n is parsed in PHP as a new line item.. it would "fwrite" the file contents, then would parse the \n as a new line. and begin writting the next conf to the next line.


$conf  .= '	ServerAdmin '.$sadmin.' \n';

Have you tried using an associative array and writing the contents of the array to the file vs using .=?

IE:
<?php
$data =  array ('1','2','3','4');

$filename = 'data.txt';
$string   = '';

foreach($data as $key => $val) {
	$string .= "$key = $val\n";
}

file_put_contents($filename, $string);
?>

Edited by NDBoost, 11 September 2009 - 03:14 PM.


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 September 2009 - 06:45 PM

1. There are no new lines in the $conf variable as you have it written there.
2. If you were to add "\n" to them, you need to make sure to not use single quotes.

Try:
$conf  = '<VirtualHost '.$ip.'>'."\n";
$conf  .= '	ServerAdmin '.$sadmin."\n";
$conf  .= '	DocumentRoot '.$install_dir . $NewVhostDomain.'/www/'."\n";
$conf  .= '	ServerName '.$NewVhostDomain."\n";
$conf  .= '	ErrorLog '.$install_dir . $NewVhostDomain.'/errors/'."\n";
$conf  .= '	TransferLog '.$install_dir . $NewVhostDomain.'/logs/'."\n";
$conf  .= '</VirtualHost>'."\n";
ref: http://php.net/manua...ypes.string.php

#4 timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 11 September 2009 - 06:57 PM

I have tried both methods suggested. Both output the squarish special characters [] and the data is still on one line. Unfortunately, I can't copy and paste that part of the input.

Edited by timdav83, 11 September 2009 - 07:23 PM.


#5 NDBoost

    Young Padawan

  • Members
  • Pip
  • 27 posts
  • Gender:Male
  • Location:Tempe, AZ

Posted 11 September 2009 - 06:57 PM

View Postrc69, on Sep 12 2009, 12:45 AM, said:

2. If you were to add "\n" to them, you need to make sure to not use single quotes.
duhh i should of noticed that..

#6 Neil

    Past Staff Member

  • Members
  • Pip
  • 149 posts
  • Gender:Male
  • Location:Australia

Posted 11 September 2009 - 09:44 PM

Windows = made of fail.

What rc69 gave you should work

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 11 September 2009 - 09:46 PM

View PostNeil, on Sep 11 2009, 08:44 PM, said:

Windows = made of fail.

Try:
$conf  = '<VirtualHost '.$ip.'>'."\r\n";
$conf  .= '	ServerAdmin '.$sadmin."\r\n";
$conf  .= '	DocumentRoot '.$install_dir . $NewVhostDomain.'/www/'."\r\n";
$conf  .= '	ServerName '.$NewVhostDomain."\r\n";
$conf  .= '	ErrorLog '.$install_dir . $NewVhostDomain.'/errors/'."\r\n";
$conf  .= '	TransferLog '.$install_dir . $NewVhostDomain.'/logs/'."\r\n";
$conf  .= '</VirtualHost>'."\r\n";

Edited by rc69, 11 September 2009 - 09:46 PM.


#8 timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 12 September 2009 - 05:33 AM

\r\n shows the characters as well.

#9 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 12 September 2009 - 11:32 AM

"\r\n" is how new lines are supposed to be done on Windows. If they don't work, then i don't know what else to try.

I guess you could try looking at the following comment on php.net, but i doubt it will help any:
http://php.net/manual/en/function.file-put...tents.php#91793

As an aside: If you open the file in Wordpad instead of Notepad, you should see everything as it's supposed to be (on new lines, without the square characters).

#10 timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 13 September 2009 - 07:34 AM

I believe what I'll have to do is create wild cards, %ip and use preg_replace to replace those values. Because doing it this way:

$conf  = '<VirtualHost %ip>
	ServerAdmin %sa
	DocumentRoot %dr
	ServerName %sn
	ErrorLog %el
	TransferLog %tl
</VirtualHost>';

appears to work the way I want it in notepad.

Edit *

This works:

// ip based virtual host
$conf  = '<VirtualHost ip>
	ServerAdmin sa
	DocumentRoot dr
	ServerName sn
	ErrorLog el
	TransferLog tl
</VirtualHost>'; 

// logs
$error_log = $install_dir . 'vhosts/' . $NewVhostDomain . '/logs/errors/';
$transfer_log = $install_dir . 'vhosts/' . $NewVhostDomain . '/logs/transfers/';

// doc root
$doc_root = $install_dir .'vhosts/' . $NewVhostDomain . '/www/';

// use preg_replace to replace wildcards
$conf = preg_replace('/ip/', $NewVhostIP, $conf);
$conf = preg_replace('/sa/', $sadmin, $conf);
$conf = preg_replace('/dr/', $doc_root, $conf);
$conf = preg_replace('/sn/', $NewVhostDomain, $conf);
$conf = preg_replace('/el/', $error_log, $conf);
$conf = preg_replace('/tl/', $transfer_log, $conf);

Edited by timdav83, 13 September 2009 - 08:03 AM.


#11 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 September 2009 - 11:54 AM

That shouldn't be any different from appending the new-lines like i had it, but if it works, it works. My only suggestions are:

1. Don't use preg_replace() for such simple substitutions, use str_replace().
2. Don't use either:
$conf  = '<VirtualHost '.$NewVhostIP.'>
	ServerAdmin '.$sadmin.'
	DocumentRoot '.$doc_root.'
	ServerName '.$NewVhostDomain.'
	ErrorLog '.$error_log.'
	TransferLog '.$transfer_log.'
</VirtualHost>';


#12 timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 13 September 2009 - 11:28 PM

I know, in php 4, I didn't have to replace characters. I remember not having to append a new line either. Perhaps it just has to do with using 5.3.0.

Edited by timdav83, 13 September 2009 - 11:28 PM.


#13 Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 716 posts
  • Gender:Male
  • Location:Texas

Posted 15 September 2009 - 10:30 AM

Putting it all in one variable was variable was what I was thinking as I was reading this.

I agree with rc69 not to use preg_replace or any other sort of regex or _replace. :P

$conf  = '<VirtualHost %s>
	ServerAdmin %s
	DocumentRoot %s
	ServerName %s
	ErrorLog %s
	TransferLog %s
</VirtualHost>';

$install_dir = '/path/to/your/vhosts/%s';

$output = sprintf(
   $conf,
   $ip, 
   $server_admin,
   sprintf($install_dir,$doc_root),
   $server_name,
   sprintf($install_dir,$error_log),
   sprintf($install_dir,$txfr_log)
);

then you just echo $output. You could also easily use a foreach() on it and create multiple vhosts at a time.

Edited by Hayden, 15 September 2009 - 10:33 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users