功能实现源代码:
<?php
define("__CR__", "\r\n"); //回车换行符
class fileConfigOp
{
/*配置文件*/
var $file = '';
/**
* 构造函数
*
* @param string $file 配置文件
* @return null
*/
function __construct($file = '')
{
if (empty($file)) {
die("Not specify config file!");
}
if (!file_exists($file)) {
die("File '{$this->file}' does not exists!");
}
if (!is_readable($file)) {
die("File '{$this->file}' is not readable!");
}
$this->file =& $file;
}
/**
* 获取配置文件的变量
*
* @param string $varName 变量名称
* @return string|array 如果指定变量名称,则只返回该变量对应的值,否则返回所有变量所对应的一维数组
*/
function get($varName = '')
{
@$array = file($this->file);
if (empty($array)) {
return '';
}
foreach ($array AS $line)
{
$line=trim($line);
if (empty($line)||$line==""||substr($line,0,1)=='#'){
continue;
}
$temp = explode("=",$line);
$var_name = trim($temp[0]);
$var_value = trim($temp[1]);
if (empty($var_name)) {
continue;
}
if(substr($var_value,-1)==';'){
$var_value=substr($var_value,0,strlen($var_value)-1);
}
if(substr($var_value,0,1)=="'"&&substr($var_value,-1)=="'"){
$var_value=substr($var_value,1,strlen($var_value)-2);
}
if(substr($var_value,0,1)=='"'&&substr($var_value,-1)=='"'){
$var_value=substr($var_value,1,strlen($var_value)-2);
}
if ($varName != '' && $var_name == $varName) {
return $var_value;
}
$vars[$var_name] = $var_value;
}
return $vars;
}
/**
* 存储变量到配置文件中
* 不删除配置文件中原有的变量,
* 如果需要保存的变量在配置文件中已经存在,则覆盖该变量的值,否则追加存储
*
* @param array $array 需要保存的数组变量,一维数组
* @return bollen
*/
function save($array = '')
{
if ((!is_array($array)) || empty($array)) {
return false;
}
if (!is_writable($this->file)) {
die("File '{$this->file}' is not writeable, please CHMOD it to 777!");
}
$vars = $this->get();
$vars = !empty($vars) ? array_merge($vars, $array) : $array;
$str='';
foreach ($vars AS $key => $value)
{
$str .= trim($key) . ' = "' . trim($value).'"' . __CR__;
}
//dump($str);
if (!empty($str)) {
@$fd =&fopen($this->file,"w+");
@fputs($fd,$str);
@fclose($fd);
}
return true;
}
/**
* 删除一个配置
* @param unknown_type $varName
*/
function del($varName = ''){
if(empty($varName)){
return false;
}
if (!is_writable($this->file)) {
die("File '{$this->file}' is not writeable, please CHMOD it to 777!");
}
$vars = $this->get();
$str='';
$flag=0;
foreach ($vars as $key=>$value){
if($key!=$varName){
$str .= trim($key) . ' = "' . trim($value).'"' . __CR__;
}else {
$flag=1;
}
}
if (!empty($str)&&$flag==1) {
@$fd =&fopen($this->file,"w+");
@fputs($fd,$str);
@fclose($fd);
}
return true;
}
}
调用代码实例:
$ini = new fileConfigOp("venocap.conf"); //对象实例化
$t1=$ini->get(); //获取所有变量名和值,返回:一维数组
$t2=$ini->get("nic"); //获取某个变量的值,返回:字符串
dump($t1);
dump($t2);
$array=array();
$array['nic1']='eth0';
$array['nic']='eth1';
dump($ini->save($array));
dump($ini->get());
dump($ini->del('nic1'));
dump($ini->get());
配置文件格式:
# network interface for capturing
nic = "eth0";
# redirection URL for HTTP request in IPv4
url = "http://www.veno2.com";
# redirection URL for HTTP request in IPv6
url6 = "http://ipv6.veno2.com";
# libpcap filter, see tcpdump manual for reference
# The following filter means capturing tcp target port 80,
# excluding ip address 2001:dc7:eee4:1::98 (i.e. ipv6.veno2.com) and 221.122.51.98 (www.veno2.com)
filter = "dst port 80 and ! ip6 host 2001:dc7:eee4:1::98 and ! ip host 221.122.51.98";
# HTTP request containing the following User-Agent will be bypassed
agent = "veno";
# controlling log message level
# logmask = 7; # trace message
# logmask = 15; # highest verbosity