init
[garradin.git] / include / libs / template_lite / class.config.php
1 <?php
2 /*
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
7 *
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.
12 *
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.
17 *
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
21 *
22 * The latest version of template_lite can be obtained from:
23 * http://templatelite.sourceforge.net
24 *
25 */
26
27 class config {
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?
32
33 var $_db_qstr_regexp = null;
34 var $_bool_true_regexp = null;
35 var $_bool_false_regexp = null;
36 var $_qstr_regexp = null;
37
38 function config()
39 {
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 . ')';
44 }
45
46 function config_load($file, $section_name = null, $var_name = null)
47 {
48 $_result = array();
49 $contents = file_get_contents($file);
50 if (empty($contents))
51 {
52 die("Could not open $file");
53 }
54
55 // insert new line into beginning of file
56 $contents = "\n" . $contents;
57 // fix new-lines
58 if ($this->fix_new_lines)
59 {
60 $contents = str_replace("\r\n","\n",$contents);
61 }
62
63 // match globals
64 if (preg_match("/^(.*?)(\n\[|\Z)/s", $contents, $match))
65 {
66 $_result["globals"] = $this->_parse_config_section($match[1]);
67 }
68
69 // match sections
70 if (preg_match_all("/^\[(.*?)\]/m", $contents, $match))
71 {
72 foreach ($match[1] as $section)
73 {
74 if ($section{0} == '.' && !$this->read_hidden)
75 {
76 continue;
77 }
78 preg_match("/\[".preg_quote($section)."\](.*?)(\n\[|\Z)/s",$contents,$match);
79 if ($section{0} == '.')
80 {
81 $section = substr($section, 1);
82 }
83 $_result[$section] = $this->_parse_config_section($match[1]);
84 }
85 }
86
87
88 if (!empty($var_name))
89 {
90 if (empty($section_name))
91 {
92 return $_result["globals"][$var_name];
93 }
94 else
95 {
96 if(isset($_result[$section_name][$var_name]))
97 {
98 return $_result[$section_name][$var_name];
99 }
100 else
101 {
102 return array();
103 }
104 }
105 }
106 else
107 {
108 if (empty($section_name))
109 {
110 return $_result;
111 }
112 else
113 {
114 if(isset($_result[$section_name]))
115 {
116 return $_result[$section_name];
117 }
118 else
119 {
120 return array();
121 }
122 }
123 }
124 }
125
126 function _parse_config_section($body)
127 {
128 $_result = array();
129 preg_match_all('!(\n\s*[a-zA-Z0-9_]+)\s*=\s*(' . $this->_qstr_regexp . ')!s', $body, $ini);
130 $keys = $ini[1];
131 $values = $ini[2];
132 for($i = 0, $for_max = count($ini[0]); $i < $for_max; $i++)
133 {
134 if ($this->booleanize)
135 {
136 if (preg_match('/^(' . $this->_bool_true_regexp . ')$/i', $values[$i]))
137 {
138 $values[$i] = true;
139 }
140 elseif (preg_match('/^(' . $this->_bool_false_regexp . ')$/i', $values[$i]))
141 {
142 $values[$i] = false;
143 }
144 }
145 if (!is_numeric($values[$i]) && !is_bool($values[$i]))
146 {
147 $values[$i] = str_replace("\n",'',stripslashes(substr($values[$i], 1, -1)));
148 }
149 if ($this->overwrite || !isset($_result[trim($keys[$i])]))
150 {
151 $_result[trim($keys[$i])] = $values[$i];
152 }
153 else
154 {
155 if (!is_array($_result[trim($keys[$i])]))
156 {
157 $_result[trim($keys[$i])] = array($_result[trim($keys[$i])]);
158 }
159 $_result[trim($keys[$i])][] = $values[$i];
160 }
161 }
162 return $_result;
163 }
164 }
165 ?>