| 1 |
lars |
1 |
<?php
|
|
|
2 |
// +----------------------------------------------------------------------+
|
|
|
3 |
// | PHP Version 4 |
|
|
|
4 |
// +----------------------------------------------------------------------+
|
|
|
5 |
// | Copyright (c) 1997-2003 The PHP Group |
|
|
|
6 |
// +----------------------------------------------------------------------+
|
|
|
7 |
// | This source file is subject to version 2.0 of the PHP license, |
|
|
|
8 |
// | that is bundled with this package in the file LICENSE, and is |
|
|
|
9 |
// | available at through the world-wide-web at |
|
|
|
10 |
// | http://www.php.net/license/2_02.txt. |
|
|
|
11 |
// | If you did not receive a copy of the PHP license and are unable to |
|
|
|
12 |
// | obtain it through the world-wide-web, please send a note to |
|
|
|
13 |
// | license@php.net so we can mail you a copy immediately. |
|
|
|
14 |
// +----------------------------------------------------------------------+
|
|
|
15 |
// | Author: Bertrand Mansion <bmansion@mamasam.com> |
|
|
|
16 |
// +----------------------------------------------------------------------+
|
|
|
17 |
//
|
|
|
18 |
// $Id: GenericConf.php 306537 2010-12-21 08:09:34Z cweiske $
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Config parser for generic .conf files like
|
|
|
22 |
* htdig.conf...
|
|
|
23 |
*
|
|
|
24 |
* @author Bertrand Mansion <bmansion@mamasam.com>
|
|
|
25 |
* @package Config
|
|
|
26 |
*/
|
|
|
27 |
class Config_Container_GenericConf {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* This class options:
|
|
|
31 |
* Ex: $options['comment'] = '#';
|
|
|
32 |
* Ex: $options['equals'] = ':';
|
|
|
33 |
* Ex: $options['newline'] = '\\';
|
|
|
34 |
*
|
|
|
35 |
* @var array
|
|
|
36 |
*/
|
|
|
37 |
var $options = array();
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Constructor
|
|
|
41 |
*
|
|
|
42 |
* @access public
|
|
|
43 |
* @param string $options (optional)Options to be used by renderer
|
|
|
44 |
*/
|
|
|
45 |
function Config_Container_GenericConf($options = array())
|
|
|
46 |
{
|
|
|
47 |
if (empty($options['comment'])) {
|
|
|
48 |
$options['comment'] = '#';
|
|
|
49 |
}
|
|
|
50 |
if (empty($options['equals'])) {
|
|
|
51 |
$options['equals'] = ':';
|
|
|
52 |
}
|
|
|
53 |
if (empty($options['newline'])) {
|
|
|
54 |
$options['newline'] = '\\';
|
|
|
55 |
}
|
|
|
56 |
$this->options = $options;
|
|
|
57 |
} // end constructor
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Parses the data of the given configuration file
|
|
|
61 |
*
|
|
|
62 |
* @access public
|
|
|
63 |
* @param string $datasrc path to the configuration file
|
|
|
64 |
* @param object $obj reference to a config object
|
|
|
65 |
* @return mixed returns a PEAR_ERROR, if error occurs or true if ok
|
|
|
66 |
*/
|
|
|
67 |
function &parseDatasrc($datasrc, &$obj)
|
|
|
68 |
{
|
|
|
69 |
$return = true;
|
|
|
70 |
if (!is_readable($datasrc)) {
|
|
|
71 |
return PEAR::raiseError("Datasource file cannot be read.", null, PEAR_ERROR_RETURN);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$lines = file($datasrc);
|
|
|
75 |
$n = 0;
|
|
|
76 |
$lastline = '';
|
|
|
77 |
$currentSection =& $obj->container;
|
|
|
78 |
foreach ($lines as $line) {
|
|
|
79 |
$n++;
|
|
|
80 |
if (!preg_match('/^\s*'.$this->options['comment'].'/', $line) &&
|
|
|
81 |
preg_match('/^\s*(.*)'.$this->options['newline'].'\s*$/', $line, $match)) {
|
|
|
82 |
// directive on more than one line
|
|
|
83 |
$lastline .= $match[1];
|
|
|
84 |
continue;
|
|
|
85 |
}
|
|
|
86 |
if ($lastline != '') {
|
|
|
87 |
$line = $lastline.trim($line);
|
|
|
88 |
$lastline = '';
|
|
|
89 |
}
|
|
|
90 |
if (preg_match('/^\s*'.$this->options['comment'].'+\s*(.*?)\s*$/', $line, $match)) {
|
|
|
91 |
// a comment
|
|
|
92 |
$currentSection->createComment($match[1]);
|
|
|
93 |
} elseif (preg_match('/^\s*$/', $line)) {
|
|
|
94 |
// a blank line
|
|
|
95 |
$currentSection->createBlank();
|
|
|
96 |
} elseif (preg_match('/^\s*([\w-]+)\s*'.$this->options['equals'].'\s*((.*?)|)\s*$/', $line, $match)) {
|
|
|
97 |
// a directive
|
|
|
98 |
$currentSection->createDirective($match[1], $match[2]);
|
|
|
99 |
} else {
|
|
|
100 |
return PEAR::raiseError("Syntax error in '$datasrc' at line $n.", null, PEAR_ERROR_RETURN);
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
return $return;
|
|
|
104 |
} // end func parseDatasrc
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Returns a formatted string of the object
|
|
|
108 |
* @param object $obj Container object to be output as string
|
|
|
109 |
* @access public
|
|
|
110 |
* @return string
|
|
|
111 |
*/
|
|
|
112 |
function toString(&$obj)
|
|
|
113 |
{
|
|
|
114 |
$string = '';
|
|
|
115 |
switch ($obj->type) {
|
|
|
116 |
case 'blank':
|
|
|
117 |
$string = "\n";
|
|
|
118 |
break;
|
|
|
119 |
case 'comment':
|
|
|
120 |
$string = $this->options['comment'].$obj->content."\n";
|
|
|
121 |
break;
|
|
|
122 |
case 'directive':
|
|
|
123 |
$string = $obj->name.$this->options['equals'].$obj->content."\n";
|
|
|
124 |
break;
|
|
|
125 |
case 'section':
|
|
|
126 |
// How to deal with sections ???
|
|
|
127 |
if (count($obj->children) > 0) {
|
|
|
128 |
for ($i = 0; $i < count($obj->children); $i++) {
|
|
|
129 |
$string .= $this->toString($obj->getChild($i));
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
break;
|
|
|
133 |
default:
|
|
|
134 |
$string = '';
|
|
|
135 |
}
|
|
|
136 |
return $string;
|
|
|
137 |
} // end func toString
|
|
|
138 |
} // end class Config_Container_GenericConf
|
|
|
139 |
?>
|