Standardized the diagnosis code searching in the Rules admin GUI
[openemr.git] / library / Config_File.class.php
blob0ba9636a0d7cc5d0ae50f3a3913cb8bb6979de46
1 <?php
3 /**
4 * Config_File class.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * @link http://smarty.php.net/
21 * @version 2.6.2
22 * @copyright Copyright: 2001-2004 ispi of Lincoln, Inc.
23 * @author Andrei Zmievski <andrei@php.net>
24 * @access public
25 * @package Smarty
28 /* $Id$ */
30 /**
31 * Config file reading class
32 * @package Smarty
34 class Config_File {
35 /**#@+
36 * Options
37 * @var boolean
39 /**
40 * Controls whether variables with the same name overwrite each other.
42 var $overwrite = true;
44 /**
45 * Controls whether config values of on/true/yes and off/false/no get
46 * converted to boolean values automatically.
48 var $booleanize = true;
50 /**
51 * Controls whether hidden config sections/vars are read from the file.
53 var $read_hidden = true;
55 /**
56 * Controls whether or not to fix mac or dos formatted newlines.
57 * If set to true, \r or \r\n will be changed to \n.
59 var $fix_newlines = true;
60 /**#@-*/
62 /** @access private */
63 var $_config_path = "";
64 var $_config_data = array();
65 /**#@-*/
67 /**
68 * Constructs a new config file class.
70 * @param string $config_path (optional) path to the config files
72 function Config_File($config_path = NULL)
74 if (isset($config_path))
75 $this->set_path($config_path);
79 /**
80 * Set the path where configuration files can be found.
82 * @param string $config_path path to the config files
84 function set_path($config_path)
86 if (!empty($config_path)) {
87 if (!is_string($config_path) || !file_exists($config_path) || !is_dir($config_path)) {
88 $this->_trigger_error_msg("Bad config file path '$config_path'");
89 return;
91 if(substr($config_path, -1) != DIRECTORY_SEPARATOR) {
92 $config_path .= DIRECTORY_SEPARATOR;
95 $this->_config_path = $config_path;
101 * Retrieves config info based on the file, section, and variable name.
103 * @param string $file_name config file to get info for
104 * @param string $section_name (optional) section to get info for
105 * @param string $var_name (optional) variable to get info for
106 * @return string|array a value or array of values
108 function &get($file_name, $section_name = NULL, $var_name = NULL)
110 if (empty($file_name)) {
111 $this->_trigger_error_msg('Empty config file name');
112 return;
113 } else {
114 $file_name = $this->_config_path . $file_name;
115 if (!isset($this->_config_data[$file_name]))
116 $this->load_file($file_name, false);
119 if (!empty($var_name)) {
120 if (empty($section_name)) {
121 return $this->_config_data[$file_name]["vars"][$var_name];
122 } else {
123 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name]))
124 return $this->_config_data[$file_name]["sections"][$section_name]["vars"][$var_name];
125 else
126 return array();
128 } else {
129 if (empty($section_name)) {
130 return (array)$this->_config_data[$file_name]["vars"];
131 } else {
132 if(isset($this->_config_data[$file_name]["sections"][$section_name]["vars"]))
133 return (array)$this->_config_data[$file_name]["sections"][$section_name]["vars"];
134 else
135 return array();
142 * Retrieves config info based on the key.
144 * @param $file_name string config key (filename/section/var)
145 * @return string|array same as get()
146 * @uses get() retrieves information from config file and returns it
148 function &get_key($config_key)
150 list($file_name, $section_name, $var_name) = explode('/', $config_key, 3);
151 $result = &$this->get($file_name, $section_name, $var_name);
152 return $result;
156 * Get all loaded config file names.
158 * @return array an array of loaded config file names
160 function get_file_names()
162 return array_keys($this->_config_data);
167 * Get all section names from a loaded file.
169 * @param string $file_name config file to get section names from
170 * @return array an array of section names from the specified file
172 function get_section_names($file_name)
174 $file_name = $this->_config_path . $file_name;
175 if (!isset($this->_config_data[$file_name])) {
176 $this->_trigger_error_msg("Unknown config file '$file_name'");
177 return;
180 return array_keys($this->_config_data[$file_name]["sections"]);
185 * Get all global or section variable names.
187 * @param string $file_name config file to get info for
188 * @param string $section_name (optional) section to get info for
189 * @return array an array of variables names from the specified file/section
191 function get_var_names($file_name, $section = NULL)
193 if (empty($file_name)) {
194 $this->_trigger_error_msg('Empty config file name');
195 return;
196 } else if (!isset($this->_config_data[$file_name])) {
197 $this->_trigger_error_msg("Unknown config file '$file_name'");
198 return;
201 if (empty($section))
202 return array_keys($this->_config_data[$file_name]["vars"]);
203 else
204 return array_keys($this->_config_data[$file_name]["sections"][$section]["vars"]);
209 * Clear loaded config data for a certain file or all files.
211 * @param string $file_name file to clear config data for
213 function clear($file_name = NULL)
215 if ($file_name === NULL)
216 $this->_config_data = array();
217 else if (isset($this->_config_data[$file_name]))
218 $this->_config_data[$file_name] = array();
223 * Load a configuration file manually.
225 * @param string $file_name file name to load
226 * @param boolean $prepend_path whether current config path should be
227 * prepended to the filename
229 function load_file($file_name, $prepend_path = true)
231 if ($prepend_path && $this->_config_path != "")
232 $config_file = $this->_config_path . $file_name;
233 else
234 $config_file = $file_name;
236 ini_set('track_errors', true);
237 $fp = @fopen($config_file, "r");
238 if (!is_resource($fp)) {
239 $this->_trigger_error_msg("Could not open config file '$config_file'");
240 return false;
243 $contents = fread($fp, filesize($config_file));
244 fclose($fp);
246 $this->_config_data[$config_file] = $this->parse_contents($contents);
247 return true;
251 * Store the contents of a file manually.
253 * @param string $config_file file name of the related contents
254 * @param string $contents the file-contents to parse
256 function set_file_contents($config_file, $contents)
258 $this->_config_data[$config_file] = $this->parse_contents($contents);
259 return true;
263 * parse the source of a configuration file manually.
265 * @param string $contents the file-contents to parse
267 function parse_contents($contents)
269 if($this->fix_newlines) {
270 // fix mac/dos formatted newlines
271 $contents = preg_replace('!\r\n?!', "\n", $contents);
274 $config_data = array();
275 $config_data['sections'] = array();
276 $config_data['vars'] = array();
278 /* reference to fill with data */
279 $vars =& $config_data['vars'];
281 /* parse file line by line */
282 preg_match_all('!^.*\r?\n?!m', $contents, $match);
283 $lines = $match[0];
284 for ($i=0, $count=count($lines); $i<$count; $i++) {
285 $line = $lines[$i];
286 if (empty($line)) continue;
288 if ( $line{0} == '[' && preg_match('!^\[(.*?)\]!', $line, $match) ) {
289 /* section found */
290 if ($match[1]{0} == '.') {
291 /* hidden section */
292 if ($this->read_hidden) {
293 $section_name = substr($match[1], 1);
294 } else {
295 /* break reference to $vars to ignore hidden section */
296 unset($vars);
297 $vars = array();
298 continue;
300 } else {
301 $section_name = $match[1];
303 if (!isset($config_data['sections'][$section_name]))
304 $config_data['sections'][$section_name] = array('vars' => array());
305 $vars =& $config_data['sections'][$section_name]['vars'];
306 continue;
309 if (preg_match('/^\s*(\.?\w+)\s*=\s*(.*)/s', $line, $match)) {
310 /* variable found */
311 $var_name = rtrim($match[1]);
312 if (strpos($match[2], '"""') === 0) {
313 /* handle multiline-value */
314 $lines[$i] = substr($match[2], 3);
315 $var_value = '';
316 while ($i<$count) {
317 if (($pos = strpos($lines[$i], '"""')) === false) {
318 $var_value .= $lines[$i++];
319 } else {
320 /* end of multiline-value */
321 $var_value .= substr($lines[$i], 0, $pos);
322 break;
325 $booleanize = false;
327 } else {
328 /* handle simple value */
329 $var_value = preg_replace('/^([\'"])(.*)\1$/', '\2', rtrim($match[2]));
330 $booleanize = $this->booleanize;
333 $this->_set_config_var($vars, $var_name, $var_value, $booleanize);
335 /* else unparsable line / means it is a comment / means ignore it */
337 return $config_data;
340 /**#@+ @access private */
342 * @param array &$container
343 * @param string $var_name
344 * @param mixed $var_value
345 * @param boolean $booleanize determines whether $var_value is converted to
346 * to true/false
348 function _set_config_var(&$container, $var_name, $var_value, $booleanize)
350 if ($var_name{0} == '.') {
351 if (!$this->read_hidden)
352 return;
353 else
354 $var_name = substr($var_name, 1);
357 if (!preg_match("/^[a-zA-Z_]\w*$/", $var_name)) {
358 $this->_trigger_error_msg("Bad variable name '$var_name'");
359 return;
362 if ($booleanize) {
363 if (preg_match("/^(on|true|yes)$/i", $var_value))
364 $var_value = true;
365 else if (preg_match("/^(off|false|no)$/i", $var_value))
366 $var_value = false;
369 if (!isset($container[$var_name]) || $this->overwrite)
370 $container[$var_name] = $var_value;
371 else {
372 settype($container[$var_name], 'array');
373 $container[$var_name][] = $var_value;
378 * @uses trigger_error() creates a PHP warning/error
379 * @param string $error_msg
380 * @param integer $error_type one of
382 function _trigger_error_msg($error_msg, $error_type = E_USER_WARNING)
384 trigger_error("Config_File error: $error_msg", $error_type);
386 /**#@-*/