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 if (!is_array($options)) {
121 foreach ($options as $var => $value) {
122 if ($fallback_only) {
123 $this->set_value($section,$var,$value);
125 $this->reset_value($section,$var,$value);
134 function mark_as_bool ($section, $var) {
135 if (!array_key_exists ($section, $this->bools)) {
136 $this->bools[$section] = array () ;
138 $this->bools[$section][$var] = true ;
141 function is_bool ($section, $var) {
142 if (isset ($this->bools[$section])
143 && isset ($this->bools[$section][$var])) {
144 return $this->bools[$section][$var] ;
149 private function _interpret_as_bool ($val) {
150 $val = strtolower ($val) ;
164 if (!isset ($fusionforge_config)) {
165 $fusionforge_config = new FusionForgeConfig();
169 * Get value of variable "name" in section "section"
171 * @param string $section defaults to "core" if missing
172 * @return Ambigous <NULL, boolean>
174 function forge_get_config ($var, $section = 'core') {
175 $c = FusionForgeConfig::get_instance () ;
176 return $c->get_value ($section, $var) ;
179 function forge_get_config_array () {
180 $c = FusionForgeConfig::get_instance () ;
184 foreach (func_get_args() as $item) {
185 if (! is_array ($item)) {
186 $item = array ($item) ;
189 if (isset ($item[1])) {
190 $section = $item[1] ;
194 $ret[] = $c->get_value ($section, $var) ;
200 function forge_set_vars_from_config () {
201 $c = FusionForgeConfig::get_instance () ;
203 foreach (func_get_args() as $item) {
204 if (is_array ($item)) {
207 if (isset ($item[1])) {
208 $section = $item[1] ;
209 $x = $section.'__'.$var ;
210 $value = forge_get_config ($var, $section) ;
215 $value = forge_get_config ($var) ;
225 * Define a new configuration item with given name/section and default value
226 * @param string $var name
227 * @param string $section
228 * @param any $default
230 function forge_define_config_item ($var, $section, $default) {
231 $c = FusionForgeConfig::get_instance () ;
233 return $c->set_value ($section, $var, $default) ;
237 * 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)
238 * @param string $var name
239 * @param string $section
241 function forge_set_config_item_bool ($var, $section) {
242 $c = FusionForgeConfig::get_instance () ;
244 return $c->mark_as_bool ($section, $var) ;
248 * Read a *.ini file and inject its contents into the configuration database
249 * @param string $file
251 function forge_read_config_file ($filename) {
252 $c = FusionForgeConfig::get_instance () ;
254 return $c->read_config_file ($filename) ;
258 * Read all configuration files in a directory
259 * @param string $path
261 function forge_read_config_dir ($path) {
262 $c = FusionForgeConfig::get_instance () ;
267 if ($handle = opendir($path)) {
268 while (false !== ($file = readdir($handle))) {
271 // Avoid .bak, .old, .dpkg-old and so on, but keep .ini
272 && preg_match ('/^[0-9a-zA-Z_-]+(.ini)?$/', $file)) {
273 $files[] = "$path/$file" ;
281 foreach ($files as $file) {
282 $c->read_config_file ($file) ;
286 function forge_reset_config_item ($var, $section, $default) {
287 $c = FusionForgeConfig::get_instance () ;
289 return $c->reset_value ($section, $var, $default) ;
294 // c-file-style: "bsd"