3 * Project: template_lite, a smarter template engine
4 * File: class.config.php
5 * Author: Paul Lockaby <paul@paullockaby.com>, Mark Dickenson <akapanamajack@sourceforge.net>
6 * Copyright: 2003,2004,2005 by Paul Lockaby, 2005,2006 Mark Dickenson
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * The latest version of template_lite can be obtained from:
23 * http://templatelite.sourceforge.net
28 var $overwrite = false; // overwrite variables of the same name? if false, an array will be created
29 var $booleanize = true; // turn true/false, yes/no, on/off, into 1/0
30 var $fix_new_lines = true; // turns \r\n into \n?
31 var $read_hidden = true; // read hidden sections?
33 var $_db_qstr_regexp = null;
34 var $_bool_true_regexp = null;
35 var $_bool_false_regexp = null;
36 var $_qstr_regexp = null;
40 $this->_db_qstr_regexp
= '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"';
41 $this->_bool_true_regexp
= 'true|yes|on';
42 $this->_bool_false_regexp
= 'false|no|off';
43 $this->_qstr_regexp
= '(?:' . $this->_db_qstr_regexp
. '|' . $this->_bool_true_regexp
. '|' . $this->_bool_false_regexp
. ')';
46 function config_load($file, $section_name = null, $var_name = null)
49 $contents = file_get_contents($file);
52 die("Could not open $file");
55 // insert new line into beginning of file
56 $contents = "\n" . $contents;
58 if ($this->fix_new_lines
)
60 $contents = str_replace("\r\n","\n",$contents);
64 if (preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))
66 $_result["globals"] = $this->_parse_config_section($match[1]);
70 if (preg_match_all("/^\[(.*?)\]/m", $contents, $match))
72 foreach ($match[1] as $section)
74 if ($section{0} == '.' && !$this->read_hidden
)
78 preg_match("/\[".preg_quote($section)."\](.*?)(\n\[|\Z)/s",$contents,$match);
79 if ($section{0} == '.')
81 $section = substr($section, 1);
83 $_result[$section] = $this->_parse_config_section($match[1]);
88 if (!empty($var_name))
90 if (empty($section_name))
92 return $_result["globals"][$var_name];
96 if(isset($_result[$section_name][$var_name]))
98 return $_result[$section_name][$var_name];
108 if (empty($section_name))
114 if(isset($_result[$section_name]))
116 return $_result[$section_name];
126 function _parse_config_section($body)
129 preg_match_all('!(\n\s*[a-zA-Z0-9_]+)\s*=\s*(' . $this->_qstr_regexp
. ')!s', $body, $ini);
132 for($i = 0, $for_max = count($ini[0]); $i < $for_max; $i++
)
134 if ($this->booleanize
)
136 if (preg_match('/^(' . $this->_bool_true_regexp
. ')$/i', $values[$i]))
140 elseif (preg_match('/^(' . $this->_bool_false_regexp
. ')$/i', $values[$i]))
145 if (!is_numeric($values[$i]) && !is_bool($values[$i]))
147 $values[$i] = str_replace("\n",'',stripslashes(substr($values[$i], 1, -1)));
149 if ($this->overwrite ||
!isset($_result[trim($keys[$i])]))
151 $_result[trim($keys[$i])] = $values[$i];
155 if (!is_array($_result[trim($keys[$i])]))
157 $_result[trim($keys[$i])] = array($_result[trim($keys[$i])]);
159 $_result[trim($keys[$i])][] = $values[$i];