| 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 |
// | Authors: Bertrand Mansion <bmansion@mamasam.com> |
|
|
|
16 |
// +----------------------------------------------------------------------+
|
|
|
17 |
//
|
|
|
18 |
// $Id: PHPArray.php 306488 2010-12-20 08:45:09Z cweiske $
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Config parser for common PHP configuration array
|
|
|
22 |
* such as found in the horde project.
|
|
|
23 |
*
|
|
|
24 |
* Options expected is:
|
|
|
25 |
* 'name' => 'conf'
|
|
|
26 |
* Name of the configuration array.
|
|
|
27 |
* Default is $conf[].
|
|
|
28 |
* 'useAttr' => true
|
|
|
29 |
* Whether we render attributes
|
|
|
30 |
*
|
|
|
31 |
* @author Bertrand Mansion <bmansion@mamasam.com>
|
|
|
32 |
* @package Config
|
|
|
33 |
*/
|
|
|
34 |
class Config_Container_PHPArray {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* This class options:
|
|
|
38 |
* - name of the config array to parse/output
|
|
|
39 |
* Ex: $options['name'] = 'myconf';
|
|
|
40 |
* - Whether to add attributes to the array
|
|
|
41 |
* Ex: $options['useAttr'] = false;
|
|
|
42 |
* - Whether to treat numbered arrays as duplicates of their parent directive
|
|
|
43 |
* or as individual directives
|
|
|
44 |
* Ex: $options['duplicateDirectives'] = false;
|
|
|
45 |
*
|
|
|
46 |
* @var array
|
|
|
47 |
*/
|
|
|
48 |
var $options = array('name' => 'conf',
|
|
|
49 |
'useAttr' => true,
|
|
|
50 |
'duplicateDirectives' => true);
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Constructor
|
|
|
54 |
*
|
|
|
55 |
* @access public
|
|
|
56 |
* @param string $options Options to be used by renderer
|
|
|
57 |
*/
|
|
|
58 |
function Config_Container_PHPArray($options = array())
|
|
|
59 |
{
|
|
|
60 |
foreach ($options as $key => $value) {
|
|
|
61 |
$this->options[$key] = $value;
|
|
|
62 |
}
|
|
|
63 |
} // end constructor
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Parses the data of the given configuration file
|
|
|
67 |
*
|
|
|
68 |
* @access public
|
|
|
69 |
* @param string $datasrc path to the configuration file
|
|
|
70 |
* @param object $obj reference to a config object
|
|
|
71 |
* @return mixed returns a PEAR_ERROR, if error occurs or true if ok
|
|
|
72 |
*/
|
|
|
73 |
function &parseDatasrc($datasrc, &$obj)
|
|
|
74 |
{
|
|
|
75 |
$return = true;
|
|
|
76 |
if (empty($datasrc)) {
|
|
|
77 |
return PEAR::raiseError("Datasource file path is empty.", null, PEAR_ERROR_RETURN);
|
|
|
78 |
}
|
|
|
79 |
if (is_array($datasrc)) {
|
|
|
80 |
$this->_parseArray($datasrc, $obj->container);
|
|
|
81 |
} else {
|
|
|
82 |
if (!file_exists($datasrc)) {
|
|
|
83 |
return PEAR::raiseError("Datasource file does not exist.", null, PEAR_ERROR_RETURN);
|
|
|
84 |
} else {
|
|
|
85 |
include($datasrc);
|
|
|
86 |
if (!isset(${$this->options['name']}) || !is_array(${$this->options['name']})) {
|
|
|
87 |
return PEAR::raiseError("File '$datasrc' does not contain a required '".$this->options['name']."' array.", null, PEAR_ERROR_RETURN);
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
$this->_parseArray(${$this->options['name']}, $obj->container);
|
|
|
91 |
}
|
|
|
92 |
return $return;
|
|
|
93 |
} // end func parseDatasrc
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Parses the PHP array recursively
|
|
|
97 |
* @param array $array array values from the config file
|
|
|
98 |
* @param object $container reference to the container object
|
|
|
99 |
* @access private
|
|
|
100 |
* @return void
|
|
|
101 |
*/
|
|
|
102 |
function _parseArray($array, &$container)
|
|
|
103 |
{
|
|
|
104 |
foreach ($array as $key => $value) {
|
|
|
105 |
switch ((string)$key) {
|
|
|
106 |
case '@':
|
|
|
107 |
$container->setAttributes($value);
|
|
|
108 |
break;
|
|
|
109 |
case '#':
|
|
|
110 |
$container->setType('directive');
|
|
|
111 |
$container->setContent($value);
|
|
|
112 |
break;
|
|
|
113 |
default:
|
|
|
114 |
if (is_array($value)) {
|
|
|
115 |
if ($this->options['duplicateDirectives'] == true
|
|
|
116 |
//speed (first/one key is numeric)
|
|
|
117 |
&& is_integer(key($value))
|
|
|
118 |
//accuracy (all keys are numeric)
|
|
|
119 |
&& 1 == count(array_unique(array_map('is_numeric', array_keys($value))))
|
|
|
120 |
) {
|
|
|
121 |
foreach ($value as $nestedValue) {
|
|
|
122 |
if (is_array($nestedValue)) {
|
|
|
123 |
$section =& $container->createSection($key);
|
|
|
124 |
$this->_parseArray($nestedValue, $section);
|
|
|
125 |
} else {
|
|
|
126 |
$container->createDirective($key, $nestedValue);
|
|
|
127 |
}
|
|
|
128 |
}
|
|
|
129 |
} else {
|
|
|
130 |
$section =& $container->createSection($key);
|
|
|
131 |
$this->_parseArray($value, $section);
|
|
|
132 |
}
|
|
|
133 |
} else {
|
|
|
134 |
$container->createDirective($key, $value);
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
} // end func _parseArray
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Returns a formatted string of the object
|
|
|
142 |
* @param object $obj Container object to be output as string
|
|
|
143 |
* @access public
|
|
|
144 |
* @return string
|
|
|
145 |
*/
|
|
|
146 |
function toString(&$obj)
|
|
|
147 |
{
|
|
|
148 |
if (!isset($string)) {
|
|
|
149 |
$string = '';
|
|
|
150 |
}
|
|
|
151 |
switch ($obj->type) {
|
|
|
152 |
case 'blank':
|
|
|
153 |
$string .= "\n";
|
|
|
154 |
break;
|
|
|
155 |
case 'comment':
|
|
|
156 |
$string .= '// '.$obj->content."\n";
|
|
|
157 |
break;
|
|
|
158 |
case 'directive':
|
|
|
159 |
$attrString = '';
|
|
|
160 |
$parentString = $this->_getParentString($obj);
|
|
|
161 |
$attributes = $obj->getAttributes();
|
|
|
162 |
if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
|
|
|
163 |
// Directive with attributes '@' and value '#'
|
|
|
164 |
$string .= $parentString."['#']";
|
|
|
165 |
foreach ($attributes as $attr => $val) {
|
|
|
166 |
$attrString .= $parentString."['@']"
|
|
|
167 |
."['".$attr."'] = '".addcslashes($val, "\\'")."';\n";
|
|
|
168 |
}
|
|
|
169 |
} else {
|
|
|
170 |
$string .= $parentString;
|
|
|
171 |
}
|
|
|
172 |
$string .= ' = ';
|
|
|
173 |
if (is_string($obj->content)) {
|
|
|
174 |
$string .= "'".addcslashes($obj->content, "\\'")."'";
|
|
|
175 |
} elseif (is_int($obj->content) || is_float($obj->content)) {
|
|
|
176 |
$string .= $obj->content;
|
|
|
177 |
} elseif (is_bool($obj->content)) {
|
|
|
178 |
$string .= ($obj->content) ? 'true' : 'false';
|
|
|
179 |
} elseif ($obj->content === null) {
|
|
|
180 |
$string .= 'null';
|
|
|
181 |
}
|
|
|
182 |
$string .= ";\n";
|
|
|
183 |
$string .= $attrString;
|
|
|
184 |
break;
|
|
|
185 |
case 'section':
|
|
|
186 |
$attrString = '';
|
|
|
187 |
$attributes = $obj->getAttributes();
|
|
|
188 |
if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
|
|
|
189 |
$parentString = $this->_getParentString($obj);
|
|
|
190 |
foreach ($attributes as $attr => $val) {
|
|
|
191 |
$attrString .= $parentString."['@']"
|
|
|
192 |
."['".$attr."'] = '".addcslashes($val, "\\'")."';\n";
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
$string .= $attrString;
|
|
|
196 |
if ($count = count($obj->children)) {
|
|
|
197 |
for ($i = 0; $i < $count; $i++) {
|
|
|
198 |
$string .= $this->toString($obj->getChild($i));
|
|
|
199 |
}
|
|
|
200 |
}
|
|
|
201 |
break;
|
|
|
202 |
default:
|
|
|
203 |
$string = '';
|
|
|
204 |
}
|
|
|
205 |
return $string;
|
|
|
206 |
} // end func toString
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* Returns a formatted string of the object parents
|
|
|
210 |
* @access private
|
|
|
211 |
* @return string
|
|
|
212 |
*/
|
|
|
213 |
function _getParentString(&$obj)
|
|
|
214 |
{
|
|
|
215 |
$string = '';
|
|
|
216 |
if (!$obj->isRoot()) {
|
|
|
217 |
$string = is_int($obj->name) ? "[".$obj->name."]" : "['".$obj->name."']";
|
|
|
218 |
$string = $this->_getParentString($obj->parent).$string;
|
|
|
219 |
$count = $obj->parent->countChildren(null, $obj->name);
|
|
|
220 |
if ($count > 1) {
|
|
|
221 |
$string .= '['.$obj->getItemPosition(false).']';
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
else {
|
|
|
225 |
if (empty($this->options['name'])) {
|
|
|
226 |
$string .= '$'.$obj->name;
|
|
|
227 |
} else {
|
|
|
228 |
$string .= '$'.$this->options['name'];
|
|
|
229 |
}
|
|
|
230 |
}
|
|
|
231 |
return $string;
|
|
|
232 |
} // end func _getParentString
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* Writes the configuration to a file
|
|
|
236 |
*
|
|
|
237 |
* @param mixed datasrc info on datasource such as path to the configuraton file
|
|
|
238 |
* @param string configType (optional)type of configuration
|
|
|
239 |
* @access public
|
|
|
240 |
* @return string
|
|
|
241 |
*/
|
|
|
242 |
function writeDatasrc($datasrc, &$obj)
|
|
|
243 |
{
|
|
|
244 |
$fp = @fopen($datasrc, 'w');
|
|
|
245 |
if ($fp) {
|
|
|
246 |
$string = "<?php\n". $this->toString($obj) ."?>"; // <? : Fix my syntax coloring
|
|
|
247 |
$len = strlen($string);
|
|
|
248 |
@flock($fp, LOCK_EX);
|
|
|
249 |
@fwrite($fp, $string, $len);
|
|
|
250 |
@flock($fp, LOCK_UN);
|
|
|
251 |
@fclose($fp);
|
|
|
252 |
return true;
|
|
|
253 |
} else {
|
|
|
254 |
return PEAR::raiseError('Cannot open datasource for writing.', 1, PEAR_ERROR_RETURN);
|
|
|
255 |
}
|
|
|
256 |
} // end func writeDatasrc
|
|
|
257 |
} // end class Config_Container_PHPArray
|
|
|
258 |
?>
|