Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a .conf file in php

Tags:

php

httpd.conf

Is there any special function to parse a .conf file in php like parse_ini_file() function? If not how can I achieve that? Thanks!

EDIT :

Conf's like httpd.conf(Apache). (I want to read and edit httpd.conf file)

like image 755
Jayanga Kaushalya Avatar asked Jun 15 '12 20:06

Jayanga Kaushalya


People also ask

What is ini file in PHP?

The php. ini file is the default configuration file for running applications that require PHP. It is used to control variables such as upload sizes, file timeouts, and resource limits. php. ini file is the configuration file.

What is config file in PHP?

The config. php file, located in your /global folder contains the unique settings for your Form Tools installation: your database connection settings, root folder and URLs and other information. This file is the only file in the script that should be customized.

What is Parse_ini_file?

Definition and Usage. The parse_ini_file() function parses a configuration (ini) file and returns the settings. Tip: This function can be used to read in your own configuration files, and has nothing to do with the php. ini file.


2 Answers

No, there is no special function to parse httpd.conf, but a parser should be easy to write. For example, if you're only interested in the key-value settings, like ServerRoot /var/www, then this will do the trick:

<?php

define('HTTPD_CONF', '/tmp/httpd.conf');

$lines = file(HTTPD_CONF);
$config = array();

foreach ($lines as $l) {
    preg_match("/^(?P<key>\w+)\s+(?P<value>.*)/", $l, $matches);
    if (isset($matches['key'])) {
        $config[$matches['key']] = $matches['value'];
    }
}

var_dump($config);

If you want to parse the <Directory ...> and other blocks, it'll take you a few more lines of code, but it shouldn't be too taxing. It really depends on what you want to do. You can get a lot of info from $_SERVER and getenv(), so you might not need to parse a config file.

EDIT

In response to your update, for editing httpd.conf, you'll need to run your script with superuser privileges and cause httpd.conf to be reloaded (e.g., system("apachectl graceful");).

like image 67
jmdeldin Avatar answered Sep 28 '22 06:09

jmdeldin


It rather depends on what you mean by a .conf file - does the tag imply you mean an Apache httpd.conf file? In that case I'm pretty sure the answer is "no" - but it rather depends what you want to do with it.

The general case of parsing an Apache .conf file is quite a tricky problem. <Location>, <Directory>, <VirtualHost>, etc. blocks can all change how the server configuration changes for a particular request. Most critically you'll need to be able to understand <IfModule> blocks which add extra configuration if a module is enabled (so you'll need to be able to determine if that particular module is loaded into the Apache process). Once you've done all this you'll need to look at any additional configuration files loaded using Include directives. The final fly in the ointment comes from additional modules which may define their own configuration settings - your code will need to gracefully handle any configuration settings it doesn't understand.

If you restrict the problem then it becomes easier. Do you know (and control) the server configuration? If so, then you'll know what modules are loaded, what files are referenced, and whether you've included any special structures. You might be able to restrict a problem differently - do you just want a particular setting, and is that setting one that only tends to be defined once? If so, you could just search for that string.

Finally, if you're looking to parse an Apache config file as a prelude to writing data to it, then I'd caution against this - allowing a web application to modify server configuration exposes you to a whole class of security vulnerabilities which weren't available before.

Of course, if you're not talking about Apache .conf files at all, then the question is harder to answer. If you're defining your own configuration file format for a custom application, you can define it how you like.

like image 29
simpleigh Avatar answered Sep 28 '22 08:09

simpleigh