3 * FusionForge configuration functions
5 * Copyright 2009, Roland Mas
7 * This file is part of FusionForge. FusionForge is free software;
8 * you can redistribute it and/or modify it under the terms of the
9 * GNU General Public License as published by the Free Software
10 * Foundation; either version 2 of the Licence, or (at your option)
13 * FusionForge 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
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 // See docs in http://fusionforge.org/plugins/mediawiki/wiki/fusionforge/index.php/Configuration
27 * Singleton FusionForge configuration database manager TODO : Enter better description here ...
30 class FusionForgeConfig {
31 static protected $instance = NULL ;
33 private $bools = array () ;
36 * Singleton accessor to the configuration database
37 * @return FusionForgeConfig instance
39 static public function get_instance () {
40 if (self::$instance == NULL) {
41 self::$instance = new FusionForgeConfig () ;
43 return self::$instance ;
46 // TODO: add a constructor that initializes self::$instance to self ?
48 public function get_sections () {
49 return array_keys ($this->settings) ;
52 public function get_variables ($section='core') {
53 if (isset ($this->settings[$section])) {
54 return array_keys ($this->settings[$section]) ;
59 public function get_value ($section, $var) {
60 if (!isset ($this->settings[$section])
61 || !isset ($this->settings[$section][$var])) {
65 $tmp = $this->settings[$section][$var] ;
66 preg_match_all ('/\$[a-z_]+\/[a-z_]+/', $tmp, $matches) ;
68 foreach ($matches[0] as $m) {
69 $c = explode ('/', substr($m,1)) ;
71 if (isset ($this->settings[$c[0]][$c[1]])) {
72 $tmp = str_replace ($m, $this->get_value($c[0],$c[1]), $tmp) ;
75 if ($this->is_bool ($section, $var)) {
76 $tmp = $this->_interpret_as_bool ($tmp) ;
81 public function get_raw_value ($section, $var) {
82 if (!isset ($this->settings[$section])
83 || !isset ($this->settings[$section][$var])) {
86 return $this->settings[$section][$var] ;
89 public function set_value ($section, $var, $value) {
90 if (!isset ($this->settings[$section])) {
91 $this->settings[$section] = array () ;
94 if (!isset ($this->settings[$section][$var])) {
95 $this->settings[$section][$var] = $value ;
99 public function reset_value ($section, $var, $value) {
100 if (!isset ($this->settings[$section])) {
101 $this->settings[$section] = array () ;
104 $this->settings[$section][$var] = $value ;
107 function read_config_file ($filename) {
108 if (getenv ('FUSIONFORGE_USE_PRE_51_CONFIG') == 'true') {
109 $fallback_only = true;
111 $fallback_only = false;
114 if (file_exists($filename) && is_readable($filename)) {
115 $sections = parse_ini_file ($filename, true) ;
116 if (is_array($sections)) {
117 foreach ($sections as $section => $options) {
118 foreach ($options as $var => $value) {
119 if ($fallback_only) {
120 $this->set_value($section,$var,$value);
122 $this->reset_value($section,$var,$value);
131 function mark_as_bool ($section, $var) {
132 if (!array_key_exists ($section, $this->bools)) {
133 $this->bools[$section] = array () ;
135 $this->bools[$section][$var] = true ;
138 function is_bool ($section, $var) {
139 if (isset ($this->bools[$section])
140 && isset ($this->bools[$section][$var])) {
141 return $this->bools[$section][$var] ;
146 private function _interpret_as_bool ($val) {
147 $val = strtolower ($val) ;
161 if (!isset ($fusionforge_config)) {
162 $fusionforge_config = new FusionForgeConfig();
166 * Get value of variable "name" in section "section"
168 * @param string $section defaults to "core" if missing
169 * @return Ambigous <NULL, boolean>
171 function forge_get_config ($var, $section = 'core') {
172 $c = FusionForgeConfig::get_instance () ;
173 return $c->get_value ($section, $var) ;
176 function forge_get_config_array () {
177 $c = FusionForgeConfig::get_instance () ;
181 foreach (func_get_args() as $item) {
182 if (! is_array ($item)) {
183 $item = array ($item) ;
186 if (isset ($item[1])) {
187 $section = $item[1] ;
191 $ret[] = $c->get_value ($section, $var) ;
197 function forge_set_vars_from_config () {
198 $c = FusionForgeConfig::get_instance () ;
200 foreach (func_get_args() as $item) {
201 if (is_array ($item)) {
204 if (isset ($item[1])) {
205 $section = $item[1] ;
206 $x = $section.'__'.$var ;
207 $value = forge_get_config ($var, $section) ;
212 $value = forge_get_config ($var) ;
222 * Define a new configuration item with given name/section and default value
223 * @param string $var name
224 * @param string $section
225 * @param any $default
227 function forge_define_config_item ($var, $section, $default) {
228 $c = FusionForgeConfig::get_instance () ;
230 return $c->set_value ($section, $var, $default) ;
234 * Tag the variable as boolean, which allows human-readable values in the configuration files (such as yes, true, on and 1; anything else is mapped to false)
235 * @param string $var name
236 * @param string $section
238 function forge_set_config_item_bool ($var, $section) {
239 $c = FusionForgeConfig::get_instance () ;
241 return $c->mark_as_bool ($section, $var) ;
245 * Read a *.ini file and inject its contents into the configuration database
246 * @param string $file
248 function forge_read_config_file ($filename) {
249 $c = FusionForgeConfig::get_instance () ;
251 return $c->read_config_file ($filename) ;
255 * Read all configuration files in a directory
256 * @param string $path
258 function forge_read_config_dir ($path) {
259 $c = FusionForgeConfig::get_instance () ;
264 if ($handle = opendir($path)) {
265 while (false !== ($file = readdir($handle))) {
268 // Avoid .bak, .old, .dpkg-old and so on, but keep .ini
269 && preg_match ('/^[0-9a-zA-Z_-]+(.ini)?$/', $file)) {
270 $files[] = "$path/$file" ;
278 foreach ($files as $file) {
279 $c->read_config_file ($file) ;
283 function forge_reset_config_item ($var, $section, $default) {
284 $c = FusionForgeConfig::get_instance () ;
286 return $c->reset_value ($section, $var, $default) ;
291 // c-file-style: "bsd"