How to parse a .conf file in php

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 02:14:36

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");).

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!